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

Commit 22c9620

Browse files
authored
feature: Importing extra build/ content in Plugins (#215)
1 parent a7575a1 commit 22c9620

File tree

6 files changed

+43
-3
lines changed

6 files changed

+43
-3
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Instructions on development and using this project's source code are in [CONTRIB
2929
- [Package your code generator](#package-your-code-generator)
3030
- [Separate out the attribute](#separate-out-the-attribute)
3131
- [Create the metapackage](#create-the-metapackage)
32+
- [Add extra `build/` content in Plugin package](#add-extra-build-content-in-plugin-package)
3233

3334
## How to write your own code generator
3435

@@ -457,6 +458,14 @@ as it's dependant packages.
457458

458459
> 📋 For a sample metapackage, see [MetapackageSample](samples/MetapackageSample/).
459460
461+
462+
#### Add extra `build/` content in Plugin package
463+
464+
`CG.R.Plugin.Sdk` creates custom `build/PackageId.props/targets` files. If you want
465+
to add custom MSBuild props/targets into NuGet package's `build` folder (and have it
466+
imported when package is referenced), you'll need to use `PackageBuildFolderProjectImport`
467+
ItemGroup, as shown in `PackagedGenerator` sample.
468+
460469
[NuPkg]: https://nuget.org/packages/CodeGeneration.Roslyn
461470
[AttrNuPkg]: https://nuget.org/packages/CodeGeneration.Roslyn.Attributes
462471
[ToolNuPkg]: https://nuget.org/packages/CodeGeneration.Roslyn.Tool

samples/PackageConsumer/PackageConsumer.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,10 @@
1414
</PackageReference>
1515
</ItemGroup>
1616

17+
<!-- This target tests whether using PackageBuildFolderProjectImport in Plugin works as expected. -->
18+
<Target Name="CheckPluginBuildPropsTargetsImported" AfterTargets="AfterBuild">
19+
<Error Text="Generator Test.props not imported." Condition=" '$(TestPropertyProps)' != 'true' " />
20+
<Error Text="Generator Test.targets not imported." Condition=" '$(TestPropertyTargets)' != 'true' " />
21+
</Target>
22+
1723
</Project>

samples/PackagedGenerator/PackagedGenerator.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
88
</PropertyGroup>
99

10+
<ItemGroup>
11+
<None Include="build/**" Pack="true" PackagePath="build" KeepDuplicates="false" />
12+
<PackageBuildFolderProjectImport Include="@(None->'%(Filename)%(Extension)')"/>
13+
</ItemGroup>
14+
1015
<Import Project="$(CodeGenerationRoslynPluginSdkPath)Sdk.targets" />
1116

1217
</Project>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<Project>
2+
<PropertyGroup>
3+
<TestPropertyProps>true</TestPropertyProps>
4+
</PropertyGroup>
5+
</Project>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<Project>
2+
<PropertyGroup>
3+
<TestPropertyTargets>true</TestPropertyTargets>
4+
</PropertyGroup>
5+
</Project>

src/CodeGeneration.Roslyn.Plugin.Sdk/build/BuildPluginPackage.targets

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,27 @@
4141
</Target>
4242

4343
<!--
44-
This target writes a PackageId.props file that will get packed into build/ folder in NuGet.
45-
That props file adds the path to the Plugin dll to the CodeGenerationRoslynPlugin ItemGroup,
46-
so that CodeGeneration.Roslyn tooling can read and load generator plugins.
44+
This target writes PackageId.props/targets files that will get packed into build/ folder in NuGet.
45+
Those MSBuild files add the path to the Plugin dll to the CodeGenerationRoslynPlugin ItemGroup,
46+
so that CodeGeneration.Roslyn tooling can read and load generator plugins, and additionally
47+
create a target that checks whether the Tool targets were imported, and warns if they weren't.
48+
49+
It also imports any PackageBuildFolderProjectImport items as per extension - .props in .props,
50+
.targets in .targets. It doesn't handle actually packaging those files correctly.
4751
-->
4852
<Target Name="CreatePluginPackageBuildProps"
4953
DependsOnTargets="DefinePluginPackagePath"
5054
Condition=" '$(PackAsCodeGenerationRoslynPlugin)' == 'true' ">
55+
<ItemGroup>
56+
<_PackageBuildFolderProjectImport_props Include="@(PackageBuildFolderProjectImport->Distinct())" Condition=" '%(Extension)' == '.props' " />
57+
<_PackageBuildFolderProjectImport_targets Include="@(PackageBuildFolderProjectImport->Distinct())" Condition=" '%(Extension)' == '.targets' " />
58+
</ItemGroup>
5159
<PropertyGroup>
5260
<PackagePropsContent>
5361
<![CDATA[
5462
<?xml version="1.0" encoding="utf-8" ?>
5563
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
64+
@(_PackageBuildFolderProjectImport_props->' <Import Project="%(Identity)" />', '%0D%0A')
5665
<ItemGroup>
5766
<CodeGenerationRoslynPlugin Include="%24(MSBuildThisFileDirectory)../$(PluginPackagePath)/$(TargetFileName)" />
5867
</ItemGroup>
@@ -63,6 +72,7 @@
6372
<![CDATA[
6473
<?xml version="1.0" encoding="utf-8" ?>
6574
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
75+
@(_PackageBuildFolderProjectImport_targets->' <Import Project="%(Identity)" />', '%0D%0A')
6676
<Target Name="CheckCgrToolUsed" BeforeTargets="Build">
6777
<Warning Condition=" '%24(UsingCodeGenerationRoslynToolTargets)' != 'true' " Text="CodeGeneration.Roslyn.Tool build targets weren't detected. CG.R Plugins (generators) won't be run without importing targets from the CodeGeneration.Roslyn.Tool package (v$(PackageVersion))." />
6878
</Target>

0 commit comments

Comments
 (0)