File Coverage

blib/lib/Shipwright/Source.pm
Criterion Covered Total %
statement 63 65 96.9
branch 30 34 88.2
condition 11 24 45.8
subroutine 10 10 100.0
pod 2 2 100.0
total 116 135 85.9


line stmt bran cond sub pod time code
1             package Shipwright::Source;
2              
3 3     3   462 use warnings;
  3         5  
  3         98  
4 3     3   13 use strict;
  3         4  
  3         82  
5 3     3   597 use UNIVERSAL::require;
  3         1109  
  3         23  
6 3     3   73 use File::Temp qw/tempdir/;
  3         6  
  3         166  
7 3     3   14 use File::Spec::Functions qw/catfile catdir/;
  3         4  
  3         128  
8 3     3   12 use Shipwright::Util;
  3         5  
  3         217  
9 3     3   19 use File::Path qw/make_path/;
  3         3  
  3         1921  
10              
11             =head1 NAME
12              
13             Shipwright::Source - Source
14              
15             =head1 SYNOPSIS
16              
17             shipwright import -r ... cpan:XML::LibXML
18              
19             use Shipwright::Source;
20              
21             =head1 SUPPORTED SOURCES
22              
23             Currently, the supported sources are L, L, L, L, L, L, L, L and L.
24              
25             =head1 METHODS
26              
27             =head2 new
28              
29             =cut
30              
31             $ENV{SHIPWRIGHT_SOURCE_ROOT} ||=
32             tempdir( 'shipwright_source_XXXXXX', CLEANUP => 1, TMPDIR => 1 );
33              
34             sub new {
35 14     14 1 3525 my $class = shift;
36 14         63 my %args = (
37             follow => 1,
38             directory => $ENV{SHIPWRIGHT_SOURCE_ROOT},
39             @_,
40             );
41              
42 14   33     77 $args{download_directory} ||=
43             catdir( shipwright_user_root(), 'downloads' );
44              
45 14   33     77 $args{scripts_directory} ||= catdir( $args{directory}, '__scripts' );
46 14   33     72 $args{map_path} ||= catfile( $args{directory}, 'map.yml' );
47 14   33     65 $args{url_path} ||= catfile( $args{directory}, 'url.yml' );
48 14   33     61 $args{version_path} ||= catfile( $args{directory}, 'version.yml' );
49 14   33     61 $args{branches_path} ||= catfile( $args{directory}, 'branches.yml' );
50              
51 14         29 for (qw/map_path url_path version_path branches_path/) {
52 56 100       646 next if -e $args{$_};
53 8 50       341 open my $fh, '>', $args{$_} or confess_or_die "can't write to $args{$_}: $!";
54 8         58 close $fh;
55             }
56              
57 14 100       77 confess_or_die "need source arg" unless exists $args{source};
58              
59 13         19 for my $dir (qw/directory download_directory scripts_directory/) {
60 39 100       870 make_path( $args{$dir} ) unless -e $args{$dir};
61             }
62              
63 13         37 my $type = type( \$args{source} );
64              
65 13 100       46 confess_or_die "invalid source: $args{source}" unless $type;
66              
67 10         17 my $module = 'Shipwright::Source::' . $type;
68 10         81 $module->require;
69 10         194 return $module->new(%args);
70             }
71              
72             =head2 type
73              
74             =cut
75              
76             sub type {
77 13     13 1 19 my $source = shift;
78 13 100       23 return unless $$source;
79              
80 12         27 _translate_source($source);
81              
82 12 100       40 if ( $$source =~ /\.(?:tar\.(?:gz|bz2)|tgz|tbz|zip)$/ ) {
83 4 100 66     34 if ( $$source =~ s/^file://i || -f $$source ) {
84 1         3 return 'Compressed';
85             }
86             }
87              
88 11 100       50 return 'Directory' if $$source =~ s/^dir(?:ectory)?:(?!:\w+)//i;
89              
90 10 50       29 return 'Shipyard' if $$source =~ s/^(?:shipyard|shipwright)://i;
91              
92             # prefix that can be omitted
93 10         17 for my $type (qw/svn http ftp git/) {
94 34 100       345 if ( $$source =~ /^$type:(?!:\w+)/i ) {
95 4         32 $$source =~ s{^$type:(?!//)}{}i;
96 4 100       22 return $type eq 'git' ? 'Git' : uc $type;
97             }
98             }
99              
100 6 100       28 if ( $$source =~ m{^(//|svk:(?!:\w+))}i ) {
101 1         4 $$source =~ s/^svk://i;
102 1         4 return 'SVK';
103             }
104              
105 5 50       33 return 'Directory' if -d $$source;
106              
107             # default is cpan module or distribution
108 5         15 $$source =~ s/^cpan:(?!:\w+)//i;
109              
110 5 100 100     34 return if $$source =~ /:/ && $$source !~ /::/;
111              
112              
113             # if it's not a distribution name like
114             # 'S/SU/SUNNAVY/IP-QQWry-v0.0.15.tar.gz', convert '-' to '::'.
115 3 100       16 $$source =~ s/-/::/g
116             unless $$source =~ /\.(?:tar\.(?:gz|bz2)|tgz|tbz)$/;
117              
118 3         14 return 'CPAN';
119              
120             }
121              
122             sub _translate_source {
123 12     12   13 my $source = shift;
124 12 50       36 if ( $$source =~ /^(file|dir(ectory)?|shipwright):~/i ) {
125              
126             # replace prefix ~ with real home dir
127 0           $$source =~ s/~/user_home/e;
  0            
128             }
129             }
130              
131             1;
132              
133             __END__