tools/versionchanger.pl
author Borja Ferrer <faluco@alliedmods.net>
Fri Nov 16 21:34:29 2007 +0000 (2007-11-16)
changeset 1686 185cac1820fe
parent 570 c6b959fbef07
permissions -rw-r--r--
fixed amb1164, SM was crashing due to interface changes from valve (this change is just to make the build update using the latest sdk)
     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 	'svnrev' => 'global',
    11 	'path' => '',
    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 	if (exists($modules{'PRODUCT'}{'svnrev'}))
    77 	{
    78 		$arguments{'svnrev'} = $modules{'PRODUCT'}{'svnrev'};
    79 	}
    80 }
    81 
    82 #Get the global SVN revision if we have none
    83 my $rev;
    84 if ($arguments{'build'} == undef)
    85 {
    86 	$rev = GetRevision(undef);
    87 } else {
    88 	$rev = int($arguments{'build'});
    89 }
    90 
    91 my $major = $arguments{'major'};
    92 my $minor = $arguments{'minor'};
    93 my $revision = $arguments{'revision'};
    94 my $svnrev = $arguments{'svnrev'};
    95 
    96 #Go through everything now
    97 my $mod_i;
    98 while ( ($cur_module, $mod_i) = each(%modules) )
    99 {
   100 	#Skip the magic one
   101 	if ($cur_module eq "PRODUCT")
   102 	{
   103 		next;
   104 	}
   105 	#Prepare path
   106 	my %mod = %{$mod_i};
   107 	my $infile = $mod{'in'};
   108 	my $outfile = $mod{'out'};
   109 	if ($mod{'folder'})
   110 	{
   111 		if (!(-d $mod{'folder'}))
   112 		{
   113 			die "Folder " . $mod{'folder'} . " not found.\n";
   114 		}
   115 		$infile = $mod{'folder'} . '/' . $infile;
   116 		$outfile = $mod{'folder'} . '/' . $outfile;
   117 	}
   118 	if (!(-f $infile))
   119 	{
   120 		die "File $infile is not a file.\n";
   121 	}
   122 	my $global_rev = $rev;
   123 	my $local_rev = GetRevision($mod{'folder'});
   124 	if ($arguments{'svnrev'} eq 'local')
   125 	{
   126 		$global_rev = $local_rev;
   127 	}
   128 	#Start rewriting
   129 	open(INFILE, $infile) or die "Could not open file for reading: $infile\n";
   130 	open(OUTFILE, '>'.$outfile) or die "Could not open file for writing: $outfile\n";
   131 	while (<INFILE>)
   132 	{
   133 		s/\$PMAJOR\$/$major/g;
   134 		s/\$PMINOR\$/$minor/g;
   135 		s/\$PREVISION\$/$revision/g;
   136 		s/\$GLOBAL_BUILD\$/$rev/g;
   137 		s/\$LOCAL_BUILD\$/$local_rev/g;
   138 		print OUTFILE $_;
   139 	}
   140 	close(OUTFILE);
   141 	close(INFILE);
   142 }
   143 
   144 sub GetRevision
   145 {
   146 	my ($path)=(@_);
   147 	my $rev;
   148 	if (!$path)
   149 	{
   150 		$rev = `svnversion --committed`;
   151 	} else {
   152 		$rev = `svnversion --committed $path`;
   153 	}
   154 	if ($rev =~ /exported/)
   155 	{
   156 		die "Path specified is not a working copy\n";
   157 	} elsif ($rev =~ /(\d+):(\d+)/) {
   158 		$rev = int($2);
   159 	} elsif ($rev =~ /(\d+)/) {
   160 		$rev = int($1);
   161 	} else {
   162 		die "Unknown svnversion response: $rev\n";
   163 	}
   164 	return $rev;
   165 }