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

How it works

Amadeusz Sadowski edited this page Jun 15, 2019 · 1 revision

Contents

Definitions

See Features#definitions

Involved elements

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 defines PrepareGenerateCodeFromAttributes and GenerateCodeFromAttributesCore 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 the CodeGenerationAttributeAttribute

Order of build targets

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.

Clone this wiki locally