Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 2259a9579a
Fetching contributors…

Cannot retrieve contributors at this time

executable file 108 lines (84 sloc) 2.685 kb
#!/usr/bin/perl -w # -*- perl -*-
#
# Fixes the lib/Template/Config.pm file to restore the correct
# (default) values for a distribution release. Because the
# Makefile.PL updates this file, it means that I could send out
# a "standard" lib/Template/Config.pm with XS Stash enabled by
# default, for example.
#
use strict;
use Getopt::Std;
my $PROGRAM = 'fixconfig';
my $CONFIG = 'lib/Template/Config.pm';
die $! unless -f $CONFIG;
my $fixes = {
'$STASH' => 'Template::Stash',
'$INSTDIR' => '',
};
my $args = { };
getopts('vhy', $args);
usage() if $args->{ h };
my $verbose = $args->{ v };
my $yes = $args->{ y };
if ($verbose || ! $yes) {
print "About to make the following changes to ${CONFIG}:\n";
while (my ($key, $value) = each %$fixes) {
printf(" %-14s => %s\n", $key, $value);
}
}
if (! $yes) {
exit(1) unless ttprompt("Go ahead and make the changes?", 'n') =~ /y/i;
}
print "Fixing $CONFIG...\n" if $verbose;
fix_file($CONFIG, $fixes);
#------------------------------------------------------------------------
# fix_file($file, $fixes)
#
# Fixes a variable definition in a file. e.g.
# fix_file('lib/Template/Config.pm', '$INSTDIR', '/usr/local/tt2')
#------------------------------------------------------------------------
sub fix_file {
my ($file, $fixes) = @_;
local *FP;
local $/ = undef;
my $regex = '(' . join('|', map { quotemeta } keys %$fixes) . ')';
open(FP, "< $file") || die "$file: $!\n";
my $text = <FP>;
close(FP);
$text =~ s/^(\s*${regex}\s*=\s*)'.*?'/"$1'$fixes->{$2}'"/meg;
open(FP, "> $file") || die "$file: $!\n";
print FP $text;
close(FP);
}
#------------------------------------------------------------------------
# ttprompt($message, $default)
#------------------------------------------------------------------------
sub ttprompt {
my ($msg, $def)=@_;
my $ISA_TTY = -t STDIN && (-t STDOUT || !(-f STDOUT || -c STDOUT)) ; # Pipe?
my $dispdef = defined $def ? "[$def] " : " ";
$def = defined $def ? $def : "";
my $ans = '';
local $|=1;
print "$msg $dispdef";
if (! $ISA_TTY) {
print "$def\n";
}
else {
chomp($ans = <STDIN>);
}
return ($ans ne '') ? $ans : $def;
}
#------------------------------------------------------------------------
# usage
#------------------------------------------------------------------------
sub usage {
print STDERR <<EOF;
$PROGRAM: restore $CONFIG to original for distribution
usage: $PROGRAM [ -y | -v | -h ]
-y yes, do it
-v verbose mode
-h this help
EOF
exit();
}
Jump to Line
Something went wrong with that request. Please try again.