Package Description
$ dotnet add package CodeGeneratorCLICodeGeneratorCLI is a .NET Core command-line interface (CLI) tool that generates code based on provided arguments.
To use CodeGeneratorCLI, you can download the source code and build the project, or you can install the tool from NuGet using the following command:
dotnet tool install -g CodeGeneratorCLI
Once installed, you can run the tool from the command line using the CodeGeneratorCLI command.
CodeGeneratorCLI provides several commands for generating code based on different templates and settings.
The init command initializes the templates that can be used as an example to generate code and is not required. You can specify the output directory for the templates using the --outDir option.
CodeGeneratorCLI init --outDir Templates
The custom-template command generates a new class based on a custom template. You can specify the path to the template using the --template-path option, and you can provide a settings JSON file using the --settings-file option.
CodeGeneratorCLI custom --template-path Templates --param ThisNamespaceName=MyApp.Models --param NamespaceName=MyApp.Models --param Name=MyModels --param SingularName=MyModel --settings-file ControllerGenerator/controller.template.settings.json
The JSON configuration file contains directives for generating code in a .NET project. It specifies the base output path, notes for developers, and details about what projects and files need to be generated. Each entry under "Projects" represents a different part of the application (e.g., main app, tests) and contains a list of "Generations" which are specific files to be generated.
{
"BaseOutPath": "",
"Projects": [
{
"DestinationRootFolderPath": ".",
"Generations": [
{
"TemplateRelativePath": "Commands\\Create_NAME_Command.cshtml",
"DestinationFolderPath": "C:\\source\\$(Name)\\Commands\\Create$(Name)Command.cs"
},
{
"TemplateRelativePath": "Handlers\\Create_NAME_Handler.cshtml",
"DestinationFolderPath": "C:\\source\\$(Name)\\Handlers\\Create$(Name)Handler.cs"
},
{
"TemplateRelativePath": "Queries\\Get_NAME_Query.cshtml",
"DestinationFolderPath": "C:\\source\\$(Name)\\Queries\\Get$(Name)Query.cs"
},
{
"TemplateRelativePath": "Responses\\Get_NAME_Response.cshtml",
"DestinationFolderPath": "C:\\source\\$(Name)\\Responses\\Get$(Name)Response.cs"
},
{
"TemplateRelativePath": "Validators\\Create_NAME_CommandValidator.cshtml",
"DestinationFolderPath": "C:\\source\\$(Name)\\Validators\\Create$(Name)CommandValidator.cs"
},
{
"TemplateRelativePath": "Controllers\\v2.0\\_PRULARNAME_Controller.cshtml",
"DestinationFolderPath": "C:\\source\\Services\\Profile\\Profile.API\\Controllers\\v2.0\\$(PrularName)Controller.cs"
}
]
}
],
"Notes": null
}
The buildtemplates command of CodeGeneratorCLI is designed to scaffold templates from existing source code. These templates can then be utilized by the CustomTemplateCommand to generate new files. This command streamlines the process of creating consistent and repeatable code structures for your projects instead of creating the templates yourself manually.
To generate templates from your existing code base, run the following command:
codegeneratorcli buildtemplates --outDir . --settings-file settings.json
This command will process the source files specified in the settings.json configuration file and produce templates in the output directory (--outDir). These templates capture the structure and design of your code, ready to be used for generating new code files.
The settings.json file contains the configuration for the buildtemplates command. It specifies the source files (or directories) from which to generate templates and defines any content or file name replacements to be made during the template creation process.
Here's an example of what settings.json might look like:
{
"OutputFolder": "templates",
"Destinations": [
{
"Path": "C:\\src\\CreditCard",
"SearchPattern": "*.cs"
},
{
"Path": "C:\\src\\API\\Validators\\CreateCreditCardCommandValidator.cs"
}
],
"ContentReplacements": [
{
"Key": "CreditCards",
"Value": "@(Model.PrularName)"
},
{
"Key": "CreditCard",
"Value": "@(Model.Name)"
},
{
"Key": "creditCards",
"Value": "@(char.ToLower(Model.PrularName[0]) + Model.PrularName.Substring(1))"
},
{
"Key": "creditCard",
"Value": "@(char.ToLower(Model.Name[0]) + Model.Name.Substring(1))"
},
{
"Key": "creditcards",
"Value": "@(Model.PrularName.ToLower())"
},
{
"Key": "creditcard",
"Value": "@(Model.Name.ToLower())"
}
],
"FileNameReplacements": {
"PrularName": "CreditCards",
"Name": "CreditCard"
}
}
Destinations: An array of objects where each object specifies a Path to the source code and an optional SearchPattern for filtering files.ContentReplacements: An array of name-value pairs for replacing text within the templates.FileNameReplacements: An object where each name-value pair specifies how file names should be transformed into template names.When you run the buildtemplates command, the CodeGeneratorCLI will:
Destinations.SearchPattern if provided to filter files.ContentReplacements and FileNameReplacements as configured.These templates will serve as blueprints for the CustomTemplateCommand, enabling you to generate new files that follow the patterns and conventions of your existing code.
If you would like to contribute to CodeGeneratorCLI, please fork the repository and submit a pull request.
Ramy Victor
CodeGeneratorCLI is licensed under the MIT License.