Created
August 14, 2011 13:55
-
-
Save kuk/1144889 to your computer and use it in GitHub Desktop.
Installer for my perl scripts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use Data::Dumper; | |
use Getopt::Long; | |
use Digest::MD5 'md5_hex'; | |
$ENV{DEBUG} = 0; | |
my $BASHRC = '~/.bashrc'; | |
my %options; | |
sub EscapeQuotes { | |
my ($string) = @_; | |
$string =~ s/'/'\\''/g; | |
return $string; | |
} | |
sub ShortenString { | |
my ($string) = @_; | |
my @lines = split /\n/, $string; | |
if (@lines > 5) { | |
return join "\n", | |
($lines[0], | |
$lines[1], | |
"...", | |
$lines[-2], | |
$lines[-1]); | |
} else { | |
return $string; | |
} | |
} | |
sub GetScriptId { | |
my ($scriptPath) = @_; | |
return md5_hex($scriptPath); | |
} | |
sub MakeExportString { | |
my ($variable, $value) = @_; | |
return "export $variable=\$$variable:$value" | |
} | |
sub Log { | |
my ($message) = @_; | |
$message = ShortenString($message); | |
print STDERR "$message\n" if $options{verbose}; | |
} | |
sub RunSystem { | |
my ($command) = @_; | |
Log("RunSystem: $command"); | |
unless ($options{dryrun}) { | |
system($command) == 0 | |
or die "Unable to run: $command"; | |
} | |
} | |
sub CopyFiles { | |
my ($source, $prefix) = @_; | |
RunSystem("mkdir -p $prefix && scp -r $source $prefix"); | |
} | |
sub RemoveFiles { | |
my ($prefix) = @_; | |
RunSystem("rm -r $prefix"); | |
} | |
sub AppendBashrc { | |
my ($prefix) = @_; | |
my $scriptId = GetScriptId($prefix); | |
my $bashrcString = join "\n", | |
("# $scriptId", | |
MakeExportString('PATH', $prefix), | |
MakeExportString('PERL5LIB', $prefix), | |
"" | |
); | |
$bashrcString = EscapeQuotes($bashrcString); | |
RunSystem("echo '$bashrcString' >> $BASHRC"); | |
} | |
sub ClearBashrc { | |
my ($prefix) = @_; | |
my $bashrcString = `cat $BASHRC`; | |
my $scriptId = GetScriptId($prefix); | |
$bashrcString =~ s/# $scriptId.+?\n\n//sg; | |
$bashrcString = EscapeQuotes($bashrcString); | |
RunSystem("echo '$bashrcString' > $BASHRC"); | |
} | |
sub FinalMessage { | |
my ($prefix) = @_; | |
print join "\n", | |
("", | |
"To complete installation run", | |
MakeExportString('PATH', $prefix), | |
MakeExportString('PERL5LIB', $prefix), | |
"", | |
); | |
} | |
sub Uninstall { | |
my ($prefix) = @_; | |
RemoveFiles($prefix); | |
ClearBashrc($prefix); | |
Log("Script from $prefix was uninstalled"); | |
} | |
sub Install { | |
my ($source, $prefix) = @_; | |
ClearBashrc($prefix); | |
CopyFiles($source, $prefix); | |
AppendBashrc($prefix); | |
FinalMessage($prefix); | |
} | |
$options{uninstall} = 0; | |
$options{dryrun} = 1 if $ENV{DEBUG} or 0; | |
$options{verbose} = 1 if $ENV{DEBUG} or 0; | |
GetOptions('uninstall' => \$options{uninstall}, | |
'prefix=s' => \$options{prefix}, | |
'source=s' => \$options{source}, | |
'verbose!' => \$options{verbose}, | |
'dryrun!' => \$options{dryrun}) | |
or die "Unable to parse options"; | |
if ($options{uninstall}) { | |
defined $options{prefix} | |
or die "Usage: install.pl -uninstall " . | |
"-prefix \"abs/dir/to/uninstall\""; | |
Uninstall($options{prefix}); | |
} else { | |
defined $options{prefix} | |
&& defined $options{source} | |
or die "Usage: install.pl " . | |
"-source \"path/to/scripts\" " . | |
"-prefix \"abs/dir/to/install\" "; | |
Install($options{source}, $options{prefix}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment