sdk icon indicating copy to clipboard operation
sdk copied to clipboard

Reimagine dotnet-new (dotnet new enhanchements 2)

Open thesmithee opened this issue 10 years ago • 35 comments

As it currently stands, dotnet new is a good command for people to get started with simple projects. However, it suffers from several things:

  • Templates are coupled with the implementation, thus requiring a new release of the CLI when adding/changing templates
  • Extending templates is tough
  • No support for naming projects via the command line

This issue suggests a new form of dotnet new that solves the above problems. It also replaces the old dotnet/sdk#4695 that will be closed in favor of this one.

/cc @DamianEdwards @piotrpMSFT @Eilon @enricosada

.NET Core CLI: new

The new command is used to create new .NET Core projects. Projects are created using templates installed via NuGet packages.

Templates are laid out in their NuGet packages in a hierarchical fashion that enables the creation of simple trees of templates with categories that are then merged across the installed template packages to create an overall template tree. This tree allows the display of an interactive menu when creating new projects, while also allowing for direct project creation using a template node's full path, e.g. dotnet new -t aspnetcore/mvc/empty

As templates are in normal NuGet packages and installed into the standard NuGet packages folder, they can depend on other NuGet packages that are used in their templates, such that when they're installed, all the required packages to create new projects using the contained templates are installed too.

Using the Command Line

Displaying help:

~/code> dotnet new --help
    Usage:
        dotnet new
        dotnet new -t|--template <template/path> [-n|--name <ProjectName>] [[-v|--variable <Name> <Value>] [-v|--variable <Name> <Value>]]
        dotnet new <arguments>

    Arguments:
        -t|--template <template/path>                 The path of the template to use
        -n|--name <ProjectName>                       The name of the new project, created in current folder if not specified
        -v|--variable <Name> <Value>                  Variable to be passed to the template (can be specified more than once)

        -l|--list                                     Lists installed templates
        -i|--install <TemplatePackageId> [version]    Installs templates from a package with optional version
        -i|--install <TemplatePackagePath>            Installs templates from a package
        -u|--uninstall <TemplatePackageId>            Uninstalls a templates package
        -r|--restore                                  Restores installed template packages

Listing installed templates:

~/code> dotnet new --list

MS.DotNetCore.New.Templates 1.0.0
  - Console Application    [console]
  - Class Library          [classlib]

~/code> 

Installing new templates from a package:

~/code> dotnet new --install MS.DotNetCore.New.Templates.AspNetCore 1.0.0
Installing templates from MS.DotNetCore.New.Templates.AspNetCore

Installed 3 templates:
  - ASP.NET Core Empty Application          [aspnetcore/empty]
  - ASP.NET Core MVC Web Site               [aspnetcore/mvc/website]
  - ASP.NET Core MVC Web API Application    [aspnetcore/mvc/webapi]

~/code> dotnet new --list

MS.DotNetCore.New.Templates 1.0.0
  - Console Application    [console]
  - Class Library          [classlib]

MS.DotNetCore.New.Templates.AspNetCore 1.0.0
  - ASP.NET Core Empty Application          [aspnetcore/empty]
  - ASP.NET Core MVC Web Site               [aspnetcore/mvc/website]
  - ASP.NET Core MVC Web API Application    [aspnetcore/mvc/webapi]

~/code> 

Creating a new project with a specific template:

~/code> mkdir MyApp && cd MyApp
~/code/MyApp> dotnet new --template console

Created project "MyApp" in ~/code/MyApp

~/code/MyApp> cd ../
~/code> dotnet new -t console --name MyApp2

Created project "MyApp2" in ~/code/MyApp2

~/code> dotnet new -t aspnetcore/mvc/website --name MyWebApp

Created project "MyWebApp" in ~/code/MyWebApp

~/code> 

Creating a new project using the template menu:

~/code> dotnet new

  Templates
  -----------------------------------
  1. Console Application [console]
  2. Class Library       [classlib]
  3. ASP.NET Core        [aspnetcore]

Select a template [1]: 1
Enter a project name [Project1]: MyApp

Created project "MyApp" in ~/code/MyApp

~/code/MyApp> cd../
~/code> dotnet new

  Templates
  -----------------------------------
  1. Console Application [console]
  2. Class Library       [classlib]
  3. ASP.NET Core        [aspnetcore]

