File Coverage

blib/lib/Dist/Zilla/Plugin/NexusRelease.pm
Criterion Covered Total %
statement 49 50 98.0
branch 5 6 83.3
condition n/a
subroutine 15 15 100.0
pod 1 1 100.0
total 70 72 97.2


line stmt bran cond sub pod time code
1 1     1   74607 use strict;
  1         2  
  1         21  
2 1     1   3 use warnings;
  1         1  
  1         16  
3 1     1   7 use v5.8.0;
  1         2  
4              
5             package Dist::Zilla::Plugin::NexusRelease;
6             $Dist::Zilla::Plugin::NexusRelease::VERSION = '1.0.1';
7              
8             # ABSTRACT: Release a Dist::Zilla build to a Sonatype Nexus instance.
9              
10 1     1   484 use utf8;
  1         7  
  1         4  
11 1     1   462 use Moose;
  1         266969  
  1         7  
12             with 'Dist::Zilla::Role::Releaser';
13              
14 1     1   5432 use Log::Any qw($log);
  1         8400  
  1         4  
15 1     1   2933 use Moose::Util::TypeConstraints;
  1         1  
  1         9  
16 1     1   1257 use Scalar::Util qw(weaken);
  1         1  
  1         51  
17 1     1   3 use Carp;
  1         1  
  1         39  
18              
19 1     1   404 use namespace::autoclean;
  1         5462  
  1         3  
20              
21             {
22              
23             package Dist::Zilla::Plugin::NexusRelease::_Uploader;
24             $Dist::Zilla::Plugin::NexusRelease::_Uploader::VERSION = '1.0.1';
25             # Nexus::Uploader will be loaded later if used
26             our @ISA = 'Nexus::Uploader';
27              
28             sub log {
29 8     8   1741 my $self = shift;
30 8         30 $self->{'Dist::Zilla'}{plugin}->log(@_);
31             }
32             }
33              
34              
35             has credentials_stash => (
36             is => 'ro',
37             isa => 'Str',
38             default => '%Nexus'
39             );
40              
41             has _credentials_stash_obj => (
42             is => 'ro',
43             isa => maybe_type( class_type('Dist::Zilla::Stash::Nexus') ),
44             lazy => 1,
45             init_arg => undef,
46             default => sub { $_[0]->zilla->stash_named( $_[0]->credentials_stash ) },
47             );
48              
49             sub _credential {
50 6     6   12 my ( $self, $name ) = @_;
51              
52 6 50       190 return unless my $stash = $self->_credentials_stash_obj;
53 0         0 return $stash->$name;
54             }
55              
56              
57             has username => (
58             is => 'ro',
59             isa => 'Str',
60             required => 1,
61             lazy => 1,
62             default => sub {
63             my $self = shift;
64             return $self->_credential('username')
65             || $self->zilla->chrome->prompt_str('Nexus username: ');
66             }
67             );
68              
69              
70             has password => (
71             is => 'ro',
72             isa => 'Str',
73             required => 1,
74             lazy => 1,
75             default => sub {
76             my $self = shift;
77             return $self->_credential('password')
78             || $self->zilla->chrome->prompt_str( 'Nexus password: ',
79             { noecho => 1 } );
80             }
81             );
82              
83              
84             has nexus_URL => (
85             is => 'ro',
86             isa => 'Str',
87             required => 1,
88             default => 'http://localhost:8081/nexus/repository/maven-releases',
89             );
90              
91              
92             has group => (
93             is => 'ro',
94             isa => 'Str',
95             lazy => 1,
96             default => sub {
97             my $self = shift;
98             return $self->zilla->chrome->prompt_str('Nexus group: ');
99             },
100             );
101              
102              
103             has artefact =>
104             ( is => 'ro', isa => 'Str', lazy => 1, builder => '_build_artefact' );
105 1     1   2 sub _build_artefact { my $self = shift; $self->zilla->name; }
  1         26  
106              
107              
108             has version =>
109             ( is => 'ro', isa => 'Str', lazy => 1, builder => '_build_version' );
110 1     1   1 sub _build_version { my $self = shift; $self->zilla->version; }
  1         28  
