Skip to content

Commit

Permalink
Microsoft.Build.Traversal: propogate well known global properties (#208)
Browse files Browse the repository at this point in the history
Fixes #207
  • Loading branch information
forrestcoward authored Oct 7, 2020
1 parent 5912581 commit d1a4c68
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 1 deletion.
67 changes: 67 additions & 0 deletions src/Traversal.UnitTests/TraversalTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,73 @@ public void TraversalGlobalPropertiesPreserveAdditionalProperties(string additio
item.GetMetadataValue("AdditionalProperties").ShouldBe(expected);
}

[Theory]
[InlineData("Platform=x86", "x86", null, null, null, null)]
[InlineData(null, null, "Configuration=Debug", "Debug", null, null)]
[InlineData(null, null, null, null, "TargetFramework=net472", "net472")]
[InlineData("Platform=x64", "x64", "Configuration=Debug", "Debug", null, null)]
public void TraversalPreserveWellKnownProperties(string setPlatformMetadata, string expectedPlatform, string setConfigurationMetadata, string expectedConfiguration, string setTargetFrameworkMetadata, string expectedTargetFramework)
{
// Create a project that prints out its Platform, Configuration, and TargetFramework in the Build target.
string csProj = GetSkeletonCSProjWithMessageTasksPrintingWellKnownMetadata("A").FullPath;

// Create a traversal project that invokes the csproj.
ProjectCreator subTraversalProject = ProjectCreator
.Templates
.TraversalProject(
path: GetTempFile("dirs.proj"),
customAction: creator =>
{
var metadata = new Dictionary<string, string>();
if (setPlatformMetadata != null)
{
metadata["SetPlatform"] = setPlatformMetadata;
}
if (setConfigurationMetadata != null)
{
metadata["SetConfiguration"] = setConfigurationMetadata;
}
if (setTargetFrameworkMetadata != null)
{
metadata["SetTargetFramework"] = setTargetFrameworkMetadata;
}
creator.ItemProjectReference(csProj, metadata: metadata);
})
.Save()
.TryBuild("Build", out bool result, out BuildOutput buildOutput);

ProjectCreator GetSkeletonCSProjWithMessageTasksPrintingWellKnownMetadata(string projectName)
{
return ProjectCreator.Templates.SdkCsproj(path: GetTempFile($"{projectName}.csproj"), sdk: string.Empty)
.Target("Build")
.TaskMessage("Platform: $(Platform)")
.TaskMessage("Configuration: $(Configuration)")
.TaskMessage("TargetFramework: $(TargetFramework)")
.Save();
}

// Verify we received three normal messages and that the csproj received the right properties from the traversal project.
buildOutput.Messages.Normal.Count().ShouldBe(3);

if (setPlatformMetadata != null)
{
buildOutput.Messages.Normal.ShouldContain($"Platform: {expectedPlatform}");
}

if (setConfigurationMetadata != null)
{
buildOutput.Messages.Normal.ShouldContain($"Configuration: {expectedConfiguration}");
}

if (setTargetFrameworkMetadata != null)
{
buildOutput.Messages.Normal.ShouldContain($"TargetFramework: {expectedTargetFramework}");
}
}

[Theory]
[InlineData("Build")]
[InlineData("Clean")]
Expand Down
1 change: 1 addition & 0 deletions src/Traversal/Sdk/Sdk.targets
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@
<MSBuild Projects="@(ProjectReference)"
Condition="'%(ProjectReference.Build)' != 'false'"
BuildInParallel="$([MSBuild]::ValueOrDefault('%(ProjectReference.BuildInParallel)', '$(BuildInParallel)'))"
Properties="%(ProjectReference.SetConfiguration); %(ProjectReference.SetPlatform); %(ProjectReference.SetTargetFramework)"
SkipNonexistentProjects="$(SkipNonexistentProjects)"
SkipNonexistentTargets="$(SkipNonexistentTargets)"
StopOnFirstFailure="$(StopOnFirstFailure)"
Expand Down
2 changes: 1 addition & 1 deletion src/Traversal/version.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"inherit": true,
"version": "2.1"
"version": "2.2"
}

0 comments on commit d1a4c68

Please sign in to comment.