| 1 | #!/usr/bin/env perl | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use WWW::Shopify::Liquid; | ||
| 7 | use Getopt::Long; | ||
| 8 | use File::Find; | ||
| 9 | use File::Slurp; | ||
| 10 | use Pod::Usage; | ||
| 11 | |||
| 12 | |||
| 13 | =head1 NAME | ||
| 14 | |||
| 15 | shopify-liquid-compile.pl - Compile a file, or watch a file/directory for changes, and recompile as needed. | ||
| 16 | |||
| 17 | =head1 SYNOPSIS | ||
| 18 | |||
| 19 | shopify-liquid-compile.pl action [files] | ||
| 20 | |||
| 21 | action The action to take. Can be either 'compile' | ||
| 22 | or 'watch'. | ||
| 23 | |||
| 24 | files The list of files and folders to check. | ||
| 25 | |||
| 26 | --json Takes in either a file name, or a JSON | ||
| 27 | string. If filename, will also check | ||
| 28 | for changes to this file before recompiling. | ||
| 29 | |||
| 30 | --base Optionally specifies the path to a base | ||
| 31 | liquid implementation. If no --class | ||
| 32 | is specified attempts to determine | ||
| 33 | the class. Takes paths or package names. | ||
| 34 | |||
| 35 | --class Specifically sets the class to use for | ||
| 36 | the liquid parser. By default, this is | ||
| 37 | WWW::Shopify::Liquid. If --base is used, | ||
| 38 | will attempt to automatically determine. | ||
| 39 | |||
| 40 | --verbose Displays extra info. | ||
| 41 | --quiet Suppress everything except errors. | ||
| 42 | |||
| 43 | --help Displays this messaqge. | ||
| 44 | --fullhelp Displays the full pod doc. | ||
| 45 | |||
| 46 | This tool essentially prints out whether or not it thinks a file's | ||
| 47 | liquid is correct, in terms of sytnax. If it finds an issue, it'll | ||
| 48 | point out the first error in that file. Otherwise it'll print OK. | ||
| 49 | |||
| 50 | By default, the behaviour of this script is recursive, meaning if | ||
| 51 | you specify a directory amongst your file listing, this script | ||
| 52 | will expand it and look at the subfolders. | ||
| 53 | |||
| 54 | =cut | ||
| 55 | |||
| 56 | =head1 EXAMPLES | ||
| 57 | |||
| 58 | shopify-liquid-compile.pl *.liquid | ||
| 59 | |||
| 60 | =cut | ||
| 61 | |||
| 62 | my @ARGS; | ||
| 63 | |||
| 64 | GetOptions( | ||
| 65 | 'json=s' => \my $json, | ||
| 66 | 'help' => \my $help, | ||
| 67 | 'fullhelp' => \my $fullhelp, | ||
| 68 | 'base=s' => \my @bases, | ||
| 69 | 'class=s' => \my $class, | ||
| 70 | 'verbose' => \my $verbose, | ||
| 71 | 'quiet' => \my $quiet, | ||
| 72 | '<>' => sub { push(@ARGS, $_[0]); } | ||
| 73 | ); | ||
| 74 | |||
| 75 | pod2usage(-verbose => 2) if ($fullhelp); | ||
| 76 | pod2usage() if $help; | ||
| 77 | |||
| 78 | eval { | ||
| 79 | |||
| 80 | die "Requires action as argument, and at least one file." unless int(@ARGS) > 1; | ||
| 81 | |||
| 82 | |||
| 83 | if (@bases) { | ||
| 84 | for my $base (@bases) { | ||
| 85 | die "Can't find base $base." unless -e $base; | ||
| 86 | if ($base =~ m/::/) { | ||
| 87 | eval("require $base;") or die "Can't compile base $base."; | ||
| 88 | if (my $exp = $@) { | ||
| 89 | die $exp; | ||
| 90 | } | ||
| 91 | $class = $base; | ||
| 92 | } else { | ||
| 93 | require($base) or die "Can't compile base."; | ||
| 94 | ($class) = map { $_ =~ /\s*package\s+(.*?\:\:Liquid);/ ? ($1) : () } (read_file($base)) if !$class; | ||
| 95 | } | ||
| 96 | } | ||
| 97 | } | ||
| 98 | |||
| 99 | my $action = shift(@ARGS); | ||
| 100 | |||
| 101 | my $errors = 0; | ||
| 102 | $class = 'WWW::Shopify::Liquid' if !$class; | ||
| 103 | print "Using liquid class $class.\n" if $verbose; | ||
| 104 | my $liquid = $class->new; | ||
| 105 | write_file($_ =~ s/\.liquid//r, $liquid->render_file({ }, $_)) for (@ARGS); | ||
| 106 | |||
| 107 | |||
| 108 | }; | ||
| 109 | if (my $exp = $@) { | ||
| 110 | $exp =~ s/ at .*? line \d+.//; | ||
| 111 | chomp($exp); | ||
| 112 | print STDERR "$exp\n"; | ||
| 113 | exit(-1); | ||
| 114 | } |