File Coverage

blib/lib/Dist/Zilla/Plugin/UploadToCPAN.pm
Criterion Covered Total %
statement 40 42 95.2
branch 6 6 100.0
condition n/a
subroutine 14 16 87.5
pod 0 4 0.0
total 60 68 88.2


line stmt bran cond sub pod time code
1             # ABSTRACT: upload the dist to CPAN
2              
3             use Moose;
4 9     9   6221 with 'Dist::Zilla::Role::BeforeRelease',
  9         28  
  9         89  
5             'Dist::Zilla::Role::Releaser';
6              
7             use Dist::Zilla::Pragmas;
8 9     9   59566  
  9         24  
  9         85  
9             use File::Spec;
10 9     9   79 use Moose::Util::TypeConstraints;
  9         26  
  9         289  
11 9     9   61 use Scalar::Util qw(weaken);
  9         21  
  9         113  
12 9     9   18869 use Dist::Zilla::Util;
  9         25  
  9         638  
13 9     9   65 use Try::Tiny;
  9         37  
  9         218  
14 9     9   65  
  9         22  
  9         572  
15             use namespace::autoclean;
16 9     9   67  
  9         22  
  9         85  
17             #pod =head1 SYNOPSIS
18             #pod
19             #pod If loaded, this plugin will allow the F<release> command to upload to the CPAN.
20             #pod
21             #pod =head1 DESCRIPTION
22             #pod
23             #pod This plugin looks for configuration in your C<dist.ini> or (more
24             #pod likely) C<~/.dzil/config.ini>:
25             #pod
26             #pod [%PAUSE]
27             #pod username = YOUR-PAUSE-ID
28             #pod password = YOUR-PAUSE-PASSWORD
29             #pod
30             #pod If this configuration does not exist, it can read the configuration from
31             #pod C<~/.pause>, in the same format that L<cpan-upload> requires:
32             #pod
33             #pod user YOUR-PAUSE-ID
34             #pod password YOUR-PAUSE-PASSWORD
35             #pod
36             #pod If neither configuration exists, it will prompt you to enter your
37             #pod username and password during the BeforeRelease phase. Entering a
38             #pod blank username or password will abort the release.
39             #pod
40             #pod You can't put your password in your F<dist.ini>. C'mon now!
41             #pod
42             #pod =cut
43              
44             {
45             package
46             Dist::Zilla::Plugin::UploadToCPAN::_Uploader;
47             # CPAN::Uploader will be loaded later if used
48             our @ISA = 'CPAN::Uploader';
49             # Report CPAN::Uploader's version, not ours:
50              
51 0     0   0 my $self = shift;
52             $self->{'Dist::Zilla'}{plugin}->log(@_);
53             }
54 6     6   1696 }
55 6         41  
56             #pod =attr credentials_stash
57             #pod
58             #pod This attribute holds the name of a L<PAUSE stash|Dist::Zilla::Stash::PAUSE>
59             #pod that will contain the credentials to be used for the upload. By default,
60             #pod UploadToCPAN will look for a C<%PAUSE> stash.
61             #pod
62             #pod =cut
63              
64             has credentials_stash => (
65             is => 'ro',
66             isa => 'Str',
67             default => '%PAUSE'
68             );
69              
70             has _credentials_stash_obj => (
71             is => 'ro',
72             isa => maybe_type( class_type('Dist::Zilla::Stash::PAUSE') ),
73             lazy => 1,
74             init_arg => undef,
75             default => sub { $_[0]->zilla->stash_named( $_[0]->credentials_stash ) },
76             );
77              
78             my ($self, $name) = @_;
79              
80             return unless my $stash = $self->_credentials_stash_obj;
81             return $stash->$name;
82 8     8   50 }
83              
84 8 100       277 return { user => 'username' };
85 2         72 }
86              
87             #pod =attr username
88             #pod
89 13     13 0 1971 #pod This option supplies the user's PAUSE username.
90             #pod It will be looked for in the user's PAUSE configuration; if not
91             #pod found, the user will be prompted.
92             #pod
93             #pod =cut
94              
95             has username => (
96             is => 'ro',
97             isa => 'Str',
98             lazy => 1,
99             default => sub {
100             my ($self) = @_;
101             return $self->_credential('username')
102             || $self->pause_cfg->{user}
103             || $self->zilla->chrome->prompt_str("PAUSE username: ");
104             },
105             );
106              
107              
108             #pod =attr password
109             #pod
110             #pod This option supplies the user's PAUSE password. It cannot be provided via
111             #pod F<dist.ini>. It will be looked for in the user's PAUSE configuration; if not
112 0     0 0 0 #pod found, the user will be prompted.
113             #pod
114             #pod =cut
115              
116             has password => (
117             is => 'ro',
118             isa => 'Str',
119             init_arg => undef,
120             lazy => 1,
121             default => sub {
122             my ($self) = @_;
123             my $pw = $self->_credential('password') || $self->pause_cfg->{password};
124              
125             unless ($pw){
126             my $uname = $self->username;
127             $pw = $self->zilla->chrome->prompt_str(
128             "PAUSE password for $uname: ",
129             { noecho => 1 },
130             );
131             }
132              
133             return $pw;
134             },
135             );
136              
137             #pod =attr pause_cfg_file
138             #pod
139             #pod This is the name of the file containing your pause credentials. It defaults
140             #pod F<.pause>. If you give a relative path, it is taken to be relative to
141             #pod L</pause_cfg_dir>.
142             #pod
143             #pod =cut
144              
145             has pause_cfg_file => (
146             is => 'ro',
147             isa => 'Str',
148             lazy => 1,
149             default => sub { '.pause' },
150             );
151              
152             #pod =attr pause_cfg_dir
153             #pod
154             #pod This is the directory for resolving a relative L</pause_cfg_file>.
155             #pod it defaults to the glob expansion of F<~>.
156             #pod
157             #pod =cut
158              
159             has pause_cfg_dir => (
160             is => 'ro',
161             isa => 'Str',
162             lazy => 1,
163             default => sub { Dist::Zilla::Util->homedir },
164             );
165              
166             #pod =attr pause_cfg
167             #pod
168             #pod This is a hashref of defaults loaded from F<~/.pause> -- this attribute is
169             #pod subject to removal in future versions, as the config-loading behavior in
170             #pod CPAN::Uploader is improved.
171             #pod
172             #pod =cut
173              
174             has pause_cfg => (
175             is => 'ro',
176             isa => 'HashRef[Str]',
177             lazy => 1,
178             default => sub {
179             my $self = shift;
180             require CPAN::Uploader;
181             my $file = $self->pause_cfg_file;
182             $file = File::Spec->catfile($self->pause_cfg_dir, $file)
183             unless File::Spec->file_name_is_absolute($file);
184             return {} unless -e $file && -r _;
185             my $cfg = try {
186             CPAN::Uploader->read_config_file($file)
187             } catch {
188             $self->log("Couldn't load credentials from '$file': $_");
189             {};
190             };
191             return $cfg;
192             },
193             );
194              
195             #pod =attr subdir
196             #pod
197             #pod If given, this specifies a subdirectory under the user's home directory to
198             #pod which to upload. Using this option is not recommended.
199             #pod
200             #pod =cut
201              
202             has subdir => (
203             is => 'ro',
204             isa => 'Str',
205             predicate => 'has_subdir',
206             );
207              
208             #pod =attr upload_uri
209             #pod
210             #pod If given, this specifies an alternate URI for the PAUSE upload form. By
211             #pod default, the default supplied by L<CPAN::Uploader> is used. Using this option
212             #pod is not recommended in most cases.
213             #pod
214             #pod =cut
215              
216             has upload_uri => (
217             is => 'ro',
218             isa => 'Str',
219             predicate => 'has_upload_uri',
220             );
221              
222             #pod =attr retries
223             #pod
224             #pod The number of retries to perform on upload failure (5xx response). The default
225             #pod is set to 3 by this plugin. This option will be passed to L<CPAN::Uploader>.
226             #pod
227             #pod =cut
228              
229             has retries => (
230             is => 'ro',
231             isa => 'Int',
232             default => 3,
233             );
234              
235             #pod =attr retry_delay
236             #pod
237             #pod The number of seconds to wait between retries. The default is set to 5 seconds
238             #pod by this plugin. This option will be passed to L<CPAN::Uploader>.
239             #pod
240             #pod =cut
241              
242             has retry_delay => (
243             is => 'ro',
244             isa => 'Int',
245             default => 5,
246             );
247              
248             has uploader => (
249             is => 'ro',
250             isa => 'CPAN::Uploader',
251             lazy => 1,
252             default => sub {
253             my ($self) = @_;
254              
255             # Load the module lazily
256             require CPAN::Uploader;
257             CPAN::Uploader->VERSION('0.103004'); # require HTTPS
258              
259             my $uploader = Dist::Zilla::Plugin::UploadToCPAN::_Uploader->new({
260             user => $self->username,
261             password => $self->password,
262             ($self->has_subdir
263             ? (subdir => $self->subdir) : ()),
264             ($self->has_upload_uri
265             ? (upload_uri => $self->upload_uri) : ()),
266             ($self->retries
267             ? (retries => $self->retries) : ()),
268             ($self->retry_delay
269             ? (retry_delay => $self->retry_delay) : ()),
270             });
271              
272             $uploader->{'Dist::Zilla'}{plugin} = $self;
273             weaken $uploader->{'Dist::Zilla'}{plugin};
274              
275             return $uploader;
276             }
277             );
278              
279             my $self = shift;
280              
281             my $problem;
282             try {
283             for my $attr (qw(username password)) {
284             $problem = $attr;
285             die unless length $self->$attr;
286 5     5 0 35 }
287             undef $problem;
288 5         27 };
289              
290 5     5   362 $self->log_fatal(['You need to supply a %s', $problem]) if $problem;
291 9         44 }
292 9 100       336  
293             my ($self, $archive) = @_;
294 2         7  
295 5         164 $self->uploader->upload_file("$archive");
296             }
297 5 100       862  
298             __PACKAGE__->meta->make_immutable;
299             1;
300              
301 2     2 0 7  
302             =pod
303 2         77  
304             =encoding UTF-8
305              
306             =head1 NAME
307              
308             Dist::Zilla::Plugin::UploadToCPAN - upload the dist to CPAN
309              
310             =head1 VERSION
311              
312             version 6.028
313              
314             =head1 SYNOPSIS
315              
316             If loaded, this plugin will allow the F<release> command to upload to the CPAN.
317              
318             =head1 DESCRIPTION
319              
320             This plugin looks for configuration in your C<dist.ini> or (more
321             likely) C<~/.dzil/config.ini>:
322              
323             [%PAUSE]
324             username = YOUR-PAUSE-ID
325             password = YOUR-PAUSE-PASSWORD
326              
327             If this configuration does not exist, it can read the configuration from
328             C<~/.pause>, in the same format that L<cpan-upload> requires:
329              
330             user YOUR-PAUSE-ID
331             password YOUR-PAUSE-PASSWORD
332              
333             If neither configuration exists, it will prompt you to enter your
334             username and password during the BeforeRelease phase. Entering a
335             blank username or password will abort the release.
336              
337             You can't put your password in your F<dist.ini>. C'mon now!
338              
339             =head1 PERL VERSION
340              
341             This module should work on any version of perl still receiving updates from
342             the Perl 5 Porters. This means it should work on any version of perl released
343             in the last two to three years. (That is, if the most recently released
344             version is v5.40, then this module should work on both v5.40 and v5.38.)
345              
346             Although it may work on older versions of perl, no guarantee is made that the
347             minimum required version will not be increased. The version may be increased
348             for any reason, and there is no promise that patches will be accepted to lower
349             the minimum required perl.
350              
351             =head1 ATTRIBUTES
352              
353             =head2 credentials_stash
354              
355             This attribute holds the name of a L<PAUSE stash|Dist::Zilla::Stash::PAUSE>
356             that will contain the credentials to be used for the upload. By default,
357             UploadToCPAN will look for a C<%PAUSE> stash.
358              
359             =head2 username
360              
361             This option supplies the user's PAUSE username.
362             It will be looked for in the user's PAUSE configuration; if not
363             found, the user will be prompted.
364              
365             =head2 password
366              
367             This option supplies the user's PAUSE password. It cannot be provided via
368             F<dist.ini>. It will be looked for in the user's PAUSE configuration; if not
369             found, the user will be prompted.
370              
371             =head2 pause_cfg_file
372              
373             This is the name of the file containing your pause credentials. It defaults
374             F<.pause>. If you give a relative path, it is taken to be relative to
375             L</pause_cfg_dir>.
376              
377             =head2 pause_cfg_dir
378              
379             This is the directory for resolving a relative L</pause_cfg_file>.
380             it defaults to the glob expansion of F<~>.
381              
382             =head2 pause_cfg
383              
384             This is a hashref of defaults loaded from F<~/.pause> -- this attribute is
385             subject to removal in future versions, as the config-loading behavior in
386             CPAN::Uploader is improved.
387              
388             =head2 subdir
389              
390             If given, this specifies a subdirectory under the user's home directory to
391             which to upload. Using this option is not recommended.
392              
393             =head2 upload_uri
394              
395             If given, this specifies an alternate URI for the PAUSE upload form. By
396             default, the default supplied by L<CPAN::Uploader> is used. Using this option
397             is not recommended in most cases.
398              
399             =head2 retries
400              
401             The number of retries to perform on upload failure (5xx response). The default
402             is set to 3 by this plugin. This option will be passed to L<CPAN::Uploader>.
403              
404             =head2 retry_delay
405              
406             The number of seconds to wait between retries. The default is set to 5 seconds
407             by this plugin. This option will be passed to L<CPAN::Uploader>.
408              
409             =head1 AUTHOR
410              
411             Ricardo SIGNES 😏 <cpan@semiotic.systems>
412              
413             =head1 COPYRIGHT AND LICENSE
414              
415             This software is copyright (c) 2022 by Ricardo SIGNES.
416              
417             This is free software; you can redistribute it and/or modify it under
418             the same terms as the Perl 5 programming language system itself.
419              
420             =cut