blib/lib/App/Buuilt/CLI.pm

filter: Statementstmt 37.3% 34 / 91 Branchbran 0.0% 0 / 22 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 57.8% 11 / 19 Podpod 66.6% 4 / 6 totaltotal 35.5% 49 / 138 CC 13 SCAR 31.2
CC
13
Cov
30%
__ANON__
72.0
run
20.0
asset_put
6.0
CRAP 22.5
↑ Summary
1 package App::Buuilt::CLI;
2 1 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 our $VERSION = "0.02";
7  
8 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use EV;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use AnyEvent;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use AnyEvent::Filesys::Notify;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Archive::Zip;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Path::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 ▶use Mojo::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Mojo::Asset::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 0 ▶sub run {
Subroutine run: not called CC=4 SCAR=30.0
Pod undocumented
19  
20 0 my ( $self, @args ) = @_;
21  
22 0 my $action = shift @args;
23 0 my $directory = shift @args;
24  
25 0 ▶ if ( $action eq 'checkout' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($action eq 'checkout') { } 0 0 0
elsif ($action eq 'watch') { } 0 0 0
26  
27 0 my $dir = dir($directory);
28  
29 0 ▶ if ( -e $dir ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $dir) { } 0 0 0
30  
31 0 print "Error: Checkout directory already exists, please choose a fresh one";
32  
33 } else {
34  
35 0 $dir->mkpath;
36 0 $self->checkout($directory);
37 }
38  
39 } elsif ( $action eq 'watch' ) {
40  
41 0 $self->watch($directory);
42  
43 } else {
44  
45 0 print 'Usage: ./buuilt checkout|watch $directory' . "\n\n";
46  
47 }
48 }
49  
50 0 ▶sub config {
Subroutine config: not called CC=1 SCAR=6.9
Pod undocumented
51  
52 return {
53  
54 0 host => "",
55 token => "",
56 theme_id => "",
57 };
58  
59 }
60  
61 =head2 checkout
62  
63 Checkout the whole theme into the given directory
64  
65 =cut
66  
67 0 ▶sub checkout {
Subroutine checkout: not called CC=1 SCAR=6.9
Pod documented
68  
69 0 my ( $self, $directory ) = @_;
70  
71 0 my $file = file( $directory, '.buuilt.zip' );
72 0 my $assets = dir( $directory, 'assets' );
73 0 my $elements = dir( $directory, 'elements' );
74  
75 0 my $config = $self->config();
76  
77 0 my $ua = Mojo::UserAgent->new;
78  
79 # Follow redirect
80 0 $ua->max_redirects(5)->get( $config->{host} . "/api/theme/checkout" => $config )->res->content->asset->move_to($file);
81  
82 0 my $zip = Archive::Zip->new();
83  
84 0 $zip->read( $file->stringify );
85 0 $zip->extractTree( 'assets', $assets );
86  
87 }
88  
89 =head2 watch
90  
91 Watch for changed or new files and directory, upload to cms
92  
93 =cut
94  
95 0 ▶sub watch {
Subroutine watch: not called CC=1 SCAR=6.9
Pod documented
96  
97 0 my ( $self, $directory ) = @_;
98  
99 0 my $config = $self->config();
100  
101 0 my $assets = dir( $directory, 'assets' );
102  
103 my $notifier = AnyEvent::Filesys::Notify->new(
104 dirs => [$directory],
105 interval => 2.0, # Optional depending on underlying watcher
106 0 ▶ filter => sub { shift !~ /\.(swp|tmp|un~|sw\w)$/ },
Subroutine __ANON__: not called CC=1 SCAR=6.9
107 0 ▶ cb => sub {
Subroutine __ANON__: not called CC=8 SCAR=42.8
108 0 my (@events) = @_;
109  
110 0 foreach my $event (@events) {
111  
112 0 my $dir = dir($directory);
113  
114 0 ▶ my $asset = -d $event->path ? dir $event->path : file $event->path;
Branch: 1 branch
BranchTrueFalseTotal
-d $event->path ? : 0 0 0
115 0 my $asset_relative = $asset->relative($assets);
116  
117 0 ▶ if ( $event->is_dir ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($event->is_dir) { } 0 0 0
118  
119 0 ▶ if ( $event->is_deleted ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($event->is_deleted) 0 0 0
120  
121 0 $self->asset_delete($asset_relative);
122  
123 0 printf STDERR ( "dir deleted : %s\n", $asset_relative );
124 }
125  
126 } else {
127  
128 0 ▶ if ( $event->is_created ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($event->is_created) { } 0 0 0
elsif ($event->is_modified) { } 0 0 0
elsif ($event->is_deleted) { } 0 0 0
129  
130 0 $self->asset_put( $asset_relative, $asset );
131 0 printf STDERR ( "file created : %s\n", $asset_relative );
132  
133 } elsif ( $event->is_modified ) {
134  
135 0 $self->asset_put( $asset_relative, $asset );
136 0 printf STDERR ( "file modified: %s\n", $asset_relative );
137  
138 } elsif ( $event->is_deleted ) {
139  
140 0 $self->asset_delete($asset_relative);
141 0 printf STDERR ( "file deleted : %s\n", $asset_relative );
142 }
143  
144 }
145  
146 }
147  
148 # ... process @events ...
149 },
150  
151 #parse_events => 1, # Improves efficiency on certain platforms
152 0 );
153  
154 # Run unless end
155 0 AnyEvent->condvar->recv;
156  
157 }
158  
159 =head2 asset_put
160  
161 Put an asset example stylesheets/demo.css
162
163 =cut
164  
165 0 ▶sub asset_put {
Subroutine asset_put: not called CC=2 SCAR=17.9
Pod documented
166  
167 0 my ( $self, $asset_relative, $asset ) = @_;
168  
169 0 my $config = $self->config();
170  
171 0 my $ua = Mojo::UserAgent->new;
172 0 my $tx = $ua->build_tx( PUT => $config->{host} . '/api/theme/assets/' . $asset_relative => $config );
173 0 $tx->req->content->asset( Mojo::Asset::File->new( path => $asset ) );
174 0 my $txr = $ua->start($tx);
175  
176 0 ▶ Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
Branch: 1 branch
BranchTrueFalseTotal
unless "Mojo::IOLoop"->is_running 0 0 0
177  
178 }
179  
180 =head2 asset_delete
181  
182 Delete an asset, example stylesheet/demo.css
183  
184 =cut
185  
186 0 ▶sub asset_delete {
Subroutine asset_delete: not called CC=2 SCAR=17.9
Pod documented
187  
188 0 my ( $self, $asset_relative ) = @_;
189  
190 0 my $config = $self->config();
191  
192 0 my $ua = Mojo::UserAgent->new;
193 0 my $tx = $ua->build_tx( DELETE => $config->{host} . '/api/theme/assets/' . $asset_relative => $config );
194 0 my $txr = $ua->start($tx);
195  
196 0 ▶ Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
Branch: 1 branch
BranchTrueFalseTotal
unless "Mojo::IOLoop"->is_running 0 0 0
197  
198 }
199  
200 1 1;
201 __END__
↑ Summary