111              
112              
113             has uploader => (
114             is => 'ro',
115             isa => 'Nexus::Uploader',
116             lazy => 1,
117             default => sub {
118             my ($self) = @_;
119              
120             # Load the module lazily
121             require Nexus::Uploader;
122              
123             my $uploader = Dist::Zilla::Plugin::NexusRelease::_Uploader->new(
124             { username => $self->username,
125             password => $self->password,
126             nexus_URL => $self->nexus_URL,
127             group => $self->group,
128             artefact => $self->artefact,
129             version => $self->version,
130             }
131             );
132              
133             $uploader->{'Dist::Zilla'}{plugin} = $self;
134             weaken $uploader->{'Dist::Zilla'}{plugin};
135              
136             return $uploader;
137             }
138             );
139              
140             sub release {
141 3     3 1 243782 my $self = shift;
142 3         7 my $archive = shift;
143              
144 3         8 my @missing_attributes = ();
145 3         10 for my $attr (qw(username password group)) {
146 9 100       343 if ( !length $self->$attr ) {
147 4         27 $self->log("You need to supply a $attr");
148 4         1086 push( @missing_attributes, $attr );
149             }
150             }
151 3 100       8 if ( scalar @missing_attributes ) {
152 2         406 croak "Missing attributes " . join( ', ', @missing_attributes );
153             }
154              
155 1         49 my $uploader = $self->uploader;
156              
157 1         23 printf "Releasing to Sonatype Nexus: repository '%s', GAV '%s:%s:%s'\n",
158             $uploader->nexus_URL, $uploader->group, $uploader->artefact,
159             $uploader->version;
160 1         191 $uploader->upload_file("$archive");
161             }
162              
163             __PACKAGE__->meta->make_immutable;
164              
165             1;
166              
167             __END__
168              
169             =pod
170              
171             =encoding UTF-8
172              
173             =head1 NAME
174              
175             Dist::Zilla::Plugin::NexusRelease - Release a Dist::Zilla build to a Sonatype Nexus instance.
176              
177             =head1 VERSION
178              
179             version 1.0.1
180              
181             =head1 SYNOPSIS
182              
183             If loaded, this plugin will allow the C<release> command to upload a distribution to a Sonatype Nexus instance.
184              
185             =head1 DESCRIPTION
186              
187             This plugin looks for configuration in your F<dist.ini> or (more
188             likely) F<~/.dzil/config.ini>:
189              
190             [NexusRelease]
191             username = Nexus tokenised username
192             password = Nexus tokenised password
193             group = Nexus group ID to use for the upload
194              
195             The following are optional but very likely to be used:
196              
197             nexus_URL = Nexus repository URL
198              
199             The Nexus Artefact is set to the Perl distribution name (C<name> in F<dist.ini>, and the version is set to the Perl distribution version.
200              
201             =head1 ATTRIBUTES
202              
203             =head2 credentials_stash
204              
205             This attribute holds the name of a L<Nexus stash|Dist::Zilla::Stash::Nexus>
206             that will contain the credentials to be used for the upload. By default,
207             NexusRelease will look for a C<%Nexus> stash.
208              
209             =head2 username
210              
211             This is the Nexus user to log in with.
212              
213             User will be prompted if this is not set in the C<%Nexus> stash.
214              
215             =head2 password
216              
217             The Nexus password. It is *strongly* advised that you take advantage of the Nexus user tokens feature!
218              
219             User will be prompted if this is not set in the C<%Nexus> stash.
220              
221             =head2 nexus_URL
222              
223             The Nexus URL (base URL) to use. Defaults to L<http://localhost:8081/nexus/repository/maven-releases>.
224              
225             =head2 group
226              
227             The group to use when uploading. There is no default although a reasonable value would be your CPAN ID.
228              
229             User will be prompted if this is not set in F<dist.ini>.
230              
231             =head2 artefact
232              
233             The artefact name to use when uploading - defaults to the distribution name.
234              
235             =head2 version
236              
237             The version of the distribution - defaults to the $VERSION set in the distribution.
238              
239             =head1 METHODS
240              
241             =head2 release
242              
243             The C<release> method required by L<Dist::Zilla::Role::Releaser>.
244              
245             =head1 SEE ALSO
246              
247             - L<Nexus::Uploader>
248             - L<Dist::Zilla::Plusin::UploadToCPAN>
249              
250             =head1 AUTHOR
251              
252             Brad Macpherson <brad@teched-creations.com>
253              
254             =head1 COPYRIGHT AND LICENSE
255              
256             This software is copyright (c) 2016 by Brad Macpherson.
257              
258             This is free software; you can redistribute it and/or modify it under
259             the same terms as the Perl 5 programming language system itself.
260              
261             =cut