script/shopify-themer.pl untested

filter: Statementstmt 0.0% 0 / 209 Branchbran 0.0% 0 / 94 Conditioncond 0.0% 0 / 25 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 358 CC 4 SCAR 17.6
CC
4
Cov
0%
prompt_directory
20.0
BEGIN
2.0
BEGIN
2.0
CRAP 5.8
1 #!/usr/local/bin/perl
2 use strict;
3 use warnings;
4  
5 use File::Slurp;
6 use Getopt::Long;
7 use IO::Handle;
8 use File::Basename;
9 use Term::Prompt;
10 use File::ShareDir qw(dist_dir);
11 use File::Path qw(make_path);
12 use Pod::Usage;
13 use Cwd 'abs_path';
14 STDERR->autoflush(1);
15 STDOUT->autoflush(1);
16  
17 =head1 NAME
18  
19 shopify-themer.pl - Supports the pushing and pulling of themes from a shopify store.
20  
21 =head1 SYNOPSIS
22  
23 shopify-themer.pl action [options]
24  
25 action Action can be one of several things.
26
27 info
28 Spits out a bunch of theme information in JSON form.
29 Mainly used for debugging, and gedit integration.
30  
31 pullAll
32 Pulls all themes from the shop
33  
34 pushAll
35 Pushes all assets from all themes.
36  
37 push <ID/Name>
38 Pushes all assets from the specified theme.
39  
40 pull <ID/Name>
41 Pulls all assets form the specified theme.
42
43 activate <ID/Name>
44 Sets the specified theme as the main theme.
45
46 watch <ID/Name>
47 Watches the specified folder for changes,
48 and then pushes those changes when necessary.
49 If an ID or name is specified, it only
50 watches that folder.
51  
52 installGedit
53 Checks for gedit on the system and then
54 installs the appropriate plugin into
55 the gedit configuration folder.
56
57 interactive
58 Causes the script to be interactive; useful if
59 you want to do something non-transactional,
60 like continuous interaction with gedit. Will
61 drop to a command line, and await the above
62 actions. To exit, type exit.
63
64 --help Displays this messaqge.
65 --fullhelp Displays the full pod doc.
66  
67 --wd Sets the working directory to be something other
68 than .
69 --interval Sets the interval of time that the 'watch' command
70 checks files at. Default is 1 second.
71  
72 This following three parameters only need to be speciefied once
73 per working directory, as it will be saved in a hidden file in
74 that directory.
75  
76 --url Sets the shop url.
77 --api_key Sets the api key of your private application.
78 --email Sets the email you want to log in with.
79 --password Either your private application's password when
80 used with api_key, or your account's password
81 when used with email.
82  
83 =cut
84  
85 =head1 DESCRIPTION
86  
87 Shopify themer is a simple script which uses L<WWW::Shopify::Private> and
88 L<WWW::Shopify> to fetch themes and assets from a Shopify store. It is
89 meant to be used as either a standalone application, or integrated with
90 Gedit as a plugin. It also has the ability to fetch/push pages from the
91 Shopify store.
92  
93 The gedit plugin is written in python, but ultimately is simply a wrapper
94 around this script. Currently, support is limited to those OSs which can
95 make symlinks and can install gedit. This means that it should also work
96 (experimentally) on Windows and Mac. The system is mainly tested on Linux,
97 so support for Linux is definitely higher priority than for other systmes.
98  
99 Normally, you only have to specify the shop url, api key/email and password
100 once per working directory/site. Don't try and create multiple site themes
101 in the same directory as this is a _BAD_ _IDEA_.
102  
103 The Shopify shop is the ultimate arbitrator of what is the 'final' version
104 of a file; this makes good sense when multiple people are working on a shop,
105 but may be somewhat annoying. What this means, is that:
106  
107 For pushing:
108 Files that are locally changed, and remotely not, will be pushed.
109 Files that are locally changed, and remotely changed, will not be pushed.
110 Files that are locally unchanged will only be pushed if the file is missing on the server.
111  
112 For pulling:
113 Files that are locally and remotely changed will be overwritten locally, so keep an eye out for this.
114 Files that are locally not, and remotely chagned will be overritten locally.
115 Files that are not present locally and remotely present will be pulled.
116  
117 =cut
118  
119 use WWW::Shopify::Tools::Themer;
120  
121 use JSON qw(encode_json decode_json);
122  
123 my @ARGS = ();
124  
125 my $settings = {directory => '.'};
126 my $interval = 1;
127 GetOptions(
128 "url=s" => \$settings->{url},
129 "api_key=s" => \$settings->{apikey},
130 "password=s" => \$settings->{password},
131 "email=s" => \$settings->{email},
132 "wd=s" => \$settings->{directory},
133 "help" => \my $help,
134 "fullhelp" => \my $fullhelp,
135 "interval" => \$interval,
136 '<>' => sub { push(@ARGS, $_[0]); }
137 );
138  
139 my $action = $ARGS[0];
140  
141 pod2usage(-verbose => 2) if ($fullhelp);
142 pod2usage() if ($help || !defined $action);
143  
144 if ($action eq 'installGedit') {
145 die "You system doesn't support symlinks. Which is crazy. Still on XP, eh? (probably) Not supported, aborting install.\n" unless eval { symlink("", ""); 1; };
146  
147 sub prompt_directory {
148 my ($directory) = @_;
149 if (!-d $directory) {
150 # We shoudln't need to create any folders in Windows; they should all be there.
151 die "Can't find $directory, aborting install.\n" if ($^O =~ m/MSWin/i);
152 print "No.\n";
153 my $result = &prompt("y", "Would you like to create it?", undef, "y");
154 if (!$result) {
155 print "Aborting install.\n";
156 exit(0);
157 }
158 make_path($directory);
159 }
160 else {
161 print "Yes.\n";
162 }
163 }
164  
165 my $dist_directory = dist_dir('WWW-Shopify-Tools-Themer');
166 my ($plugin_directory, $target_directory, $language_directory, $icon_directory);
167 if ($^O !~ m/MSWin/i) {
168 # *NIX Derivatives install, probably.
169 die "Must have HOME environment variable defined; Sorry, automatic gedit installation isn't supported without this.\n" unless $ENV{'HOME'};
170 print "Checking to see if gedit exists... ";
171 `gedit --version`;
172 die "Can't detect gedit.\n" unless $? == 0;
173 print "Yes.\n";
174 print "Checking to see if python exists... ";
175 `python --version`;
176 die "Can't detect python.\n" unless $? == 0;
177 my $share_directory = $ENV{'HOME'} . "/.local/share";
178 $plugin_directory = "$share_directory/gedit/plugins";
179 $target_directory = "$plugin_directory/shopifyeditor";
180 $language_directory = "$share_directory/gtksourceview-3.0";
181 $icon_directory = "$share_directory/icons/hicolor/scalable/apps";
182 }
183 else {
184 die "Can't find program files environment variable." unless $ENV{'PROGRAMFILES'};
185 my $share_directory = $ENV{'PROGRAMFILES'} . "/gedit";
186 if (!(-d $share_directory) && $ENV{'PROGRAMFILES(X86)'}) {
187 $share_directory = $ENV{'PROGRAMFILES(X86)'} . "/gedit";
188 }
189 if (!-d $share_directory) {
190 print "Can't find gedit directory in Program Files; enter it here: " unless -d $share_directory;
191 $share_directory = <STDIN>;
192 }
193 die "Directory doesn't exist." unless -d $share_directory;
194 $share_directory = "$share_directory/share";
195 $plugin_directory = "$share_directory/plugins";
196 $target_directory = "$plugin_directory/shopifyeditor";
197 $language_directory = "$share_directory/gtksourceview-2.0";
198 $icon_directory = "$share_directory/icons/hicolor/scalable/apps";
199 }
200 if (!-e $target_directory) {
201 print "Checking for presence of gedit settings directory in $plugin_directory... ";
202 prompt_directory($plugin_directory);
203 print "Symlinking sharedir to directory... ";
204 die "Can't symlink, for some reason.\n" if symlink($dist_directory, $target_directory) != 1;
205 print "OK.\n";
206 }
207 if (!-e "$language_directory/language-specs") {
208 print "Checking for presence of source view languages in $language_directory... ";
209 prompt_directory($language_directory);
210 print "Symlinking language dir to directory... ";
211 die "Can't symlink for some reason.\n" if symlink("$dist_directory/languages", "$language_directory/language-specs") != 1;
212 print "OK.\n";
213 }
214 if (!-e "$icon_directory") {
215 print "Checking for presence of icon directory in $icon_directory... ";
216 prompt_directory($icon_directory);
217 }
218 if (!-l "$icon_directory/shopify-icon.png") {
219 print "Symlinking icon to directory... ";
220 die "Can't symlink, for some reason.\n" if symlink("$dist_directory/shopify-icon.png", "$icon_directory/shopify-icon.png") != 1;
221 print "OK.\n";
222 }
223 print "Done.\n";
224 exit(0);
225 }
226  
227 my ($settingFile, $manifestFile) = ($settings->{directory} . "/.shopsettings", $settings->{directory} . "/.shopmanifest");
228 my $filesettings = decode_json(read_file($settingFile)) if (-e $settingFile);
229 for (keys(%$filesettings)) { $settings->{$_} = $filesettings->{$_} unless defined $settings->{$_}; }
230 die "Please specify a --url, --apikey xor --email and --password when using for the first time.\n" unless defined $settings->{url} && defined $settings->{password} && (defined $settings->{apikey} xor defined $settings->{email});
231 my %saveSettings = map { $_ => $settings->{$_} } grep { $_ ne "directory" } keys(%$settings);
232 write_file($settingFile, encode_json(\%saveSettings));
233  
234 my $STC = new WWW::Shopify::Tools::Themer($settings);
235  
236 use List::Util qw(first);
237 my $interactive = $action eq "interactive";
238  
239 my %actions = (
240 'info' => sub {
241 my @themes = $STC->get_themes;
242 print encode_json(int(@themes) > 0 ? \@themes : []) . "\n";
243 },
244 'pullAll' => sub {
245 $STC->pull_all;
246 },
247 'pushAll' => sub {
248 $STC->push_all;
249 },
250 'push_pages' => sub {
251 $STC->push_pages;
252 },
253 'pull_pages' => sub {
254 $STC->pull_pages;
255 },
256 'watch' => sub {
257 use File::Find;
258 my $theme = undef;
259 if ($ARGS[1]) {
260 if ($ARGS[1] =~ m/^\d+$/) {
261 $theme = first { $_->{id} eq $ARGS[1] } @{$STC->manifest->{themes}};
262 }
263 else {
264 $theme = first { $_->{name} eq $ARGS[1] } @{$STC->manifest->{themes}}
265 }
266 }
267 die "Unable to find theme " . $ARGS[1] . "\n" unless $theme;
268 print "Entering Check Loop for " . ($theme ? "theme " . $theme->{name} . " (" . $theme->{id} . ") " : "all themes ") . "...\n";
269 my $directory = $theme ? $STC->directory . "/" . $STC->shop_url_truncated . "-" . $theme->{id} : $STC->directory;
270 while (1) {
271 my $should_push = 0;
272 find({ wanted => sub {
273 my ($file) = $_;
274 $should_push = 1 if !$should_push && !-d $file && $file !~ m/\/\.[^\/]+$/ && $STC->manifest->has_local_changes($file);
275 }, no_chdir => 1 }, $directory);
276 if ($should_push) {
277 print "Change detected. Pushing ...\n";
278 if ($theme) {
279 $STC->push(new WWW::Shopify::Model::Theme($theme));
280 }
281 else {
282 $STC->push_all;
283 }
284 print "Done.\n";
285 }
286 sleep($interval);
287 }
288 },
289 'push' => sub {
290 die "Please specify a specific theme to push.\n" unless int(@ARGS) >= 2;
291 my $theme = undef;
292 if ($ARGS[1] =~ m/^\d+$/) {
293 $theme = first { $_->{id} eq $ARGS[1] } @{$STC->manifest->{themes}};
294 }
295 else {
296 $theme = first { $_->{name} eq $ARGS[1] } @{$STC->manifest->{themes}}
297 }
298 die "Unable to find theme " . $ARGS[1] . "\n" unless $theme;
299 $STC->push(new WWW::Shopify::Model::Theme($theme));
300 },
301 'pull' => sub {
302 die "Please specify a specific theme to pull.\n" unless int(@ARGS) >= 2;
303 my $theme = undef;
304 if ($ARGS[1] =~ m/^\d+$/) {
305 $theme = first { $_->{id} eq $ARGS[1] } @{$STC->manifest->{themes}};
306 }
307 else {
308 $theme = first { $_->{name} eq $ARGS[1] } @{$STC->manifest->{themes}}
309 }
310 die "Unable to find theme " . $ARGS[1] . "\n" unless $theme;
311 $STC->pull(new WWW::Shopify::Model::Theme($theme));
312 },
313 'activate' => sub {
314 die "Please specify a specific theme to pull.\n" unless int(@ARGS) >= 2;
315 my $theme = undef;
316 if ($ARGS[1] =~ m/^\d+$/) {
317 $theme = first { $_->{id} eq $ARGS[1] } @{$STC->manifest->{themes}};
318 }
319 else {
320 $theme = first { $_->{name} eq $ARGS[1] } @{$STC->manifest->{themes}}
321 }
322 die "Unable to find theme " . $ARGS[1] . "\n" unless $theme;
323 $STC->activate(new WWW::Shopify::Model::Theme($theme));
324 },
325 'exit' => sub { $interactive = undef; }
326 );
327  
328  
329 do {
330 if ($interactive) {
331 $action = <STDIN>;
332 chomp $action;
333 }
334 if (exists ($actions{$action})) {
335 eval {
336 $actions{$action}();
337 print "Done.\n";
338 $STC->manifest->save($manifestFile);
339 };
340 if (my $exception = $@) {
341 print STDERR "Error: " . $STC->read_exception($exception) . "\n";
342 }
343 }
344 else {
345 print STDERR "Unknown action: $action.\n";
346 }
347 } while ($interactive);
348  
349 exit 0;
350  
351  
352 =head1 SEE ALSO
353  
354 L<WWW::Shopify>, L<WWW::Shopify::Private>
355  
356 =head1 AUTHOR
357  
358 Adam Harrison (adamdharrison@gmail.com)
359  
360 =head1 LICENSE
361  
362 Copyright (C) 2013 Adam Harrison
363  
364 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
365  
366 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
367  
368 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
369  
370 =cut
371