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

Commit 5e77c83

Browse files
kzuadalon
authored andcommitted
Split GetPackageContents from the inference behavior for additional flexibility
This allows targets that run BeforeTargets=GetPackageContents to also tweak the @(PackageFile) with the inferred items before they are assigned a target path and passed down to Pack. This makes the mechanism even more flexible. Accompanying documentation update on the wiki done too.
1 parent da96bb0 commit 5e77c83

7 files changed

Lines changed: 60 additions & 38 deletions

File tree

src/Build/NuGet.Build.Packaging.Tasks/NuGet.Build.Packaging.targets

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -197,24 +197,34 @@ Copyright (c) .NET Foundation. All rights reserved.
197197
-->
198198
<PropertyGroup>
199199
<!-- Only depend on ResolveReferences if we need to include framework references -->
200-
<GetPackageContentsDependsOn Condition="'$(IncludeFrameworkReferencesInPackage)' == 'true'">
200+
<InferPackageContentsDependsOn Condition="'$(IncludeFrameworkReferencesInPackage)' == 'true'">
201201
ResolveReferences
202-
</GetPackageContentsDependsOn>
203-
<GetPackageContentsDependsOn>
204-
$(GetPackageContentsDependsOn);
202+
</InferPackageContentsDependsOn>
203+
<InferPackageContentsDependsOn>
204+
$(InferPackageContentsDependsOn);
205205
GetPackageTargetPath;
206-
ReadLegacyDependencies;
207-
</GetPackageContentsDependsOn>
208-
<GetPackageContentsDependsOn Condition="'$(IncludeOutputsInPackage)' == 'true'">
209-
$(GetPackageContentsDependsOn)
206+
ReadLegacyDependencies
207+
</InferPackageContentsDependsOn>
208+
<InferPackageContentsDependsOn Condition="'$(IncludeOutputsInPackage)' == 'true'">
209+
$(InferPackageContentsDependsOn);
210210
AssignProjectConfiguration;
211211
_SplitProjectReferencesByFileExistence;
212212
_SplitProjectReferencesByIsNuGetized;
213213
AllProjectOutputGroups;
214-
_GetReferenceAssemblyTargetPaths;
215-
</GetPackageContentsDependsOn>
214+
_GetReferenceAssemblyTargetPaths
215+
</InferPackageContentsDependsOn>
216+
<GetPackageContentsDependsOn>InferPackageContents</GetPackageContentsDependsOn>
216217
</PropertyGroup>
218+
217219
<Target Name="GetPackageContents" DependsOnTargets="$(GetPackageContentsDependsOn)" Returns="@(_PackageContent)">
220+
<!-- We batch depending on the IsPackaging metadata, which is only specified for referenced content
221+
if the current project is building a package, to force retargeting of the referenced content. -->
222+
<AssignPackagePath Files="@(PackageFile)" Kinds="@(PackageItemKind)" IsPackaging="%(PackageFile.IsPackaging)">
223+
<Output TaskParameter="AssignedFiles" ItemName="_PackageContent" />
224+
</AssignPackagePath>
225+
</Target>
226+
227+
<Target Name="InferPackageContents" DependsOnTargets="$(InferPackageContentsDependsOn)" Returns="@(PackageFile)">
218228
<Error Condition="'@(_NonNuGetizedProjectReference)' != ''"
219229
Code="NG0011"
220230
Text="Some project references cannot be properly packaged. Please install the NuGet.Build.Packaging package on the following projects: @(_NonNuGetizedProjectReference)." />
@@ -295,10 +305,6 @@ Copyright (c) .NET Foundation. All rights reserved.
295305
</PackageFile>
296306
</ItemGroup>
297307

