Skip to content

Commit

Permalink
MSBuild caching
Browse files Browse the repository at this point in the history
  • Loading branch information
adamralph committed Aug 15, 2024
1 parent 2e48c6e commit 808898e
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 9 deletions.
19 changes: 19 additions & 0 deletions MSBuild.Caching/CacheGet.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Microsoft.Build.Framework;
using Task = Microsoft.Build.Utilities.Task;

namespace MSBuild.Caching;

public class CacheGet : Task
{
[Required]
public string? Key { get; set; }

[Output]
public string? Value { get; set; }

public override bool Execute()
{
this.Value = (string?)this.BuildEngine4.GetRegisteredTaskObject(this.Key, RegisteredTaskObjectLifetime.Build);
return true;
}
}
19 changes: 19 additions & 0 deletions MSBuild.Caching/CacheSet.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Microsoft.Build.Framework;
using Task = Microsoft.Build.Utilities.Task;

namespace MSBuild.Caching;

public class CacheSet : Task
{
[Required]
public string? Key { get; set; }

[Required]
public string? Value { get; set; }

public override bool Execute()
{
this.BuildEngine4.RegisterTaskObject(this.Key, this.Value, RegisteredTaskObjectLifetime.Build, default);
return true;
}
}
13 changes: 13 additions & 0 deletions MSBuild.Caching/MSBuild.Caching.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<IsPackable>false</IsPackable>
<TargetFrameworks>net472;net6.0</TargetFrameworks>
<LangVersion>12.0</LangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Build.Utilities.Core" Version="17.0.1" PrivateAssets="All" ExcludeAssets="runtime" />
</ItemGroup>