Select a template [1]: 3

  ASP.NET Core Templates
  -----------------------------------
  1. ASP.NET Core Empty Application [aspnetcore/empty]
  2. ASP.NET Core MVC               [aspnetcore/mvc]

Select an ASP.NET Core template [1]: 2

  ASP.NET Core MVC Templates
  -----------------------------------
  1. ASP.NET Core MVC Empty               [aspnetcore/mvc/empty]
  2. ASP.NET Core MVC Web Site            [aspnetcore/mvc/website]
  3. ASP.NET Core MVC Web API Application [aspnetcore/mvc/webapi]

Select an ASP.NET Core MVC template [1]: 2
Enter a project name [Project1]: MyWebApp

Created "MyWebApp" in ~/code/MyWebApp

~/code/MyWebApp> 

Authoring Templates

Templates are placed in NuGet packages in the content/templates folder. Optionally, in the root of the folder is a templates.json file that describes the templates contained and if present is used to enhance the project creation experience.

The layout allows for multi-level category nodes with template nodes as the leaves. In the case a category node contains just a single template node, that template will be automatically selected when the category is chosen.

Example templates.json

Example templates.json for the default templates:

{
    "projectTemplates": {
        "console": {
            "title": "Console Application",
            "children": {
                "csharp": {
                    "title": "Console Application (C#)",
                    "variables": {
                        "framework": {
                            "question": "Please select a target framework",
                            "default": "netstandardapp1.5",
                            "conditions": {
                                "rid": ["win7"]
                            }
                        }
                    }
                }
            }
        },
        "classlib": {
            "title": "Class Library",
            "children": {
                "csharp": {
                    "title": "Class Library (C#)",
                    "variables": {
                        "framework": {
                            "question": "Please select a target framework",
                            "default": "netstandardapp1.5",
                            "conditions": {
                                "rid": ["win7"]   
                            }
                        }
                    }
                }
            }
        }
    }
}

Package Files Layout

The following demonstrates the layout of template NuGet packages. Note how templates from separate packages can share their structure such that they're templates can occupy the same parts of the resultant installed template "tree".

~/.nuget/
    packages/
        Microsoft.NETCore.New.Templates/
            templates/
                templates.json
                console/
                    csharp/
                        files/
                            Program.cs
                classlib/
                    csharp/
                        files/
                            project.json
                            Program.cs
        Microsoft.NETCore.New.Templates.VB/
            templates/
                templates.json
                console/
                    vb/
                        files/
                            project.json
                            Program.vb
                classlib/
                    vb/
                        files/
                            projext.json
                            Program.vb
        Microsoft.NETCore.New.Templates.AspNetCore/
            templates/
                templates.json
                aspnetcore/
                    empty/
                        csharp/
                            files/
                                project.json
                                Program.cs
                    mvc/
                        empty/
                            csharp/
                                files/
                                    project.json
                                    Program.cs
                                    Startup.cs
                                    Controllers/
                                        HomeController.cs
                                    wwwroot/
                        website/
                            csharp/
                        webapi/
                            csharp/

thesmithee avatar Mar 25 '16 06:03 thesmithee

There is a functional prototype at https://github.com/DamianEdwards/dotnet-new2

DamianEdwards avatar Mar 25 '16 06:03 DamianEdwards

@ajaybhargavb this is for you.

Eilon avatar Mar 25 '16 06:03 Eilon

nice having a way for templates outside instead of embedded resources :+1:

I'd like to add the feedback from old thread dotnet/sdk#4695:

Make the common use case short using positional arguments (in addition to named arg)

the most common use case: dotnet new name type ( about --lang, see next comment )

so these are equivalent:

  • dotnet new hello_world console
  • dotnet new --name hello_world --type console

some examples in https://github.com/dotnet/cli/pull/1124#issue-129854435

Using another language is ok with --lang ( or -l )

dotnet new hello_world console --lang f#

or if you want, the old proposal was [lang]/[type] like

dotnet new hello_world fsharp/console

enricosada avatar Mar 25 '16 10:03 enricosada

