diff --git a/src/Cake.Common.Tests/Unit/Tools/MSBuild/MSBuildRunnerTests.cs b/src/Cake.Common.Tests/Unit/Tools/MSBuild/MSBuildRunnerTests.cs index c8abee5f95..8a290cf70b 100644 --- a/src/Cake.Common.Tests/Unit/Tools/MSBuild/MSBuildRunnerTests.cs +++ b/src/Cake.Common.Tests/Unit/Tools/MSBuild/MSBuildRunnerTests.cs @@ -745,9 +745,55 @@ public void Should_Use_No_Logo_If_Specified() public void Should_Append_Targets_To_Process_Arguments() { // Given - var fixture = new MSBuildRunnerFixture(false, PlatformFamily.Windows); - fixture.Settings.WithTarget("A"); - fixture.Settings.WithTarget("B"); + var fixture = new MSBuildRunnerFixture(false, PlatformFamily.Windows) + { + Settings = new MSBuildSettings + { + Target = "A;B" + } + }; + + // When + var result = fixture.Run(); + + // Then + Assert.Equal("/v:normal /target:A;B " + + "\"C:/Working/src/Solution.sln\"", result.Args); + } + + [Fact] + public void Should_Add_Single_Target_With_Initializer() + { + // Given + var fixture = new MSBuildRunnerFixture(false, PlatformFamily.Windows) + { + Settings = new MSBuildSettings + { + Target = "A", + ToolVersion = MSBuildToolVersion.VS2019, + } + }; + + // When + var result = fixture.Run(); + + // Then + Assert.Equal("/v:normal /target:A " + + "\"C:/Working/src/Solution.sln\"", result.Args); + } + + [Fact] + public void Should_Add_Multiple_Targets_With_Initializer() + { + // Given + var fixture = new MSBuildRunnerFixture(false, PlatformFamily.Windows) + { + Settings = new MSBuildSettings + { + Target = "A;B", + ToolVersion = MSBuildToolVersion.VS2019, + } + }; // When var result = fixture.Run(); @@ -757,6 +803,30 @@ public void Should_Append_Targets_To_Process_Arguments() "\"C:/Working/src/Solution.sln\"", result.Args); } + [Fact] + public void Should_Add_Multiple_Targets_With_Initializer_And_AddTarget() + { + // Given + var fixture = new MSBuildRunnerFixture(false, PlatformFamily.Windows) + { + Settings = new MSBuildSettings + { + Target = "A;B", + ToolVersion = MSBuildToolVersion.VS2019, + } + }; + + fixture.Settings.WithTarget("C"); + fixture.Settings.WithTarget("D"); + + // When + var result = fixture.Run(); + + // Then + Assert.Equal("/v:normal /target:A;B;C;D " + + "\"C:/Working/src/Solution.sln\"", result.Args); + } + [Fact] public void Should_Append_Property_To_Process_Arguments() { diff --git a/src/Cake.Common/Tools/MSBuild/MSBuildSettings.cs b/src/Cake.Common/Tools/MSBuild/MSBuildSettings.cs index d2f4a2980b..4b7aeaf923 100644 --- a/src/Cake.Common/Tools/MSBuild/MSBuildSettings.cs +++ b/src/Cake.Common/Tools/MSBuild/MSBuildSettings.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; +using System.Linq; using Cake.Core.Diagnostics; using Cake.Core.Tooling; @@ -47,6 +48,16 @@ public sealed class MSBuildSettings : ToolSettings /// The MSBuild platform. public MSBuildPlatform MSBuildPlatform { get; set; } + /// + /// Gets or sets the MSBuild target. + /// + /// The MSBuild target. + public string Target + { + get => string.Join(";", Targets); + set => SetTargets(value); + } + /// /// Gets or sets the tool version. /// @@ -269,6 +280,7 @@ public MSBuildSettings() Configuration = string.Empty; Verbosity = Verbosity.Normal; MSBuildPlatform = MSBuildPlatform.Automatic; + Target = string.Empty; } private string GetPropertyValueOrDefault(string propertyName, string @default = null) @@ -281,5 +293,13 @@ private string GetPropertyValueOrDefault(string propertyName, string @default = var propertyValue = string.Join(";", propertyValues); return propertyValue; } + + private void SetTargets(string value) + { + foreach (var target in value.Split(";").Where(p => !string.IsNullOrEmpty(p))) + { + Targets.Add(target); + } + } } } \ No newline at end of file diff --git a/src/Cake.Common/Tools/MSBuild/MSBuildSettingsExtensions.cs b/src/Cake.Common/Tools/MSBuild/MSBuildSettingsExtensions.cs index 9b63667a16..7041b61c60 100644 --- a/src/Cake.Common/Tools/MSBuild/MSBuildSettingsExtensions.cs +++ b/src/Cake.Common/Tools/MSBuild/MSBuildSettingsExtensions.cs @@ -23,7 +23,7 @@ public static class MSBuildSettingsExtensions /// The same instance so that multiple calls can be chained. public static MSBuildSettings WithTarget(this MSBuildSettings settings, string target) { - if (settings == null) + if (settings is null) { throw new ArgumentNullException(nameof(settings)); }