Skip to content

Commit

Permalink
Remove UseToolVersion VS2017 from MsBuild task (#18)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
owen-roberts authored and Philo committed Dec 5, 2019
1 parent d2aa29c commit cf26326
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 3 deletions.
7 changes: 6 additions & 1 deletion .cake/Build-MsBuild.cake
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
#load "Configuration.cake"
#load "Configuration-MsBuild.cake"

Task("Build:MsBuild")
.IsDependentOn("Restore")
.IsDependeeOf("Build")
.Does<Configuration>(config =>
{
var toolVersion = config.GetTaskParameter<MSBuildToolVersion>("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")
);
Expand Down
19 changes: 19 additions & 0 deletions .cake/Configuration-MsBuild.cake
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#load "Configuration.cake"
public partial class Configuration {

public Configuration SetMSBuildToolVersion(MSBuildToolVersion toolVersion, bool allowArgumentOverride = true)
{
var argument = ParseEnum<MSBuildToolVersion>(context.Argument("MSBuildToolVersion", "Default"));

var version = (allowArgumentOverride && argument != MSBuildToolVersion.Default) ? argument : toolVersion;

this.TaskParameters.Add("MsBuild:Version", version);

return this;
}

private static T ParseEnum<T>(string value)
{
return (T) Enum.Parse(typeof(T), value, true);
}
}
9 changes: 9 additions & 0 deletions .cake/Configuration.cake
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,15 @@ public partial class Configuration {
Solution.Log(logger);
}

public T GetTaskParameter<T>(string key, T defaultValue){

if(TaskParameters.TryGetValue(key, out object value) && value is T)
{
return (T)value;
}

return defaultValue;
}
}
public enum ArtifactTypeOption {
Zip,
Expand Down
7 changes: 6 additions & 1 deletion .cake/Publish-MsBuild-Folder.cake
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
#load "Configuration.cake"
#load "Configuration-MsBuild.cake"

Task("Publish:MsBuild")
.IsDependentOn("Build")
.IsDependeeOf("Publish")
.Does<Configuration>(config =>
{
var toolVersion = config.GetTaskParameter<MSBuildToolVersion>("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}";
Expand All @@ -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")
Expand Down
7 changes: 6 additions & 1 deletion .cake/Publish-MsBuild-WebDeploy.cake
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
#load "Configuration.cake"
#load "Configuration-MsBuild.cake"

Task("Publish:MsBuild")
.IsDependentOn("Build")
.IsDependeeOf("Publish")
.Does<Configuration>(config =>
{
var toolVersion = config.GetTaskParameter<MSBuildToolVersion>("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}";
Expand All @@ -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")
Expand Down

0 comments on commit cf26326

Please sign in to comment.