-
Notifications
You must be signed in to change notification settings - Fork 59
How it works
This is a list of elements (artifacts, nugets, tools, build tasks) involved in running code generation during consumer build.
-
CodeGeneration.Roslyn.BuildTime.targets
is an MSBuild project that definesPrepareGenerateCodeFromAttributes
andGenerateCodeFromAttributesCore
targets -
dotnet-codegen
CLI tool which is a command line executable that is run by the above target -
CodeGeneration.Roslyn.Engine
which is a library that compiles the project, invokes generators and handles their results -
CodeGeneration.Roslyn
is a library that declares a contract for generators to implement -
CodeGeneration.Roslyn.Attributes
contains theCodeGenerationAttributeAttribute
The GenerateCodeFromAttributesCore
target depends on ResolveReferences
and has to happen before CoreCompile
. It prepares the response file (.rsp
) for the dotnet-codegen
to read parameters from, and then reads the results file to add generated files to Compile
ItemGroup.
The dotnet-codegen
tool reads the arguments (and resolves them from response file if such is passed), sets up CodeGeneration.Roslyn.Engine.CompilationGenerator
and invokes it's method. Then it saves the list of generated files to the filepath given in arguments.
CodeGeneration.Roslyn.Engine.CompilationGenerator
compiles the project into Roslyn's Compilation
instance. Then it analyzes all the SyntaxTrees
(files) in it, searching for members annotated with trigger attributes. When found, instantiates the ICodeGenerator
implementation, invokes it and saves the results. After obtaining all the results from all generators for a given SyntaxTree
, the results are merged into CompilationUnitSyntax
and saved as a generated source file.