Skip to content

Commit a1fcd7e

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 19b8298 commit a1fcd7e

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
@@ -37,4 +37,11 @@
3737
<ContinuousIntegrationBuild>$(TF_BUILD)</ContinuousIntegrationBuild>
3838
</PropertyGroup>
3939

40+
<ItemDefinitionGroup>
41+
<PackageFile>
42+
<Visible>False</Visible>
43+
<TargetPath>\</TargetPath>
44+
</PackageFile>
45+
</ItemDefinitionGroup>
46+
4047
</Project>

build/Community.Toolkit.Common.targets

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,52 @@
1212
</PropertyGroup>
1313

1414
<ItemGroup Condition="$(IsPackable)">
15-
<None Pack="true" PackagePath="\" Visible="False" Include="$(BuildToolsDirectory)Icon.png" />
16-
<None Pack="true" PackagePath="\" Visible="False" Include="$(RepositoryDirectory)License.md" />
17-
<None Pack="true" PackagePath="\" Visible="False" Include="$(RepositoryDirectory)ThirdPartyNotices.txt" />
15+
<PackageFile Include="$(BuildToolsDirectory)Icon.png" />
16+
<PackageFile Include="$(RepositoryDirectory)License.md" />
17+
<PackageFile Include="$(RepositoryDirectory)ThirdPartyNotices.txt" />
1818
</ItemGroup>
1919

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

0 commit comments

Comments
 (0)