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

Commit

Permalink
Use McMaster.NETCore.Plugins for assembly loading (#156)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
amis92 authored Jan 5, 2020
1 parent 0641357 commit dadd639
Show file tree
Hide file tree
Showing 19 changed files with 558 additions and 319 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,22 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
* GeneratorInCustomerSolution sample in `samples` folder
* GitHub Actions CI
* Support for plugin dependencies! 🎉 ([#156]).

### Changed
* .NET Core SDK version bumped to `3.1.100` ([#178]).
* `Attributes` package now targets `net20;net40` in addition to `netstandard1.0` ([#178]).
* `dotnet-codegen` now has `RollForward=Major` policy to allow it to run on newer runtimes than 2.x,
e.g. .NET Core SDK v3.x *only* should suffice for most usage scenarios ([#178]).
* MSBuild ItemGroup used for registration of plugin paths changed to `CodeGenerationRoslynPlugin`
(was `GeneratorAssemblySearchPaths`). A warning for using old one is introduced (`CGR1002`). ([#156])
* ItemGroup now should contain full path to generator dll (previously it was a containing folder path)
* Old behavior has a compat-plug and the paths are searched for any dll, and those found are added to new ItemGroup.
* 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].

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


## [0.6.1] - 2019-06-16
Expand Down
377 changes: 248 additions & 129 deletions README.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
SkipGetTargetFrameworkProperties="true"
UndefineProperties=";TargetFramework;RuntimeIdentifier"
PrivateAssets="all" />-->
<ProjectReference Include="..\Sample.Generator\Sample.Generator.csproj" PrivateAssets="all" />
<ProjectReference Include="..\Sample.Generator\Sample.Generator.csproj"
OutputItemType="CodeGenerationRoslynPlugin"
PrivateAssets="all" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8" ?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

<ItemDefinitionGroup>
<Compile>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
<?xml version="1.0" encoding="utf-8" ?>
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

<PropertyGroup>
<GenerateCodeFromAttributesDependsOn>
$(GenerateCodeFromAttributesDependsOn);
ResolveReferences;
PrepareGenerateCodeFromAttributes;
GenerateCodeFromAttributesCore
GenerateCodeFromAttributesCore;
ReadGenerateCodeFromAttributesResults
</GenerateCodeFromAttributesDependsOn>
<CgrUrl>https://github.com/AArnott/CodeGeneration.Roslyn</CgrUrl>
</PropertyGroup>

<Target
Expand All @@ -18,11 +20,26 @@

<Target Name="PrepareGenerateCodeFromAttributes">
<ItemGroup>
<Compile_CodeGenInputs
<_CodeGenToolInputs_Compile
Include="@(Compile)"
Condition=" '%(Compile.Generator)' == 'MSBuild:GenerateCodeFromAttributes' " />
<DefineConstantsItems Include="$(DefineConstants)" />
<_CodeGenToolInputs_DefineConstants Include="$(DefineConstants)" />
<!-- Map GeneratorAssemblySearchPaths to actual DLLs until support is removed -->
<GeneratorAssemblySearchPaths
Update="@(GeneratorAssemblySearchPaths)"
PathWithTrailingSlash="$([MSBuild]::EnsureTrailingSlash(%(Identity)))" />
<_GeneratorAssemblySearchPathsResolved
Include="%(GeneratorAssemblySearchPaths.PathWithTrailingSlash)*.dll" />
<!-- Include resolved DLLs into CodeGenerationRoslynPlugin -->
<CodeGenerationRoslynPlugin
Include="@(_GeneratorAssemblySearchPathsResolved)"
Condition="Exists('%(Identity)')" />
</ItemGroup>
<!-- Warning for the time GeneratorAssemblySearchPaths are deprecated but supported -->
<Warning
Code="CGR1002"
Text="Using GeneratorAssemblySearchPaths is deprecated, please use CodeGenerationRoslynPlugin ItemGroup. See $(CgrUrl) for more info. Suppress this warning by adding CGR1002 to MSBuildWarningsAsMessages PropertyGroup."
Condition=" '@(GeneratorAssemblySearchPaths)' != '' " />
<PropertyGroup>
<GenerateCodeFromAttributesToolPathOverride
Condition="'$(GenerateCodeFromAttributesToolPathOverride)' == ''">codegen</GenerateCodeFromAttributesToolPathOverride>
Expand All @@ -31,19 +48,19 @@
<_CodeGenToolGeneratedFileListFullPath>$(_CodeGenToolOutputBasePath).GeneratedFileList.txt</_CodeGenToolGeneratedFileListFullPath>
<_CodeGenToolResponseFileLines>
@(ReferencePath->'-r;%(Identity)');
@(DefineConstantsItems->'-d;%(Identity)');
@(GeneratorAssemblySearchPaths->'--generatorSearchPath;%(Identity)');
@(_CodeGenToolInputs_DefineConstants->'-d;%(Identity)');
@(CodeGenerationRoslynPlugin->'--plugin;%(Identity)');
--out;
$(IntermediateOutputPath);
--projectDir;
$(MSBuildProjectDirectory);
--generatedFilesList;
$(_CodeGenToolGeneratedFileListFullPath);
--;
@(Compile_CodeGenInputs)
@(_CodeGenToolInputs_Compile)
</_CodeGenToolResponseFileLines>
<_GenerateCodeToolVersion>(n/a)</_GenerateCodeToolVersion>
<_GenerateCodeToolVersionExitCode></_GenerateCodeToolVersionExitCode>
<_CodeGenToolVersionOutput>(n/a)</_CodeGenToolVersionOutput>
<_CodeGenToolVersionExitCode></_CodeGenToolVersionExitCode>
</PropertyGroup>
<ItemGroup>
<_CodeGenToolResponseFileContent Include="$(_CodeGenToolResponseFileLines)" />
Expand All @@ -52,42 +69,53 @@
<WriteLinesToFile
File="$(_CodeGenToolResponseFileFullPath)"
Lines="@(_CodeGenToolResponseFileContent)"
Overwrite="true" />
<Delete
Condition="Exists('$(_CodeGenToolGeneratedFileListFullPath)') == 'true'"
Files="$(_CodeGenToolGeneratedFileListFullPath)"
ContinueOnError="true" />
Overwrite="true"
WriteOnlyWhenDifferent="true" />
<!--Check and print tool version used-->
<Exec
Command="dotnet $(GenerateCodeFromAttributesToolPathOverride) --version"
ConsoleToMsBuild="true"
StandardOutputImportance="normal"
ContinueOnError="true">
<Output TaskParameter="ConsoleOutput" PropertyName="_GenerateCodeToolVersion"/>
<Output TaskParameter="ExitCode" PropertyName="_GenerateCodeToolVersionExitCode"/>
<Output TaskParameter="ConsoleOutput" PropertyName="_CodeGenToolVersionOutput"/>
<Output TaskParameter="ExitCode" PropertyName="_CodeGenToolVersionExitCode"/>
</Exec>
<Message
Text="CodeGeneration.Roslyn.Tool (dotnet-codegen) version: $(_GenerateCodeToolVersion)"
Text="CodeGeneration.Roslyn.Tool (dotnet-codegen) version: $(_CodeGenToolVersionOutput)"
Importance="normal"
Condition="'$(_GenerateCodeToolVersionExitCode)' == '0'" />
Condition="'$(_CodeGenToolVersionExitCode)' == '0'" />
<Error
Code="CGR1001"
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."
Condition="'$(_GenerateCodeToolVersionExitCode)' != '0'" />
Text="CodeGeneration.Roslyn.Tool (dotnet-codegen) is not available, code generation won't run. Please check $(CgrUrl) for usage instructions."
Condition="'$(_CodeGenToolVersionExitCode)' != '0'" />
<ItemGroup>
<FileWrites Include="$(_CodeGenToolResponseFileFullPath)" />
</ItemGroup>
</Target>

<Target Name="GenerateCodeFromAttributesCore" Condition="'@(Compile_CodeGenInputs)' != ''">
<!--Run the tool and process results-->
<!--
Inputs consist of all plugin assemblies, all compiled sources and the response file.
Outputs is the result file that the tool writes.
If the result file is newer than any of the inputs, we can safely skip calling the tool at all,
and just read the existing file in ReadGenerateCodeFromAttributesResults target.
-->
<Target Name="GenerateCodeFromAttributesCore"
Condition=" '@(_CodeGenToolInputs_Compile)' != '' AND '$(_CodeGenToolVersionExitCode)' == '0' "
Inputs="$(_CodeGenToolResponseFileFullPath);@(CodeGenerationRoslynPlugin);@(_CodeGenToolInputs_Compile)"
Outputs="$(_CodeGenToolGeneratedFileListFullPath)">
<!--Run the tool and raise an error when failed-->
<Exec
Command="dotnet $(GenerateCodeFromAttributesToolPathOverride) &quot;%40$(_CodeGenToolResponseFileFullPath)&quot;"
StandardOutputImportance="normal" />
<Error
Code="CGR1000"
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."
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."
Condition="Exists('$(_CodeGenToolGeneratedFileListFullPath)') != 'true'" />
</Target>

<Target Name="ReadGenerateCodeFromAttributesResults"
Condition=" Exists('$(_CodeGenToolGeneratedFileListFullPath)') ">
<!-- Process tool results: read generated files list and add them to Compile -->
<ReadLinesFromFile File="$(_CodeGenToolGeneratedFileListFullPath)">
<Output TaskParameter="Lines" ItemName="CodeGenerationRoslynOutput_Compile"/>
<Output TaskParameter="Lines" ItemName="FileWrites"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFramework>netcoreapp2.0</TargetFramework>
<Description>The engine of source code generation used by `dotnet-codegen` tool.</Description>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyModel" Version="2.0.4" />
<PackageReference Include="System.Runtime.Loader" Version="4.3.0" />
<PackageReference Include="McMaster.NETCore.Plugins" Version="0.3.1" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="[$(RoslynNugetVersion)]" />
<PackageReference Include="Validation" Version="2.4.18" />
</ItemGroup>
Expand Down
Loading

0 comments on commit dadd639

Please sign in to comment.