Back in 2013, I created a nice little sample project demonstrating code generation among other things. Scott Hanselman had this to say about it:
Really col http://t.co/dzTs3fnv “Northwind” demo app by @achingono on GitHub. Clever use of T4 and more. Starred! https://t.co/1GTE055f
— Scott Hanselman (@shanselman) January 9, 2013
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:
@mkristensen @VisualStudio is there an equivalent to T4Scaffolding for #dotnercore? I need to generate multiple files based on a template.
— Alfero Chingono (@achingono) December 1, 2017
Glenn von Breadmeister was kind enough to respond with:
Not really no. At least not one that I am aware of. People like @anurse will tell you that you can use razor. But afaik there isnt anything quite like t4.
— Glenn von Breadmeister (@condrong) December 1, 2017
And Andrew Stanton-Nurse took it further by saying:
Razor isn’t for generating anything other than HTML. My understanding is that T4Scaffolding generates code, which Razor is really bad at. I don’t think there’s a good replacement right now
— analogrelay 🐘 (@analogrelay) December 5, 2017
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.
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 ofSingleFile
orMultipleFile
. As the names suggest, theSingleFile
generator creates a single file and theMultipleFile
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 theMultipleFile
generator to generate file names.--verbosity
: Set the verbosity level. Allowed values areDebug
,Info
, andQuiet
.
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:
|
|
produces the following output:
|
|
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:
|
|
Contents of controllers.rsp:
|
|
How To Install
You can install the latest build of the tool using the following command.
|
|
How To Uninstall
You can uninstall the tool using the following command.
|
|