Skip to content

Instantly share code, notes, and snippets.

@flacodirt
Created September 26, 2013 22:09
Show Gist options
  • Save flacodirt/6721280 to your computer and use it in GitHub Desktop.
Save flacodirt/6721280 to your computer and use it in GitHub Desktop.
/// Updates a .wxs or other text file containing a /// <?define version="..."?> preprocessor directive /// and increments the least-significant version /// grouping present and applicable to Major Upgrades.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace VersionUpdater
{
class VersionUpdater
{
/// <summary>
///
/// Updates a .wxs or other text file containing a
/// <?define version="..."?> preprocessor directive
/// and increments the least-significant version
/// grouping present and applicable to Major Upgrades.
///
/// <?define version="3.0.0"?> becomes <?define version="3.0.1"?>
/// <?define version="3.0.0.0"?> becomes <?define version="3.0.1.0"?>
/// <?define version="3.0"?> becomes <?define version="3.1"?>
/// <?define version="3"?> becomes <?define version="4"?>
///
/// For reference within the WiX project as in this example: <Product Version="$(var.version)" />
///
/// In Votive, a pre-build event of "...VersionUpdater.exe" "$(ProjectDir)Product.wxs" updates
/// the version number for each build.
///
/// </summary>
/// <param name="args">A single command-line parameter</param>
static void Main(string[] args)
{
if (args.Length < 1)
{
Console.WriteLine("Error: Expected the path to a file to update as an argument");
Console.ReadKey();
Environment.Exit(1);
}
if (!File.Exists(args[0]))
{
Console.WriteLine("Error: File {0} not found", args[0]);
Console.ReadKey();
Environment.Exit(2);
}
using (var fs = File.Open(args[0], FileMode.Open, FileAccess.ReadWrite, FileShare.None))
{
using (StreamReader sr = new StreamReader(fs))
{
string data = sr.ReadToEnd(); // Read in the entire file
fs.Seek(0, SeekOrigin.Begin); // Reset to the beginning of the file
System.Text.RegularExpressions.Regex regEx = new System.Text.RegularExpressions.Regex(@"\<\?define\s+version\s*\=\s*\""(?<majorMinor>(\d*\.){0,2})(?<version>\d*)(.\d*)?\""\s*\?\>");
var versionMatch = regEx.Match(data);
var version = versionMatch.Groups["version"];
var majorMinor = versionMatch.Groups["majorMinor"];
if (!version.Success || !majorMinor.Success)
{
Console.WriteLine(
"Error: Version match not found. " +
"Expected <?define version=\"w.x.y\"?> (Recommended), " +
"<?define version=\"w\"?>, <?define version=\"w.x\"?>, " +
"or <?define version=\"w.x.y.z\"?>",
args[0]);
Console.ReadKey();
Environment.Exit(3);
}
int versionNumber;
int.TryParse(version.Value, out versionNumber);
Console.WriteLine("Updating {0}{1} to {0}{2}",
majorMinor.Value,
versionNumber,
versionNumber + 1);
versionNumber++;
string newData = data.Substring(0, version.Index) + versionNumber.ToString() +
data.Substring(version.Index + version.Length);
using (StreamWriter sw = new StreamWriter(fs))
{
sw.Write(newData);
sw.Flush();
fs.SetLength(newData.Length);
fs.Flush();
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment