Skip to content

Commit 8c0869d

Browse files
committed
If an item provides TargetPath, preserve it as relative to the PackFolder
Add doc note on TargetPath and how it's used (also with Link), and make sure we don't overwrite the provided one by the user if it's present.
1 parent e9430c0 commit 8c0869d

File tree

5 files changed

+25
-1
lines changed

5 files changed

+25
-1
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ If the item does **not** provide a *PackagePath*, and *Pack* is not *false*, the
7979

8080
a. **PackFolder**: typically one of the [built-in package folders](https://github.com/NuGet/NuGet.Client/blob/dev/src/NuGet.Core/NuGet.Packaging/PackagingConstants.cs#L19), such as *build*, *lib*, etc.
8181
b. **FrameworkSpecific**: *true*/*false*, determines whether the project's target framework is used when building the final *PackagePath*.
82+
c. **TargetPath**: optional PackFolder-relative path for the item. If not provided, the relative path of the item in the project (or its *Link* metadata) is used.
83+
8284

8385
When an item specifies *FrameworkSpecific=true*, the project's target framework is added to the final package path, such as `lib\netstandard2.0\My.dll`. Since the package folder itself typically determines whether it contains framework-specific files or not, the *FrameworkSpecific* value has sensible defaults so you don't have to specify it unless you wnat to override it. The [default values from NuGetizer.props](src/NuGetizer.Tasks/NuGetizer.props) are:
8486

src/NuGetizer.Tasks/NuGetizer.Inference.targets

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ Copyright (c) .NET Foundation. All rights reserved.
114114
<PackagePath />
115115
<PackFolder />
116116
<ShouldPack />
117+
<TargetPath />
117118
</InferenceCandidate>
118119
</ItemDefinitionGroup>
119120

@@ -173,10 +174,13 @@ Copyright (c) .NET Foundation. All rights reserved.
173174
<InferenceCandidate>
174175
<ShouldPack Condition="('%(Pack)' == 'true' or '%(PackagePath)' != '' or '%(PackFolder)' != '') and '%(Pack)' != 'false'">true</ShouldPack>
175176
</InferenceCandidate>
177+
<!-- No need to re-assign a target path if the item already provides one. We do this because otherwise the built-in AssignTargetPath
178+
task unconditionally re-sets it. See https://github.com/dotnet/msbuild/blob/master/src/Tasks/AssignTargetPath.cs -->
179+
<_InferenceCandidateWithTargetPath Include="@(InferenceCandidate)" Condition="'%(ShouldPack)' == 'true' and '%(TargetPath)' != ''" />
176180
</ItemGroup>
177181

178182
<AssignTargetPath Files="@(InferenceCandidate)" RootFolder="$(MSBuildProjectDirectory)"
179-
Condition="'%(ShouldPack)' == 'true'">
183+
Condition="'%(ShouldPack)' == 'true' and '%(TargetPath)' == ''">
180184
<Output TaskParameter="AssignedFiles" ItemName="_InferenceCandidateWithTargetPath" />
181185
</AssignTargetPath>
182186

src/NuGetizer.Tests/Scenarios/given_a_library_with_content/content-with-targetpath.txt

Whitespace-only changes.

src/NuGetizer.Tests/Scenarios/given_a_library_with_content/library_with_content.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
<Content Include="relative\content-copy.txt" CopyToOutputDirectory="PreserveNewest" />
4747
<None Include="none-with-packagepath.txt" PackagePath="build\%(Filename)%(Extension)" />
4848
<Content Include="content-with-packagepath.txt" PackagePath="build\%(Filename)%(Extension)" />
49+
<Content Include="content-with-targetpath.txt" Pack="true" TargetPath="relative\docs\%(Filename)%(Extension)" />
4950
<None Include="non-existent-file.txt" Pack="true" />
5051
</ItemGroup>
5152
<Target Name="RemoveContent" Condition="'$(RemoveContent)' == 'true'" BeforeTargets="GetPackageContents">

src/NuGetizer.Tests/given_a_library_with_content.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,23 @@ public void content_with_package_path_is_included_even_with_pack_content_false()
366366
}));
367367
}
368368

369+
[Fact]
370+
public void content_with_target_path_is_included_relative_to_pack_folder()
371+
{
372+
var result = Builder.BuildScenario(nameof(given_a_library_with_content), new
373+
{
374+
PackageId = "ContentPackage",
375+
PackContent = "false"
376+
});
377+
378+
result.AssertSuccess(output);
379+
380+
Assert.Contains(result.Items, item => item.Matches(new
381+
{
382+
PackagePath = @"contentFiles\any\monoandroid51\relative\docs\content-with-targetpath.txt",
383+
}));
384+
}
385+
369386
#endregion
370387

371388
#region None scenarios

0 commit comments

Comments
 (0)