</Project>
6 changes: 6 additions & 0 deletions MinVer.sln
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MinVerTests.Infra", "MinVer
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MinVerTests.Packages", "MinVerTests.Packages\MinVerTests.Packages.csproj", "{694ECAC7-FD05-4550-A4FB-8F497D29ECB0}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MSBuild.Caching", "MSBuild.Caching\MSBuild.Caching.csproj", "{3A9A6053-076E-47D4-9912-E04BD11C666F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -51,6 +53,10 @@ Global
{694ECAC7-FD05-4550-A4FB-8F497D29ECB0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{694ECAC7-FD05-4550-A4FB-8F497D29ECB0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{694ECAC7-FD05-4550-A4FB-8F497D29ECB0}.Release|Any CPU.Build.0 = Release|Any CPU
{3A9A6053-076E-47D4-9912-E04BD11C666F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3A9A6053-076E-47D4-9912-E04BD11C666F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3A9A6053-076E-47D4-9912-E04BD11C666F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3A9A6053-076E-47D4-9912-E04BD11C666F}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
3 changes: 3 additions & 0 deletions MinVer/MinVer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

<ItemGroup>
<ProjectReference Include="..\MinVer.Lib\MinVer.Lib.csproj" PrivateAssets="All" />
<ProjectReference Include="..\MSBuild.Caching\MSBuild.Caching.csproj" ReferenceOutputAssembly="false" Private="false" />
</ItemGroup>

<ItemGroup>
Expand All @@ -41,6 +42,8 @@
<None Remove="buildMultiTargeting\**\*" />
<Content Include="build\**\*" PackagePath="build" />
<Content Include="buildMultiTargeting\**\*" PackagePath="buildMultiTargeting" />
<None Include="..\MSBuild.Caching\bin\$(Configuration)\net472\MSBuild.Caching.dll" Pack="true" PackagePath="build\bin\net472" Visible="false" />
<None Include="..\MSBuild.Caching\bin\$(Configuration)\net6.0\MSBuild.Caching.dll" Pack="true" PackagePath="build\bin\net6.0" Visible="false" />
</ItemGroup>

<Target Name="AddMinVerOutput" BeforeTargets="_GetPackageFiles">
Expand Down
17 changes: 13 additions & 4 deletions MinVer/build/MinVer.targets
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@
<MSBuildAllProjects Condition="'$(MSBuildAssemblyVersion)' == '' Or '$(MSBuildAssemblyVersion)' &lt; '16.0'">$(MSBuildAllProjects);$(MSBuildThisFileFullPath)</MSBuildAllProjects>
<GenerateNuspecDependsOn>$(GenerateNuspecDependsOn);MinVer</GenerateNuspecDependsOn>
<GetPackageVersionDependsOn>$(GetPackageVersionDependsOn);MinVer</GetPackageVersionDependsOn>
<MinVerMSBuildCachingPath Condition="'$(MSBuildRuntimeType)' == 'Core'">$(MSBuildThisFileDirectory)bin\net6.0\MSBuild.Caching.dll</MinVerMSBuildCachingPath>
<MinVerMSBuildCachingPath Condition="'$(MSBuildRuntimeType)' != 'Core'">$(MSBuildThisFileDirectory)bin\net472\MSBuild.Caching.dll</MinVerMSBuildCachingPath>
</PropertyGroup>

<UsingTask TaskName="MSBuild.Caching.CacheGet" AssemblyFile="$(MinVerMSBuildCachingPath)" />
<UsingTask TaskName="MSBuild.Caching.CacheSet" AssemblyFile="$(MinVerMSBuildCachingPath)" />

<PropertyGroup>
<MinVerDetailed>low</MinVerDetailed>
<MinVerDetailed Condition="'$(MinVerVerbosity)' == 'detailed' Or '$(MinVerVerbosity)' == 'd' Or '$(MinVerVerbosity)' == 'diagnostic' Or '$(MinVerVerbosity)' == 'diag'">high</MinVerDetailed>
Expand Down Expand Up @@ -34,7 +39,6 @@
<MinVerOutputVersion Remove="@(MinVerOutputVersion)" />
</ItemGroup>
<ItemGroup>
<MinVerInputs Include="&quot;$(MSBuildProjectDirectory)&quot;" />
<MinVerInputs Include="--auto-increment &quot;$(MinVerAutoIncrement)&quot;" />
<MinVerInputs Include="--build-metadata &quot;$(MinVerBuildMetadata)&quot;" />
<MinVerInputs Include="--default-pre-release-identifiers &quot;$(MinVerDefaultPreReleaseIdentifiers)&quot;" />
Expand All @@ -45,14 +49,19 @@
<MinVerInputs Include="--verbosity &quot;$(MinVerVerbosity)&quot;" />
<MinVerInputs Include="--version-override &quot;$(MinVerVersionOverride)&quot;" />
</ItemGroup>
<Exec Command="dotnet &quot;$(MSBuildThisFileDirectory)bin/$(MinVerTargetFramework)/MinVer.dll&quot; @(MinVerInputs->'%(Identity)', ' ')" ConsoleToMSBuild="true" StandardOutputImportance="Low" >
<CacheGet Key="@(MinVerInputs->'%(Identity)', ' ')"><Output TaskParameter="Value" PropertyName="MinVerVersion" /></CacheGet>
<Message Condition="'$(MinVerVersion)' != ''" Importance="$(MinVerDetailed)" Text="MinVer: Using cached MinVerVersion $(MinVerVersion)" />
<Exec Condition="'$(MinVerVersion)' == ''" Command="dotnet &quot;$(MSBuildThisFileDirectory)bin/$(MinVerTargetFramework)/MinVer.dll&quot; &quot;$(MSBuildProjectDirectory)&quot; @(MinVerInputs->'%(Identity)', ' ')" ConsoleToMSBuild="true" StandardOutputImportance="Low" >
<Output TaskParameter="ConsoleOutput" ItemName="MinVerConsoleOutput" />
</Exec>
<ItemGroup>
<ItemGroup Condition="'$(MinVerVersion)' == ''" >
<MinVerOutputVersion Include="@(MinVerConsoleOutput)" Condition="'$([System.String]::new(`%(Identity)`).StartsWith(`MinVer:`))' != 'true'" />
</ItemGroup>
<PropertyGroup>
<PropertyGroup Condition="'$(MinVerVersion)' == ''" >
<MinVerVersion>@(MinVerOutputVersion)</MinVerVersion>
</PropertyGroup>
<CacheSet Key="@(MinVerInputs->'%(Identity)', ' ')" Value="$(MinVerVersion)" />
<PropertyGroup>
<MinVerMajor>$(MinVerVersion.Split(`.`)[0])</MinVerMajor>
<MinVerMinor>$(MinVerVersion.Split(`.`)[1])</MinVerMinor>
<MinVerPatch>$(MinVerVersion.Split(`.`)[2].Split(`-`)[0].Split(`+`)[0])</MinVerPatch>
Expand Down
2 changes: 0 additions & 2 deletions MinVerTests.Packages/MultipleProjects.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ public async Task MultipleTagPrefixes()
Assert.Collection(
versionCalculations,
message => Assert.Equal("MinVer: Calculated version 2.3.4.", message),
message => Assert.Equal("MinVer: Calculated version 5.6.7.", message),
message => Assert.Equal("MinVer: Calculated version 2.3.4.", message),
message => Assert.Equal("MinVer: Calculated version 5.6.7.", message));

Assert.Collection(
Expand Down
6 changes: 3 additions & 3 deletions targets/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@

Target(
"eyeball-minver-logs",
"build a test project with the MinVer package to eyeball the diagnostic logs",
"build a test solution with the MinVer package to eyeball the diagnostic logs",
DependsOn("build"),
async () =>
{
var path = TestDirectory.Get("MinVer.Targets", "eyeball-minver-logs");

await Sdk.CreateProject(path, "Release");
await Sdk.CreateSolution(path, ["project0", "project1",], "Release");

await Git.Init(path);
await Git.Commit(path);
Expand All @@ -43,7 +43,7 @@

await RunAsync(
"dotnet",
"build --no-restore --nologo",
"build --no-restore --nologo -maxCpuCount:1",
path,
configureEnvironment: env =>
{
Expand Down

0 comments on commit 808898e

Please sign in to comment.