From 808898e9afa0f326c622b13bb1718f3dea310c12 Mon Sep 17 00:00:00 2001 From: Adam Ralph Date: Sat, 20 Jul 2024 18:19:40 +0200 Subject: [PATCH] MSBuild caching --- MSBuild.Caching/CacheGet.cs | 19 +++++++++++++++++++ MSBuild.Caching/CacheSet.cs | 19 +++++++++++++++++++ MSBuild.Caching/MSBuild.Caching.csproj | 13 +++++++++++++ MinVer.sln | 6 ++++++ MinVer/MinVer.csproj | 3 +++ MinVer/build/MinVer.targets | 17 +++++++++++++---- MinVerTests.Packages/MultipleProjects.cs | 2 -- targets/Program.cs | 6 +++--- 8 files changed, 76 insertions(+), 9 deletions(-) create mode 100644 MSBuild.Caching/CacheGet.cs create mode 100644 MSBuild.Caching/CacheSet.cs create mode 100644 MSBuild.Caching/MSBuild.Caching.csproj diff --git a/MSBuild.Caching/CacheGet.cs b/MSBuild.Caching/CacheGet.cs new file mode 100644 index 00000000..9c06d32a --- /dev/null +++ b/MSBuild.Caching/CacheGet.cs @@ -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; + } +} diff --git a/MSBuild.Caching/CacheSet.cs b/MSBuild.Caching/CacheSet.cs new file mode 100644 index 00000000..74c367b6 --- /dev/null +++ b/MSBuild.Caching/CacheSet.cs @@ -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; + } +} diff --git a/MSBuild.Caching/MSBuild.Caching.csproj b/MSBuild.Caching/MSBuild.Caching.csproj new file mode 100644 index 00000000..5b2e954a --- /dev/null +++ b/MSBuild.Caching/MSBuild.Caching.csproj @@ -0,0 +1,13 @@ + + + + false + net472;net6.0 + 12.0 + + + + + + + diff --git a/MinVer.sln b/MinVer.sln index bf1988b1..84b2bd6c 100644 --- a/MinVer.sln +++ b/MinVer.sln @@ -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 @@ -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 diff --git a/MinVer/MinVer.csproj b/MinVer/MinVer.csproj index 263a10c6..f29e4649 100644 --- a/MinVer/MinVer.csproj +++ b/MinVer/MinVer.csproj @@ -32,6 +32,7 @@ + @@ -41,6 +42,8 @@ + + diff --git a/MinVer/build/MinVer.targets b/MinVer/build/MinVer.targets index c094e9fc..bff32d51 100644 --- a/MinVer/build/MinVer.targets +++ b/MinVer/build/MinVer.targets @@ -4,8 +4,13 @@ $(MSBuildAllProjects);$(MSBuildThisFileFullPath) $(GenerateNuspecDependsOn);MinVer $(GetPackageVersionDependsOn);MinVer + $(MSBuildThisFileDirectory)bin\net6.0\MSBuild.Caching.dll + $(MSBuildThisFileDirectory)bin\net472\MSBuild.Caching.dll + + + low high @@ -34,7 +39,6 @@ - @@ -45,14 +49,19 @@ - + + + - + - + @(MinVerOutputVersion) + + + $(MinVerVersion.Split(`.`)[0]) $(MinVerVersion.Split(`.`)[1]) $(MinVerVersion.Split(`.`)[2].Split(`-`)[0].Split(`+`)[0]) diff --git a/MinVerTests.Packages/MultipleProjects.cs b/MinVerTests.Packages/MultipleProjects.cs index 593c2065..9fa81fae 100644 --- a/MinVerTests.Packages/MultipleProjects.cs +++ b/MinVerTests.Packages/MultipleProjects.cs @@ -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( diff --git a/targets/Program.cs b/targets/Program.cs index d2a83675..ba9db034 100644 --- a/targets/Program.cs +++ b/targets/Program.cs @@ -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); @@ -43,7 +43,7 @@ await RunAsync( "dotnet", - "build --no-restore --nologo", + "build --no-restore --nologo -maxCpuCount:1", path, configureEnvironment: env => {