Package.pm untested

filter: Statementstmt 0.0% 0 / 152 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 24 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 228 CC 13 SCAR 27.4
CC
13
Cov
0%
execute
56.0
write_file
20.0
execute
12.0
CRAP 15.6
1 package Package;
2 our $VERSION = '0.26';
3  
4 #------------------------------------------------------------------------------#
5 package Package::Command;
6 use App::Cmd::Setup -command;
7 use Mouse;
8 extends 'MouseX::App::Cmd::Command';
9  
10 sub validate_args {}
11  
12 # Semi-brutal hack to suppress extra options I don't care about.
13 around usage => sub {
14 my $orig = shift;
15 my $self = shift;
16 my $opts = $self->{usage}->{options};
17 @$opts = grep { $_->{name} ne 'help' } @$opts;
18 return $self->$orig(@_);
19 };
20  
21 #-----------------------------------------------------------------------------#
22 package Package;
23 use App::Cmd::Setup -app;
24 use Mouse;
25 extends 'MouseX::App::Cmd';
26  
27 use Module::Pluggable
28 require => 1,
29 search_path => [ 'Package::Command' ];
30 Package->plugins;
31  
32 #------------------------------------------------------------------------------#
33 package Package::Command::new;
34 Package->import( -command );
35 use Mouse;
36 extends 'Package::Command';
37  
38 use Cwd 'abs_path';
39 use IO::All;
40 use Template::Toolkit::Simple;
41  
42 use constant abstract => 'Create new module package directory from template';
43 use constant usage_desc => 'pkg init --from=<dir> --module=<Name> --to=<dir>';
44  
45 has dryrun => (
46 is => 'ro',
47 isa => 'Bool',
48 default => 0,
49 documentation => 'Create new package but skip git',
50 );
51  
52  
53 sub execute {
54 my ($self, $opt, $args) = @_;
55 my $pkg_name = pop(@$args) || '';
56 $self->_args($args);
57  
58 my $to = io($pkg_name || '.')->absolute;
59  
60 $pkg_name ||= do {
61 my $cwd = Cwd::cwd;
62 $cwd =~ s!.*/!!;
63 $cwd;
64 };
65 $self->_pkg_name($pkg_name);
66  
67 my $stash = $self->conf->stash;
68  
69 if ($to->exists) {
70 die <<"..." if not $to->empty;
71 $to is not an empty directory.
72 Can't make a new pkg here.
73  
74 Either specify the name of a new directory you wish to create,
75 or name an empty directory, or cd to an empty directory.
76 ...
77 }
78 else {
79 $to->assert->mkdir;
80 }
81 $to->chdir or die "Can't chdir to $to";
82  
83 for my $file (sort keys %{$self->conf->manifest}) {
84 if (not $file =~ /%(.*?)%/) {
85 $self->write_file($file, $file);
86 next;
87 }
88 my $key = $1;
89 my $path = $self->conf->lookup($key)
90 or die "Config variable '$key' is not defined.\n";
91 (my $f = $file) =~ s/%.*?%/$path/;
92 $self->write_file($f, $file);
93 }
94  
95 if ($stash->{git}{init}) {
96 if ($self->dryrun or $to =~ /^.*\/[xyz]{1,5}\d?$/) {
97 (my $url = $stash->{git}{origin}) =~
98 s/\%pkg\.name\%/$stash->{pkg}{name}/e;
99 print <<"...";
100 New package '$to' successfully created, but GitHub repo creation skipped.
101  
102 The GitHub info would be:
103  
104 origin: $url
105 repo: $pkg_name
106 desc: $stash->{desc}
107 ...
108 return;
109 }
110 system("git init; git add .; git commit -m 'First commit'");
111 if (my $url = $stash->{git}{origin}) {
112 $url =~ s/\%pkg\.name\%/$stash->{pkg}{name}/e;
113 system("git remote add origin $url");
114 }
115 $self->create_git_repo($pkg_name, $stash->{desc});
116 }
117  
118 print "New package '$to' successfully created!\n";
119 }
120  
121 sub write_file {
122 my ($self, $file, $key) = @_;
123 my $path = $self->conf->manifest->{$key};
124 my $template = io($path)->all;
125 my $text = eval {
126 tt
127 ->path([])
128 ->strict(1)
129 ->data($self->conf->stash)
130 ->render(\$template);
131 };
132 if ($@) {
133 if ($@ =~ /var\.undef error - undefined variable: (\S+)/) {
134 die "Config variable '$1' is not defined.\n";
135 }
136 die $@;
137 }
138 io($file)->assert->print($text);
139 if (-x $path) {
140 chmod 0755, $file;
141 }
142 }
143  
144 #------------------------------------------------------------------------------#
145 package Package::Command::repo;
146 Package->import( -command );
147 use Mouse;
148 extends 'Package::Command';
149  
150 use constant abstract => 'Create a new repo on github';
151 use constant usage_desc => 'pkg repo --from=<dir>';
152  
153 sub execute {
154 my ($self, $opt, $args) = @_;
155 die "Directory has no git repo"
156 unless -d '.git';
157  
158 my $stash = $self->conf->stash;
159 for (@$args) {
160 if (/^--(\w+)=(.*)/) {
161 my ($k, $v) = ($1, $2);
162 # $v =~ s/^'(.*)'$/$1/;
163 # $v =~ s/^"(.*)"$/$1/;
164 $stash->{$k} = $v;
165 }
166 }
167  
168 my $pkg_name = do {
169 my $cwd = Cwd::cwd;
170 $cwd =~ s!.*/!!;
171 $cwd;
172 };
173  
174 $self->create_git_repo($pkg_name, $stash->{desc});
175 }
176  
177 #------------------------------------------------------------------------------#
178 package Package::Command::listvars;
179 Package->import( -command );
180 use Mouse;
181 extends 'Package::Command';
182  
183 use constant abstract => 'Print list of config variables from template';
184 use constant usage_desc => 'pkg listvars --from=<dir>';
185  
186 sub execute {
187 my ($self, $opt, $args) = @_;
188 print YAML::XS::Dump($self->conf->stash);
189 }
190  
191 #------------------------------------------------------------------------------#
192 package Package::Command;
193  
194 has from => (
195 is => 'ro',
196 isa => 'Str',
197 required => 1,
198 documentation => 'Pkg template directory',
199 );
200  
201 has _conf => (
202 is => 'ro',
203 lazy => 1,
204 reader => 'conf',
205 default => sub {
206 require Package::Conf;
207 my ($self) = @_;
208 Package::Conf->new(
209 src_dir => $self->from,
210 cli_args => $self->_args,
211 pkg_name => $self->_pkg_name,
212 );
213 },
214 );
215  
216 has _pkg_name => (
217 is => 'rw',
218 );
219  
220 has _args => (
221 is => 'rw',
222 default => sub {[]},
223 );
224  
225 sub create_git_repo {
226 my ($self, $pkg_name, $desc) = @_;
227 my $stash = $self->conf->stash;
228 # XXX Check Net::Ping->new->ping("github.com");
229 if (my $github = $stash->{git}{github}) {
230 my $login = $github->{login};
231 my $token = $github->{token};
232 system(qq{curl -F login=$login -F token=$token https://github.com/api/v2/yaml/repos/create -F name=$pkg_name -F "description=$desc"});
233 system("git push origin master");
234 }
235 }
236  
237 1;