From cf263262848d8774f2e52f8483b304c114d4d383 Mon Sep 17 00:00:00 2001 From: Owen Roberts <10622911+owen-roberts@users.noreply.github.com> Date: Thu, 5 Dec 2019 09:00:06 +0000 Subject: [PATCH] Remove UseToolVersion VS2017 from MsBuild task (#18) * Remove UseToolVersion VS2017 from MsBuild task * Add MsBuildToolVersion script argument * Refactor MSBuildToolVersion setting to partial class within the MSBuild task * Refactor configuration partial class into separate configuration-MsBuild.cake file * Refactor configuration partial to use TaskParameters rather than a MSBuildToolVersion property --- .cake/Build-MsBuild.cake | 7 ++++++- .cake/Configuration-MsBuild.cake | 19 +++++++++++++++++++ .cake/Configuration.cake | 9 +++++++++ .cake/Publish-MsBuild-Folder.cake | 7 ++++++- .cake/Publish-MsBuild-WebDeploy.cake | 7 ++++++- 5 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 .cake/Configuration-MsBuild.cake diff --git a/.cake/Build-MsBuild.cake b/.cake/Build-MsBuild.cake index acad65a..ee7d3ad 100644 --- a/.cake/Build-MsBuild.cake +++ b/.cake/Build-MsBuild.cake @@ -1,14 +1,19 @@ #load "Configuration.cake" +#load "Configuration-MsBuild.cake" Task("Build:MsBuild") .IsDependentOn("Restore") .IsDependeeOf("Build") .Does(config => { + var toolVersion = config.GetTaskParameter("MsBuild:Version", MSBuildToolVersion.Default); + + Information("MS Build Tool Version: " + toolVersion.ToString()); + MSBuild(config.Solution.Path.ToString(), c => c .SetConfiguration(config.Solution.BuildConfiguration) .SetVerbosity(Verbosity.Minimal) - .UseToolVersion(MSBuildToolVersion.VS2017) + .UseToolVersion(toolVersion) .WithWarningsAsError() .WithTarget("Build") ); diff --git a/.cake/Configuration-MsBuild.cake b/.cake/Configuration-MsBuild.cake new file mode 100644 index 0000000..e3d9806 --- /dev/null +++ b/.cake/Configuration-MsBuild.cake @@ -0,0 +1,19 @@ +#load "Configuration.cake" +public partial class Configuration { + + public Configuration SetMSBuildToolVersion(MSBuildToolVersion toolVersion, bool allowArgumentOverride = true) + { + var argument = ParseEnum(context.Argument("MSBuildToolVersion", "Default")); + + var version = (allowArgumentOverride && argument != MSBuildToolVersion.Default) ? argument : toolVersion; + + this.TaskParameters.Add("MsBuild:Version", version); + + return this; + } + + private static T ParseEnum(string value) + { + return (T) Enum.Parse(typeof(T), value, true); + } +} diff --git a/.cake/Configuration.cake b/.cake/Configuration.cake index 92e8916..6608b24 100644 --- a/.cake/Configuration.cake +++ b/.cake/Configuration.cake @@ -90,6 +90,15 @@ public partial class Configuration { Solution.Log(logger); } + public T GetTaskParameter(string key, T defaultValue){ + + if(TaskParameters.TryGetValue(key, out object value) && value is T) + { + return (T)value; + } + + return defaultValue; + } } public enum ArtifactTypeOption { Zip, diff --git a/.cake/Publish-MsBuild-Folder.cake b/.cake/Publish-MsBuild-Folder.cake index bc73a00..7d8a63e 100644 --- a/.cake/Publish-MsBuild-Folder.cake +++ b/.cake/Publish-MsBuild-Folder.cake @@ -1,10 +1,15 @@ #load "Configuration.cake" +#load "Configuration-MsBuild.cake" Task("Publish:MsBuild") .IsDependentOn("Build") .IsDependeeOf("Publish") .Does(config => { + var toolVersion = config.GetTaskParameter("MsBuild:Version", MSBuildToolVersion.Default); + + Information("MsBuild Tool Version: " + toolVersion.ToString()); + foreach(var webProject in config.Solution.WebProjects) { var assemblyName = config.Solution.GetProjectName(webProject); var projectArtifactDirectory = $"{config.Artifacts.GetRootFor(ArtifactTypeOption.Zip)}/{assemblyName}"; @@ -14,7 +19,7 @@ Task("Publish:MsBuild") MSBuild(webProject.ProjectFilePath, c => c .SetConfiguration(config.Solution.BuildConfiguration) .SetVerbosity(Verbosity.Quiet) - .UseToolVersion(MSBuildToolVersion.VS2017) + .UseToolVersion(toolVersion) .WithWarningsAsError() .WithTarget("Package") .WithProperty("DeployTarget", "PipelinePreDeployCopyAllFilesToOneFolder") diff --git a/.cake/Publish-MsBuild-WebDeploy.cake b/.cake/Publish-MsBuild-WebDeploy.cake index 44c6277..297aba1 100644 --- a/.cake/Publish-MsBuild-WebDeploy.cake +++ b/.cake/Publish-MsBuild-WebDeploy.cake @@ -1,10 +1,15 @@ #load "Configuration.cake" +#load "Configuration-MsBuild.cake" Task("Publish:MsBuild") .IsDependentOn("Build") .IsDependeeOf("Publish") .Does(config => { + var toolVersion = config.GetTaskParameter("MsBuild:Version", MSBuildToolVersion.Default); + + Information("MsBuild Tool Version: " + toolVersion.ToString()); + foreach(var webProject in config.Solution.WebProjects) { var assemblyName = config.Solution.GetProjectName(webProject); var projectArtifactDirectory = $"{config.Artifacts.GetRootFor(ArtifactTypeOption.WebDeploy)}/{assemblyName}"; @@ -14,7 +19,7 @@ Task("Publish:MsBuild") MSBuild(webProject.ProjectFilePath, c => c .SetConfiguration(config.Solution.BuildConfiguration) .SetVerbosity(Verbosity.Quiet) - .UseToolVersion(MSBuildToolVersion.VS2017) + .UseToolVersion(toolVersion) .WithWarningsAsError() .WithTarget("Package") .WithProperty("PackageAsSingleFile", "true")