File Coverage

blib/lib/Nexus/Uploader.pm
Criterion Covered Total %
statement 35 61 57.3
branch 0 6 0.0
condition 0 3 0.0
subroutine 12 15 80.0
pod 3 3 100.0
total 50 88 56.8


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