« blib/script/shopify-liquid-beautify.pl
↑ Summary
blib/lib/WWW/Shopify/Liquid/Operator/Assignment.pm »
| 1 | #!/usr/local/bin/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-verify.pl - Verifies the syntax of liquid files. | ||
| 16 | |||
| 17 | =head1 SYNOPSIS | ||
| 18 | |||
| 19 | shopify-liquid-verify.pl [files] | ||
| 20 | |||
| 21 | files The list of files and folders to check. | ||
| 22 | |||
| 23 | --base Optionally specifies the path to a base | ||
| 24 | liquid implementation. If no --class | ||
| 25 | is specified attempts to determine | ||
| 26 | the class. Takes paths or package names. | ||
| 27 | |||
| 28 | --class Specifically sets the class to use for | ||
| 29 | the liquid parser. By default, this is | ||
| 30 | WWW::Shopify::Liquid. If --base is used, | ||
| 31 | will attempt to automatically determine. | ||
| 32 | |||
| 33 | --verbose Displays extra info. | ||
| 34 | --quiet Suppress everything except errors. | ||
| 35 | |||
| 36 | --help Displays this messaqge. | ||
| 37 | --fullhelp Displays the full pod doc. | ||
| 38 | |||
| 39 | This tool essentially prints out whether or not it thinks a file's | ||
| 40 | liquid is correct, in terms of sytnax. If it finds an issue, it'll | ||
| 41 | point out the first error in that file. Otherwise it'll print OK. | ||
| 42 | |||
| 43 | By default, the behaviour of this script is recursive, meaning if | ||
| 44 | you specify a directory amongst your file listing, this script | ||
| 45 | will expand it and look at the subfolders. | ||
| 46 | |||
| 47 | =cut | ||
| 48 | |||
| 49 | =head1 EXAMPLES | ||
| 50 | |||
| 51 | shopify-liquid-verify.pl * | ||
| 52 | |||
| 53 | Will essentially look at all files in the folder, and give you a print out of their status. | ||
| 54 | |||
| 55 | shopify-liquid-verify.pl * --base WWW::Shopify::Liquid::Extended | ||
| 56 | |||
| 57 | If WWW::Shopify::Liquid::Extended exists, and is installed in a normal inclusion pathway, | ||
| 58 | this class will be used to perform the verification. | ||
| 59 | |||
| 60 | shopify-liquid-verify.pl test.liquid --base ~/MyLiquidClass.pm | ||
| 61 | |||
| 62 | Will load MyLiquidClass.pm and attempt to determine the primary class, and use that for | ||
| 63 | the base verification. | ||
| 64 | |||
| 65 | |||
| 66 | shopify-liquid-verify.pl test.liquid --base ~/MyLiquidClass.pm --class NewLiquid | ||
| 67 | |||
| 68 | Will load MyLiquidClass.pm and specifically tell it to use NewLiquid as hte base class. | ||
| 69 | |||
| 70 | =cut | ||
| 71 | |||
| 72 | my @ARGS; | ||
| 73 | |||
| 74 | GetOptions( | ||
| 75 | 'help' => \my $help, | ||
| 76 | 'fullhelp' => \my $fullhelp, | ||
| 77 | 'base=s' => \my @bases, | ||
| 78 | 'class=s' => \my $class, | ||
| 79 | 'verbose' => \my $verbose, | ||
| 80 | 'quiet' => \my $quiet, | ||
| 81 | '<>' => sub { push(@ARGS, $_[0]); } | ||
| 82 | ); | ||
| 83 | |||
| 84 | pod2usage(-verbose => 2) if ($fullhelp); | ||
| 85 | pod2usage() if $help; | ||
| 86 | |||
| 87 | eval { | ||
| 88 | |||
| 89 | die "Requires file as argument." unless int(@ARGS) > 0; | ||
| 90 | |||
| 91 | if (@bases) { | ||
| 92 | for my $base (@bases) { | ||
| 93 | die "Can't find base $base." unless -e $base; | ||
| 94 | if ($base =~ m/::/) { | ||
| 95 | eval("require $base;") or die "Can't compile base $base."; | ||
| 96 | if (my $exp = $@) { | ||
| 97 | die $exp; | ||
| 98 | } | ||
| 99 | $class = $base; | ||
| 100 | } else { | ||
| 101 | require($base) or die "Can't compile base."; | ||
| 102 | ($class) = map { $_ =~ /\s*package\s+(.*?\:\:Liquid);/ ? ($1) : () } (read_file($base)) if !$class; | ||
| 103 | } | ||
| 104 | } | ||
| 105 | } | ||
| 106 | |||
| 107 | my $errors = 0; | ||
| 108 | $class = 'WWW::Shopify::Liquid' if !$class; | ||
| 109 | print "Using liquid class $class.\n" if $verbose; | ||
| 110 | my $liquid = $class->new; | ||
| 111 | for my $potential (@ARGS) { | ||
| 112 | die "File $potential doesn't exist." unless -e $potential; | ||
| 113 | my @files; | ||
| 114 | if (-d $potential) { | ||
| 115 | find({ no_chdir => 1, wanted => sub { | ||
| 116 | push(@files, $_) if !-d $_; | ||
| 117 | } }, $potential); | ||
| 118 | } else { | ||
| 119 | @files = $potential; | ||
| 120 | } | ||
| 121 | for my $file (@files) { | ||
| 122 | eval { | ||
| 123 | $liquid->verify_file($file); | ||
| 124 | }; | ||
| 125 | if (my $exp = $@) { | ||
| 126 | print STDERR "Error with $file: $exp.\n"; | ||
| 127 | $errors++; | ||
| 128 | } else { | ||
| 129 | print "$file syntax OK.\n" unless $quiet; | ||
| 130 | } | ||
| 131 | } | ||
| 132 | } | ||
| 133 | exit($errors); | ||
| 134 | }; | ||
| 135 | if (my $exp = $@) { | ||
| 136 | $exp =~ s/ at .*? line \d+.//; | ||
| 137 | chomp($exp); | ||
| 138 | print STDERR "$exp\n"; | ||
| 139 | exit(-1); | ||
| 140 | } |