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
4 changes: 4 additions & 0 deletions src/Shared/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@ 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";
internal const string dependentUpon = "DependentUpon";
internal const string msbuildSourceProjectFile = "MSBuildSourceProjectFile";
Expand Down
64 changes: 37 additions & 27 deletions src/Tasks.UnitTests/AssignTargetPath_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using Microsoft.Build.Shared;
using Microsoft.Build.Tasks;
using Microsoft.Build.Utilities;
using Shouldly;
using System.Collections.Generic;
benvillalobos marked this conversation as resolved.
Show resolved Hide resolved
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,33 @@ 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("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 TargetPathAlreadySet(string targetPath)
{
AssignTargetPath t = new AssignTargetPath();
t.BuildEngine = new MockEngine();
Dictionary<string, string> metaData = new Dictionary<string, string>();
metaData.Add("TargetPath", 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
15 changes: 11 additions & 4 deletions src/Tasks/AssignTargetPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,20 @@ 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]);
string targetPath = Files[i].GetMetadata(ItemMetadataNames.targetPath);

// If file has a link, use that.
string targetPath = link;
// If TargetPath is already set, copy it over.
// https://github.com/dotnet/msbuild/issues/2795
if (!string.IsNullOrEmpty(targetPath))
{
AssignedFiles[i].SetMetadata(ItemMetadataNames.targetPath, EscapingUtilities.Escape(targetPath));
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: Instead of the continue and duplication you could let it fall through. First try TargetPath. If null or empty try Link. If null or empty run that if block. Then set the escaped value and return.

Copy link
Member

Choose a reason for hiding this comment

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

Consider using some new variable ("targetPathOverride"? "targetPath2"? Choose a good name) to allow users to provide a custom targetPath while maintaining that this task can be called twice without adverse consequences. (Escaping seems necessary as part of what TaskItem requires for all its metadata.)

Copy link
Member Author

Choose a reason for hiding this comment

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

Going with TargetPathOverride for clarity.

continue;
}

targetPath = Files[i].GetMetadata(ItemMetadataNames.link);

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