123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- using System;
- using System.Diagnostics;
- using LJLib.Cmd;
- using System.IO;
- using System.Text.RegularExpressions;
- using System.Windows.Forms;
- namespace NetGitTool
- {
- class Program
- {
- static void Main(string[] args)
- {
- if (args == null || args.Length < 2)
- {
- return;
- }
- var op = args[0].Trim();
- var projectPath = args[1].Trim();
- var config = args.Length >= 3 ? args[2] : string.Empty;
- try
- {
- if (projectPath.StartsWith("\""))
- {
- projectPath = projectPath.Substring(1, projectPath.Length - 1);
- }
- if (projectPath.EndsWith("\""))
- {
- projectPath = projectPath.Substring(0, projectPath.Length - 1);
- }
- else
- {
- var lastindex = projectPath.LastIndexOf("\"");
- if (lastindex > 0)
- {
- config = projectPath.Substring(lastindex + 1).Trim().ToUpper();
- projectPath = projectPath.Substring(0, lastindex);
- }
- else if (!Directory.Exists(projectPath))
- {
- lastindex = projectPath.LastIndexOf(" ");
- if (lastindex > 0)
- {
- config = projectPath.Substring(lastindex + 1).Trim().ToUpper();
- projectPath = projectPath.Substring(0, lastindex);
- }
- }
- }
- if (projectPath.EndsWith("\\"))
- {
- projectPath = projectPath.Substring(0, projectPath.Length - 1);
- }
- if (!Directory.Exists(projectPath))
- {
- return;
- }
- if (op == "/before")
- {
- var versionName = string.Empty;
- var gitlog = CMDHelper.RunWithReturn("git log -1 --format=%D", projectPath);
- if (gitlog.Contains("tag"))
- {
- var dscrpArr = gitlog.Split(',');
- foreach(var item in dscrpArr)
- {
- var dscrpItem = item.Trim();
- if(dscrpItem.StartsWith("tag: V"))
- {
- versionName = dscrpItem.Substring(5);
- break;
- }
- }
- }
- if (string.IsNullOrEmpty(versionName))
- {
- versionName = CMDHelper.RunWithReturn("git describe --tags --dirty", projectPath);
- }
- else
- {
- var dirtyDscrp = CMDHelper.RunWithReturn("git describe --dirty", projectPath);
- if (dirtyDscrp.EndsWith("-dirty"))
- {
- versionName += "-dirty";
- }
- }
- if (!versionName.StartsWith("V"))
- {
- return;
- }
- if (!string.IsNullOrEmpty(config) && config != "RELEASE")
- {
- versionName += config;
- }
- var file = projectPath + "\\Properties\\AssemblyInfo.cs";
- if (!File.Exists(file))
- {
- return;
- }
- string content;
- using (var reader = File.OpenText(file))
- {
- content = reader.ReadToEnd();
- }
- var bakfile = file + ".bak";
- if (File.Exists(bakfile))
- {
- File.Delete(bakfile);
- }
- File.Move(file, bakfile);
- Regex regex = new Regex("AssemblyFileVersion\\(\".*\"\\)");
- content = regex.Replace(content, string.Format("AssemblyFileVersion(\"{0}\")", versionName));
- using (var writer = File.CreateText(file))
- {
- writer.Write(content);
- }
- }
- else if (op == "/after")
- {
- var file = projectPath + "\\Properties\\AssemblyInfo.cs";
- var bakfile = file + ".bak";
- if (!File.Exists(bakfile))
- {
- return;
- }
- if (File.Exists(file))
- {
- File.Delete(file);
- }
- File.Move(bakfile, file);
- }
- }
- catch (Exception ex)
- {
- Trace.Write(string.Format("{0} {1}\r\n{2}", args[0].Trim(), args[1].Trim(), ex.ToString()));
- }
- }
- }
- }
|