About other languages support ( just to check my use case ):

  • templates support multiple languages using subdirectory, nice, :+1: :white_check_mark:
  • --list should output all templates installed, --list --lang f# only the templates who support f#
  • --list --lang vb should show grouped templates ( like aspnetcore ) if at least one of aspnetcore subtemplates support vb (let's filter out useless groups, but i'd like aspnet to support f# :smile: )

That's useful not only for people who use only one language, but for poliglot who have multiple languages installed (i use c# and f#, both a lot)

Now the bonus points (suggestions) for a better cross language experience. The c# is the default language, np about that, there is --lang and it's ok (was already discussed and it's ok). But i think we can improve experience for others without changing c# experience:

  • support something (env var, global settings) to have a default language (#1118) for dotnet-new for example an env var DOTNET_DEFAULT_LANGUAGE (see https://github.com/dotnet/cli/issues/1118#issuecomment-188790061 )
  • --list write name of supported languages near template name
~/code> dotnet new --list

MS.DotNetCore.New.Templates 1.0.0
  - Console Application    [console] (C#, F#)
  - Class Library              [classlib]  (C#, F#)

MS.DotNetCore.New.Templates.AspNetCore 1.0.0
  - ASP.NET Core Empty Application          [aspnetcore/empty]   (C#, VB)
  • write language name on output, it's easier to see the default or chosen language
Created C# project "MyApp" in ~/code/MyApp
Created F# project "MyApp" in ~/code/MyApp

enricosada avatar Mar 25 '16 13:03 enricosada

about the issue load templates from nuget (last feedback i promise).

  • when i install a template, it's global or per .net cli? i dont want to mess the global config when i install a package, for example in a ci scenario with multiple .net cli installed by directory.
  • if i want to add more nuget feeds (personal/work templates), i need to modify global nuget.config? or add a local nuget.config in working directory?
  • it's possibile to install multiple version of same package? ( TemplatePackageId? )
  • -u|--uninstall <TemplatePackageId> should have a [version] optional argument, because maybe i want to have multiple version of same package (bugfix, stable vs dev) and i want to install/uninstall specific versions

The following is more a request, and maybe it's already like that.

Not everyone use nuget always, or publish to default nuget.org feed I understand the pro, it's nice for discoverability/versioning/tag/author etc but it's a big requirement a zip files with some static template files inside (and no dependencies).

i see --install <TemplatePackagePath> i hope that mean i can install from a package directly (using url or file path).

It's possibile to not bind the implementation to nuget packages only? templates are only a zip file with a structure and a template.json, we can consume it as zip file, no need to parse as nuget packages.

It's nice if i can just install a zip from url (from example from a github repo zip like https://github.com/dotnet/cli/archive/rel/1.0.0.zip ) or a local directory zipped. Offline help a lot too, no need for directory + project.json + dotnet pack.

enricosada avatar Mar 25 '16 13:03 enricosada

Pinging @sayedihashimi just in case he hasn't seen this...(although I'm sure he has)

jchannon avatar Mar 29 '16 10:03 jchannon

:eyes:

muratg avatar Mar 29 '16 16:03 muratg

Thoughts on command line project creation

With dotnew new we are interested in enabling an experience for users to create new projects from the command line. Creating projects from the command line is not a new concept, it's been around for a while especially on the web development side. One example of a successful command line project generator is yeoman. Yeoman is a super popular tool for developers, and if dotnet new is as successful as yeoman, IMO, that would be a really wonderful thing. To enable a meaningful discussion on dotnet new let's start by taking a look at a typical user experience when using yeoman to create a new project. Below are typical steps a user would take to create a new project from the command line.

Steps to create a new project from yeoman

  1. Install yeoman and it's dependencies
  2. Search for generator
  3. Install desired generator
  4. Invoke generator
  5. Enter info about the project; i.e. project name, dest dir and other options
  6. Generate the project (happens automatically after the previous step)

From the set of steps above, we can break this down into the following categories.

  1. Template discovery (local and remote)
  2. Command line user interaction - For example; selecting a generator, project name, dest dir, other project options, displaying help, etc.
  3. Going from template -> generated project

I think that dotnet new should focus on the overall end user experience. Which consists of the following, Template discovery and Command line interaction. When the user selects and invokes a template, dotnet new will handle prompting for project properties and then call the generator (with a consistent way to pass project properties). The template should be able to use any "template generator" that it desires. dotnet new will have a template generator available out of the box which can be used. For scenarios in which the default template generator doesn't meet the needs, it can always be switched out for a different implementation.

In my case I work on delivering templates in the following areas.

  • ASP.NET templates in Visual Studio
  • ASP.NET templates in yo aspnet
  • ASP.NET templates in dotnet new (future)

Today for VS and yo aspnet we have to author templates once for VS and once for yo aspnet. In addition the template infrastructure that we use for the VS side is very fragile and difficult to maintain. Once we enable templates in dotnet new it's yet another place for us to invest in creating templates. Instead of maintaining three different templates I'd like to have one way to create templates from the command line, and then reuse that tool everywhere. So we author templates once in dotnet new which enables the command line experience with dotnet new. Then in Visual Studio we create "shim templates" which will allow us to present the UI to gather parameters and then call into dotnet new for the project creation process. For yo aspnet we would do a similar thing.

Ideas for implementation

diagram

Brokers are decoupled from specific generators via a set of C# interfaces. This will allow us to maximize reusability of generators in different scenarios including; command line, Visual Studio, yeoman, etc.

Here is a typical user session when creating a new project and a high level of what happens.

Setup

When dotnet-new or Visual Studio are installed the following happens.

  1. Broker add template sources
  2. Broker adds generators

Users can add additional sources to enable using additional generators.

Project Creation

  1. Use uses broker to list templates
    1. Broker "collects" template sources
    2. Broker asks each generator for info on available templates for each template source
  2. User asks broker to create using a selected template (X)
    1. Broker "collects" template sources
    2. Broker finds generator for X
      1. Broker asks each generator if it supports X and stops when a match is found (or some other heuristic to find the single generator to use)
    3. Broker gets info on the given template from the generator
      • Contains metadata to display about the template
      • Contains info about template parameters
  3. Broker gathers info for generation
    1. Get info from the user (i.e. project name, destination, etc.)
    2. Get info from the environment and target
  4. Broker asks the generator to create using a given template
    1. Broker passes the properties acquired from the previous step to the generator to start generation
  5. Broker completes experience
    • VS Broker opens the project in Visual Studio
    • Command line broker displays a success message and more info on next steps

FAQ

Q: Does this mean yo aspnet will go away?

A: Absolutely not. yo aspnet has had some wonderful success it's not going anywhere. The idea is that we will create a self-contained template tool and then plug that into: VS, dotnet new, yo aspnet, etc. By allowing us to author templates once it will allow us to bring a better experience in yo aspnet. And it will enable us to keep templates in sync at all times across all end-user experiences.

Q: Why not just use yeoman here?

A: Yeoman has a lot of dependencies which are required to use it. It makes sense to have an entry point in yeoman because many people already have it installed (or some of it's dependencies). There are drawbacks from shipping/using yeoman directly by Visual Studio include the following.

  • Yeoman has a lot of dependencies. This increases the download size, install time, and also introduces issues where each dependency needs to be reviewed to ensure we don't violate any licenses.
  • If a bug is found in a dependency it may take a while for that to get fixed, especially if it's a platform specific bug. For example https://github.com/OmniSharp/generator-aspnet/issues/351, which was opened in Sept 2015, and we are just now able to get the fix out.
  • Since the dependency graph for yeoman is deep there are issues when different versions of dependencies don't work together well. For example: https://github.com/OmniSharp/generator-aspnet/issues/582 and https://github.com/OmniSharp/generator-aspnet/issues/580
  • In addition to this, authoring templates can be made a lot easier than what's required with yeoman today.

sayedihashimi avatar Apr 07 '16 18:04 sayedihashimi

@sayedihashimi I like this approach, supporting existing solutions with a new abstraction (dotnet new) and making VS templates easier by providing a shim to dotnet new templates. Also agree Yeoman has too much baggage to be the official solution. Well documented and thought out!

rschiefer avatar Apr 08 '16 11:04 rschiefer

@sayedihashimi can you add some examples of invocation of this new dotnet new you imagined on the command line? I'm fine with VS, but would love to see how complex/simple this would be in the shell.

thesmithee avatar Apr 12 '16 19:04 thesmithee

@blackdwarf the specific user interaction is orthogonal to my ideas. What you have above should work with my proposal. Since there is an abstraction between the broker (dotnet new) and the template generator, you should be able to define the end user experience completely.

I'd like to make sure the local template story is good, so we may need to add more to what you have.

sayedihashimi avatar Apr 12 '16 22:04 sayedihashimi

@sayedihashimi ah, I see. When you say a "local template story", you mean templates that are installed with the SDK/other?

thesmithee avatar Apr 13 '16 01:04 thesmithee

@blackdwarf

@sayedihashimi ah, I see. When you say a "local template story", you mean templates that are installed with the SDK/other?

I should have said "local template dev story". Let me explain by example.

When developing templates in Visual Studio with SideWaffle/TemplateBuilder I can CTRL+F5 (or F5) to start the VS experimental instance with my template vsix loaded.

When developing templates with yeoman I can use npm link to point to my local copy of yo aspnet so that I can try the templates via yo aspnet during development.

When developing templates for dotnet new there should be a similar story. Requiring users to create a .nupkg to share their templates widely is fine (similar to yo with npm packages), but users shouldn't have to create templates from .nupkg files locally during template development.

I think what we have here is a good start but for a complete template story we need more details like.

  • What's the local template development story?
  • What's the process of shipping a security fix to a template (ideally publish a new nuget package, and flag old versions of the package as containing a security issue then dotnet new picks up the new version)?
  • If NuGet packages are used how do users point to specific NuGet servers for templates? Do we rely on the global nuget.config here or provide users the option of adding specific feeds for dotnet new?
  • How are replacements handled, and what specific features do we need for replacements? From my prototypes and previous experience I have a good sense for what's needed. I also have OSS code from @mlorbetske which we likely can directly use. He owns the dev side of One ASP.NET so he's very interested in this.
  • What specific command line features do we need to realize scenarios from the One ASP.NET dialog (for example displaying a select one list or select many list)?

sayedihashimi avatar Apr 13 '16 03:04 sayedihashimi

What about "dotnet new http://github/shanselman/whatever/something.git" where a repo has the template

shanselman avatar Jun 21 '16 13:06 shanselman

@shanselman Would that effectively be just git clone http://github/shanselman/whatever/something.git && rm .git?

svick avatar Jun 21 '16 14:06 svick

@svick I'm thinking it'd be more like the Uppercut project years back. You'd say dotnet new "foo" gitrepo and then {{classname}} would be replaced with foo. So a clone, then a global search and replace. Brainstorming.

shanselman avatar Jun 21 '16 15:06 shanselman

@shanselman @sayedihashimi's design actually allows for that, I think. Honestly, the more I think about it, the more it sounds like a good idea. But then why not go the full monty here and allow something like dotnet new http://tempuri.org/template/thing or similar for file shares? Brainstorming (I like this notation :))

thesmithee avatar Jun 21 '16 16:06 thesmithee

@shanselman we can directly use github zip link as url, instead of git repo so is a dumb zip (no deps on git or github). also easier to host in S3. The proposed format define arguments/parameters/questions, so can be used by gui or command line. That's nice (arguments) because it's not bound on a specific language.

@blackdwarf it's something someone is implementing now or can be up-for-grab from community? last time i think i read about was in progress. Maybe a simple iteration first (zip+url) because it can help a lot to show new developer (new of .netcore) how to do something. atm are embedded in dotnet new , so each new tempalte is waiting merge (i'd like to merge F# lib for example, it's confusing for new users)

enricosada avatar Jun 21 '16 16:06 enricosada

@enricosada at this point we are hashing out the design. Once design is in place, we will figure out the next level of details. :smiley:

But it is interesting to do the ZIP thing. However, do take into account that not all people use GH for hosting, that is, maybe someone would like to point this to a random git repo hosted somewhere/somehow and have it work.

thesmithee avatar Jun 21 '16 16:06 thesmithee

do take into account that not all people use GH for hosting, that is ..

@blackdwarf i was proposing zip from github repo, instead of git repo. I dont understand why we need to add git, because we dont need to manage versions or branch. Just download a zip, unzip, done. If someone doesnt want to use github, can just upload a zip somewhere. Really useful for bootstrap things for new users (i'd like to add http://suave.io template for example).

in v1 we can do unzip only of package. In v2 add arguments/replacements. etc. So it's working asap (i really love do have it :smile: )

enricosada avatar Jun 21 '16 20:06 enricosada