298-
<AssignPackagePath Files="@(PackageFile)" Kinds="@(PackageItemKind)">
299-
<Output TaskParameter="AssignedFiles" ItemName="_PackageContent" />
300-
</AssignPackagePath>
301-
302308
<MSBuild Projects="@(_NuGetizedProjectReference)"
303309
Targets="GetPackageContents"
304310
BuildInParallel="$(BuildInParallel)"
@@ -361,16 +367,15 @@ Copyright (c) .NET Foundation. All rights reserved.
361367
</_ReferencedPackageContentWithOriginalValues>
362368
</ItemGroup>
363369

364-
<!-- Ensure referenced package content gets assigned a package path if it didn't provide one already.
365-
This happens for project references' that don't have a PackageId, since their package path will
366-
depend on the referencing project's TFM.
367-
-->
368-
<AssignPackagePath Files="@(_ReferencedPackageContentWithOriginalValues);@(_ReferencedPackageDependency)"
369-
Kinds="@(PackageItemKind)"
370-
IsPackaging="$(BuildingPackage)"
371-
Condition="'@(_ReferencedPackageContentWithOriginalValues)' != '' Or '@(_ReferencedPackageDependency)' != ''">
372-
<Output TaskParameter="AssignedFiles" ItemName="_PackageContent" />
373-
</AssignPackagePath>
370+
<ItemGroup>
371+
<!-- Ensure referenced package content gets assigned a package path if it didn't provide one already.
372+
This happens for project references' that don't have a PackageId, since their package path will
373+
depend on the referencing project's TFM.
374+
-->
375+
<PackageFile Include="@(_ReferencedPackageContentWithOriginalValues);@(_ReferencedPackageDependency)">
376+
<IsPackaging>$(BuildingPackage)</IsPackaging>
377+
</PackageFile>
378+
</ItemGroup>
374379
</Target>
375380

376381
<!-- This target separates project references that have the packaging targets from those that don't -->
@@ -573,4 +578,4 @@ Copyright (c) .NET Foundation. All rights reserved.
573578
</Target>
574579

575580
<Import Project="NuGet.Build.Packaging.Authoring.targets" Condition="'$(IsPackagingProject)' == 'true'" />
576-
</Project>
581+
</Project>

src/Build/NuGet.Build.Packaging.Tests/Scenarios/Scenario.props

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,6 @@
22
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
33
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
44

5-
<PropertyGroup>
6-
<!-- This will be the right path when we run from xunit in the bin/$(Configuration) folder -->
7-
<NuGetTargetsPath Condition="'$(NuGetTargetsPath)' == ''">$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), NuGet.Build.Packaging.props))</NuGetTargetsPath>
8-
<!-- This will be the right path when we run from MSBuild from the source location inside the tests project -->
9-
<NuGetTargetsPath Condition="!Exists('$(NuGetTargetsPath)\NuGet.Build.Packaging.props')">$(MSBuildThisFileDirectory)..\..\NuGet.Build.Packaging.Tasks\bin\Debug</NuGetTargetsPath>
10-
</PropertyGroup>
11-
125
<PropertyGroup>
136
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
147
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
@@ -47,6 +40,11 @@
4740
<!-- Required package metadata -->
4841
<Authors>NuGet</Authors>
4942
<Description>Package for '$(MSBuildProjectName)' project.</Description>
43+
44+
<!-- This will be the right path when we run from xunit in the bin/$(Configuration) folder -->
45+
<NuGetTargetsPath Condition="'$(NuGetTargetsPath)' == ''">$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), NuGet.Build.Packaging.props))</NuGetTargetsPath>
46+
<!-- This will be the right path when we run from MSBuild from the source location inside the tests project -->
47+
<NuGetTargetsPath Condition="!Exists('$(NuGetTargetsPath)\NuGet.Build.Packaging.props')">$(MSBuildThisFileDirectory)..\..\NuGet.Build.Packaging.Tasks\bin\$(Configuration)</NuGetTargetsPath>
5048
</PropertyGroup>
5149

