Skip to content

Commit 895b55f

Browse files
committed
Always include common files in the NuGet 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 a7a93ae commit 895b55f

6 files changed

+95
-12
lines changed

eng/Toolkit.Common.props

+3
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,7 @@
3939
<!-- Use custom build logic for projects targeting multiple version of the Roslyn Compiler -->
4040
<Import Project="Toolkit.CompilerTargeting.props" Condition="'$(IsCompilerTargeting)' == 'true'" />
4141

42+
<!-- Extends NuGet packaging with specific features required for packing toolkit projects -->
43+
<Import Project="Toolkit.Packaging.props" Condition="'$(IsPackable)' != 'false'" />
44+
4245
</Project>

eng/Toolkit.Common.targets

+6-3
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020
</PropertyGroup>
2121

2222
<ItemGroup Condition="'$(IsPackable)' == 'true'">
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

2828
<!-- Checked Version properties to use down the line -->
@@ -47,4 +47,7 @@
4747
</PropertyGroup>
4848
</Target>
4949

50+
<!-- Extends NuGet packaging with specific features required for packing toolkit projects -->
51+
<Import Project="Toolkit.Packaging.targets" Condition="'$(IsPackable)' == 'true'" />
52+
5053
</Project>

eng/Toolkit.CompilerTargeting.props

+7
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@
3939
<TargetCompiler Condition="'$(TargetCompiler)' == ''">$(_ShortCompilerIdentifier)$(TargetCompilerVersion)</TargetCompiler>
4040
</PropertyGroup>
4141

42+
<!-- Use a default 'TargetLanguage' based on project extension if not set in project file -->
43+
<PropertyGroup Condition="'$(TargetLanguage)' == ''">
44+
<TargetLanguage Condition="'$(MSBuildProjectExtension)' == '.csproj'">cs</TargetLanguage>
45+
<TargetLanguage Condition="'$(MSBuildProjectExtension)' == '.fsproj'">fs</TargetLanguage>
46+
<TargetLanguage Condition="'$(MSBuildProjectExtension)' == '.vbproj'">vb</TargetLanguage>
47+
</PropertyGroup>
48+
4249
<!-- Since we're are separating the assets, we can use the project name without the "Roslyn40" identifier for other default properties -->
4350
<PropertyGroup>
4451
<ProjectName Condition="'$(ProjectName)' == ''">$(MSBuildProjectName.Replace('.$(TargetCompilerIdentifier)$(_ShortCompilerVersion)', ''))</ProjectName>

eng/Toolkit.Packaging.props

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<Project>
2+
3+
<!-- Any item metadata defined in item definitions is default for all items of that name -->
4+
<ItemDefinitionGroup>
5+
<PackageFile>
6+
<Visible>False</Visible>
7+
<TargetPath>\</TargetPath>
8+
</PackageFile>
9+
</ItemDefinitionGroup>
10+
11+
<!-- Any item types included via 'AvailableItemName' adds to build action UI list in VS IDE -->
12+
<ItemGroup>
13+
<AvailableItemName Include="PackageFile" />
14+
</ItemGroup>
15+
16+
</Project>

eng/Toolkit.Packaging.targets

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<Project>
2+
3+
<!--
4+
Use 'GenerateNuSpecDependsOn' extensibility point to include custom global assets in the package.
5+
Use 'TargetsForTfmSpecificContentInPackage' extensibility point to include custom TFM-specific assets in the package.
6+
-->
7+
<PropertyGroup>
8+
<GenerateNuSpecDependsOn>_AddGlobalPackageFilesToNuGetPack;$(GenerateNuSpecDependsOn)</GenerateNuSpecDependsOn>
9+
<TargetsForTfmSpecificContentInPackage>_AddPackageFilesPerTargetFrameworkToNuGetPack</TargetsForTfmSpecificContentInPackage>
10+
</PropertyGroup>
11+
12+
<!--
13+
When the '$(IncludeContentInPack)' property is false, files specified via '@(None)', '@(Content)' items
14+
are excluded from the NuGet package. Adding '@(PackageFile)' items directly to '%(_PackageFiles)' item
15+
via the following target ensures that they will always be included in the package.
16+
17+
So, Use '%(PackageFile)' item to always include files in the package. By default, they are included in
18+
the root of the package but can be overridden via '%(PackageFile.TargetPath)' metadata. Since, 'TargetPath'
19+
is just an alias of the 'PackagePath' metadata, multiple paths and different file names can also be specified.
20+
-->
21+
<Target Name="_AddGlobalPackageFilesToNuGetPack"
22+
AfterTargets="_CalculateInputsOutputsForPack">
23+
<ItemGroup>
24+
<_PackageFiles Include="@(PackageFile)" Exclude="@(PackageFile->WithMetadataValue('IsSpecific', 'true'))" Condition="'%(PackageFile.Pack)' != 'false'">
25+
<PackagePath Condition="'%(PackageFile.TargetPath)' != ''">%(PackageFile.TargetPath)</PackagePath>
26+
</_PackageFiles>
27+
<!-- Use the 'FinalOutputPath' metadata to mark the inputs thus enabling incremental builds -->
28+
<NuGetPackInput Include="@(_PackageFiles->WithMetadata('FinalOutputPath')->%(FinalOutputPath))" />
29+
</ItemGroup>
30+
</Target>
31+
32+
<!--
33+
Same 'PackageFile' as above but processed per target framework when 'IsSpecific: True' metadata is specified.
34+
Since it is target specific, it also validates whether the package path contains the current framework alias.
35+
-->
36+
<Target Name="_AddPackageFilesPerTargetFrameworkToNuGetPack">
37+
<ItemGroup>
38+
<TFMSpecificPackageFile Include="@(PackageFile->WithMetadataValue('IsSpecific', 'true'))" Condition="'%(PackageFile.Pack)' != 'false'">
39+
<PackagePath Condition="'%(PackageFile.TargetPath)' != ''">%(PackageFile.TargetPath)</PackagePath>
40+
</TFMSpecificPackageFile>
41+
</ItemGroup>
42+
43+
<!-- Error out when the target path of the package file doesn't contain target framework to differentiate itself within the package -->
44+
<Error Code="NCTDEV02" Condition="!$([System.String]::new('%(PackagePath)').Contains($(TargetFramework))) AND '@(TFMSpecificPackageFile)' != ''"
45+
Text="The package file ('%(Identity)') is 'TargetFramework' specific and should include the value ('$(TargetFramework)') somewhere in the target path ('%(PackagePath)')." />
46+
</Target>
47+
48+
</Project>

