Featured image of post Dotnet Spawn Global Tool

Dotnet Spawn Global Tool

Introducing dotnet-spawn code generation global tool

Back in 2013, I created a nice little sample project demonstrating code generation among other things. Scott Hanselman had this to say about it:

I really enjoyed code generation with T4 in Visual Studio that I used it in all my projects. Four years later, I asked the following on Twitter:

Glenn von Breadmeister was kind enough to respond with:

And Andrew Stanton-Nurse took it further by saying:

I somewhat forgot about these conversations until recently when I started building another sample project and needed to generate some code for rapid development. Looking around, I couldn’t find something that suited my needs and besides, I had a lingering question: “Is razor really that bad at code generation?”. I figured maybe it’s worth a try.

So, I created a dotnet global tool for generating project files.

Nuget

dotnet-spawn is a roslyn-based code generator for dotnet that adds files to a project or solution. Code generation parameters can be supplied inline or from a response file. At this time dotnet-spawn is able to create a single file or multiple files of any extension based on a Razor file template.

How To Use

Invoking the dotnet spawn command will generate project files based on the supplied parameters or response file. You can control how verbose the output will be by using the --verbosity option.

Command Options

  • --project: The path to the project or solution file to analyze with Roslyn
  • --template: The path to the template file used for code generation
  • --output: The path to a file/folder. If the tool is generating a single file, then the path should be a file, otherwise, it should be a folder.
  • --namespace: The namespace used for generated files, if applicable. This parameter is available in the Razor template and can be customized further.
  • --generator: One of SingleFile or MultipleFile. As the names suggest, the SingleFile generator creates a single file and the MultipleFile generator creates multiple files.
  • --match: The lambda expression representing the Roslyn symbols that should be used for code generation.
  • --pattern: The lambda expression used by the MultipleFile generator to generate file names.
  • --verbosity: Set the verbosity level. Allowed values are Debug, Info, and Quiet.

The Help Option

This tool provides an option to display a brief description of the available commands, options, and arguments. System.CommandLine automatically generates help output. For example:

1
dotnet spawn --help

produces the following output:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
Description:
  Roslyn based code generator.

Usage:
  dotnet-spawn [options]

Options:
  -p, --project <project> (REQUIRED)      Project file, Solution file or directory
  -t, --template <template> (REQUIRED)    Specify the template used for the generated code file(s)
  -g, --generator <generator> (REQUIRED)  Specify the generator used for the generated code file(s)
  -n, --namespace <namespace> (REQUIRED)  Specify the namespace for the generated code file(s)
  -m, --match <match>                     Specify the lambda expression used for the nanes of the generated code file(s)
  -p, --pattern <pattern>                 Specify the lambda expression used for the nanes of the generated code file(s)
  -o, --output <output> (REQUIRED)        Specify the file or folder used for the generated code file(s) [default: .]
  -v, --verbosity <Debug|Info|Quiet>      Output verbosity [default: Info]
  -f, --force                             Force execution of command
  --version                               Show version information
  -?, -h, --help                          Show help and usage information

Response File

A response file is a file that contains a set of tokens for a command-line app. Response files are a feature of System.CommandLine that is useful in two scenarios:

  • To invoke a command-line app by specifying input that is longer than the character limit of the terminal. To invoke the same command repeatedly without retyping the whole line.
  • To use a response file, enter the file name prefixed by an @ sign wherever in the line you want to insert commands, options, and arguments. The following lines are equivalent:
1
dotnet spawn @controllers.rsp

Contents of controllers.rsp:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
--project
./src/Sample.sln
--template
./src/Sample/Templates/Controller.cshtml
--output
./src/Sample/Controllers
--namespace
Sample.Controllers
--generator
MultipleFile
--match
symbol => symbol.IsReferenceType && !(symbol.IsAbstract || symbol.IsNamespace || symbol.IsVirtual)
--pattern
model => $"{model.Name}.cs"
--verbosity
Info

How To Install

You can install the latest build of the tool using the following command.

1
dotnet tool install -g dotnet-spawn 

How To Uninstall

You can uninstall the tool using the following command.

1
dotnet tool uninstall -g dotnet-spawn
Built with Hugo
Theme Stack designed by Jimmy