yuxi-lee 4 years ago
commit
845c5ecc3f
6 changed files with 362 additions and 0 deletions
  1. 11 0
      .gitignore
  2. 97 0
      CMD/CMDHelper.cs
  3. 59 0
      NetGitTool.csproj
  4. 22 0
      NetGitTool.sln
  5. 137 0
      Program.cs
  6. 36 0
      Properties/AssemblyInfo.cs

+ 11 - 0
.gitignore

@@ -0,0 +1,11 @@
+packages
+bin
+obj
+*.csproj.user
+*.vsp
+*.psess
+*.suo
+.DS_Store
+build
+*.pidb
+TestResult.xml

+ 97 - 0
CMD/CMDHelper.cs

@@ -0,0 +1,97 @@
+using System.Diagnostics;
+using System.IO;
+
+namespace LJLib.Cmd
+{
+    public static class CMDHelper
+    {
+        public static string RunWithReturn(string cmd, string workingdir = null)
+        {
+            using (Process p = new Process())
+            {
+                if (!string.IsNullOrEmpty(workingdir) && Directory.Exists(workingdir))
+                {
+                    p.StartInfo.WorkingDirectory = workingdir;
+                }
+
+                RunCmd(p, cmd);
+
+                string line = p.StandardOutput.ReadLine();
+                while (line != cmd)
+                {
+                    line = p.StandardOutput.ReadLine();
+                }
+
+                string rslt = string.Empty;
+                line = p.StandardOutput.ReadLine();
+                while (line != "exit")
+                {
+                    if (string.IsNullOrEmpty(rslt))
+                    {
+                        rslt = line;
+                    }
+                    else
+                    {
+                        rslt += "\r\n" + line;
+                    }
+                    line = p.StandardOutput.ReadLine();
+                }
+
+                string errmsg = p.StandardError.ReadToEnd().Trim();
+                if (!string.IsNullOrEmpty(errmsg))
+                {
+                    rslt += "\r\nERROR:\r\n" + errmsg;
+                }
+                return rslt;
+            }
+        }
+
+        //public static string RunWithReturn(string cmd, Encoding encoding)
+        //{
+        //    Process p = new Process();
+        //    RunCmd(p, cmd);
+        //    string rslt = "";
+
+        //    byte[] St = new byte[1024];
+        //    using (MemoryStream ms = new MemoryStream())
+        //    {
+        //        int cnt = 0;
+        //        while ((cnt = p.StandardOutput.BaseStream.Read(St, 0, St.Length)) > 0)
+        //        {
+        //            ms.Write(St, 0, cnt);
+        //        }
+        //        St = ms.ToArray();
+        //    }
+        //    string msg = Encoding.UTF8.GetString(St);
+
+        //    using (StreamReader reader = new StreamReader(p.StandardOutput.BaseStream, encoding))
+        //    using (StreamReader errReader = new StreamReader(p.StandardError.BaseStream, encoding))
+        //    {
+        //        rslt += reader.ReadToEnd() + "\r\n" + errReader.ReadToEnd();
+        //    }
+        //    return rslt;
+        //}
+
+        public static void Run(string cmd)
+        {
+            using (Process p = new Process())
+            {
+                RunCmd(p, cmd);
+            }
+        }
+
+        private static void RunCmd(Process p, string cmd)
+        {
+            p.StartInfo.FileName = "cmd.exe";
+            p.StartInfo.UseShellExecute = false;
+            p.StartInfo.RedirectStandardInput = true;
+            p.StartInfo.RedirectStandardOutput = true;
+            p.StartInfo.RedirectStandardError = true;
+            p.StartInfo.CreateNoWindow = true;
+            p.Start();
+            p.StandardInput.WriteLine("echo off");
+            p.StandardInput.WriteLine(cmd);
+            p.StandardInput.WriteLine("exit");
+        }
+    }
+}

+ 59 - 0
NetGitTool.csproj

