package App::GitGitr::Cmd;
BEGIN {
$App::GitGitr::Cmd::VERSION = '0.1'; # TRIAL
}
BEGIN {
$App::GitGitr::Cmd::AUTHORITY = 'cpan:GENEHACK';
}
# ABSTRACT: GitGitr command support. See 'gitgitr'.
use base 'App::Cmd::Simple';
use strictures 1;
use strict;
use 5.010;
use Archive::Extract;
use Carp;
use File::Remove 'remove';
use LWP::Simple;
sub opt_spec {
return (
[ "run_tests|t" => 'run "make test" after building' ] ,
[ "verbose|V" => 'be verbose about what is being done' ] ,
[ "version|v" => 'Which git version to build. Default = most recent' ] ,
);
}
sub validate_args {
my( $self , $opt , $args ) = @_;
}
sub execute {
my( $self , $opt , $args ) = @_;
my $version = $opt->{version} // _build_version();
my $install_dir = "/opt/git-$version";
say "CURRENT VERSION: $version"
if $opt->{verbose};
unless ( -e $install_dir ) {
chdir( '/tmp' )
or die "Can't cd to /tmp";
say "BUILD/INSTALL git-$version"
if $opt->{verbose};
my $pkg_path = $self->_download( $opt , $version );
$self->_extract( $opt , $pkg_path );
$self->_configure( $opt , $version , $install_dir );
$self->_make( $opt );
$self->_make_test( $opt ) if $opt->{run_tests};
$self->_make_install( $opt );
$self->_cleanup( $opt , $version );
say "\n\nBuilt new git $version."
if $opt->{verbose};
}
die "No new version?!"
unless -e "/opt/git-$version";
$self->_symlink( $opt , $version );
say "New version ($version) symlinked into /opt/git";
}
sub _build_version {
my $content = get( 'http://git-scm.com/' );
my( $version ) = $content =~ m|
v([\d\.]+)
|
or croak "Can't parse version from Git web page!";
return $version;
}
sub _download {
my( $self , $opt , $version ) = @_;
say "*** download" if $opt->{verbose};
my $pkg_path = sprintf "git-%s.tar.gz" , $version;
my $url = sprintf "http://kernel.org/pub/software/scm/git/%s" , $pkg_path;
my $ret = getstore( $url , $pkg_path );
die $ret unless $ret eq '200';
return $pkg_path;
};
sub _extract {
my( $self , $opt , $pkg_path ) = @_;
say "*** extract" if $opt->{verbose};
my $ae = Archive::Extract->new( archive => $pkg_path );
$ae->extract or die $ae->error;
unlink $pkg_path;
};
sub _configure {
my( $self , $opt , $version , $install_dir ) = @_;
say "*** configure" if $opt->{verbose};
chdir "git-$version";
_run( "./configure --prefix=$install_dir" );
};
sub _make {
my( $self , $opt ) = @_;
say "*** make" if $opt->{verbose};
_run( 'make' );
};
sub _make_test {
my( $self , $opt ) = @_;
say "*** make test" if $opt->{verbose};
_run( 'make test' );
};
sub _make_install {
my( $self , $opt ) = @_;
say "*** make install" if $opt->{verbose};
_run( 'make install' );
};
sub _cleanup {
my( $self , $opt , $version ) = @_;
say "*** cleanup" if $opt->{verbose};
chdir '..';
remove( \1 , "git-$version" );
};
sub _symlink {
my( $self , $opt , $version ) = @_;
say "*** symlink" if $opt->{verbose};
chdir '/opt';
remove( 'git' );
symlink( "git-$version" , 'git' );
};
sub _run {
my $arg = shift;
$arg .= ' 2>&1 >/dev/null';
system( $arg ) == 0
or die "$arg failed ($?)";
}
1;
__END__
=pod
=head1 NAME
App::GitGitr::Cmd - GitGitr command support. See 'gitgitr'.
=head1 VERSION
version 0.1
=head1 AUTHOR
John SJ Anderson
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2010 by John SJ Anderson.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=cut