-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: AraHaan <[email protected]>
- Loading branch information
Showing
8 changed files
with
242 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,4 +25,4 @@ protected GenerationFailedException(SerializationInfo info, StreamingContext con | |
{ | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,13 @@ | ||
namespace GitBuildInfo.SourceGenerator | ||
{ | ||
/// <summary> | ||
/// Describes the options that feed into code generation. | ||
/// </summary> | ||
using System.Text.Json.Serialization; | ||
|
||
public record GeneratorOptions | ||
{ | ||
/// <summary> | ||
/// Gets the type to use to apply the attribute to embed the git information in that is within the assembly it is being applied to. | ||
/// </summary> | ||
/// <value>The type to use to apply the attribute to embed the git information in that is within the assembly it is being applied to.</value> | ||
[JsonPropertyName("AssemblyType")] | ||
public string? AssemblyType { get; set; } | ||
|
||
/// <summary> | ||
/// Gets if the type specified in AssemblyType is a generic type, by default this is set to false to indicate that the type is not a generic type. | ||
/// </summary> | ||
/// <value>If the type specified in AssemblyType is a generic type, by default this is set to false to indicate that the type is not a generic type.</value> | ||
[JsonPropertyName("IsGeneric")] | ||
public bool IsGeneric { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
namespace GitBuildInfo.SourceGenerator | ||
{ | ||
using System.Text.Json.Serialization; | ||
|
||
public record GitInfo | ||
{ | ||
[JsonPropertyName("GitHead")] | ||
public string? GitHead { get; set; } | ||
|
||
[JsonPropertyName("CommitHash")] | ||
public string? CommitHash { get; set; } | ||
|
||
[JsonPropertyName("GitBranch")] | ||
public string? GitBranch { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
|
||
<UsingTask TaskName="GitBuildInfo.GitInfoTask" | ||
TaskFactory="RoslynCodeTaskFactory" | ||
AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll"> | ||
<ParameterGroup> | ||
<OutputPath Required="true" /> | ||
</ParameterGroup> | ||
<Task> | ||
<Using Namespace="System" /> | ||
<Using Namespace="System.ComponentModel" /> | ||
<Using Namespace="System.Diagnostics" /> | ||
<Using Namespace="System.IO" /> | ||
<Using Namespace="System.Text" /> | ||
<Using Namespace="Microsoft.Build.Framework" /> | ||
<Using Namespace="Microsoft.Build.Utilities" /> | ||
<Code Type="Class" Language="cs"> | ||
<![CDATA[ | ||
namespace GitBuildInfo | ||
{ | ||
using System; | ||
using System.ComponentModel; | ||
using System.Diagnostics; | ||
using System.IO; | ||
using System.Text; | ||
using Microsoft.Build.Framework; | ||
using Microsoft.Build.Utilities; | ||
/// <summary> | ||
/// A MSBuild task that generates the msbuild information for an assembly. | ||
/// | ||
/// Note: use in the BeforeBuild target. | ||
/// </summary> | ||
public class GitInfoTask : Task | ||
{ | ||
/// <summary> | ||
/// Gets or sets the generated output file path. | ||
/// </summary> | ||
[Required] | ||
public string OutputPath { get; set; } | ||
/// <inheritdoc/> | ||
public override bool Execute() | ||
{ | ||
this.RunGit("describe --all --always --dirty", out var git_out1); | ||
this.RunGit("rev-parse --short HEAD", out var git_out2); | ||
this.RunGit("name-rev --name-only HEAD", out var git_out3); | ||
var outputData = $"{{{Environment.NewLine} \"GitHead\": \"{git_out1}\",{Environment.NewLine} \"CommitHash\": \"{git_out2}\",{Environment.NewLine} \"GitBranch\": \"{git_out3}\"{Environment.NewLine}}}"; | ||
// patch 112019: only print the getting build info from git message from the initial call to this task. | ||
// all other calls will not print anything to avoid spamming up the build output. | ||
try | ||
{ | ||
if (!File.Exists(this.OutputPath) || (File.Exists(this.OutputPath) && !string.Equals(outputData, File.ReadAllText(this.OutputPath), StringComparison.Ordinal))) | ||
{ | ||
this.Log.LogMessage(MessageImportance.High, "Getting build info from git"); | ||
File.WriteAllText(this.OutputPath, outputData); | ||
} | ||
} | ||
catch (IOException) | ||
{ | ||
// catch I/O error from being unable to open the file for checking it's contents. | ||
} | ||
return true; | ||
} | ||
private void RunGit(string arguments, out string git_out) | ||
{ | ||
using var pro1 = new Process(); | ||
pro1.StartInfo.FileName = "git"; | ||
pro1.StartInfo.Arguments = arguments; | ||
pro1.StartInfo.RedirectStandardOutput = true; | ||
pro1.StartInfo.UseShellExecute = false; | ||
pro1.StartInfo.CreateNoWindow = true; | ||
pro1.StartInfo.WorkingDirectory = Path.GetFullPath(this.OutputPath).Replace(Path.GetFileName(this.OutputPath), string.Empty); | ||
try | ||
{ | ||
_ = pro1.Start(); | ||
git_out = pro1.StandardOutput.ReadToEnd(); | ||
pro1.WaitForExit(); | ||
// handle all cases of possible endlines. | ||
git_out = git_out.Replace("\r\n", string.Empty); | ||
git_out = git_out.Replace("\n", string.Empty); | ||
git_out = git_out.Replace("\r", string.Empty); | ||
} | ||
catch (Win32Exception) | ||
{ | ||
git_out = "Not a git clone or git is not in Path."; | ||
} | ||
} | ||
} | ||
}]]> | ||
</Code> | ||
</Task> | ||
</UsingTask> | ||
|
||
<Target Name="BeforeCompile"> | ||
<GitInfoTask OutputPath="$(MSBuildProjectDirectory)\GitInfo.json" /> | ||
</Target> | ||
</Project> |