@@ -0,0 +1,59 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
+  <PropertyGroup>
+    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+    <ProjectGuid>{5B57CACB-8C3E-4E44-9D07-9F942803E693}</ProjectGuid>
+    <OutputType>WinExe</OutputType>
+    <AppDesignerFolder>Properties</AppDesignerFolder>
+    <RootNamespace>NetGitTool</RootNamespace>
+    <AssemblyName>NetGitTool</AssemblyName>
+    <TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
+    <FileAlignment>512</FileAlignment>
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+    <PlatformTarget>AnyCPU</PlatformTarget>
+    <DebugSymbols>true</DebugSymbols>
+    <DebugType>full</DebugType>
+    <Optimize>false</Optimize>
+    <OutputPath>bin\Debug\</OutputPath>
+    <DefineConstants>DEBUG;TRACE</DefineConstants>
+    <ErrorReport>prompt</ErrorReport>
+    <WarningLevel>4</WarningLevel>
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
+    <PlatformTarget>AnyCPU</PlatformTarget>
+    <DebugType>pdbonly</DebugType>
+    <Optimize>true</Optimize>
+    <OutputPath>bin\Release\</OutputPath>
+    <DefineConstants>TRACE</DefineConstants>
+    <ErrorReport>prompt</ErrorReport>
+    <WarningLevel>4</WarningLevel>
+  </PropertyGroup>
+  <PropertyGroup>
+    <StartupObject />
+  </PropertyGroup>
+  <ItemGroup>
+    <Reference Include="System" />
+    <Reference Include="System.Core" />
+    <Reference Include="System.Windows.Forms" />
+    <Reference Include="System.Xml.Linq" />
+    <Reference Include="System.Data.DataSetExtensions" />
+    <Reference Include="System.Data" />
+    <Reference Include="System.Xml" />
+  </ItemGroup>
+  <ItemGroup>
+    <Compile Include="CMD\CMDHelper.cs" />
+    <Compile Include="Program.cs" />
+    <Compile Include="Properties\AssemblyInfo.cs" />
+  </ItemGroup>
+  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
+  <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
+       Other similar extension points exist, see Microsoft.Common.targets.
+  <Target Name="BeforeBuild">
+  </Target>
+  <Target Name="AfterBuild">
+  </Target>
+  -->
+</Project>

+ 22 - 0
NetGitTool.sln

@@ -0,0 +1,22 @@
+
+Microsoft Visual Studio Solution File, Format Version 12.00
+# Visual Studio 2013
+VisualStudioVersion = 12.0.21005.1
+MinimumVisualStudioVersion = 10.0.40219.1
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetGitTool", "NetGitTool.csproj", "{5B57CACB-8C3E-4E44-9D07-9F942803E693}"
+EndProject
+Global
+	GlobalSection(SolutionConfigurationPlatforms) = preSolution
+		Debug|Any CPU = Debug|Any CPU
+		Release|Any CPU = Release|Any CPU
+	EndGlobalSection
+	GlobalSection(ProjectConfigurationPlatforms) = postSolution
+		{5B57CACB-8C3E-4E44-9D07-9F942803E693}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{5B57CACB-8C3E-4E44-9D07-9F942803E693}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{5B57CACB-8C3E-4E44-9D07-9F942803E693}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{5B57CACB-8C3E-4E44-9D07-9F942803E693}.Release|Any CPU.Build.0 = Release|Any CPU
+	EndGlobalSection
+	GlobalSection(SolutionProperties) = preSolution
+		HideSolutionNode = FALSE
+	EndGlobalSection
+EndGlobal

+ 137 - 0
Program.cs

@@ -0,0 +1,137 @@
+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();
+                    }
+                    File.Move(file, file + ".bak");
+                    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()));
+            }
+        }
+    }
+}

+ 36 - 0
Properties/AssemblyInfo.cs

@@ -0,0 +1,36 @@
+using System.Reflection;
+using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
+
+// 有关程序集的常规信息通过以下
+// 特性集控制。更改这些特性值可修改
+// 与程序集关联的信息。
+[assembly: AssemblyTitle("NetGitTool")]
+[assembly: AssemblyDescription("")]
+[assembly: AssemblyConfiguration("")]
+[assembly: AssemblyCompany("")]
+[assembly: AssemblyProduct("NetGitTool")]
+[assembly: AssemblyCopyright("Copyright ©  2017")]
+[assembly: AssemblyTrademark("")]
+[assembly: AssemblyCulture("")]
+
+// 将 ComVisible 设置为 false 使此程序集中的类型
+// 对 COM 组件不可见。  如果需要从 COM 访问此程序集中的类型,
+// 则将该类型上的 ComVisible 特性设置为 true。
+[assembly: ComVisible(false)]
+
+// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
+[assembly: Guid("9550ab81-4628-48ec-8bf0-51b1afb5b245")]
+
+// 程序集的版本信息由下面四个值组成: 
+//
+//      主版本
+//      次版本 
+//      生成号
+//      修订号
+//
+// 可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值,
+// 方法是按如下所示使用“*”: 
+// [assembly: AssemblyVersion("1.0.*")]
+[assembly: AssemblyVersion("1.0.0.0")]
+[assembly: AssemblyFileVersion("1.0.0.0")]