blib/lib/Dist/Zilla/Plugin/Pinto/Add.pm untested

filter: Statementstmt 0.0% 0 / 88 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 27 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 177 CC 12 SCAR 30.3
CC
12
Cov
0%
before_release
42.0
_generate_pinto_args
42.0
release
6.0
CRAP 20.8
1 # ABSTRACT: Ship your dist to a Pinto repository
2  
3 package Dist::Zilla::Plugin::Pinto::Add;
4  
5 #------------------------------------------------------------------------------
6  
7 use Moose;
8 use MooseX::Types::Moose qw(Str ArrayRef Bool);
9  
10 use IPC::Run;
11 use File::Which;
12  
13 use version;
14  
15 #------------------------------------------------------------------------------
16  
17 our $VERSION = '0.088'; # VERSION
18  
19 #------------------------------------------------------------------------------
20  
21 with qw( Dist::Zilla::Role::Releaser Dist::Zilla::Role::BeforeRelease );
22  
23 #------------------------------------------------------------------------------
24  
25 sub mvp_multivalue_args { return qw(roots) }
26 sub mvp_aliases { return { root => 'roots' } }
27  
28 #------------------------------------------------------------------------------
29  
30 has author => (
31 is => 'ro',
32 isa => Str,
33 predicate => 'has_author',
34 );
35  
36  
37 has recurse => (
38 is => 'ro',
39 isa => Bool,
40 predicate => 'has_recurse',
41 );
42  
43  
44 has stack => (
45 is => 'ro',
46 isa => Str,
47 predicate => 'has_stack'
48 );
49  
50  
51 has authenticate => (
52 is => 'ro',
53 isa => Bool,
54 default => 0,
55 );
56  
57  
58 has username => (
59 is => 'ro',
60 isa => Str,
61 default => sub { $ENV{PINTO_USERNAME} || $ENV{USER} || $ENV{LOGIN} || $ENV{USERNAME} || $ENV{LOGNAME} },
62 lazy => 1,
63 );
64  
65  
66 has password => (
67 is => 'ro',
68 isa => Str,
69 default => sub { $ENV{PINTO_PASSWORD} || shift->_ask_for_password },
70 lazy => 1,
71 );
72  
73  
74 has pinto_exe => (
75 is => 'ro',
76 isa => Str,
77 default => \&_find_pinto_executable,
78 lazy => 1,
79 );
80  
81  
82 has roots => (
83 is => 'ro',
84 isa => ArrayRef[Str],
85 default => sub { [ $ENV{PINTO_REPOSITORY_ROOT} || shift->log_fatal('must specify a root') ] },
86 lazy => 1,
87 );
88  
89  
90 has live_roots => (
91 is => 'ro',
92 isa => ArrayRef[Str],
93 writer => '_set_live_roots',
94 default => sub { [] },
95 init_arg => undef,
96 );
97  
98 #------------------------------------------------------------------------------
99  
100 our $MINIMUM_PINTO_VERSION = version->parse('0.098');
101  
102 #------------------------------------------------------------------------------
103  
104 sub BUILD {
105 my ($self) = @_;
106  
107 my $pinto_exe = $self->pinto_exe
108 or $self->log_fatal("unable to find pinto in your PATH");
109  
110 my $pinto_version = $self->_installed_pinto_version
111 or $self->log_fatal("unable to determine the version of pinto at $pinto_exe");
112  
113 $pinto_version >= $MINIMUM_PINTO_VERSION
114 or $self->log_fatal("need version $MINIMUM_PINTO_VERSION of pinto. You only have $pinto_version");
115  
116 return $self;
117 }
118  
119 #------------------------------------------------------------------------------
120  
121 sub before_release {
122 my ($self) = @_;
123  
124 my @live_roots;
125 for my $root ( @{ $self->roots } ) {
126  
127 my @args = (
128 -root => $root,
129 $self->authenticate ? (-username => $self->username) : (),
130 $self->authenticate ? (-password => $self->password) : (),
131 );
132  
133 $self->log("checking if repository at $root is available");
134 my ($ok, $output) = $self->_run_pinto( nop => @args );
135  
136 if (not $ok) {
137 $self->log("repository at $root is not available");
138 my $abort = $self->zilla->chrome->prompt_yn('Abort release? ', {default => 'Y'});
139 $self->log_fatal('Aborting') if $abort; # dies!
140 next;
141 }
142  
143 push @live_roots, $root;
144 }
145  
146 $self->log_fatal('none of your repositories are available') if not @live_roots;
147 $self->_set_live_roots(\@live_roots);
148  
149 return $self;
150 }
151  
152 #------------------------------------------------------------------------------
153  
154 sub release {
155 my ($self, $archive) = @_;
156  
157 for my $root ( @{ $self->live_roots } ) {
158  
159 $self->log("adding $archive to repository at $root");
160 my @args = $self->_generate_pinto_args($root, $archive);
161 my ($ok, $output) = $self->_run_pinto( add => @args );
162  
163 $ok ? $self->log("added $archive to $root ok")
164 : $self->log_fatal("failed to add $archive to $root: $output");
165 }
166  
167 return 1;
168 }
169  
170 #------------------------------------------------------------------------------
171  
172 sub _generate_pinto_args {
173 my ($self, $root, $archive) = @_;
174  
175 my @recurse_opt = $self->has_recurse
176 ? ($self->recurse ? qw(-recurse) : qw(-no-recurse)) : ();
177  
178 return (
179 -root => $root,
180 -message => "Added " . $archive->basename,
181  
182 $self->authenticate ? (-username => $self->username) : (),
183 $self->authenticate ? (-password => $self->password) : (),
184 $self->has_stack ? (-stack => $self->stack) : (),
185 @recurse_opt,
186  
187 $archive,
188 );
189  
190 }
191  
192 #------------------------------------------------------------------------------
193  
194 sub _ask_for_password {
195 my ($self) = @_;
196  
197 my $prompt = sprintf 'Pinto password for %s: ', $self->username;
198 my $password = $self->zilla->chrome->prompt_str($prompt, { noecho => 1 });
199  
200 return $password;
201 }
202  
203 #------------------------------------------------------------------------------
204  
205 sub _run_pinto {
206 my ($self, @args) = @_;
207  
208 local $ENV{PINTO_NO_COLOR} = 1;
209 local $ENV{PINTO_PAGER} = local $ENV{PAGER} = undef;
210  
211 s/^-/--/ for @args;
212  
213 my $output = my $input = '';
214 my @cmd = ($self->pinto_exe, @args);
215 my $timeout = IPC::Run::timeout(300);
216 my $ok = IPC::Run::run(\@cmd, \$input, \$output, \$output, $timeout);
217 return ($ok, $output);
218 }
219  
220 #------------------------------------------------------------------------------
221  
222 sub _find_pinto_executable {
223 my ($class) = @_;
224  
225 return File::Which::which('pinto');
226 }
227  
228 #------------------------------------------------------------------------------
229  
230 sub _installed_pinto_version {
231 my ($class) = @_;
232  
233 my $pinto_exe = $class->_find_pinto_executable;
234 my ($pinto_version) = (qx($pinto_exe --version) =~ m/version ([\d\._v]+) /);
235 return version->parse($pinto_version);
236 }
237  
238 #------------------------------------------------------------------------------
239 1;
240  
241 __END__