File Coverage

blib/lib/Dist/Zilla/Plugin/NexusRelease.pm
Criterion Covered Total %
statement 43 43 100.0
branch 4 4 100.0
condition n/a
subroutine 13 13 100.0
pod 1 1 100.0
total 61 61 100.0


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