Search code examples
c#.netasp.net-coreyeomanyeoman-generator

How to crete a new .net core project in the Yeoman generator


I'm writing a Yeoman generator. How can I call

dotnet new

inside my generator? Is there a better way to create a new .net core project in yeoman generator?


Solution

  • One possibility is to call dotnet new directly.

    const { execSync, exec } = require('child_process');
    
    function formated_exec_sync(command) {
        console.log("exec->" + command);
        execSync(command, {stdio: 'inherit'});
    }
    
    function new_solution(name) {
        formated_exec_sync(`dotnet new sln --name ${name}`);
    }
    
    function new_console_project(name) {
        formated_exec_sync(`dotnet new console -lang "C#" --name ${name}`);
    }
    
    function add_project_to_solution(project_name, solution_name) {
        formated_exec_sync(`dotnet sln ${solution_name}.sln add ${project_name}`);
    }
    
    function add_package_to_project(package, project, version = null) {
        let command = `dotnet add ${project} package`;
        if (version != null)
            command += ` --version ${version}`;
        command += ` ${package}`
        formated_exec_sync(command);
    }
    
    module.exports = {
        new_solution,
        new_console_project,
        add_project_to_solution,
        add_package_to_project,
    }