File Coverage

blib/lib/Shipwright/Source.pm
Criterion Covered Total %
statement 63 65 96.9
branch 28 32 87.5
condition 8 21 38.1
subroutine 10 10 100.0
pod 2 2 100.0
total 111 130 85.3


line stmt bran cond sub pod time code
1             package Shipwright::Source;
2              
3 3     3   438 use warnings;
  3         5  
  3         117  
4 3     3   12 use strict;
  3         3  
  3         69  
5 3     3   438 use UNIVERSAL::require;
  3         1000  
  3         20  
6 3     3   62 use File::Temp qw/tempdir/;
  3         4  
  3         132  
7 3     3   14 use File::Spec::Functions qw/catfile catdir/;
  3         4  
  3         113  
8 3     3   13 use Shipwright::Util;
  3         3  
  3         219  
9 3     3   14 use File::Path qw/make_path/;
  3         3  
  3         1998  
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 12     12 1 7425 my $class = shift;
36 12         60 my %args = (
37             follow => 1,
38             directory => $ENV{SHIPWRIGHT_SOURCE_ROOT},
39             @_,
40             );
41              
42 12   33     74 $args{download_directory} ||=
43             catdir( shipwright_user_root(), 'downloads' );
44              
45 12   33     67 $args{scripts_directory} ||= catdir( $args{directory}, '__scripts' );
46 12   33     64 $args{map_path} ||= catfile( $args{directory}, 'map.yml' );
47 12   33     60 $args{url_path} ||= catfile( $args{directory}, 'url.yml' );
48 12   33     53 $args{version_path} ||= catfile( $args{directory}, 'version.yml' );
49 12   33     52 $args{branches_path} ||= catfile( $args{directory}, 'branches.yml' );
50              
51 12         23 for (qw/map_path url_path version_path branches_path/) {
52 48 100       568 next if -e $args{$_};
53 8 50       484 open my $fh, '>', $args{$_} or confess_or_die "can't write to $args{$_}: $!";
54 8         132 close $fh;
55             }
56              
57 12 100       80 confess_or_die "need source arg" unless exists $args{source};
58              
59 11         18 for my $dir (qw/directory download_directory scripts_directory/) {
60 33 100       861 make_path( $args{$dir} ) unless -e $args{$dir};
61             }
62              
63 11         35 my $type = type( \$args{source} );
64              
65 11 100       28 confess_or_die "invalid source: $args{source}" unless $type;
66              
67 10         17 my $module = 'Shipwright::Source::' . $type;
68 10         85 $module->require;
69 10         195 return $module->new(%args);
70             }
71              
72             =head2 type
73              
74             =cut
75              
76             sub type {
77 11     11 1 14 my $source = shift;
78 11 100       21 return unless $$source;
79              
80 10         21 _translate_source($source);
81              
82 10 100       52 if ( $$source =~ /\.(?:tar\.(?:gz|bz2)|tgz|tbz|zip)$/ ) {
83 4 100 66     70 if ( $$source =~ s/^file://i || -f $$source ) {
84 1         3 return 'Compressed';
85             }
86             }
87              
88 9 100       35 return 'Directory' if $$source =~ s/^dir(?:ectory)?:(?!:\w+)//i;
89              
90 8 50       28 return 'Shipyard' if $$source =~ s/^(?:shipyard|shipwright)://i;
91              
92             # prefix that can be omitted
93 8         13 for my $type (qw/svn http ftp git/) {
94 26 100       296 if ( $$source =~ /^$type:(?!:\w+)/i ) {
95 4         38 $$source =~ s{^$type:(?!//)}{}i;
96 4 100       24 return $type eq 'git' ? 'Git' : uc $type;
97             }
98             }
99              
100 4 100       18 if ( $$source =~ m{^(//|svk:(?!:\w+))}i ) {
101 1         3 $$source =~ s/^svk://i;
102 1         3 return 'SVK';
103             }
104              
105 3 50       18 return 'Directory' if -d $$source;
106              
107             # default is cpan module or distribution
108 3         13 $$source =~ s/^cpan:(?!:\w+)//i;
109              
110             # in case typos like IO:File
111 3         8 $$source =~ s/(?
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       14 $$source =~ s/-/::/g
116             unless $$source =~ /\.(?:tar\.(?:gz|bz2)|tgz|tbz)$/;
117              
118 3         10 return 'CPAN';
119              
120             }
121              
122             sub _translate_source {
123 10     10   10 my $source = shift;
124 10 50       34 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__