CodeGeneratorCLI 1.0.81

dotnet tool install --global CodeGeneratorCLI --version 1.0.81
This package contains a .NET tool you can call from the shell/command line.
dotnet new tool-manifest # if you are setting up this repo
dotnet tool install --local CodeGeneratorCLI --version 1.0.81
This package contains a .NET tool you can call from the shell/command line.
#tool dotnet:?package=CodeGeneratorCLI&version=1.0.81
nuke :add-package CodeGeneratorCLI --version 1.0.81

CodeGeneratorCLI

CodeGeneratorCLI is a .NET Core command-line interface (CLI) tool that generates code based on provided arguments.

Getting Started

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.

Usage

CodeGeneratorCLI provides several commands for generating code based on different templates and settings.

Initialize

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

CustomTemplate

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

Settings Configuration File

Overview

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.

Example

{
  "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
}

BuildTemplateCommand

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.

Usage

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.

Settings JSON File

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.

Generating Templates

When you run the buildtemplates command, the CodeGeneratorCLI will:

  1. Traverse the paths specified under Destinations.
  2. Apply the SearchPattern if provided to filter files.
  3. Generate templates from the source files, applying ContentReplacements and FileNameReplacements as configured.
  4. Save the generated templates to the specified output directory.

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.

Contributing

If you would like to contribute to CodeGeneratorCLI, please fork the repository and submit a pull request.

Author

Ramy Victor

"Buy Me A Coffee"

License

CodeGeneratorCLI is licensed under the MIT License.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

Version Downloads Last updated
1.0.81 2,644 12/15/2023
1.0.80 218 12/13/2023
1.0.79 204 12/13/2023
1.0.78 192 12/13/2023
1.0.77 186 12/13/2023
1.0.76 187 12/13/2023
1.0.75 171 12/13/2023
1.0.74 303 12/10/2023