Skip to content

Commit

Permalink
Fix bug in build task allowing it to run multiple times when multitar…
Browse files Browse the repository at this point in the history
…geting and when packing nuget packages. (#46)

Signed-off-by: AraHaan <seandhunt_7@yahoo.com>
  • Loading branch information
AraHaan authored Jun 27, 2021
1 parent 3c1323d commit 199c611
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 7 deletions.
2 changes: 1 addition & 1 deletion build/GitBuildInfo.SourceGenerator.props
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Project>

<PropertyGroup>
<GitBuildInfoIsGeneric Condition="'$(GitBuildInfoIsGeneric)' == ''">false</GitBuildInfoIsGeneric>
Expand Down
5 changes: 2 additions & 3 deletions build/GitBuildInfo.SourceGenerator.targets
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Project>

<UsingTask TaskName="GitBuildInfo.GitInfoTask"
TaskFactory="RoslynCodeTaskFactory"
Expand All @@ -15,7 +14,7 @@
</Task>
</UsingTask>

<Target Name="GitBuildInfo" AfterTargets="PrepareForBuild">
<Target Name="GitBuildInfo" AfterTargets="BeforeBuild">
<GitInfoTask ProjectDir="$(MSBuildProjectDirectory)">
<Output TaskParameter="GitHead" PropertyName="GitHead" />
<Output TaskParameter="CommitHash" PropertyName="CommitHash" />
Expand Down
25 changes: 25 additions & 0 deletions build/GitInfoTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,26 @@ public class GitInfoTask : Task
/// <inheritdoc/>
public override bool Execute()
{
var cache = (GitInfo)BuildEngine4.GetRegisteredTaskObject(this.ProjectDir, RegisteredTaskObjectLifetime.Build);
if (cache is not null)
{
this.GitHead = cache.Head;
this.CommitHash = cache.CommitHash;
this.GitBranch = cache.Branch;
return true;
}

this.GitHead = this.RunGit("describe --all --always --dirty");
this.CommitHash = this.RunGit("rev-parse --short HEAD");
this.GitBranch = this.RunGit("name-rev --name-only HEAD");
this.Log.LogMessage(MessageImportance.High, "Getting build info from git");
cache = new GitInfo()
{
Head = this.GitHead,
CommitHash = this.CommitHash,
Branch = this.GitBranch,
};
BuildEngine4.RegisterTaskObject(this.ProjectDir, cache, RegisteredTaskObjectLifetime.Build, false);
return true;
}

Expand Down Expand Up @@ -74,5 +90,14 @@ private string RunGit(string arguments)
return "Not a git clone or git is not in Path.";
}
}

private class GitInfo
{
public string Head { get; set; }

public string CommitHash { get; set; }

public string Branch { get; set; }
}
}
}
5 changes: 4 additions & 1 deletion src/Elskom.GitInformation/stylecop.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
// https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/EnableConfiguration.md

"$schema": "https://raw.githubusercontent.com/DotNetAnalyzers/StyleCopAnalyzers/master/StyleCop.Analyzers/StyleCop.Analyzers/Settings/stylecop.schema.json",
"settings": {
"settings": {
"layoutRules": {
"newlineAtEndOfFile": "require"
},
"documentationRules": {
"companyName": "Els_kom org.",
"copyrightText": "Copyright (c) 2019-2021, {companyName}\nhttps://github.com/Elskom/\nAll rights reserved.\nlicense: MIT, see LICENSE for more details.",
Expand Down
4 changes: 2 additions & 2 deletions src/GitBuildInfo.SourceGenerator/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<IsPackable>true</IsPackable>
<Version>1.0.11</Version>
<PackageReleaseNotes>Fix bug in build task treating MSBuildProjectDirectory as MSBuildProjectFullPath.</PackageReleaseNotes>
<Version>1.0.12</Version>
<PackageReleaseNotes>Fix bug in build task allowing it to run multiple times when multitargeting and when packing nuget packages.</PackageReleaseNotes>
<Copyright>Copyright (c) 2021</Copyright>
<!-- Special properties for analyzer packages. -->
<IncludeBuildOutput>false</IncludeBuildOutput>
Expand Down

0 comments on commit 199c611

Please sign in to comment.