File Coverage

blib/lib/Shipwright/Source.pm
Criterion Covered Total %
statement 63 65 96.9
branch 29 34 85.2
condition 8 21 38.1
subroutine 10 10 100.0
pod 2 2 100.0
total 112 132 84.8


line stmt bran cond sub pod time code
1             package Shipwright::Source;
2              
3 3     3   411 use warnings;
  3         7  
  3         79  
4 3     3   15 use strict;
  3         3  
  3         48  
5 3     3   245 use UNIVERSAL::require;
  3         758  
  3         20  
6 3     3   66 use File::Temp qw/tempdir/;
  3         5  
  3         119  
7 3     3   15 use File::Spec::Functions qw/catfile catdir/;
  3         6  
  3         104  
8 3     3   14 use Shipwright::Util;
  3         5  
  3         203  
9 3     3   15 use File::Path qw/make_path/;
  3         6  
  3         2009  
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 3332 my $class = shift;
36             my %args = (
37             follow => 1,
38             directory => $ENV{SHIPWRIGHT_SOURCE_ROOT},
39 12         49 @_,
40             );
41              
42             $args{download_directory} ||=
43 12   33     67 catdir( shipwright_user_root(), 'downloads' );
44              
45 12   33     739 $args{scripts_directory} ||= catdir( $args{directory}, '__scripts' );
46 12   33     73 $args{map_path} ||= catfile( $args{directory}, 'map.yml' );
47 12   33     65 $args{url_path} ||= catfile( $args{directory}, 'url.yml' );
48 12   33     64 $args{version_path} ||= catfile( $args{directory}, 'version.yml' );
49 12   33     59 $args{branches_path} ||= catfile( $args{directory}, 'branches.yml' );
50              
51 12         27 for (qw/map_path url_path version_path branches_path/) {
52 48 100       345 next if -e $args{$_};
53 8 50       207 open my $fh, '>', $args{$_} or confess_or_die "can't write to $args{$_}: $!";
54 8         47 close $fh;
55             }
56              
57 12 100       29 confess_or_die "need source arg" unless exists $args{source};
58              
59 11         19 for my $dir (qw/directory download_directory scripts_directory/) {
60 33 100       678 make_path( $args{$dir} ) unless -e $args{$dir};
61             }
62              
63 11         35 my $type = type( \$args{source} );
64              
65 11 100       33 confess_or_die "invalid source: $args{source}" unless $type;
66              
67 10         25 my $module = 'Shipwright::Source::' . $type;
68 10         80 $module->require;
69 10         217 return $module->new(%args);
70             }
71              
72             =head2 type
73              
74             =cut
75              
76             sub type {
77 11     11 1 16 my $source = shift;
78 11 100       27 return unless $$source;
79              
80 10         22 _translate_source($source);
81              
82 10 100       45 if ( $$source =~ /\.(?:tar\.(?:gz|bz2)|tgz|tbz|zip)$/ ) {
83 4 100 66     62 if ( $$source =~ s/^file://i || -f $$source ) {
84 1         4 return 'Compressed';
85             }
86             }
87              
88 9 100       41 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         16 for my $type (qw/svn http https ftp git/) {
94 32 100       291 if ( $$source =~ /^$type:(?!:\w+)/i ) {
95 4         49 $$source =~ s{^$type:(?!//)}{}i;
96 4 50       28 return $type eq 'git' ? 'Git' : $type eq 'https' ? 'HTTP' : uc $type;
    100          
97             }
98             }
99              
100 4 100       23 if ( $$source =~ m{^(//|svk:(?!:\w+))}i ) {
101 1         4 $$source =~ s/^svk://i;
102 1         3 return 'SVK';
103             }
104              
105 3 50       70 return 'Directory' if -d $$source;
106              
107             # default is cpan module or distribution
108 3         21 $$source =~ s/^cpan:(?!:\w+)//i;
109              
110             # in case typos like IO:File
111 3         11 $$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       18 $$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   18 my $source = shift;
124 10 50       29 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__