« blib/lib/WWW/Shopify/Liquid/Tag/Increment.pm
↑ Summary
blib/lib/WWW/Shopify/Liquid/Operator/NotRegex.pm »
| 1 | =head1 | ||
| 2 | |||
| 3 | WWW::Shopify:::Liquid::Precompiler - Tool to automatically compile .liquid files, which have multiple extensions. | ||
| 4 | |||
| 5 | =cut | ||
| 6 | |||
| 7 | package WWW::Shopify::Liquid::Precompiler; | ||
| 8 | use File::Find; | ||
| 9 | use File::stat; | ||
| 10 | use File::Slurp; | ||
| 11 | use File::Basename; | ||
| 12 | |||
| 13 | use strict; | ||
| 14 | use warnings; | ||
| 15 | |||
| 16 | use WWW::Shopify::Liquid::Analyzer; | ||
| 17 | |||
| 18 | sub new { | ||
| 19 | my $package = shift; | ||
| 20 | my ($self) = bless { | ||
| 21 | options => {}, | ||
| 22 | analyzer => undef, | ||
| 23 | entities => {}, | ||
| 24 | liquid => undef, | ||
| 25 | @_ | ||
| 26 | }, $package; | ||
| 27 | $self->{liquid} = WWW::Shopify::Liquid->new if !$self->liquid; | ||
| 28 | $self->{analyzer} = WWW::Shopify::Liquid::Analyzer->new(liquid => $self->liquid) if !$self->analyzer; | ||
| 29 | return $self; | ||
| 30 | } | ||
| 31 | sub liquid { return $_[0]->{liquid}; } | ||
| 32 | sub analyzer { return $_[0]->{analyzer}; } | ||
| 33 | |||
| 34 | # Destination can be a folder, or a filename. | ||
| 35 | sub should_compile { | ||
| 36 | my ($self, $target, $destination) = @_; | ||
| 37 | return 0 if -d $target; | ||
| 38 | return 0 unless $target =~ m/^(.*?[^\/]+\.[^\/]+)\.liquid$/ && -e $target; | ||
| 39 | my $file; | ||
| 40 | if ($destination) { | ||
| 41 | $file = -d $destination ? $destination . "/" . basename($1) : $file; | ||
| 42 | } else { | ||
| 43 | $file = $1; | ||
| 44 | } | ||
| 45 | return $file if !-e $file; | ||
| 46 | my $mtime = stat($target)->mtime; | ||
| 47 | return $file if stat($file)->mtime < $mtime; | ||
| 48 | my $entity = $self->analyzer->add_refresh_path($target, values(%{$self->{entities}})); | ||
| 49 | $self->{entities}->{$entity->id} = $entity; | ||
| 50 | return $file if int(grep { $mtime < stat($_)->mtime } (@{$entity->{full_dependencies}})) > 0; | ||
| 51 | return 0; | ||
| 52 | } | ||
| 53 | |||
| 54 | sub options { | ||
| 55 | my ($self, $target) = @_; | ||
| 56 | return $_[0]->{options}; | ||
| 57 | } | ||
| 58 | |||
| 59 | sub compile { | ||
| 60 | my ($self, $file, $target) = @_; | ||
| 61 | my $text = $self->{liquid}->render_file($self->options($file), $file); | ||
| 62 | write_file($target, $text); | ||
| 63 | return $target; | ||
| 64 | } | ||
| 65 | |||
| 66 | sub compile_directories { | ||
| 67 | my ($self, @directories) = @_; | ||
| 68 | find({ | ||
| 69 | wanted => sub { | ||
| 70 | my $file = $_; | ||
| 71 | if (my $target = $self->should_compile($file)) { | ||
| 72 | eval { $self->compile($file, $target); }; | ||
| 73 | if (my $exp = $@) { | ||
| 74 | die new WWW::Shopify::Liquid::Exception("Can't target.") unless $file =~ m/^(.*?[^\/]+\.[^\/]+)\.liquid$/ && -e $file; | ||
| 75 | my $target = $1; | ||
| 76 | if (!-e $target) { | ||
| 77 | open(my $fh, ">>", $target) or die $!; close($fh); | ||
| 78 | } else { | ||
| 79 | utime(undef, undef, $target); | ||
| 80 | } | ||
| 81 | die $exp; | ||
| 82 | } | ||
| 83 | } | ||
| 84 | }, no_chdir => 1 | ||
| 85 | }, @directories) | ||
| 86 | } | ||
| 87 | |||
| 88 | sub watch_directories { | ||
| 89 | my ($self, @directories) = @_; | ||
| 90 | while (1) { | ||
| 91 | eval { | ||
| 92 | $self->compile_directories(@directories); | ||
| 93 | }; | ||
| 94 | if (my $exp = $@) { | ||
| 95 | print STDERR "Error compiling: " . $exp->error . "\n"; | ||
| 96 | } | ||
| 97 | sleep(1); | ||
| 98 | } | ||
| 99 | } | ||
| 100 | |||
| 101 | 1; |