blib/lib/App/GitGitr.pm

filter: Statementstmt 30.1% 32 / 106 Branchbran 0.0% 0 / 46 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 45.4% 10 / 22 Podpod 100% 2 / 2 totaltotal 23.2% 44 / 189 CC 27 SCAR 40.0
CC
27
Cov
20%
execute
182.0
_build_version
20.0
_download
12.0
CRAP 54.8
↑ Summary
1 package App::GitGitr;
2 1 our $AUTHORITY = 'cpan:GENEHACK';
3 1 $App::GitGitr::VERSION = '0.907';
4 # ABSTRACT: GitGitr command support. See L<gitgitr> for full documentation.
5  
6 1 ▶use parent 'App::Cmd::Simple';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶use autodie qw/ :all /;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 ▶use Archive::Extract;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use File::Remove 'remove';
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use HTML::TreeBuilder::XPath;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use HTTP::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 0 ▶sub opt_spec {
Subroutine opt_spec: not called CC=1 SCAR=6.9
Pod documented
21 return (
22 0 [ "no_symlink|N" => 'do not symlink final build to /opt/git' ],
23 [ "prefix|p" => 'directory to install under (defaults to /opt/git-$VERSION)' ],
24 [ "reinstall|r" => 'build even if installation directory exists (default=false)' ],
25 [ "run_tests|t" => 'run "make test" after building' ] ,
26 [ "verbose|V" => 'be verbose about what is being done' ] ,
27 [ "version|v=s" => 'Which git version to build. Default = most recent' ] ,
28 );
29 }
30  
31 0 ▶sub execute {
Subroutine execute: not called CC=13 SCAR=52.0
Pod documented
32 0 my( $self , $opt , $args ) = @_;
33  
34 0 ▶ my $version = $opt->{version} // _build_version();
Condition0%0/3
$opt->{"version"} // _build_version()
l!l&&r!l&&!r
000
MC/DC0%0/2
$opt->{"version"} // _build_version()
$opt->{"version"} _build_version()
Truth table0%0/3
A // B
A= $opt->{"version"}B= _build_version()
ABresult
1X1
011
000
35 0 ▶ my $install_dir = $opt->{prefix} // "/opt/git-$version";
Condition0%0/2
$opt->{"prefix"} // "/opt/git-$version"
l!l
00
MC/DC0%0/1
$opt->{"prefix"} // "/opt/git-$version"
$opt->{"prefix"}
Truth table0%0/2
A
A= $opt->{"prefix"}
Aresult
11
00
36  
37 say "CURRENT VERSION: $version"
38 0 ▶ if $opt->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{"verbose"} 0 0 0
39  
40 0 ▶ if ( -e $install_dir and ! $opt->{reinstall} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $install_dir and not $opt->{'reinstall'}) { } 0 0 0
Condition0%0/3
-e $install_dir and not $opt->{"reinstall"}
!ll&&!rl&&r
000
MC/DC0%0/2
-e $install_dir and not $opt->{"reinstall"}
-e $install_dir not $opt->{"reinstall"}
Truth table0%0/3
A and not(B)
A= -e $install_dirB= not $opt->{"reinstall"}
ABresult
0X0
100
111
41 0 ▶ if( $opt->{no_symlink} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{'no_symlink'}) { } 0 0 0
42 0 say "Most recent version ($version) already installed.";
43 }
44 else {
45 0 $self->_symlink( $opt , $version );
46 0 say "Most recent version ($version) already installed. /opt/git redirected to that version";
47 }
48 }
49 else {
50 0 chdir( '/tmp' );
51  
52 say "BUILD/INSTALL git-$version"
53 0 ▶ if $opt->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{"verbose"} 0 0 0
54  
55 0 my $pkg_path = $self->_download( $opt , $version );
56 0 $self->_extract( $opt , $pkg_path );
57 0 $self->_configure( $opt , $version , $install_dir );
58 0 $self->_make( $opt );
59 0 ▶ $self->_make_test( $opt ) if $opt->{run_tests};
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{"run_tests"} 0 0 0
60 0 $self->_make_install( $opt );
61 0 $self->_cleanup( $opt , $version );
62 0 ▶ $self->_symlink( $opt , $version ) unless $opt->{no_symlink};
Branch: 1 branch
BranchTrueFalseTotal
unless $opt->{"no_symlink"} 0 0 0
63  
64 say "\n\nBuilt new git $version."
65 0 ▶ if $opt->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{"verbose"} 0 0 0
66 say "/opt/git symlink switched to new version ($version)."
67 0 ▶ unless $opt->{no_symlink};
Branch: 1 branch
BranchTrueFalseTotal
unless $opt->{"no_symlink"} 0 0 0
68 }
69  
70 0 ▶ die "No new version?!"
Branch: 1 branch
BranchTrueFalseTotal
unless -e "/opt/git-$version" 0 0 0
71 unless -e "/opt/git-$version";
72  
73 }
74  
75 0 ▶sub _build_version {
Subroutine _build_version: not called CC=4 SCAR=30.0
76 0 my $response = HTTP::Tiny->new->get('http://git-scm.com/');
77 0 ▶ unless ( $response->{success} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->{"success"}) 0 0 0
78 my $error = sprintf "Failed to fetch content from Git web page!\nSTATUS: %s REASON: %s" ,
79 0 $response->{status}, $response->{reason};
80 0 croak $error;
81 }
82  
83 0 my $tree = HTML::TreeBuilder::XPath->new;
84 $tree->parse_content( $response->{content} )
85 0 ▶ or croak "Failed to parse content from Git web page!";
Branch: 1 branch
BranchTrueFalseTotal
$tree->parse_content($response->{"content"}) or croak("Failed to parse content from Git web page!") 0 0 0
86  
87 0 ▶ my $version = $tree->findvalue('/html/body//span[@class="version"]')
Branch: 1 branch
BranchTrueFalseTotal
my $version = $tree->findvalue("/html/body//span[\@class=\"version\"]") or croak("Can't parse version from Git web page!") 0 0 0
88 or croak "Can't parse version from Git web page!";
89 0 $version =~ s/^\s*//; $version =~ s/\s*$//;
90  
91 0 return $version;
92 }
93  
94 0 ▶sub _download {
Subroutine _download: not called CC=3 SCAR=24.8
95 0 my( $self , $opt , $version ) = @_;
96  
97 0 ▶ say "*** download" if $opt->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{"verbose"} 0 0 0
98  
99 0 my $pkg_path = sprintf "git-%s.tar.gz" , $version;
100  
101 0 my $url = sprintf "https://kernel.org/pub/software/scm/git/%s" , $pkg_path;
102 #my $url = sprintf "http://git-core.googlecode.com/files/%s" , $pkg_path;
103  
104 0 my $response = HTTP::Tiny->new->mirror( $url , $pkg_path );
105 0 ▶ unless ( $response->{success} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->{"success"}) 0 0 0
106 my $message = sprintf "DOWNLOAD FAILED!\n** STATUS: %s REASON: %s",
107 0 $response->{status}, $response->{reason};
108 0 croak $message;
109 }
110  
111 0 return $pkg_path;
112 };
113  
114 0 ▶sub _extract {
Subroutine _extract: not called CC=3 SCAR=24.8
115 0 my( $self , $opt , $pkg_path ) = @_;
116 0 ▶ say "*** extract" if $opt->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{"verbose"} 0 0 0
117 0 my $ae = Archive::Extract->new( archive => $pkg_path );
118 0 ▶ $ae->extract or die $ae->error;
Branch: 1 branch
BranchTrueFalseTotal
$ae->extract or die $ae->error 0 0 0
119 0 unlink $pkg_path;
120 };
121  
122 0 ▶sub _configure {
Subroutine _configure: not called CC=3 SCAR=24.8
123 0 my( $self , $opt , $version , $install_dir ) = @_;
124 0 ▶ say "*** configure" if $opt->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{"verbose"} 0 0 0
125 0 chdir "git-$version";
126 ### FIXME should have some way to allow override of these args
127 0 my $cmd = "./configure --prefix=$install_dir --without-tcltk";
128 # MacOS doesn't have openssl.h anymore, i guess?
129 0 ▶ $cmd .= " --without-openssl" if $^O eq 'darwin';
Branch: 1 branch
BranchTrueFalseTotal
if $^O eq "darwin" 0 0 0
130 0 _run( $cmd );
131 };
132  
133 0 ▶sub _make {
Subroutine _make: not called CC=2 SCAR=17.9
134 0 my( $self , $opt ) = @_;
135 0 ▶ say "*** make" if $opt->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{"verbose"} 0 0 0
136 0 _run( 'make' );
137 };
138  
139 0 ▶sub _make_test {
Subroutine _make_test: not called CC=2 SCAR=17.9
140 0 my( $self , $opt ) = @_;
141 0 ▶ say "*** make test" if $opt->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{"verbose"} 0 0 0
142 0 _run( 'make test' );
143 };
144  
145 0 ▶sub _make_install {
Subroutine _make_install: not called CC=2 SCAR=17.9
146 0 my( $self , $opt ) = @_;
147 0 ▶ say "*** make install" if $opt->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{"verbose"} 0 0 0
148 0 _run( 'make install' );
149 };
150  
151 0 ▶sub _cleanup {
Subroutine _cleanup: not called CC=2 SCAR=17.9
152 0 my( $self , $opt , $version ) = @_;
153 0 ▶ say "*** cleanup" if $opt->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{"verbose"} 0 0 0
154 0 chdir '..';
155 0 remove( \1 , "git-$version" );
156 };
157  
158 0 ▶sub _symlink {
Subroutine _symlink: not called CC=2 SCAR=17.9
159 0 my( $self , $opt , $version ) = @_;
160 0 ▶ say "*** symlink" if $opt->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{"verbose"} 0 0 0
161 0 chdir '/opt';
162 0 remove( 'git' );
163 0 symlink( "git-$version" , 'git' );
164 };
165  
166 0 ▶sub _run {
Subroutine _run: not called CC=1 SCAR=6.9
167 0 my $arg = shift;
168 0 $arg .= ' 2>&1 >/dev/null';
169 0 system( $arg ) == 0;
170 }
171  
172 1 1;
173  
174 __END__
↑ Summary