Skip to content

Commit 199c611

Browse files
authored
Fix bug in build task allowing it to run multiple times when multitargeting and when packing nuget packages. (#46)
Signed-off-by: AraHaan <seandhunt_7@yahoo.com>
1 parent 3c1323d commit 199c611

File tree

5 files changed

+34
-7
lines changed

5 files changed

+34
-7
lines changed

build/GitBuildInfo.SourceGenerator.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
1+
<Project>
22

33
<PropertyGroup>
44
<GitBuildInfoIsGeneric Condition="'$(GitBuildInfoIsGeneric)' == ''">false</GitBuildInfoIsGeneric>

build/GitBuildInfo.SourceGenerator.targets

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
<?xml version="1.0" encoding="utf-8"?>
2-
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
1+
<Project>
32

43
<UsingTask TaskName="GitBuildInfo.GitInfoTask"
54
TaskFactory="RoslynCodeTaskFactory"
@@ -15,7 +14,7 @@
1514
</Task>
1615
</UsingTask>
1716

18-
<Target Name="GitBuildInfo" AfterTargets="PrepareForBuild">
17+
<Target Name="GitBuildInfo" AfterTargets="BeforeBuild">
1918
<GitInfoTask ProjectDir="$(MSBuildProjectDirectory)">
2019
<Output TaskParameter="GitHead" PropertyName="GitHead" />
2120
<Output TaskParameter="CommitHash" PropertyName="CommitHash" />

build/GitInfoTask.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,26 @@ public class GitInfoTask : Task
4242
/// <inheritdoc/>
4343
public override bool Execute()
4444
{
45+
var cache = (GitInfo)BuildEngine4.GetRegisteredTaskObject(this.ProjectDir, RegisteredTaskObjectLifetime.Build);
46+
if (cache is not null)
47+
{
48+
this.GitHead = cache.Head;
49+
this.CommitHash = cache.CommitHash;
50+
this.GitBranch = cache.Branch;
51+
return true;
52+
}
53+
4554
this.GitHead = this.RunGit("describe --all --always --dirty");
4655
this.CommitHash = this.RunGit("rev-parse --short HEAD");
4756
this.GitBranch = this.RunGit("name-rev --name-only HEAD");
4857
this.Log.LogMessage(MessageImportance.High, "Getting build info from git");
58+
cache = new GitInfo()
59+
{
60+
Head = this.GitHead,
61+
CommitHash = this.CommitHash,
62+
Branch = this.GitBranch,
63+
};
64+
BuildEngine4.RegisterTaskObject(this.ProjectDir, cache, RegisteredTaskObjectLifetime.Build, false);
4965
return true;
5066
}
5167

@@ -74,5 +90,14 @@ private string RunGit(string arguments)
7490
return "Not a git clone or git is not in Path.";
7591
}
7692
}
93+
94+
private class GitInfo
95+
{
96+
public string Head { get; set; }
97+
98+
public string CommitHash { get; set; }
99+
100+
public string Branch { get; set; }
101+
}
77102
}
78103
}

src/Elskom.GitInformation/stylecop.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
// https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/EnableConfiguration.md
77

88
"$schema": "https://raw.githubusercontent.com/DotNetAnalyzers/StyleCopAnalyzers/master/StyleCop.Analyzers/StyleCop.Analyzers/Settings/stylecop.schema.json",
9-
"settings": {
9+
"settings": {
10+
"layoutRules": {
11+
"newlineAtEndOfFile": "require"
12+
},
1013
"documentationRules": {
1114
"companyName": "Els_kom org.",
1215
"copyrightText": "Copyright (c) 2019-2021, {companyName}\nhttps://github.com/Elskom/\nAll rights reserved.\nlicense: MIT, see LICENSE for more details.",

src/GitBuildInfo.SourceGenerator/Directory.Build.props

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
<PropertyGroup>
44
<TargetFramework>netstandard2.0</TargetFramework>
55
<IsPackable>true</IsPackable>
6-
<Version>1.0.11</Version>
7-
<PackageReleaseNotes>Fix bug in build task treating MSBuildProjectDirectory as MSBuildProjectFullPath.</PackageReleaseNotes>
6+
<Version>1.0.12</Version>
7+
<PackageReleaseNotes>Fix bug in build task allowing it to run multiple times when multitargeting and when packing nuget packages.</PackageReleaseNotes>
88
<Copyright>Copyright (c) 2021</Copyright>
99
<!-- Special properties for analyzer packages. -->
1010
<IncludeBuildOutput>false</IncludeBuildOutput>

0 commit comments

Comments
 (0)