| 1 | // XAutoBuild.cs Version 1.0
|
|---|
| 2 | //
|
|---|
| 3 | // Author: Hans Dietrich
|
|---|
| 4 | // hdietrich@gmail.com
|
|---|
| 5 | //
|
|---|
| 6 | // Description:
|
|---|
| 7 | // The XAutoBuild utility automatically increments the build number
|
|---|
| 8 | // in the file AutoBuild.h.
|
|---|
| 9 | //
|
|---|
| 10 | // History
|
|---|
| 11 | // Version 1.0 - 2007 June 6
|
|---|
| 12 | // - Initial public release
|
|---|
| 13 | //
|
|---|
| 14 | // License:
|
|---|
| 15 | // This software is released into the public domain. You are free to use
|
|---|
| 16 | // it in any way you like, except that you may not sell this source code.
|
|---|
| 17 | //
|
|---|
| 18 | // This software is provided "as is" with no expressed or implied warranty.
|
|---|
| 19 | // I accept no liability for any damage or loss of business that this
|
|---|
| 20 | // software may cause.
|
|---|
| 21 | //
|
|---|
| 22 | ///////////////////////////////////////////////////////////////////////////////
|
|---|
| 23 |
|
|---|
| 24 | //#define XAUTOBUILD_VERBOSE
|
|---|
| 25 |
|
|---|
| 26 | using System;
|
|---|
| 27 | using System.IO;
|
|---|
| 28 | using XGetoptCS;
|
|---|
| 29 |
|
|---|
| 30 | namespace XAutoBuild
|
|---|
| 31 | {
|
|---|
| 32 | class Version
|
|---|
| 33 | {
|
|---|
| 34 | public Version(bool verbose)
|
|---|
| 35 | {
|
|---|
| 36 | _filever = new uint [4] { 1, 0, 0, 1 };
|
|---|
| 37 | _productver = new uint [4] { 1, 0, 0, 1 };
|
|---|
| 38 | _autoIncrement = true;
|
|---|
| 39 | _verbose = verbose;
|
|---|
| 40 | }
|
|---|
| 41 |
|
|---|
| 42 | /// <summary>
|
|---|
| 43 | /// ReadVersion() reads the information from AutoBuild.h
|
|---|
| 44 | /// </summary>
|
|---|
| 45 | /// <param name="path">fully qualified path to AutoBuild.h</param>
|
|---|
| 46 | /// <returns>true = success</returns>
|
|---|
| 47 | public bool ReadVersion(string path)
|
|---|
| 48 | {
|
|---|
| 49 | _filever[0] = 1;
|
|---|
| 50 | _filever[1] = 0;
|
|---|
| 51 | _filever[2] = 0;
|
|---|
| 52 | _filever[3] = 1;
|
|---|
| 53 | _productver[0] = 1;
|
|---|
| 54 | _productver[1] = 0;
|
|---|
| 55 | _productver[2] = 0;
|
|---|
| 56 | _productver[3] = 1;
|
|---|
| 57 | _autoIncrement = true;
|
|---|
| 58 |
|
|---|
| 59 | StreamReader xabfile = null;
|
|---|
| 60 |
|
|---|
| 61 | try
|
|---|
| 62 | {
|
|---|
| 63 | xabfile = new StreamReader(path);
|
|---|
| 64 | }
|
|---|
| 65 | catch
|
|---|
| 66 | {
|
|---|
| 67 | Console.WriteLine("XAutoBuild: Error opening XAutoBuild file {0}.", path);
|
|---|
| 68 | return false;
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | string line;
|
|---|
| 72 | int pos;
|
|---|
| 73 | while ((line = xabfile.ReadLine()) != null)
|
|---|
| 74 | {
|
|---|
| 75 | line = line.ToUpper();
|
|---|
| 76 |
|
|---|
| 77 | if ((pos = line.IndexOf("INCREMENT_VERSION")) >= 0)
|
|---|
| 78 | {
|
|---|
| 79 | pos += "INCREMENT_VERSION".Length + 1;
|
|---|
| 80 | string flagIncrement = line.Substring(pos);
|
|---|
| 81 | if (flagIncrement.IndexOf("FALSE") >= 0)
|
|---|
| 82 | _autoIncrement = false;
|
|---|
| 83 | if (_verbose)
|
|---|
| 84 | Console.WriteLine("XAutoBuild: _autoIncrement = {0}", _autoIncrement);
|
|---|
| 85 | continue;
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | if ((pos = line.IndexOf("FILEVER")) >= 0)
|
|---|
| 89 | {
|
|---|
| 90 | pos += "FILEVER".Length + 1;
|
|---|
| 91 | string temp = line.Substring(pos).Trim();
|
|---|
| 92 | string [] filever = temp.Split(",".ToCharArray());
|
|---|
| 93 | int i = 0;
|
|---|
| 94 | foreach (string s in filever)
|
|---|
| 95 | {
|
|---|
| 96 | if (s.Length > 0)
|
|---|
| 97 | _filever[i] = Convert.ToUInt32(s);
|
|---|
| 98 | if (++i > 3)
|
|---|
| 99 | break;
|
|---|
| 100 | }
|
|---|
| 101 | if (_verbose)
|
|---|
| 102 | Console.WriteLine("XAutoBuild: _filever = {0}.{1}.{2}.{3}",
|
|---|
| 103 | _filever[0], _filever[1], _filever[2], _filever[3]);
|
|---|
| 104 | continue;
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | if ((pos = line.IndexOf("PRODUCTVER")) >= 0)
|
|---|
| 108 | {
|
|---|
| 109 | pos += "PRODUCTVER".Length + 1;
|
|---|
| 110 | string temp = line.Substring(pos).Trim();
|
|---|
| 111 | string [] productver = temp.Split(",".ToCharArray());
|
|---|
| 112 | int i = 0;
|
|---|
| 113 | foreach (string s in productver)
|
|---|
| 114 | {
|
|---|
| 115 | if (s.Length > 0)
|
|---|
| 116 | _productver[i] = Convert.ToUInt32(s);
|
|---|
| 117 | if (++i > 3)
|
|---|
| 118 | break;
|
|---|
| 119 | }
|
|---|
| 120 | if (_verbose)
|
|---|
| 121 | Console.WriteLine("XAutoBuild: _productver = {0}.{1}.{2}.{3}",
|
|---|
| 122 | _productver[0], _productver[1], _productver[2], _productver[3]);
|
|---|
| 123 | break;
|
|---|
| 124 | }
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | xabfile.Close();
|
|---|
| 128 |
|
|---|
| 129 | return true;
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 | /// <summary>
|
|---|
| 133 | /// WriteVersion() writes AutoBuild.h based on information from
|
|---|
| 134 | /// this class.
|
|---|
| 135 | /// </summary>
|
|---|
| 136 | /// <param name="path">fully qualified path to AutoBuild.h</param>
|
|---|
| 137 | /// <returns>true = success</returns>
|
|---|
| 138 | public bool WriteVersion(string path)
|
|---|
| 139 | {
|
|---|
| 140 |
|
|---|
| 141 | StreamWriter xabfile = null;
|
|---|
| 142 |
|
|---|
| 143 | try
|
|---|
| 144 | {
|
|---|
| 145 | xabfile = new StreamWriter(path);
|
|---|
| 146 | }
|
|---|
| 147 | catch
|
|---|
| 148 | {
|
|---|
| 149 | Console.WriteLine("XAutoBuild: Error opening XAutoBuild file {0}.", path);
|
|---|
| 150 | return false;
|
|---|
| 151 | }
|
|---|
| 152 |
|
|---|
| 153 | string auto = "FALSE";
|
|---|
| 154 | if (_autoIncrement)
|
|---|
| 155 | auto = "TRUE";
|
|---|
| 156 |
|
|---|
| 157 | xabfile.WriteLine("#ifndef AUTOBUILD_H");
|
|---|
| 158 | xabfile.WriteLine("#define AUTOBUILD_H");
|
|---|
| 159 | xabfile.WriteLine("// change the FALSE to TRUE for autoincrement of build number");
|
|---|
| 160 | xabfile.WriteLine("#define INCREMENT_VERSION {0}", auto);
|
|---|
| 161 | xabfile.WriteLine("#define FILEVER {0},{1},{2},{3}", _filever[0], _filever[1], _filever[2], _filever[3]);
|
|---|
| 162 | xabfile.WriteLine("#define PRODUCTVER {0},{1},{2},{3}", _productver[0], _productver[1], _productver[2], _productver[3]);
|
|---|
| 163 | xabfile.WriteLine("#define STRFILEVER _T(\"{0}.{1}.{2}.{3}\")", _filever[0], _filever[1], _filever[2], _filever[3]);
|
|---|
| 164 | xabfile.WriteLine("#define STRPRODUCTVER _T(\"{0}.{1}.{2}.{3}\")", _productver[0], _productver[1], _productver[2], _productver[3]);
|
|---|
| 165 | xabfile.WriteLine("#define STRPRODUCTDATE _T(\"{0}\")", DateTime.Now.ToString("yyyy-MM-dd"));
|
|---|
| 166 | xabfile.WriteLine("#endif // AUTOBUILD_H");
|
|---|
| 167 |
|
|---|
| 168 | xabfile.Close();
|
|---|
| 169 |
|
|---|
| 170 | return true;
|
|---|
| 171 | }
|
|---|
| 172 |
|
|---|
| 173 | /// <summary>
|
|---|
| 174 | /// AutoIncrement (readonly) returns the _autoincrement flag.
|
|---|
| 175 | /// </summary>
|
|---|
| 176 | public bool AutoIncrement
|
|---|
| 177 | {
|
|---|
| 178 | get
|
|---|
| 179 | {
|
|---|
| 180 | return _autoIncrement;
|
|---|
| 181 | }
|
|---|
| 182 | }
|
|---|
| 183 |
|
|---|
| 184 | /// <summary>
|
|---|
| 185 | /// Increment() increments the file version and product version build numbers.
|
|---|
| 186 | /// </summary>
|
|---|
| 187 | public void Increment()
|
|---|
| 188 | {
|
|---|
| 189 | _filever[3] += 1;
|
|---|
| 190 | _productver[3] += 1;
|
|---|
| 191 | }
|
|---|
| 192 |
|
|---|
| 193 | private uint[] _filever, _productver;
|
|---|
| 194 | private bool _autoIncrement;
|
|---|
| 195 | private bool _verbose;
|
|---|
| 196 | }
|
|---|
| 197 |
|
|---|
| 198 | class Program
|
|---|
| 199 | {
|
|---|
| 200 | private const string FILE_NAME = "AutoBuild.h";
|
|---|
| 201 |
|
|---|
| 202 | static void Usage()
|
|---|
| 203 | {
|
|---|
| 204 | Console.WriteLine("XAutoBuild: Copyright (c) 2007 by Hans Dietrich");
|
|---|
| 205 | Console.WriteLine("XAutoBuild: Usage: XAutoBuild -f <path to autobuild header> [-v]");
|
|---|
| 206 | }
|
|---|
| 207 |
|
|---|
| 208 | static int Main(string[] args)
|
|---|
| 209 | {
|
|---|
| 210 | #if XAUTOBUILD_VERBOSE
|
|---|
| 211 | int i = 0;
|
|---|
| 212 | foreach (string arg in args)
|
|---|
| 213 | {
|
|---|
| 214 | Console.WriteLine("arg[{0}]: {1}", i, arg);
|
|---|
| 215 | i++;
|
|---|
| 216 | }
|
|---|
| 217 | #endif
|
|---|
| 218 |
|
|---|
| 219 | string path = string.Empty;
|
|---|
| 220 | bool verbose = false;
|
|---|
| 221 |
|
|---|
| 222 | char c;
|
|---|
| 223 | XGetopt go = new XGetopt();
|
|---|
| 224 | while ((c = go.Getopt(args.Length, args, "f:v")) != '\0')
|
|---|
| 225 | {
|
|---|
| 226 | #if XAUTOBUILD_VERBOSE
|
|---|
| 227 | Console.WriteLine("Getopt returned '{0}'", c);
|
|---|
| 228 | #endif
|
|---|
| 229 |
|
|---|
| 230 | switch (c)
|
|---|
| 231 | {
|
|---|
| 232 | case 'f':
|
|---|
| 233 | path = go.Optarg;
|
|---|
| 234 | break;
|
|---|
| 235 |
|
|---|
| 236 | case 'v':
|
|---|
| 237 | verbose = true;
|
|---|
| 238 | break;
|
|---|
| 239 |
|
|---|
| 240 | case '?':
|
|---|
| 241 | Console.WriteLine("illegal option or missing arg");
|
|---|
| 242 | Usage();
|
|---|
| 243 | return 1;
|
|---|
| 244 | }
|
|---|
| 245 | }
|
|---|
| 246 |
|
|---|
| 247 | if (path.Length < 4)
|
|---|
| 248 | {
|
|---|
| 249 | Usage();
|
|---|
| 250 | return 1;
|
|---|
| 251 | }
|
|---|
| 252 |
|
|---|
| 253 | if (path.EndsWith("\"")) {
|
|---|
| 254 | path = path.Substring(0, path.Length - 1);
|
|---|
| 255 | }
|
|---|
| 256 | path = path + Path.DirectorySeparatorChar;
|
|---|
| 257 | try
|
|---|
| 258 | {
|
|---|
| 259 | path = Path.GetDirectoryName(path);
|
|---|
| 260 | }
|
|---|
| 261 | catch (System.ArgumentException e)
|
|---|
| 262 | {
|
|---|
| 263 | Console.WriteLine("Path not valid {0}", path);
|
|---|
| 264 | Usage();
|
|---|
| 265 | return 1;
|
|---|
| 266 | }
|
|---|
| 267 |
|
|---|
| 268 |
|
|---|
| 269 | string infile = path + Path.DirectorySeparatorChar + FILE_NAME;
|
|---|
| 270 |
|
|---|
| 271 | if (verbose)
|
|---|
| 272 | Console.WriteLine("XAutoBuild: XAutoBuild file is {0}", infile);
|
|---|
| 273 |
|
|---|
| 274 | Version ver = new Version(verbose);
|
|---|
| 275 |
|
|---|
| 276 | if (File.Exists(infile))
|
|---|
| 277 | {
|
|---|
| 278 | if (verbose)
|
|---|
| 279 | Console.WriteLine("XAutoBuild: {0} already exists.", infile);
|
|---|
| 280 |
|
|---|
| 281 | string bakfile = infile + ".bak";
|
|---|
| 282 |
|
|---|
| 283 | // make backup copy
|
|---|
| 284 | try
|
|---|
| 285 | {
|
|---|
| 286 | if (verbose)
|
|---|
| 287 | Console.WriteLine("XAutoBuild: creating {0}", bakfile);
|
|---|
| 288 | File.Copy(infile, bakfile, true);
|
|---|
| 289 | }
|
|---|
| 290 | catch
|
|---|
| 291 | {
|
|---|
| 292 | Console.WriteLine("XAutoBuild: failed to create {0}", bakfile);
|
|---|
| 293 | return 1;
|
|---|
| 294 | }
|
|---|
| 295 | }
|
|---|
| 296 | else
|
|---|
| 297 | {
|
|---|
| 298 | // file doesn't exist, start with version 1.0.0.1
|
|---|
| 299 |
|
|---|
| 300 | if (verbose)
|
|---|
| 301 | Console.WriteLine("XAutoBuild: creating {0}", infile);
|
|---|
| 302 |
|
|---|
| 303 | if (!ver.WriteVersion(infile))
|
|---|
| 304 | {
|
|---|
| 305 | return 1;
|
|---|
| 306 | }
|
|---|
| 307 | }
|
|---|
| 308 |
|
|---|
| 309 | // at this point input file does exist - now read values
|
|---|
| 310 |
|
|---|
| 311 | if (!ver.ReadVersion(infile))
|
|---|
| 312 | {
|
|---|
| 313 | return 1;
|
|---|
| 314 | }
|
|---|
| 315 |
|
|---|
| 316 | if (ver.AutoIncrement)
|
|---|
| 317 | {
|
|---|
| 318 | if (verbose)
|
|---|
| 319 | Console.WriteLine("XAutoBuild: updating {0}", infile);
|
|---|
| 320 |
|
|---|
| 321 | ver.Increment();
|
|---|
| 322 |
|
|---|
| 323 | if (!ver.WriteVersion(infile))
|
|---|
| 324 | {
|
|---|
| 325 | return 1;
|
|---|
| 326 | }
|
|---|
| 327 | }
|
|---|
| 328 | else
|
|---|
| 329 | {
|
|---|
| 330 | if (verbose)
|
|---|
| 331 | Console.WriteLine("XAutoBuild: INCREMENT_VERSION set to FALSE, skipping update");
|
|---|
| 332 | }
|
|---|
| 333 |
|
|---|
| 334 | return 0;
|
|---|
| 335 | }
|
|---|
| 336 | }
|
|---|
| 337 | }
|
|---|