/Program.cs

https://bitbucket.org/JPlenert/mercurialrev · C# · 241 lines · 195 code · 29 blank · 17 comment · 42 complexity · 49bbbe20ef41fc0fea7d266f04121c55 MD5 · raw file

  1. /**************************************************************************************************
  2. * MercurialRev - Command line tool for Windows to build revision information text file from a mercurial project.
  3. *
  4. * Copyright (C) 2012 Joerg Plenert
  5. *
  6. * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  10. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. *
  14. **************************************************************************************************/
  15. using System;
  16. using System.Diagnostics;
  17. using System.IO;
  18. using System.Reflection;
  19. namespace MercurialRev
  20. {
  21. public class Program
  22. {
  23. private string sourceFileName;
  24. private string destFileName;
  25. private string repositoryPath;
  26. private string hgRev;
  27. private string hgID;
  28. private string hgBranch;
  29. private string hgTag;
  30. private bool hgLocalMod;
  31. public Program(string[] args)
  32. {
  33. ParseArgs(args);
  34. }
  35. public enum ErrorType
  36. {
  37. None,
  38. Argument,
  39. Mercurial,
  40. SourceFile,
  41. DestFile,
  42. HandleFile
  43. }
  44. public static int Main(string[] args)
  45. {
  46. Program prg = new Program(args);
  47. return prg.Exec();
  48. }
  49. public int Exec()
  50. {
  51. StreamReader streamReader;
  52. StreamWriter streamWriter;
  53. ErrorType error;
  54. string line;
  55. error = ErrorType.None;
  56. streamReader = null;
  57. streamWriter = null;
  58. Console.WriteLine("MercurialRev V" + GetVersionString() + " (c) 2012 by Joerg Plenert");
  59. Console.WriteLine("");
  60. if (sourceFileName == null || destFileName == null || repositoryPath == null)
  61. {
  62. error = ErrorType.Argument;
  63. }
  64. if (error == ErrorType.None)
  65. {
  66. // Get infos from hg
  67. hgID = GetHGInfo(repositoryPath, "identify -i");
  68. hgRev = GetHGInfo(repositoryPath, "identify -n");
  69. hgBranch = GetHGInfo(repositoryPath, "identify -b");
  70. hgTag = GetHGInfo(repositoryPath, "identify -t");
  71. // Check for local modifications
  72. if (hgID != null && hgID.EndsWith("+"))
  73. {
  74. hgLocalMod = true;
  75. hgID = hgID.Substring(0, hgID.Length - 1);
  76. }
  77. if (hgRev != null && hgRev.EndsWith("+"))
  78. {
  79. hgLocalMod = true;
  80. hgRev = hgRev.Substring(0, hgRev.Length - 1);
  81. }
  82. if (hgID == null || hgRev == null || hgBranch == null || hgTag == null)
  83. {
  84. error = ErrorType.Mercurial;
  85. }
  86. }
  87. if (error == ErrorType.None)
  88. {
  89. try
  90. {
  91. streamReader = File.OpenText(sourceFileName);
  92. }
  93. catch
  94. {
  95. error = ErrorType.SourceFile;
  96. }
  97. }
  98. if (error == ErrorType.None)
  99. {
  100. try
  101. {
  102. streamWriter = File.CreateText(destFileName);
  103. }
  104. catch
  105. {
  106. error = ErrorType.DestFile;
  107. }
  108. }
  109. if (error == ErrorType.None)
  110. {
  111. var now = DateTime.Now;
  112. string nowIsoFormat = now.ToString("s");
  113. string nowDateOnly = now.ToString("yyyy-mm-dd");
  114. string nowYearOnly = now.ToString("yyyy");
  115. while (true)
  116. {
  117. try
  118. {
  119. line = streamReader.ReadLine();
  120. if (line == null)
  121. {
  122. break;
  123. }
  124. line = line.Replace("<$HG:REV_NUM$>", hgRev);
  125. line = line.Replace("<$HG:REV_LMOD_N$>", hgLocalMod ? "1" : "0");
  126. line = line.Replace("<$HG:REV_LMOD_P$>", hgLocalMod ? "+" : "");
  127. line = line.Replace("<$HG:REV_ID$>", hgID);
  128. line = line.Replace("<$HG:BRANCH$>", hgBranch);
  129. line = line.Replace("<$HG:TAG$>", hgTag);
  130. line = line.Replace("<$HG:NOW_ISO$>", nowIsoFormat);
  131. line = line.Replace("<$HG:NOW_DATE$>", nowDateOnly);
  132. line = line.Replace("<$HG:NOW_YEAR$>", nowYearOnly);
  133. streamWriter.WriteLine(line);
  134. }
  135. catch
  136. {
  137. error = ErrorType.HandleFile;
  138. }
  139. }
  140. }
  141. if (streamReader != null)
  142. {
  143. streamReader.Close();
  144. }
  145. if (streamWriter != null)
  146. {
  147. streamWriter.Close();
  148. }
  149. if (error != ErrorType.None)
  150. {
  151. Console.WriteLine("Replaces revision information in a tagged text file.");
  152. Console.WriteLine("");
  153. Console.WriteLine("MercurialRev <SourceFile> <DestinationFile> <RepositoryPath>");
  154. Console.WriteLine("");
  155. Console.WriteLine("Tags:");
  156. Console.WriteLine(" <$HG:REV_NUM$>");
  157. Console.WriteLine(" <$HG:REV_LMOD_N$>");
  158. Console.WriteLine(" <$HG:REV_LMOD_P$>");
  159. Console.WriteLine(" <$HG:REV_ID$>");
  160. Console.WriteLine(" <$HG:BRANCH$>");
  161. Console.WriteLine(" <$HG:TAG$>");
  162. Console.WriteLine("");
  163. Console.WriteLine("Error: " + GetErrorDescription(error));
  164. }
  165. else
  166. {
  167. Console.WriteLine("Done!");
  168. }
  169. return (error == ErrorType.None) ? 0 : 1;
  170. }
  171. private void ParseArgs(string[] args)
  172. {
  173. if (args.Length == 3)
  174. {
  175. sourceFileName = args[0];
  176. destFileName = args[1];
  177. repositoryPath = args[2];
  178. // Repository path should not end in "\"
  179. if (repositoryPath.EndsWith("\\"))
  180. {
  181. repositoryPath = repositoryPath.Substring(0, repositoryPath.Length - 1);
  182. }
  183. }
  184. }
  185. private string GetHGInfo(string repPath, string arg)
  186. {
  187. Process proc = new Process();
  188. proc.StartInfo.FileName = "hg.exe";
  189. proc.StartInfo.Arguments = string.Format("-R \"{0}\" {1}", repPath, arg);
  190. proc.StartInfo.UseShellExecute = false;
  191. proc.StartInfo.RedirectStandardOutput = true;
  192. proc.Start();
  193. return proc.StandardOutput.ReadLine();
  194. }
  195. private string GetErrorDescription(ErrorType error)
  196. {
  197. switch (error)
  198. {
  199. case ErrorType.Argument: return "Number of arguments";
  200. case ErrorType.Mercurial: return "Mercurial";
  201. case ErrorType.SourceFile: return "Source file";
  202. case ErrorType.DestFile: return "Destination file";
  203. case ErrorType.HandleFile: return "Handling files";
  204. default: return "Unexpected error";
  205. }
  206. }
  207. private string GetVersionString()
  208. {
  209. return Assembly.GetExecutingAssembly().GetName().Version.ToString();
  210. }
  211. }
  212. }