5250
<Import Project="$(NuGetTargetsPath)\NuGet.Build.Packaging.props" Condition="Exists('$(NuGetTargetsPath)\NuGet.Build.Packaging.props')" />

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,9 @@
2929
</ItemGroup>
3030
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), fakeframework.targets))\fakeframework.targets" />
3131
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), Scenario.targets))\Scenario.targets" />
32+
<Target Name="RemoveContent" Condition="'$(RemoveContent)' == 'true'" BeforeTargets="GetPackageContents">
33+
<ItemGroup>
34+
<PackageFile Remove="@(PackageFile)" Condition="'%(Kind)' == 'Content' Or '%(Kind)' == 'None'" />
35+
</ItemGroup>
36+
</Target>
3237
</Project>

src/Build/NuGet.Build.Packaging.Tests/Scenarios/given_library_with_config_dependencies/a.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,9 @@
8282
<!-- From the gazillion added packages to packages.config, we only need to keep the top level Microsoft.CodeAnalysis -->
8383
<Target Name="RemoveUnnecessaryDependencies" BeforeTargets="GetPackageContents" Condition="'@(PackageReference)' != ''">
8484
<ItemGroup>
85-
<_CodeAnalysisDependency Include="@(PackageReference)"
86-
Condition="$([System.String]::new(%(Identity)).StartsWith('Microsoft.CodeAnalysis.'))" />
87-
<PackageReference Remove="@(_CodeAnalysisDependency)" />
85+
<_CodeAnalysisDependency Include="@(PackageFile)"
86+
Condition="'%(Kind)' == 'Dependency' And $([System.String]::new(%(Identity)).StartsWith('Microsoft.CodeAnalysis.'))" />
87+
<PackageFile Remove="@(_CodeAnalysisDependency)" />
8888
</ItemGroup>
8989
</Target>
9090
</Project>

src/Build/NuGet.Build.Packaging.Tests/Scenarios/given_library_with_json_dependencies/a.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), Scenario.targets))\Scenario.targets" />
1212
<Target Name="RemoveIFluentInterface" BeforeTargets="GetPackageContents">
1313
<ItemGroup>
14-
<PackageReference Remove="IFluentInterface" />
14+
<PackageFile Remove="IFluentInterface" />
1515
</ItemGroup>
1616
</Target>
1717
</Project>

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public void when_none_item_has_include_in_package_true_then_it_is_included()
9292
}
9393

9494
[Fact]
95-
public void when_none_item_has_include_in_package_false_then_it_is_included()
95+
public void when_none_item_has_include_in_package_false_then_it_is_not_included()
9696
{
9797
var result = Builder.BuildScenario(nameof(given_a_library_with_content), new
9898
{
@@ -138,5 +138,19 @@ public void when_content_item_has_include_in_package_true_then_it_is_included()
138138
TargetPath = "content-with-include-true.txt",
139139
}));
140140
}
141+
142+
[Fact]
143+
public void when_removing_inferred_package_file_from_content_then_content_is_not_included()
144+
{
145+
var result = Builder.BuildScenario(nameof(given_a_library_with_content), new
146+
{
147+
PackageId = "ContentPackage",
148+
RemoveContent = "true",
149+
});
150+
151+
result.AssertSuccess(output);
152+
153+
Assert.DoesNotContain(result.Items, item => item.GetMetadata("PackagePath").StartsWith("contentFiles"));
154+
}
141155
}
142156
}

src/VisualStudio/NuGet.Packaging.VisualStudio.Shared.targets

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), NuGet.Build.Packaging.Shared.targets))\NuGet.Build.Packaging.Shared.targets" />
99

1010
<PropertyGroup>
11-
<DeployExtension Condition="'$(Configuration)' != 'Debug' Or '$(VisualStudioVersion)' != '$(TargetVSVersion)'" >false</DeployExtension>
11+
<DeployExtension Condition="'$(Configuration)' != 'Debug'">false</DeployExtension>
1212
<SkipValidatePackageReferences>true</SkipValidatePackageReferences>
1313

1414
<StartAction>Program</StartAction>

0 commit comments

Comments
 (0)