|
8 | 8 | <OutputPath Required="true" />
|
9 | 9 | </ParameterGroup>
|
10 | 10 | <Task>
|
11 |
| - <Using Namespace="System" /> |
12 |
| - <Using Namespace="System.ComponentModel" /> |
13 |
| - <Using Namespace="System.Diagnostics" /> |
14 |
| - <Using Namespace="System.IO" /> |
15 |
| - <Using Namespace="System.Text" /> |
16 |
| - <Using Namespace="Microsoft.Build.Framework" /> |
17 |
| - <Using Namespace="Microsoft.Build.Utilities" /> |
18 |
| - <Code Type="Class" Language="cs"> |
19 |
| - <![CDATA[ |
20 |
| -namespace GitBuildInfo |
21 |
| -{ |
22 |
| - using System; |
23 |
| - using System.ComponentModel; |
24 |
| - using System.Diagnostics; |
25 |
| - using System.IO; |
26 |
| - using System.Text; |
27 |
| - using Microsoft.Build.Framework; |
28 |
| - using Microsoft.Build.Utilities; |
29 |
| -
|
30 |
| - /// <summary> |
31 |
| - /// A MSBuild task that generates the msbuild information for an assembly. |
32 |
| - /// |
33 |
| - /// Note: use in the BeforeBuild target. |
34 |
| - /// </summary> |
35 |
| - public class GitInfoTask : Task |
36 |
| - { |
37 |
| - /// <summary> |
38 |
| - /// Gets or sets the generated output file path. |
39 |
| - /// </summary> |
40 |
| - [Required] |
41 |
| - public string OutputPath { get; set; } |
42 |
| -
|
43 |
| - /// <inheritdoc/> |
44 |
| - public override bool Execute() |
45 |
| - { |
46 |
| - this.RunGit("describe --all --always --dirty", out var git_out1); |
47 |
| - this.RunGit("rev-parse --short HEAD", out var git_out2); |
48 |
| - this.RunGit("name-rev --name-only HEAD", out var git_out3); |
49 |
| - var outputData = $"{{{Environment.NewLine} \"GitHead\": \"{git_out1}\",{Environment.NewLine} \"CommitHash\": \"{git_out2}\",{Environment.NewLine} \"GitBranch\": \"{git_out3}\"{Environment.NewLine}}}"; |
50 |
| - // patch 112019: only print the getting build info from git message from the initial call to this task. |
51 |
| - // all other calls will not print anything to avoid spamming up the build output. |
52 |
| - try |
53 |
| - { |
54 |
| - if (!File.Exists(this.OutputPath) || (File.Exists(this.OutputPath) && !string.Equals(outputData, File.ReadAllText(this.OutputPath), StringComparison.Ordinal))) |
55 |
| - { |
56 |
| - this.Log.LogMessage(MessageImportance.High, "Getting build info from git"); |
57 |
| - File.WriteAllText(this.OutputPath, outputData); |
58 |
| - } |
59 |
| - } |
60 |
| - catch (IOException) |
61 |
| - { |
62 |
| - // catch I/O error from being unable to open the file for checking it's contents. |
63 |
| - } |
64 |
| - return true; |
65 |
| - } |
66 |
| -
|
67 |
| - private void RunGit(string arguments, out string git_out) |
68 |
| - { |
69 |
| - using var pro1 = new Process(); |
70 |
| - pro1.StartInfo.FileName = "git"; |
71 |
| - pro1.StartInfo.Arguments = arguments; |
72 |
| - pro1.StartInfo.RedirectStandardOutput = true; |
73 |
| - pro1.StartInfo.UseShellExecute = false; |
74 |
| - pro1.StartInfo.CreateNoWindow = true; |
75 |
| - pro1.StartInfo.WorkingDirectory = Path.GetFullPath(this.OutputPath).Replace(Path.GetFileName(this.OutputPath), string.Empty); |
76 |
| - try |
77 |
| - { |
78 |
| - _ = pro1.Start(); |
79 |
| - git_out = pro1.StandardOutput.ReadToEnd(); |
80 |
| - pro1.WaitForExit(); |
81 |
| - // handle all cases of possible endlines. |
82 |
| - git_out = git_out.Replace("\r\n", string.Empty); |
83 |
| - git_out = git_out.Replace("\n", string.Empty); |
84 |
| - git_out = git_out.Replace("\r", string.Empty); |
85 |
| - } |
86 |
| - catch (Win32Exception) |
87 |
| - { |
88 |
| - git_out = "Not a git clone or git is not in Path."; |
89 |
| - } |
90 |
| - } |
91 |
| - } |
92 |
| -}]]> |
93 |
| - </Code> |
| 11 | + <Code Source="GitInfoTask.cs" /> |
94 | 12 | </Task>
|
95 | 13 | </UsingTask>
|
96 | 14 |
|
|
0 commit comments