Commits

Anonymous committed 9a717c0

initial import of the tagRevision.pl script

Comments (0)

Files changed (1)

+#!/usr/bin/perl
+#
+# tagRevision.pl
+#
+# Created by Thomas Westfeld on 05.10.10.
+# Copyright 2010 Thomas Westfeld. All rights reserved.
+#
+
+use strict;
+use warnings;
+
+# if a special path to plistbuddy is given in the build settings use this,
+# otherwise use the standard location
+my $plistBuddy = defined($ENV{'PLIST_BUDDY'}) ? $ENV{'PLIST_BUDDY'} :"/usr/libexec/PlistBuddy";
+
+exit 1 if &validate_input>0;
+
+# make sure everything is commited before tagging the version
+if (&checkForUncommitedChanges) {
+	print "Uncommitted changes present. Run 'hg commit' first.";
+	exit 1;
+}
+
+# tag current revision with new version number
+my $highestMinorVersion = &findHighestMinorRevision;
+$highestMinorVersion++;
+
+my $newVersionNumber = $ENV{'VERSION_PREFIX'}.$highestMinorVersion;
+&updateInfoPlist($newVersionNumber);
+
+# use mercurial to commit the changed version in the .plist file and tag this revision
+`$ENV{'MERCURIAL_EXECUTABLE'} commit -m "changed version in .plist file to $newVersionNumber"`;
+`$ENV{'MERCURIAL_EXECUTABLE'} tag $newVersionNumber`;
+print "Tagged new version as version $newVersionNumber\n";
+
+exit 0;
+
+
+sub checkForUncommitedChanges {
+    my $hghash = `$ENV{'MERCURIAL_EXECUTABLE'} id -i`;
+    chomp $hghash;
+    return 1 if (substr($hghash,-1,1) eq '+');
+}
+
+sub updateInfoPlist {
+	my $version = shift;
+	my $infoPlist = "$ENV{BUILT_PRODUCTS_DIR}/$ENV{'PRODUCT_NAME'}.app/Info.plist";
+	
+	my $projectPlist = $ENV{'PROJECT_DIR'}.'/'.$ENV{'INFOPLIST_FILE'};
+	
+	# update the info.plist in the build product and in the project
+	`$plistBuddy $infoPlist -c "set :CFBundleVersion $version"`;
+	`$plistBuddy $projectPlist -c "set :CFBundleVersion $version"`;
+	
+}	
+
+# looks through all the tags present in the repository which have the
+# same version prefix as specified by the VERSION_PREFIX build setting
+
+sub findHighestMinorRevision {
+	my $curhash = `$ENV{'MERCURIAL_EXECUTABLE'} id -i`;
+	open my $hgtags, '-|', "$ENV{'MERCURIAL_EXECUTABLE'} tags";
+	my $highestVersion = -1;
+	while (my $tag = <$hgtags>) {
+		if (my ($minorVersion, $taghash) = $tag =~ /$ENV{'VERSION_PREFIX'}(\d+)\s*\d+:(\S+)/){
+			if ($minorVersion > $highestVersion) {
+				$highestVersion = $minorVersion;
+			}
+		}
+	}
+	close $hgtags;
+
+	my $lastTag = `$ENV{'MERCURIAL_EXECUTABLE'} log --template '{tags}' -r -2`;
+	if (defined($lastTag)) {
+		if ($lastTag eq "$ENV{'VERSION_PREFIX'}$highestVersion") {
+			print "current revision is already tagged as version $ENV{'VERSION_PREFIX'}$highestVersion\n";
+			exit 0;
+		}
+	}
+	
+	return $highestVersion;
+}
+
+
+# check for the existence of all necessary build settings
+
+sub validate_input {
+	my $input_missing = 0;
+	
+	if (!defined($ENV{'MERCURIAL_EXECUTABLE'})){
+		print "MERCURIAL_EXECUTABLE not defined. Add MERCURIAL_EXECUTABLE to custom build settings\n";
+		$input_missing=1;
+	}
+	
+	if (! -e $ENV{'MERCURIAL_EXECUTABLE'}){
+		print "Mercurial executable not found: $ENV{'MERCURIAL_EXECUTABLE'}\n";
+		$input_missing=1;
+	}
+	if (!defined($ENV{'VERSION_PREFIX'})){
+		print "VERSION_PREFIX not defined. Add VERSION_PREFIX to custom build settings with a trailing . like 1.1.\n";
+		$input_missing=1;
+	}
+	if (!-e $plistBuddy) {
+		print "PlistBuddy not found at $plistBuddy. Add PLIST_BUDDY to custom build settings to specify its path.\n";
+		$input_missing=1;
+	}
+	return $input_missing;
+}
+