script/shopify-liquid-verify.pl untested

filter: Statementstmt 0.0% 0 / 70 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 8 Podpod n/a totaltotal 0.0% 0 / 120 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
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-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 --strict-filters Errors on any unrecognized filter.
36
37 --help Displays this messaqge.
38 --fullhelp Displays the full pod doc.
39  
40 This tool essentially prints out whether or not it thinks a file's
41 liquid is correct, in terms of sytnax. If it finds an issue, it'll
42 point out the first error in that file. Otherwise it'll print OK.
43  
44 By default, the behaviour of this script is recursive, meaning if
45 you specify a directory amongst your file listing, this script
46 will expand it and look at the subfolders.
47  
48 =cut
49  
50 =head1 EXAMPLES
51  
52 shopify-liquid-verify.pl *
53  
54 Will essentially look at all files in the folder, and give you a print out of their status.
55  
56 shopify-liquid-verify.pl * --base WWW::Shopify::Liquid::Extended
57
58 If WWW::Shopify::Liquid::Extended exists, and is installed in a normal inclusion pathway,
59 this class will be used to perform the verification.
60  
61 shopify-liquid-verify.pl test.liquid --base ~/MyLiquidClass.pm
62
63 Will load MyLiquidClass.pm and attempt to determine the primary class, and use that for
64 the base verification.
65  
66  
67 shopify-liquid-verify.pl test.liquid --base ~/MyLiquidClass.pm --class NewLiquid
68
69 Will load MyLiquidClass.pm and specifically tell it to use NewLiquid as hte base class.
70  
71 =cut
72  
73 my @ARGS;
74  
75 GetOptions(
76 'help' => \my $help,
77 'fullhelp' => \my $fullhelp,
78 'base=s' => \my @bases,
79 'class=s' => \my $class,
80 'verbose' => \my $verbose,
81 'strict-filters' => \my $strict_filters,
82 'quiet' => \my $quiet,
83 '<>' => sub { push(@ARGS, $_[0]); }
84 );
85  
86 pod2usage(-verbose => 2) if ($fullhelp);
87 pod2usage() if $help;
88  
89 eval {
90  
91 die "Requires file as argument." unless int(@ARGS) > 0;
92  
93 if (@bases) {
94 for my $base (@bases) {
95 die "Can't find base $base." unless -e $base;
96 if ($base =~ m/::/) {
97 eval("require $base;") or die "Can't compile base $base.";
98 if (my $exp = $@) {
99 die $exp;
100 }
101 $class = $base;
102 } else {
103 require($base) or die "Can't compile base.";
104 ($class) = map { $_ =~ /\s*package\s+(.*?\:\:Liquid);/ ? ($1) : () } (read_file($base)) if !$class;
105 }
106 }
107 }
108  
109 my $errors = 0;
110 $class = 'WWW::Shopify::Liquid' if !$class;
111 print "Using liquid class $class.\n" if $verbose;
112 my $liquid = $class->new;
113 $liquid->parser->accept_unknown_filters(1) if !$strict_filters;
114 for my $potential (@ARGS) {
115 die "File $potential doesn't exist." unless -e $potential;
116 my @files;
117 if (-d $potential) {
118 find({ no_chdir => 1, wanted => sub {
119 push(@files, $_) if !-d $_;
120 } }, $potential);
121 } else {
122 @files = $potential;
123 }
124 for my $file (@files) {
125 eval {
126 $liquid->verify_file($file);
127 };
128 if (my $exp = $@) {
129 print STDERR "Error with $file: $exp.\n";
130 $errors++;
131 } else {
132 print "$file syntax OK.\n" unless $quiet;
133 }
134 }
135 }
136 exit($errors);
137 };
138 if (my $exp = $@) {
139 $exp =~ s/ at .*? line \d+.//;
140 chomp($exp);
141 print STDERR "$exp\n";
142 exit(-1);
143 }