Skip to content
This repository was archived by the owner on Jan 12, 2024. It is now read-only.

Commit dca29e4

Browse files
kzuadalon
authored andcommitted
Add support for Linked files as PackageFile
In the IDE it's trivial to add linked files so they end up in specified locations. This makes it easy to add linked files by automatically using the %(Link) metadata if present on items that don't already have a %(TargetPath) assigned. With the addition of PackageFile as an available item name/build action, supporting linked package files makes this very easy to use. Fixes NuGet/Home#5327
1 parent 187ce0a commit dca29e4

4 files changed

Lines changed: 83 additions & 0 deletions

File tree

src/Build/NuGet.Build.Packaging.Tasks/AssignPackagePath.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,10 @@ ITaskItem EnsurePackagePath(ITaskItem file, IDictionary<string, ITaskItem> kindM
147147
}
148148

149149
var targetPath = file.GetMetadata("TargetPath");
150+
// Linked files already have the desired target path specified by the user
151+
if (string.IsNullOrEmpty(targetPath))
152+
targetPath = file.GetMetadata("Link");
153+
150154
// NOTE: TargetPath allows a framework-specific file to still specify its relative
151155
// location without hardcoding the target framework (useful for multi-targetting and
152156
// P2P references).
@@ -166,6 +170,9 @@ ITaskItem EnsurePackagePath(ITaskItem file, IDictionary<string, ITaskItem> kindM
166170
targetPath = targetPath.Substring(packageFolder.Length + 1);
167171
}
168172

173+
// At this point we have the correct calculated target path, so persist it for inspection if necessary.
174+
output.SetMetadata("TargetPath", targetPath);
175+
169176
// If we have no known package folder, files go to their RelativeDir location.
170177
// This allows custom packaging paths such as "workbooks", "docs" or whatever, which aren't prohibited by
171178
// the format.

src/Build/NuGet.Build.Packaging.Tests/AssignPackagePathTests.cs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,63 @@ public void when_file_has_target_framework_and_tfm_then_existing_value_is_preser
279279
}));
280280
}
281281

282+
[Fact]
283+
public void when_content_file_has_link_then_package_path_is_relative_to_link()
284+
{
285+
var task = new AssignPackagePath
286+
{
287+
BuildEngine = engine,
288+
Kinds = kinds,
289+
Files = new ITaskItem[]
290+
{
291+
new TaskItem(@"..\..\readme.txt", new Metadata
292+
{
293+
{ "Link", @"docs\readme.txt" },
294+
{ "PackageId", "A" },
295+
{ "Kind", "Content" },
296+
{ "TargetFramework", "any" },
297+
})
298+
}
299+
};
300+
301+
Assert.True(task.Execute());
302+
303+
Assert.Contains(task.AssignedFiles, item => item.Matches(new
304+
{
305+
TargetPath = @"docs\readme.txt",
306+
PackagePath = @"contentFiles\any\any\docs\readme.txt",
307+
}));
308+
}
309+
310+
[Fact]
311+
public void when_none_file_has_link_then_package_path_is_relative_to_link()
312+
{
313+
var task = new AssignPackagePath
314+
{
315+
BuildEngine = engine,
316+
Kinds = kinds,
317+
Files = new ITaskItem[]
318+
{
319+
new TaskItem(@"..\..\readme.txt", new Metadata
320+
{
321+
{ "Link", @"docs\readme.txt" },
322+
{ "PackageId", "A" },
323+
{ "Kind", "None" },
324+
{ "TargetFramework", "any" },
325+
})
326+
}
327+
};
328+
329+
Assert.True(task.Execute());
330+
331+
Assert.Contains(task.AssignedFiles, item => item.Matches(new
332+
{
333+
TargetPath = @"docs\readme.txt",
334+
PackagePath = @"docs\readme.txt",
335+
}));
336+
}
337+
338+
282339
[Fact]
283340
public void when_content_is_not_framework_specific_then_has_any_lang_and_tfm()
284341
{

src/Build/NuGet.Build.Packaging.Tests/Scenarios/given_a_library_with_content/library_with_content.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
<TargetFrameworkMoniker>MonoAndroid,Version=v5.1</TargetFrameworkMoniker>
77
</PropertyGroup>
88
<ItemGroup>
9+
<PackageFile Include="..\given_a_custom_build_project\Readme.txt">
10+
<Link>docs\Readme.txt</Link>
11+
</PackageFile>
912
<Content Include="quickstart\cs-any.txt">
1013
<CodeLanguage>cs</CodeLanguage>
1114
<TargetFramework>any</TargetFramework>

src/Build/NuGet.Build.Packaging.Tests/given_a_library_with_content.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,22 @@ public void when_removing_inferred_package_file_from_content_then_content_is_not
7474
Assert.DoesNotContain(result.Items, item => item.GetMetadata("PackagePath").StartsWith("contentFiles"));
7575
}
7676

77+
[Fact]
78+
public void linked_package_file_has_relative_package_path()
79+
{
80+
var result = Builder.BuildScenario(nameof(given_a_library_with_content), new
81+
{
82+
PackageId = "ContentPackage"
83+
});
84+
85+
result.AssertSuccess(output);
86+
87+
Assert.Contains(result.Items, item => item.Matches(new
88+
{
89+
PackagePath = @"docs\Readme.txt",
90+
}));
91+
}
92+
7793
#region Content scenarios
7894

7995
[Fact]

0 commit comments

Comments
 (0)