Program.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using System;
  2. using System.Diagnostics;
  3. using LJLib.Cmd;
  4. using System.IO;
  5. using System.Text.RegularExpressions;
  6. using System.Windows.Forms;
  7. namespace NetGitTool
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. if (args == null || args.Length < 2)
  14. {
  15. return;
  16. }
  17. var op = args[0].Trim();
  18. var projectPath = args[1].Trim();
  19. var config = args.Length >= 3 ? args[2] : string.Empty;
  20. try
  21. {
  22. if (projectPath.StartsWith("\""))
  23. {
  24. projectPath = projectPath.Substring(1, projectPath.Length - 1);
  25. }
  26. if (projectPath.EndsWith("\""))
  27. {
  28. projectPath = projectPath.Substring(0, projectPath.Length - 1);
  29. }
  30. else
  31. {
  32. var lastindex = projectPath.LastIndexOf("\"");
  33. if (lastindex > 0)
  34. {
  35. config = projectPath.Substring(lastindex + 1).Trim().ToUpper();
  36. projectPath = projectPath.Substring(0, lastindex);
  37. }
  38. else if (!Directory.Exists(projectPath))
  39. {
  40. lastindex = projectPath.LastIndexOf(" ");
  41. if (lastindex > 0)
  42. {
  43. config = projectPath.Substring(lastindex + 1).Trim().ToUpper();
  44. projectPath = projectPath.Substring(0, lastindex);
  45. }
  46. }
  47. }
  48. if (projectPath.EndsWith("\\"))
  49. {
  50. projectPath = projectPath.Substring(0, projectPath.Length - 1);
  51. }
  52. if (!Directory.Exists(projectPath))
  53. {
  54. return;
  55. }
  56. if (op == "/before")
  57. {
  58. var versionName = string.Empty;
  59. var gitlog = CMDHelper.RunWithReturn("git log -1 --format=%D", projectPath);
  60. if (gitlog.Contains("tag"))
  61. {
  62. var dscrpArr = gitlog.Split(',');
  63. foreach(var item in dscrpArr)
  64. {
  65. var dscrpItem = item.Trim();
  66. if(dscrpItem.StartsWith("tag: V"))
  67. {
  68. versionName = dscrpItem.Substring(5);
  69. break;
  70. }
  71. }
  72. }
  73. if (string.IsNullOrEmpty(versionName))
  74. {
  75. versionName = CMDHelper.RunWithReturn("git describe --tags --dirty", projectPath);
  76. }
  77. else
  78. {
  79. var dirtyDscrp = CMDHelper.RunWithReturn("git describe --dirty", projectPath);
  80. if (dirtyDscrp.EndsWith("-dirty"))
  81. {
  82. versionName += "-dirty";
  83. }
  84. }
  85. if (!versionName.StartsWith("V"))
  86. {
  87. return;
  88. }
  89. if (!string.IsNullOrEmpty(config) && config != "RELEASE")
  90. {
  91. versionName += config;
  92. }
  93. var file = projectPath + "\\Properties\\AssemblyInfo.cs";
  94. if (!File.Exists(file))
  95. {
  96. return;
  97. }
  98. string content;
  99. using (var reader = File.OpenText(file))
  100. {
  101. content = reader.ReadToEnd();
  102. }
  103. var bakfile = file + ".bak";
  104. if (File.Exists(bakfile))
  105. {
  106. File.Delete(bakfile);
  107. }
  108. File.Move(file, bakfile);
  109. Regex regex = new Regex("AssemblyFileVersion\\(\".*\"\\)");
  110. content = regex.Replace(content, string.Format("AssemblyFileVersion(\"{0}\")", versionName));
  111. using (var writer = File.CreateText(file))
  112. {
  113. writer.Write(content);
  114. }
  115. }
  116. else if (op == "/after")
  117. {
  118. var file = projectPath + "\\Properties\\AssemblyInfo.cs";
  119. var bakfile = file + ".bak";
  120. if (!File.Exists(bakfile))
  121. {
  122. return;
  123. }
  124. if (File.Exists(file))
  125. {
  126. File.Delete(file);
  127. }
  128. File.Move(bakfile, file);
  129. }
  130. }
  131. catch (Exception ex)
  132. {
  133. Trace.Write(string.Format("{0} {1}\r\n{2}", args[0].Trim(), args[1].Trim(), ex.ToString()));
  134. }
  135. }
  136. }
  137. }