Skip to content

Commit cb7b7f2

Browse files
committed
Always include common files in package
When the '$(IncludeContentInPack)' property is false, files specified via '@(None)', '@(Content)' items are excluded from the NuGet package. Adding '@(PackageFile)' items directly to '%(_PackageFiles)' item via the following target ensures that they will always be included in the package. So, Use '%(PackageFile)' item to always include files in the package. By default, they are included in the root of the package but can be overridden via '%(PackageFile.TargetPath)' metadata.
1 parent 157d01a commit cb7b7f2

File tree

3 files changed

+58
-11
lines changed

3 files changed

+58
-11
lines changed

CommunityToolkit.Mvvm/CommunityToolkit.Mvvm.csproj

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,9 @@
3030
Include a custom targets file to check the included source generator.
3131
Including it in .NET 6+ targets is not needed as it guarantees Roslyn 4.x.
3232
-->
33-
<ItemGroup>
34-
<None Include="CommunityToolkit.Mvvm.targets" Pack="True" PackagePath="build\netstandard2.0" />
35-
<None Include="CommunityToolkit.Mvvm.targets" Pack="True" PackagePath="build\netstandard2.1" />
36-
<None Include="CommunityToolkit.Mvvm.targets" Pack="True" PackagePath="buildTransitive\netstandard2.0" />
37-
<None Include="CommunityToolkit.Mvvm.targets" Pack="True" PackagePath="buildTransitive\netstandard2.1" />
33+
<ItemGroup Condition="'$(TargetFramework)' != '' AND '$(TargetFramework)' != 'net6.0'">
34+
<PackageFile Include="CommunityToolkit.Mvvm.targets" IsSpecific="True" TargetPath="build\$(TargetFramework)" />
35+
<PackageFile Include="CommunityToolkit.Mvvm.targets" IsSpecific="True" TargetPath="buildTransitive\$(TargetFramework)" />
3836
</ItemGroup>
3937

4038
<!-- .NET Standard 2.0 doesn't have the Span<T> and IAsyncEnumerable<T> types -->
@@ -66,9 +64,8 @@
6664
output path and building it first is the only way to ensure they are included.
6765
-->
6866
<ItemGroup>
69-
<None Include="..\CommunityToolkit.Mvvm.SourceGenerators\bin\$(Configuration)\netstandard2.0\CommunityToolkit.Mvvm.SourceGenerators.dll"
70-
PackagePath="analyzers\dotnet\roslyn4.0\cs"
71-
Pack="true" Visible="false" />
67+
<PackageFile Include="..\CommunityToolkit.Mvvm.SourceGenerators\bin\$(Configuration)\netstandard2.0\CommunityToolkit.Mvvm.SourceGenerators.dll"
68+
TargetPath="analyzers\dotnet\roslyn4.0\cs" />
7269
</ItemGroup>
7370

7471
</Project>

build/Community.Toolkit.Common.props

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,11 @@
2424
<ContinuousIntegrationBuild>$(TF_BUILD)</ContinuousIntegrationBuild>
2525
</PropertyGroup>
2626

27+
<ItemDefinitionGroup>
28+
<PackageFile>
29+
<Visible>False</Visible>
30+
<TargetPath>\</TargetPath>
31+
</PackageFile>
32+
</ItemDefinitionGroup>
33+
2734
</Project>

build/Community.Toolkit.Common.targets

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,52 @@
2020
</PropertyGroup>
2121

2222
<ItemGroup Condition="$(IsPackable)">
23-
<None Pack="true" PackagePath="\" Visible="False" Include="$(BuildToolsDirectory)Icon.png" />
24-
<None Pack="true" PackagePath="\" Visible="False" Include="$(RepositoryDirectory)License.md" />
25-
<None Pack="true" PackagePath="\" Visible="False" Include="$(RepositoryDirectory)ThirdPartyNotices.txt" />
23+
<PackageFile Include="$(BuildToolsDirectory)Icon.png" />
24+
<PackageFile Include="$(RepositoryDirectory)License.md" />
25+
<PackageFile Include="$(RepositoryDirectory)ThirdPartyNotices.txt" />
2626
</ItemGroup>
2727

28+
<!--
29+
Use 'GenerateNuSpecDependsOn' extensibility point to include custom global assets in the package.
30+
Use 'TargetsForTfmSpecificContentInPackage' extensibility point to include custom TFM-specific assets in the package.
31+
-->
32+
<PropertyGroup>
33+
<GenerateNuSpecDependsOn>_AddGlobalPackageFilesToNuGetPack</GenerateNuSpecDependsOn>
34+
<TargetsForTfmSpecificContentInPackage>_AddPackageFilesPerTargetFrameworkToNuGetPack</TargetsForTfmSpecificContentInPackage>
35+
</PropertyGroup>
36+
37+
<!--
38+
When the '$(IncludeContentInPack)' property is false, files specified via '@(None)', '@(Content)' items
39+
are excluded from the NuGet package. Adding '@(PackageFile)' items directly to '%(_PackageFiles)' item
40+
via the following target ensures that they will always be included in the package.
41+
42+
So, Use '%(PackageFile)' item to always include files in the package. By default, they are included in
43+
the root of the package but can be overridden via '%(PackageFile.TargetPath)' metadata. Since, 'TargetPath'
44+
is just an alias of the 'PackagePath' metadata, multiple paths and different file names can also be specified.
45+
-->
46+
<Target Name="_AddGlobalPackageFilesToNuGetPack"
47+
AfterTargets="_CalculateInputsOutputsForPack">
48+
<ItemGroup>
49+
<_PackageFiles Include="@(PackageFile)" Exclude="@(PackageFile->WithMetadataValue('IsSpecific', 'true'))" Condition="'%(PackageFile.Pack)' != 'false'">
50+
<PackagePath Condition="'%(PackageFile.TargetPath)' != ''">%(PackageFile.TargetPath)</PackagePath>
51+
</_PackageFiles>
52+
</ItemGroup>
53+
</Target>
54+
55+
<!--
56+
Same 'PackageFile' as above but processed per target framework when 'IsSpecific: True' metadata is specified.
57+
Since it is target specific, it also validates whether the package path contains the current framework alias.
58+
-->
59+
<Target Name="_AddPackageFilesPerTargetFrameworkToNuGetPack">
60+
<ItemGroup>
61+
<TFMSpecificPackageFile Include="@(PackageFile->WithMetadataValue('IsSpecific', 'true'))" Condition="'%(PackageFile.Pack)' != 'false'">
62+
<PackagePath Condition="'%(PackageFile.TargetPath)' != ''">%(PackageFile.TargetPath)</PackagePath>
63+
</TFMSpecificPackageFile>
64+
</ItemGroup>
65+
66+
<!-- Error out when the target path of the package file doesn't contain target framework to differentiate itself within the package -->
67+
<Error Code="NCTDEV02" Condition="!$([System.String]::new('%(PackagePath)').Contains('$(TargetFramework)')) AND '@(TFMSpecificPackageFile)' != ''"
68+
Text="The package file ('%(Identity)') is 'TargetFramework' specific and should include the value ('$(TargetFramework)') somewhere in the target path ('%(PackagePath)')." />
69+
</Target>
70+
2871
</Project>

0 commit comments

Comments
 (0)