I was searching around for a solution to easily update my MSI version
numbers with each build, but I couldn't find one.  So I wrote this and now I
don't have to remember.  Thought I'd share...

A C# 3.5 console app:

using System;
using System.IO;

static 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();
                }
            }
        }
    }
}
------------------------------------------------------------------------------
Download Intel&#174; Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev
_______________________________________________
WiX-users mailing list
WiX-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/wix-users

Reply via email to