tools/versionchanger.pl
author David Anderson <dvander@alliedmods.net>
Sun Sep 14 16:26:38 2008 -0500 (2008-09-14)
changeset 2291 e6ebd563392e
parent 2281 fcb07f41ad91
permissions -rw-r--r--
Updated versionchanger.pl for Mercurial, FINALLY fixed its whitespace.
     1 #!/usr/bin/perl
     2 
     3 our %arguments =
     4 (
     5 	'config' => 'modules.versions',
     6 	'major' => '1',
     7 	'minor' => '0',
     8 	'revision' => '0',
     9 	'build' => undef,
    10 	'path' => '',
    11 	'buildstring' => '',
    12 );
    13 
    14 my $arg;
    15 foreach $arg (@ARGV)
    16 {
    17 	$arg =~ s/--//;
    18 	@arg = split(/=/, $arg);
    19 	$arguments{$arg[0]} = $arg[1];
    20 }
    21 
    22 #Set up path info
    23 if ($arguments{'path'} ne "")
    24 {
    25 	if (!(-d $arguments{'path'}))
    26 	{
    27 		die "Unable to find path: " . $arguments{'path'} ."\n";
    28 	}
    29 	chdir($arguments{'path'});
    30 }
    31 
    32 if (!open(CONFIG, $arguments{'config'}))
    33 {
    34 	die "Unable to open config file for reading: " . $arguments{'config'} . "\n";
    35 }
    36 
    37 our %modules;
    38 my $cur_module = undef;
    39 my $line;
    40 while (<CONFIG>)
    41 {
    42 	chomp;
    43 	$line = $_;
    44 	if ($line =~ /^\[([^\]]+)\]$/)
    45 	{
    46 		$cur_module = $1;
    47 		next;
    48 	}
    49 	if (!$cur_module)
    50 	{
    51 		next;
    52 	}
    53 	if ($line =~ /^([^=]+) = (.+)$/)
    54 	{
    55 		$modules{$cur_module}{$1} = $2;
    56 	}
    57 }
    58 
    59 close(CONFIG);
    60 
    61 #Copy global configuration options...
    62 if (exists($modules{'PRODUCT'}))
    63 {
    64 	if (exists($modules{'PRODUCT'}{'major'}))
    65 	{
    66 		$arguments{'major'} = $modules{'PRODUCT'}{'major'};
    67 	}
    68 	if (exists($modules{'PRODUCT'}{'minor'}))
    69 	{
    70 		$arguments{'minor'} = $modules{'PRODUCT'}{'minor'};
    71 	}
    72 	if (exists($modules{'PRODUCT'}{'revision'}))
    73 	{
    74 		$arguments{'revision'} = $modules{'PRODUCT'}{'revision'};
    75 	}
    76 }
    77 
    78 #Get the global SVN revision if we have none
    79 my $rev;
    80 if ($arguments{'build'} == undef)
    81 {
    82 	my ($text);
    83 	$text = `hg identif -n -i`;
    84 	chomp $text;
    85 	$text =~ s/\+//g;
    86 	my ($id,$num) = split(/ /, $text);
    87 	$rev = "$num:$id";
    88 } 
    89 else 
    90 {
    91 	$rev = int($arguments{'build'});
    92 }
    93 
    94 my $major = $arguments{'major'};
    95 my $minor = $arguments{'minor'};
    96 my $revision = $arguments{'revision'};
    97 my $buildstr = $arguments{'buildstring'};
    98 
    99 #Go through everything now
   100 my $mod_i;
   101 while ( ($cur_module, $mod_i) = each(%modules) )
   102 {
   103 	#Skip the magic one
   104 	if ($cur_module eq "PRODUCT")
   105 	{
   106 		next;
   107 	}
   108 	#Prepare path
   109 	my %mod = %{$mod_i};
   110 	my $infile = $mod{'in'};
   111 	my $outfile = $mod{'out'};
   112 	if ($mod{'folder'})
   113 	{
   114 		if (!(-d $mod{'folder'}))
   115 		{
   116 			die "Folder " . $mod{'folder'} . " not found.\n";
   117 		}
   118 		$infile = $mod{'folder'} . '/' . $infile;
   119 		$outfile = $mod{'folder'} . '/' . $outfile;
   120 	}
   121 	if (!(-f $infile))
   122 	{
   123 		die "File $infile is not a file.\n";
   124 	}
   125 	#Start rewriting
   126 	open(INFILE, $infile) or die "Could not open file for reading: $infile\n";
   127 	open(OUTFILE, '>'.$outfile) or die "Could not open file for writing: $outfile\n";
   128 	while (<INFILE>)
   129 	{
   130 		s/\$PMAJOR\$/$major/g;
   131 		s/\$PMINOR\$/$minor/g;
   132 		s/\$PREVISION\$/$revision/g;
   133 		s/\$BUILD_ID\$/$rev/g;
   134 		s/\$BUILD_STRING\$/$buildstr/g;
   135 		print OUTFILE $_;
   136 	}
   137 	close(OUTFILE);
   138 	close(INFILE);
   139 }
   140