File Coverage

blib/lib/Nexus/Uploader.pm
Criterion Covered Total %
statement 10 12 83.3
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 14 16 87.5


line stmt bran cond sub pod time code
1 1     1   642 use strict;
  1         2  
  1         47  
2 1     1   6 use warnings;
  1         2  
  1         67  
3              
4             package Nexus::Uploader;
5             $Nexus::Uploader::VERSION = '0.0.1';
6              
7             # ABSTRACT: Upload files to a Sonatype Nexus instance. Modelled on L<CPAN::Uploader>.
8              
9 1     1   850 use utf8;
  1         10  
  1         6  
10 1     1   199 use Moose;
  0            
  0            
11              
12             use Carp;
13             use JSON;
14             use MIME::Base64;
15             use REST::Client;
16             use Log::Any qw($log);
17              
18             use namespace::autoclean;
19              
20              
21             has username => (
22             is => 'ro',
23             isa => 'Str',
24             required => 1,
25             default => 'anonymous',
26             );
27              
28              
29             has password => (
30             is => 'ro',
31             isa => 'Str',
32             required => 1,
33             default => '',
34             );
35              
36              
37             has nexus_URL => (
38             is => 'ro',
39             isa => 'Str',
40             required => 1,
41             default => 'http://localhost:8081/nexus/',
42             );
43              
44              
45             has repository =>
46             ( is => 'ro', isa => 'Str', required => 1, default => 'releases', );
47              
48              
49             has group =>
50             ( is => 'ro', isa => 'Str', required => 1, default => 'AUTHORID', );
51              
52              
53             has artefact => ( is => 'ro', isa => 'Str', required => 1, );
54              
55              
56             has version => (
57             is => 'ro',
58             isa => 'Str',
59             required => 1,
60             );
61              
62              
63             around [qw(group artefact)] => sub {
64             my $orig = shift;
65             my $self = shift;
66             my $value = $self->$orig;
67             $value =~ s/::/-/g;
68             $value =~ s#/#.#g;
69             return $value;
70             };
71              
72              
73             sub upload_file {
74             my $self = shift;
75             my $archive = shift;
76             if ( !-f $archive ) {
77             croak "Unable to find file '$archive'\n";
78             }
79             my $arguments = shift;
80             if ($arguments) {
81             my $uploader = __PACKAGE__->new(%$arguments);
82             return $uploader->upload_file($archive);
83             }
84              
85             use Data::Dumper;
86             $log->debug( Dumper($self) );
87              
88             my $headers = {
89             Accept => 'application/json',
90             Authorization => 'Basic '
91             . encode_base64( $self->username . ':' . $self->password )
92             };
93              
94             my $Nexus = REST::Client->new();
95             $Nexus->setFollow(1);
96             my $artefact_URL =
97              
98             join( '/',
99             $self->nexus_URL,
100             'content/repositories',
101             $self->repository,
102             $self->group,
103             $self->artefact,
104             $self->version,
105             $self->artefact . '-' . $self->version . '.tar.gz' );
106             $log->debug($artefact_URL);
107             $Nexus->PUT( $artefact_URL, $archive, $headers );
108             $log->debug( $Nexus->responseCode() );
109             my $rc = $Nexus->responseCode();
110              
111             if ( 400 <= $rc && $rc <= 599 ) {
112             croak( "HTTP error $rc: " . $Nexus->responseContent() );
113             }
114             }
115              
116              
117             sub log {
118             my $self = shift;
119             $log->info(@_);
120             }
121              
122              
123             sub log_debug {
124             my $self = shift;
125             $log->debug(@_);
126             }
127              
128             __PACKAGE__->meta->make_immutable;
129              
130             1;
131              
132             __END__
133              
134             =pod
135              
136             =encoding UTF-8
137              
138             =head1 NAME
139              
140             Nexus::Uploader - Upload files to a Sonatype Nexus instance. Modelled on L<CPAN::Uploader>.
141              
142             =head1 VERSION
143              
144             version 0.0.1
145              
146             =head1 ATTRIBUTES
147              
148             =head2 username
149              
150             This is the Nexus user to log in with. It defaults to C<anonymous>.
151              
152             =head2 password
153              
154             The Nexus password. It is *strongly* advised that you take advantage of the Nexus user tokens feature!
155              
156             Default is the empty string.
157              
158             =head2 nexus_URL
159              
160             The Nexus URL (base URL) to use. Defaults to L<http://localhost:8081/nexus>.
161              
162             =head2 repository
163              
164             Name of the repository to use for uploads. Defaults to C<releases>.
165              
166             =head2 group
167              
168             The group to use when uploading. The C<group> is a Maven concept, and the best approximation to CPAN is probably the CPAN author ID.
169              
170             Defaults to C<AUTHORID> if not provided.
171              
172             =head2 artefact
173              
174             The artefact name to use when uploading - there is no default. A good value for CPAN modules would be the distribution name.
175              
176             =head2 version
177              
178             The version of the artefact being uploaded. There is no default.
179              
180             =head2 group and artefact processing
181              
182             C<group> and C<artefact> atrributes have colons and full stops modified as follows:
183              
184             :: goes to -
185             . goes to /
186              
187             This is in order to maintain compatibility with Maven's conventions.
188              
189             =head1 METHODS
190              
191             =head2 upload_file
192              
193             The method that does the grunt work of uploading (via a PUT request) to a standard Nexus repository, i.e. not the Staging suite.
194              
195             Nexus::Uploader->upload_file($file, \%arguments);
196              
197             $uploader->upload_file($file);
198              
199             Valid C<%arguments> are the attributes specified above.
200              
201             =head2 log
202              
203             Included for compatibility with L<CPAN::Uploader> - passes straight through to the C<info> logging level.
204              
205             =head2 log_debug
206              
207             Included for compatibility with L<CPAN::Uploader> - passes straight through to the C<debug> logging level.
208              
209              
210             =head1 SEE ALSO
211              
212             - L<CPAN::Uploader>
213              
214             =head1 AUTHOR
215              
216             Brad Macpherson <brad@teched-creations.com>
217              
218             =head1 COPYRIGHT AND LICENSE
219              
220             This software is copyright (c) 2014 by Brad Macpherson.
221              
222             This is free software; you can redistribute it and/or modify it under
223             the same terms as the Perl 5 programming language system itself.
224              
225             =cut