Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug in build task allowing it to run multiple times when multitargeting and when packing nuget packages. #46

Merged
merged 1 commit into from
Jun 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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