Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Custom CopyToOutputDirectory Location With TargetPath #6237

Merged
merged 12 commits into from
Apr 5, 2021
Merged
9 changes: 9 additions & 0 deletions src/Shared/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,16 @@ internal static class ItemMetadataNames
internal const string subType = "SubType";
internal const string executableExtension = "ExecutableExtension";
internal const string embedInteropTypes = "EmbedInteropTypes";

/// <summary>
/// The output path for a given item.
/// </summary>
internal const string targetPath = "TargetPath";

/// <summary>
/// The user-specified override for TargetPath. See the AssignTargetPath task.
/// </summary>
internal const string targetPathOverride = "TargetPathOverride";
internal const string dependentUpon = "DependentUpon";
internal const string msbuildSourceProjectFile = "MSBuildSourceProjectFile";
internal const string msbuildSourceTargetName = "MSBuildSourceTargetName";
Expand Down
65 changes: 38 additions & 27 deletions src/Tasks.UnitTests/AssignTargetPath_Tests.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Collections.Generic;
using Microsoft.Build.Framework;
using Microsoft.Build.Shared;
using Microsoft.Build.Tasks;
using Microsoft.Build.Utilities;
using Shouldly;
using Xunit;

namespace Microsoft.Build.UnitTests
Expand All @@ -20,15 +22,10 @@ public void Regress314791()
{ new TaskItem(NativeMethodsShared.IsWindows ? @"c:\bin2\abc.efg" : "/bin2/abc.efg") };
t.RootFolder = NativeMethodsShared.IsWindows ? @"c:\bin" : "/bin";

bool success = t.Execute();

Assert.True(success);

Assert.Single(t.AssignedFiles);
Assert.Equal(
NativeMethodsShared.IsWindows ? @"c:\bin2\abc.efg" : "/bin2/abc.efg",
t.AssignedFiles[0].ItemSpec);
Assert.Equal(@"abc.efg", t.AssignedFiles[0].GetMetadata("TargetPath"));
t.Execute().ShouldBeTrue();
t.AssignedFiles.Length.ShouldBe(1);
t.AssignedFiles[0].ItemSpec.ShouldBe(NativeMethodsShared.IsWindows ? @"c:\bin2\abc.efg" : "/bin2/abc.efg");
t.AssignedFiles[0].GetMetadata("TargetPath").ShouldBe("abc.efg");
}

[Fact]
Expand All @@ -40,12 +37,9 @@ public void AtConeRoot()
{ new TaskItem(NativeMethodsShared.IsWindows ? @"c:\f1\f2\file.txt" : "/f1/f2/file.txt") };
t.RootFolder = NativeMethodsShared.IsWindows ? @"c:\f1\f2" : "/f1/f2";

bool success = t.Execute();

Assert.True(success);

Assert.Single(t.AssignedFiles);
Assert.Equal(@"file.txt", t.AssignedFiles[0].GetMetadata("TargetPath"));
t.Execute().ShouldBeTrue();
t.AssignedFiles.Length.ShouldBe(1);
t.AssignedFiles[0].GetMetadata("TargetPath").ShouldBe("file.txt");
}

[Fact]
Expand All @@ -64,12 +58,9 @@ public void OutOfCone()
// /f1 to /x1
t.RootFolder = NativeMethodsShared.IsWindows ? @"c:\f1" : "/x1";

bool success = t.Execute();

Assert.True(success);

Assert.Single(t.AssignedFiles);
Assert.Equal("file.txt", t.AssignedFiles[0].GetMetadata("TargetPath"));
t.Execute().ShouldBeTrue();
t.AssignedFiles.Length.ShouldBe(1);
t.AssignedFiles[0].GetMetadata("TargetPath").ShouldBe("file.txt");
}

[Fact]
Expand All @@ -84,14 +75,34 @@ public void InConeButAbsolute()
};
t.RootFolder = NativeMethodsShared.IsWindows ? @"c:\f1\f2" : "/f1/f2";

bool success = t.Execute();
t.Execute().ShouldBeTrue();
t.AssignedFiles.Length.ShouldBe(1);
t.AssignedFiles[0].GetMetadata("TargetPath").ShouldBe(NativeMethodsShared.IsWindows ? @"f3\f4\file.txt" : "f3/f4/file.txt");
}

Assert.True(success);
[Theory]
[InlineData("c:/fully/qualified/path.txt")]
[InlineData("test/output/file.txt")]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also add a fully qualified path to sanity ensure the task preserves those.

[InlineData(@"some\dir\to\file.txt")]
[InlineData("file.txt")]
[InlineData("file")]
public void TargetPathOverrideSet(string targetPath)
{
AssignTargetPath t = new AssignTargetPath();
t.BuildEngine = new MockEngine();
Dictionary<string, string> metaData = new Dictionary<string, string>();
metaData.Add("TargetPathOverride", targetPath);
t.Files = new ITaskItem[]
{
new TaskItem(
itemSpec: NativeMethodsShared.IsWindows ? @"c:\f1\f2\file.txt" : "/f1/f2/file.txt",
itemMetadata: metaData)
};
t.RootFolder = NativeMethodsShared.IsWindows ? @"c:\f1\f2" : "/f1/f2";

Assert.Single(t.AssignedFiles);
Assert.Equal(
NativeMethodsShared.IsWindows ? @"f3\f4\file.txt" : "f3/f4/file.txt",
t.AssignedFiles[0].GetMetadata("TargetPath"));
t.Execute().ShouldBeTrue();
t.AssignedFiles.Length.ShouldBe(1);
targetPath.ShouldBe(t.AssignedFiles[0].GetMetadata("TargetPath"));
benvillalobos marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Expand Down
14 changes: 10 additions & 4 deletions src/Tasks/AssignTargetPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,19 @@ public override bool Execute()

for (int i = 0; i < Files.Length; ++i)
{
string link = Files[i].GetMetadata(ItemMetadataNames.link);
AssignedFiles[i] = new TaskItem(Files[i]);

// If file has a link, use that.
string targetPath = link;
// TargetPathOverride takes priority.
// https://github.com/dotnet/msbuild/issues/2795
string targetPath = Files[i].GetMetadata(ItemMetadataNames.targetPathOverride);

if (string.IsNullOrEmpty(link))
// If TargetPathOverride not set, fall back to default behavior.
if (string.IsNullOrEmpty(targetPath))
{
targetPath = Files[i].GetMetadata(ItemMetadataNames.link);
}

if (string.IsNullOrEmpty(targetPath))
{
if (// if the file path is relative
!Path.IsPathRooted(Files[i].ItemSpec) &&
Expand Down