Skip to content

[feature] Generator command execution #1902

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
SlyCedix opened this issue Apr 2, 2025 · 1 comment
Open

[feature] Generator command execution #1902

SlyCedix opened this issue Apr 2, 2025 · 1 comment
Labels
enhancement New feature or request

Comments

@SlyCedix
Copy link

SlyCedix commented Apr 2, 2025

I am currently attempting to setup a .NET monorepo with templates for generating new projects, but to actually completely instantiate the project, I would need to both create the project files and add the project to the .sln file at the top level of the repository.

The best way to handle this would be to actually forgo the templating entirely, and instead prompt the user for inputs such as project name, root namespace, dotnet template, etc. and compose a dotnet new command from those inputs. Similar to what nx-dotnet does for nx.

My ideal generator would look something like this:

template.yml:

title: "Class Library"
description: "Scaffolds a class library project, including source and test projects"
destination: "/"
variables: 
    project_name:
        type: string
        required: true
        prompt: "Enter a name for the project:"
    project_template:
        type: string
        required: true
        prompt: "Enter the project template:"
    test_project_template:
        type: string
        required: true
        prompt: "Enter the test project template:"

command: >
    "dotnet new {{project_template}} -o 'lib/src/{{project_name | lower_case}}' -n '{{project_name | pascal_case}}' &&"
    "dotnet new {{test_project_template}} -o 'lib/test/{{project_name | lower_case}}.test' -n '{{project_name | pascal_case}}' &&"
    "dotnet solution add 'lib/src/{{project_name | lower_case}}/{{project_name | pascal_case}}.csproj' &&"
    "dotnet solution add 'lib/test/{{project_name | lower_case}}.test/{{project_name | pascal_case}}.Test.csproj'"

Used as follows:

$ moon g library
library ──────────────────────────────────────────────────────────────────────────────                                                                                                                                 
                                                                                                                                                                                                                       
  Class Library
  Scaffolds a class library project, including source and test projects

✔ Enter a name for the project: SomeLibrary                                                                                                                                                                            
✔ Enter the project template: classlib                                                                                                                                                                                 
✔ Enter the test project template: nunit       

Executing command: dotnet new classlib -o 'lib/src/somelibrary' -n SomeLibrary && dotnet new nunit -o 'lib/test/somelibrary.test' -n 'SomeLibrary.Test' && dotnet solution add 'lib/src/somelibrary/SomeLibrary.csproj' && dotnet solution add 'lib/test/somelibrary.test/SomeLibrary.Test.csproj'

Another option would be to implement this as a task, but tasks do not have a builtin mechanism for prompting the user, so they would have to be implemented with shell commands and interactive: true, but that may introduce portability issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Development

No branches or pull requests

1 participant