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

Commit dadd639

Browse files
authored
Use McMaster.NETCore.Plugins for assembly loading (#156)
* Engine uses `McMaster.NETCore.Plugins` package to resolve assemblies and dependencies, enabling generators to reference additional dependencies! 🎉 * Tasks deprecate `GeneratorAssemblySearchPaths` usage * Tasks use `CodeGenerationRoslynPlugin` ItemGroup instead, which contains concrete assembly paths (instead of containing folder paths, as was previously) * Engine targets `netcoreapp2.0` to reference McMaster package * Tests use Amadevus.RecordGenerator NuGet generator for back-compat checks * Tests.Generators use Bogus NuGet for NuGet dependency resolution check * `CodeGeneration.Roslyn.Plugin.Sdk` MSBuild project Sdk created, to help build and package plugins correctly. * Migrate to use VS2019/.NET Core SDK v3.1 * Rewritten Readme with a simpler demo and more advanced scenarios * Change input assemblies check Now the list of plugin assemblies is always read from response file (plugin list), the last modified time is calculated using those assemblies, and the .AssemblyList.txt file is not created. Also separated reading the results into another target, and set Inputs and Outputs so that the MSBuild can fully skip executing the target that invokes CLI tool. * Rename BuildTime targets private items * add comment to BuildTime targets * feature: create Plugin.Sdk project initial idea in #113 * fix GenerateCodeFromAttributesCore condition _CodeGenToolVersionExitCode was compared to zero via != instead of == * fix CGR1002 warning in Tests * refactor and cleanup BuildTime files * refactor Tests project file * docs: readme demo and more for new Plugins.Sdk * don't prefer shared types, use explicit list this will allow different plugins to have conflicting dependencies * refactor: rename dictionary to cachedPlugins * fix: Use OutputItemType in Sample * docs: Add changelog for plugins PR
1 parent 0641357 commit dadd639

19 files changed

Lines changed: 558 additions & 319 deletions

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,22 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
### Added
1111
* GeneratorInCustomerSolution sample in `samples` folder
1212
* GitHub Actions CI
13+
* Support for plugin dependencies! 🎉 ([#156]).
1314

1415
### Changed
1516
* .NET Core SDK version bumped to `3.1.100` ([#178]).
1617
* `Attributes` package now targets `net20;net40` in addition to `netstandard1.0` ([#178]).
1718
* `dotnet-codegen` now has `RollForward=Major` policy to allow it to run on newer runtimes than 2.x,
1819
e.g. .NET Core SDK v3.x *only* should suffice for most usage scenarios ([#178]).
20+
* MSBuild ItemGroup used for registration of plugin paths changed to `CodeGenerationRoslynPlugin`
21+
(was `GeneratorAssemblySearchPaths`). A warning for using old one is introduced (`CGR1002`). ([#156])
22+
* ItemGroup now should contain full path to generator dll (previously it was a containing folder path)
23+
* Old behavior has a compat-plug and the paths are searched for any dll, and those found are added to new ItemGroup.
24+
* When using P2P generator (same solution), a consuming project needs to add an attribute `OutputItemType="CodeGenerationRoslynPlugin"` to the `ProjectReference` of the generator project. See [v0.7 migration guide].
1925

26+
[#156]: https://github.com/AArnott/CodeGeneration.Roslyn/pull/156
2027
[#178]: https://github.com/AArnott/CodeGeneration.Roslyn/pull/178
28+
[v0.7 migration guide]: https://github.com/AArnott/CodeGeneration.Roslyn/wiki/Migrations#v07
2129

2230

2331
## [0.6.1] - 2019-06-16

README.md

Lines changed: 248 additions & 129 deletions
Large diffs are not rendered by default.

samples/GeneratorInConsumerSolution/Sample.Consumer/Sample.Consumer.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
SkipGetTargetFrameworkProperties="true"
2121
UndefineProperties=";TargetFramework;RuntimeIdentifier"
2222
PrivateAssets="all" />-->
23-
<ProjectReference Include="..\Sample.Generator\Sample.Generator.csproj" PrivateAssets="all" />
23+
<ProjectReference Include="..\Sample.Generator\Sample.Generator.csproj"
24+
OutputItemType="CodeGenerationRoslynPlugin"
25+
PrivateAssets="all" />
2426
</ItemGroup>
2527

2628
</Project>

src/CodeGeneration.Roslyn.BuildTime/build/CodeGeneration.Roslyn.BuildTime.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="utf-8" ?>
2-
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
2+
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
33

44
<ItemDefinitionGroup>
55
<Compile>

src/CodeGeneration.Roslyn.BuildTime/build/CodeGeneration.Roslyn.BuildTime.targets

Lines changed: 51 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
<?xml version="1.0" encoding="utf-8" ?>
2-
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
2+
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
33

44
<PropertyGroup>
55
<GenerateCodeFromAttributesDependsOn>
66
$(GenerateCodeFromAttributesDependsOn);
77
ResolveReferences;
88
PrepareGenerateCodeFromAttributes;
9-
GenerateCodeFromAttributesCore
9+
GenerateCodeFromAttributesCore;
10+
ReadGenerateCodeFromAttributesResults
1011
</GenerateCodeFromAttributesDependsOn>
12+
<CgrUrl>https://github.com/AArnott/CodeGeneration.Roslyn</CgrUrl>
1113
</PropertyGroup>
1214

1315
<Target
@@ -18,11 +20,26 @@
1820

1921
<Target Name="PrepareGenerateCodeFromAttributes">
2022
<ItemGroup>
21-
<Compile_CodeGenInputs
23+
<_CodeGenToolInputs_Compile
2224
Include="@(Compile)"
2325
Condition=" '%(Compile.Generator)' == 'MSBuild:GenerateCodeFromAttributes' " />
24-
<DefineConstantsItems Include="$(DefineConstants)" />
26+
<_CodeGenToolInputs_DefineConstants Include="$(DefineConstants)" />
27+
<!-- Map GeneratorAssemblySearchPaths to actual DLLs until support is removed -->
28+
<GeneratorAssemblySearchPaths
29+
Update="@(GeneratorAssemblySearchPaths)"
30+
PathWithTrailingSlash="$([MSBuild]::EnsureTrailingSlash(%(Identity)))" />
31+
<_GeneratorAssemblySearchPathsResolved
32+
Include="%(GeneratorAssemblySearchPaths.PathWithTrailingSlash)*.dll" />
33+
<!-- Include resolved DLLs into CodeGenerationRoslynPlugin -->
34+
<CodeGenerationRoslynPlugin
35+
Include="@(_GeneratorAssemblySearchPathsResolved)"
36+
Condition="Exists('%(Identity)')" />
2537
</ItemGroup>
38+
<!-- Warning for the time GeneratorAssemblySearchPaths are deprecated but supported -->
39+
<Warning
40+
Code="CGR1002"
41+
Text="Using GeneratorAssemblySearchPaths is deprecated, please use CodeGenerationRoslynPlugin ItemGroup. See $(CgrUrl) for more info. Suppress this warning by adding CGR1002 to MSBuildWarningsAsMessages PropertyGroup."
42+
Condition=" '@(GeneratorAssemblySearchPaths)' != '' " />
2643
<PropertyGroup>
2744
<GenerateCodeFromAttributesToolPathOverride
2845
Condition="'$(GenerateCodeFromAttributesToolPathOverride)' == ''">codegen</GenerateCodeFromAttributesToolPathOverride>
@@ -31,19 +48,19 @@
3148
<_CodeGenToolGeneratedFileListFullPath>$(_CodeGenToolOutputBasePath).GeneratedFileList.txt</_CodeGenToolGeneratedFileListFullPath>
3249
<_CodeGenToolResponseFileLines>
3350
@(ReferencePath->'-r;%(Identity)');
34-
@(DefineConstantsItems->'-d;%(Identity)');
35-
@(GeneratorAssemblySearchPaths->'--generatorSearchPath;%(Identity)');
51+
@(_CodeGenToolInputs_DefineConstants->'-d;%(Identity)');
52+
@(CodeGenerationRoslynPlugin->'--plugin;%(Identity)');
3653
--out;
3754
$(IntermediateOutputPath);
3855
--projectDir;
3956
$(MSBuildProjectDirectory);
4057
--generatedFilesList;
4158
$(_CodeGenToolGeneratedFileListFullPath);
4259
--;
43-
@(Compile_CodeGenInputs)
60+
@(_CodeGenToolInputs_Compile)
4461
</_CodeGenToolResponseFileLines>
45-
<_GenerateCodeToolVersion>(n/a)</_GenerateCodeToolVersion>
46-
<_GenerateCodeToolVersionExitCode></_GenerateCodeToolVersionExitCode>
62+
<_CodeGenToolVersionOutput>(n/a)</_CodeGenToolVersionOutput>
63+
<_CodeGenToolVersionExitCode></_CodeGenToolVersionExitCode>
4764
</PropertyGroup>
4865
<ItemGroup>
4966
<_CodeGenToolResponseFileContent Include="$(_CodeGenToolResponseFileLines)" />
@@ -52,42 +69,53 @@
5269
<WriteLinesToFile
5370
File="$(_CodeGenToolResponseFileFullPath)"
5471
Lines="@(_CodeGenToolResponseFileContent)"
55-
Overwrite="true" />
56-
<Delete
57-
Condition="Exists('$(_CodeGenToolGeneratedFileListFullPath)') == 'true'"
58-
Files="$(_CodeGenToolGeneratedFileListFullPath)"
59-
ContinueOnError="true" />
72+
Overwrite="true"
73+
WriteOnlyWhenDifferent="true" />
6074
<!--Check and print tool version used-->
6175
<Exec
6276
Command="dotnet $(GenerateCodeFromAttributesToolPathOverride) --version"
6377
ConsoleToMsBuild="true"
6478
StandardOutputImportance="normal"
6579
ContinueOnError="true">
66-
<Output TaskParameter="ConsoleOutput" PropertyName="_GenerateCodeToolVersion"/>
67-
<Output TaskParameter="ExitCode" PropertyName="_GenerateCodeToolVersionExitCode"/>
80+
<Output TaskParameter="ConsoleOutput" PropertyName="_CodeGenToolVersionOutput"/>
81+
<Output TaskParameter="ExitCode" PropertyName="_CodeGenToolVersionExitCode"/>
6882
</Exec>
6983
<Message
70-
Text="CodeGeneration.Roslyn.Tool (dotnet-codegen) version: $(_GenerateCodeToolVersion)"
84+
Text="CodeGeneration.Roslyn.Tool (dotnet-codegen) version: $(_CodeGenToolVersionOutput)"
7185
Importance="normal"
72-
Condition="'$(_GenerateCodeToolVersionExitCode)' == '0'" />
86+
Condition="'$(_CodeGenToolVersionExitCode)' == '0'" />
7387
<Error
7488
Code="CGR1001"
75-
Text="CodeGeneration.Roslyn.Tool (dotnet-codegen) is not available, code generation won't run. Please check https://github.com/AArnott/CodeGeneration.Roslyn for usage instructions."
76-
Condition="'$(_GenerateCodeToolVersionExitCode)' != '0'" />
89+
Text="CodeGeneration.Roslyn.Tool (dotnet-codegen) is not available, code generation won't run. Please check $(CgrUrl) for usage instructions."
90+
Condition="'$(_CodeGenToolVersionExitCode)' != '0'" />
7791
<ItemGroup>
7892
<FileWrites Include="$(_CodeGenToolResponseFileFullPath)" />
7993
</ItemGroup>
8094
</Target>
8195

82-
<Target Name="GenerateCodeFromAttributesCore" Condition="'@(Compile_CodeGenInputs)' != ''">
83-
<!--Run the tool and process results-->
96+
<!--
97+
Inputs consist of all plugin assemblies, all compiled sources and the response file.
98+
Outputs is the result file that the tool writes.
99+
If the result file is newer than any of the inputs, we can safely skip calling the tool at all,
100+
and just read the existing file in ReadGenerateCodeFromAttributesResults target.
101+
-->
102+
<Target Name="GenerateCodeFromAttributesCore"
103+
Condition=" '@(_CodeGenToolInputs_Compile)' != '' AND '$(_CodeGenToolVersionExitCode)' == '0' "
104+
Inputs="$(_CodeGenToolResponseFileFullPath);@(CodeGenerationRoslynPlugin);@(_CodeGenToolInputs_Compile)"
105+
Outputs="$(_CodeGenToolGeneratedFileListFullPath)">
106+
<!--Run the tool and raise an error when failed-->
84107
<Exec
85108
Command="dotnet $(GenerateCodeFromAttributesToolPathOverride) &quot;%40$(_CodeGenToolResponseFileFullPath)&quot;"
86109
StandardOutputImportance="normal" />
87110
<Error
88111
Code="CGR1000"
89-
Text="CodeGeneration.Roslyn.Tool (dotnet-codegen) failed to generate the list of generated files. The tool didn't run successfully. Please check https://github.com/AArnott/CodeGeneration.Roslyn for usage instructions."
112+
Text="CodeGeneration.Roslyn.Tool (dotnet-codegen) failed to generate the list of generated files. The tool didn't run successfully. Please check $(CgrUrl) for usage instructions."
90113
Condition="Exists('$(_CodeGenToolGeneratedFileListFullPath)') != 'true'" />
114+
</Target>
115+
116+
<Target Name="ReadGenerateCodeFromAttributesResults"
117+
Condition=" Exists('$(_CodeGenToolGeneratedFileListFullPath)') ">
118+
<!-- Process tool results: read generated files list and add them to Compile -->
91119
<ReadLinesFromFile File="$(_CodeGenToolGeneratedFileListFullPath)">
92120
<Output TaskParameter="Lines" ItemName="CodeGenerationRoslynOutput_Compile"/>
93121
<Output TaskParameter="Lines" ItemName="FileWrites"/>

src/CodeGeneration.Roslyn.Engine/CodeGeneration.Roslyn.Engine.csproj

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>netstandard2.0</TargetFramework>
4+
<TargetFramework>netcoreapp2.0</TargetFramework>
55
<Description>The engine of source code generation used by `dotnet-codegen` tool.</Description>
66
</PropertyGroup>
77

88
<ItemGroup>
9-
<PackageReference Include="Microsoft.Extensions.DependencyModel" Version="2.0.4" />
10-
<PackageReference Include="System.Runtime.Loader" Version="4.3.0" />
9+
<PackageReference Include="McMaster.NETCore.Plugins" Version="0.3.1" />
1110
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="[$(RoslynNugetVersion)]" />
1211
<PackageReference Include="Validation" Version="2.4.18" />
1312
</ItemGroup>

0 commit comments

Comments
 (0)