src/CommunityToolkit.Mvvm/CommunityToolkit.Mvvm.csproj

+15-9
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,9 @@
4646
Include a custom targets file to check the compiler version required for the source generator.
4747
It needs Roslyn 4.x and including the targets in .NET 6+ is not needed as it guarantees the same.
4848
-->
49-
<ItemGroup>
50-
<None Include="CommunityToolkit.Mvvm.targets" Pack="True" PackagePath="build\netstandard2.0" />
51-
<None Include="CommunityToolkit.Mvvm.targets" Pack="True" PackagePath="build\netstandard2.1" />
52-
<None Include="CommunityToolkit.Mvvm.targets" Pack="True" PackagePath="buildTransitive\netstandard2.0" />
53-
<None Include="CommunityToolkit.Mvvm.targets" Pack="True" PackagePath="buildTransitive\netstandard2.1" />
49+
<ItemGroup Condition="$(TargetFramework.StartsWith('netstandard'))">
50+
<PackageFile Include="CommunityToolkit.Mvvm.targets" IsSpecific="True" TargetPath="build\$(TargetFramework)" />
51+
<PackageFile Include="CommunityToolkit.Mvvm.targets" IsSpecific="True" TargetPath="buildTransitive\$(TargetFramework)" />
5452
</ItemGroup>
5553

5654
<!-- .NET Standard 2.0 doesn't have the Span<T> and IAsyncEnumerable<T> types -->
@@ -69,8 +67,12 @@
6967

7068
<!-- Reference the various multi-targeted versions of the source generator project (one per Roslyn version) -->
7169
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
72-
<ProjectReference Include="..\CommunityToolkit.Mvvm.SourceGenerators\CommunityToolkit.Mvvm.SourceGenerators.Roslyn40.csproj" ReferenceOutputAssembly="false" />
73-
<ProjectReference Include="..\CommunityToolkit.Mvvm.SourceGenerators\CommunityToolkit.Mvvm.SourceGenerators.Roslyn43.csproj" ReferenceOutputAssembly="false" />
70+
<!-- Roslyn v4.0 target -->
71+
<ProjectReference Include="..\CommunityToolkit.Mvvm.SourceGenerators\CommunityToolkit.Mvvm.SourceGenerators.Roslyn40.csproj"
72+
ReferenceOutputAssembly="false" ExcludeAssets="All" />
73+
<!-- Roslyn v4.3 target -->
74+
<ProjectReference Include="..\CommunityToolkit.Mvvm.SourceGenerators\CommunityToolkit.Mvvm.SourceGenerators.Roslyn43.csproj"
75+
ReferenceOutputAssembly="false" ExcludeAssets="All" />
7476
</ItemGroup>
7577

7678
<!--
@@ -84,8 +86,12 @@
8486
output path and building it first is the only way to ensure they are included.
8587
-->
8688
<ItemGroup>
87-
<None Include="..\CommunityToolkit.Mvvm.SourceGenerators\bin\$(Configuration)\netstandard2.0-roslyn4.0\CommunityToolkit.Mvvm.SourceGenerators.dll" PackagePath="analyzers\dotnet\roslyn4.0\cs" Pack="true" Visible="false" />
88-
<None Include="..\CommunityToolkit.Mvvm.SourceGenerators\bin\$(Configuration)\netstandard2.0-roslyn4.3\CommunityToolkit.Mvvm.SourceGenerators.dll" PackagePath="analyzers\dotnet\roslyn4.3\cs" Pack="true" Visible="false" />
89+
<!-- Roslyn v4.0 target -->
90+
<PackageFile Include="..\CommunityToolkit.Mvvm.SourceGenerators\bin\$(Configuration)\netstandard2.0-roslyn4.0\CommunityToolkit.Mvvm.SourceGenerators.dll"
91+
TargetPath="analyzers\dotnet\roslyn4.0\cs" />
92+
<!-- Roslyn v4.3 target -->
93+
<PackageFile Include="..\CommunityToolkit.Mvvm.SourceGenerators\bin\$(Configuration)\netstandard2.0-roslyn4.3\CommunityToolkit.Mvvm.SourceGenerators.dll"
94+
TargetPath="analyzers\dotnet\roslyn4.3\cs" />
8995
</ItemGroup>
9096

9197
</Project>

0 commit comments

Comments
 (0)