/Program.cs
https://bitbucket.org/JPlenert/mercurialrev · C# · 241 lines · 195 code · 29 blank · 17 comment · 42 complexity · 49bbbe20ef41fc0fea7d266f04121c55 MD5 · raw file
- /**************************************************************************************************
- * MercurialRev - Command line tool for Windows to build revision information text file from a mercurial project.
- *
- * Copyright (C) 2012 Joerg Plenert
- *
- * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
- * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
- **************************************************************************************************/
- using System;
- using System.Diagnostics;
- using System.IO;
- using System.Reflection;
-
- namespace MercurialRev
- {
- public class Program
- {
- private string sourceFileName;
- private string destFileName;
- private string repositoryPath;
-
- private string hgRev;
- private string hgID;
- private string hgBranch;
- private string hgTag;
- private bool hgLocalMod;
-
- public Program(string[] args)
- {
- ParseArgs(args);
- }
-
- public enum ErrorType
- {
- None,
- Argument,
- Mercurial,
- SourceFile,
- DestFile,
- HandleFile
- }
-
- public static int Main(string[] args)
- {
- Program prg = new Program(args);
- return prg.Exec();
- }
-
- public int Exec()
- {
- StreamReader streamReader;
- StreamWriter streamWriter;
- ErrorType error;
- string line;
-
- error = ErrorType.None;
- streamReader = null;
- streamWriter = null;
-
- Console.WriteLine("MercurialRev V" + GetVersionString() + " (c) 2012 by Joerg Plenert");
- Console.WriteLine("");
-
- if (sourceFileName == null || destFileName == null || repositoryPath == null)
- {
- error = ErrorType.Argument;
- }
-
- if (error == ErrorType.None)
- {
- // Get infos from hg
- hgID = GetHGInfo(repositoryPath, "identify -i");
- hgRev = GetHGInfo(repositoryPath, "identify -n");
- hgBranch = GetHGInfo(repositoryPath, "identify -b");
- hgTag = GetHGInfo(repositoryPath, "identify -t");
-
- // Check for local modifications
- if (hgID != null && hgID.EndsWith("+"))
- {
- hgLocalMod = true;
- hgID = hgID.Substring(0, hgID.Length - 1);
- }
-
- if (hgRev != null && hgRev.EndsWith("+"))
- {
- hgLocalMod = true;
- hgRev = hgRev.Substring(0, hgRev.Length - 1);
- }
-
- if (hgID == null || hgRev == null || hgBranch == null || hgTag == null)
- {
- error = ErrorType.Mercurial;
- }
- }
-
- if (error == ErrorType.None)
- {
- try
- {
- streamReader = File.OpenText(sourceFileName);
- }
- catch
- {
- error = ErrorType.SourceFile;
- }
- }
-
- if (error == ErrorType.None)
- {
- try
- {
- streamWriter = File.CreateText(destFileName);
- }
- catch
- {
- error = ErrorType.DestFile;
- }
- }
-
- if (error == ErrorType.None)
- {
- var now = DateTime.Now;
- string nowIsoFormat = now.ToString("s");
- string nowDateOnly = now.ToString("yyyy-mm-dd");
- string nowYearOnly = now.ToString("yyyy");
- while (true)
- {
- try
- {
- line = streamReader.ReadLine();
- if (line == null)
- {
- break;
- }
-
- line = line.Replace("<$HG:REV_NUM$>", hgRev);
- line = line.Replace("<$HG:REV_LMOD_N$>", hgLocalMod ? "1" : "0");
- line = line.Replace("<$HG:REV_LMOD_P$>", hgLocalMod ? "+" : "");
- line = line.Replace("<$HG:REV_ID$>", hgID);
- line = line.Replace("<$HG:BRANCH$>", hgBranch);
- line = line.Replace("<$HG:TAG$>", hgTag);
- line = line.Replace("<$HG:NOW_ISO$>", nowIsoFormat);
- line = line.Replace("<$HG:NOW_DATE$>", nowDateOnly);
- line = line.Replace("<$HG:NOW_YEAR$>", nowYearOnly);
- streamWriter.WriteLine(line);
- }
- catch
- {
- error = ErrorType.HandleFile;
- }
- }
- }
-
- if (streamReader != null)
- {
- streamReader.Close();
- }
-
- if (streamWriter != null)
- {
- streamWriter.Close();
- }
-
- if (error != ErrorType.None)
- {
- Console.WriteLine("Replaces revision information in a tagged text file.");
- Console.WriteLine("");
- Console.WriteLine("MercurialRev <SourceFile> <DestinationFile> <RepositoryPath>");
- Console.WriteLine("");
- Console.WriteLine("Tags:");
- Console.WriteLine(" <$HG:REV_NUM$>");
- Console.WriteLine(" <$HG:REV_LMOD_N$>");
- Console.WriteLine(" <$HG:REV_LMOD_P$>");
- Console.WriteLine(" <$HG:REV_ID$>");
- Console.WriteLine(" <$HG:BRANCH$>");
- Console.WriteLine(" <$HG:TAG$>");
- Console.WriteLine("");
-
- Console.WriteLine("Error: " + GetErrorDescription(error));
- }
- else
- {
- Console.WriteLine("Done!");
- }
-
- return (error == ErrorType.None) ? 0 : 1;
- }
-
- private void ParseArgs(string[] args)
- {
- if (args.Length == 3)
- {
- sourceFileName = args[0];
- destFileName = args[1];
- repositoryPath = args[2];
-
- // Repository path should not end in "\"
- if (repositoryPath.EndsWith("\\"))
- {
- repositoryPath = repositoryPath.Substring(0, repositoryPath.Length - 1);
- }
- }
- }
-
- private string GetHGInfo(string repPath, string arg)
- {
- Process proc = new Process();
- proc.StartInfo.FileName = "hg.exe";
- proc.StartInfo.Arguments = string.Format("-R \"{0}\" {1}", repPath, arg);
- proc.StartInfo.UseShellExecute = false;
- proc.StartInfo.RedirectStandardOutput = true;
-
- proc.Start();
-
- return proc.StandardOutput.ReadLine();
- }
-
- private string GetErrorDescription(ErrorType error)
- {
- switch (error)
- {
- case ErrorType.Argument: return "Number of arguments";
- case ErrorType.Mercurial: return "Mercurial";
- case ErrorType.SourceFile: return "Source file";
- case ErrorType.DestFile: return "Destination file";
- case ErrorType.HandleFile: return "Handling files";
- default: return "Unexpected error";
- }
- }
-
- private string GetVersionString()
- {
- return Assembly.GetExecutingAssembly().GetName().Version.ToString();
- }
- }
- }