Scott Hanselman

Trying out "dotnet new" template updates and csproj with VS2017

February 07, 2017 Comment on this post [28] Posted in Open Source | VS2017
Sponsored By

I updated my Visual Studio 2017 RC installation today. Here's the release notes. You just run "Visual Studio Installer" if you've already got a version installed and it updates. The updating processes reminds me a little of how Office 365 updates itself. It's not as scary as VS updates of the past. You can download the VS2017 RC at https://www.visualstudio.com and it works side by side with your existing installs. I haven't had any issues yet.

New Templating Engine for .NET Core CLI

It also added/updated a new .NET Core SDK. I am a fan of the command line "dotnet.exe" tooling and I've been pushing for improvements in that experience. A bunch of stuff landed in this update that I've been waiting for. Here's dotnet new:

C:\Users\scott\Desktop\poop> dotnet new
Template Instantiation Commands for .NET Core CLI.

Usage: dotnet new [arguments] [options]

Arguments:
template The template to instantiate.

Options:
-l|--list List templates containing the specified name.
-lang|--language Specifies the language of the template to create
-n|--name The name for the output being created. If no name is specified, the name of the current directory is used.
-o|--output Location to place the generated output.
-h|--help Displays help for this command.
-all|--show-all Shows all templates


Templates Short Name Language Tags
--------------------------------------------------------------------------------------
Console Application console [C#], F# Common/Console
Class library classlib [C#], F# Common/Library
Unit Test Project mstest [C#], F# Test/MSTest
xUnit Test Project xunit [C#], F# Test/xUnit
Empty ASP.NET Core Web Application web [C#] Web/Empty
MVC ASP.NET Core Web Application mvc [C#], F# Web/MVC
Web API ASP.NET Core Web Application webapi [C#] Web/WebAPI
Solution File sln Solution

Examples:
dotnet new mvc --auth None --framework netcoreapp1.0
dotnet new console --framework netcoreapp1.0
dotnet new --help

There is a whole new templating engine now. The code is here https://github.com/dotnet/templating and you can read about how to make your own templates or on the wiki.

I did a "dotnet new xunit" and it made the csproj file and a Unit Test. Here's what's inside the csproj:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp1.0</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0-preview-20170123-02" />
    <PackageReference Include="xunit" Version="2.2.0-beta5-build3474" />
    <PackageReference Include="xunit.runner.visualstudio" Version="2.2.0-beta5-build1225" />
  </ItemGroup>
</Project>

That's not too bad. Here's a a library with no references:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netstandard1.4</TargetFramework>
  </PropertyGroup>
</Project>

Note there's no GUIDs in the csproj. Sweet.

Remember also that there was talk that you wouldn't have to edit your csproj manually? Check this out:

C:\Users\scott\Desktop\poop\lib> dotnet add package Newtonsoft.Json
Microsoft (R) Build Engine version 15.1.545.13942
Copyright (C) Microsoft Corporation. All rights reserved.

Writing C:\Users\scott\AppData\Local\Temp\tmpBA1D.tmp
info : Adding PackageReference for package 'Newtonsoft.Json' into project 'C:\Users\scott\Desktop\poop\lib\lib.csproj'.
log : Restoring packages for C:\Users\scott\Desktop\poop\lib\lib.csproj...
info : GET https://api.nuget.org/v3-flatcontainer/newtonsoft.json/index.json
info : OK https://api.nuget.org/v3-flatcontainer/newtonsoft.json/index.json 1209ms
info : GET https://api.nuget.org/v3-flatcontainer/newtonsoft.json/9.0.1/newtonsoft.json.9.0.1.nupkg
info : OK https://api.nuget.org/v3-flatcontainer/newtonsoft.json/9.0.1/newtonsoft.json.9.0.1.nupkg 181ms
info : Package 'Newtonsoft.Json' is compatible with all the specified frameworks in project 'C:\Users\scott\Desktop\poop\lib\lib.csproj'.
info : PackageReference for package 'Newtonsoft.Json' version '9.0.1' added to file 'C:\Users\scott\Desktop\poop\lib\lib.csproj'.

Doing "dotnet add package foo.bar" automatically gets the package from NuGet and adds it to your csproj. Just like doing "Add NuGet Package" (or add reference) in Visual Studio. You don't even have to open or look at your csproj.

I'm going to keep digging into this. We're getting into a nice place where someone could easily make a custom template then "nuget in" that templates then "File | New | Your Company's Template" without needed yeoman, etc.

Please shared your feedback:

Also, be sure to check out the new and growing Docs site at https://docs.microsoft.com/en-us/dotnet


Sponsor: Track every change to your database. See who made changes, what they did, & why, with SQL Source Control. Get a full version history in your source control system. See how with Red Gate's SQL Source Control.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Hosting By
Hosted in an Azure App Service
February 09, 2017 3:20
offtopic, but why do you poop on the desktop?!
February 09, 2017 6:13
Are you talking about integrating https://github.com/ligershark/side-waffle into the VS product?
February 09, 2017 10:40
Hi Scott, that is very exciting. Will download the RC and play with it. Thanks again for a great Tech Summit here in South Africa. Don't be a stranger and come back soon ;)
February 09, 2017 12:11
Can some one give me some use cases for dotnet command line?

Is any one writing the code of their application at the command line? So what does being able to create a new project at the command line really get you? After all you'll have VS open in order to write the code so his remembering a ton of args really worth it for something that in all likelihood you'll do a lot less often than right code. If you are creating tutorials and things like that may be you'll use it a lot but if you are working on an app or product for your employer not so much.
February 09, 2017 12:40
This looks might cool, at my current place I've had to make an extension to 'automate' some of the processes we do to each project, whereas a template with NuGet would be ideal.

Can't wait for 2017 to be finished :)
February 09, 2017 12:46
@NickSchonning No, this is a brand new templating engine. SideWaffle made it easy to create Visual Studio Extension (VSIX) templates which only work with VS, were difficult to create and do not allow you to have optional features which you can turn on or off in a template.

@Peter 'dotnet new' is not just the CLI, it's a whole new templating engine which will be used by Visual Studio, VS Code, 'dotnet new', etc. to create new projects. It adds support for optional features in a template which you can turn on or off! In general, the command line is great for CI build machines or for doing something very quickly or repeatedly. 'dotnet new' will make it easy for someone to copy the way you created a project from a template if you were writing a blog post for example.

The line below shows how optional features work and is the coolest feature of 'dotnet new' in my opinion. For anyone intrerested, I'm working on MVC templates, you can read more here.

dotnet new mvc --auth None --framework netcoreapp1.0
February 09, 2017 14:34
@Peter These directions are identical to the directions you'd need to create a .NET Core project on Ubuntu Linux if you have rc4 of the tooling installed.

On a side note, it'd be nice if the dotnet command didn't have major changes in every release candidate. Release candidates usually mean major changes no longer happen.
February 09, 2017 15:12
Hi,
Why there aren any "Go To View" or "Create View" when you right click on action method in brand new VS2015 and VS2017? Do we have to create or find the view from Solution Explorer all the time?
February 09, 2017 15:20
Hi,

This sounds great, many times I have had to create a new project that was based off a style and structure set out by the company. Sometimes we would just copy and paste then refactor to rename things. With these new templates we could just script the whole thing passing in parameters. Great stuff, thanks Scott.
February 09, 2017 15:28
Great post!

I have one question and/or concern; you created new xnunit project with CLI tools, and it added some "visualstudio" runner package also. Why its adding anything IDE specific in this brave new cross platform world, with other IDEs like Code and Rider? I would vote against that ...
February 09, 2017 15:45
@Peter - a big use case for these is developers who are using Visual Studio Code - definitely on Mac and Linux, but also for those who are using VSC on Windows too.

Cheers
February 09, 2017 16:03
What is unclear is if there are plans to give an experience similar to Forge for F# projects. I see a template for adding a new solution file, but there is nothing immediately discoverable which allows me to add projects to that solution via the CLI. Has this workflow been thought through? Of course if the .sln file as actually in a format people understood this would be much less of an issue.

February 09, 2017 16:45
I wonder why you decided to drop Yeoman? I remember when this whole app.net core thing just started you advertised how we can use familiar tools as Yeoman, etc. But now seems that all this non-Microsoft staff is going to be abandoned.
February 09, 2017 16:58
Hi Scott

Thanks for a nice post as usual.
Did you try to deploy a project created by a new tooling to the azure appservice? When I am trying to do a usual git deploy approach I just receive an error that named csproj file is not deployable project and that is it.
Any idea how to fix this?
February 09, 2017 18:22
@Andrei Rînea - what kind of question is that?
February 09, 2017 19:10
@Tudor - Probably because of:
C:\Users\scott\Desktop\poop> dotnet new
LOL I do the same thing when trying things out ;-)
February 09, 2017 19:51
@Peter - Because some folks have seemingly decided that dropping to the lowest common denominator is the best approach. Why bother with convenient intuitive GUIs with recognizable icons and context sensitive menus we've all become familiar and efficient with since the days of DOS, when we can instead go back to memorizing all these commands and their thousands of arguments/parameters on an unforgiving command line? Besides, all the cool kids are doing it. :)

/gettingOldRant
February 09, 2017 20:44
@ScottHanselman

How exactly do I upgrade my installed version? I don't see a Visual Studio Installer on my system. What gives?
February 09, 2017 23:36
@ruslan we have not dropped yo aspnet. We are planning to update yo aspnet to call the Template Engine. This will enable us to keep the templates in sync across cli/yo aspnet/vs/vscode/etc. For more details see https://github.com/dotnet/cli/issues/2052#issuecomment-207031714
February 10, 2017 0:45
I, like many, will miss the project.json support going forward, but I will also welcome seeing the tooling solidify. I know this is the key issue my organization is concerned with.

Thank you, Scott.
February 11, 2017 7:00
For anyone that is wondering how to use solutions with the CLI tools check out the "dotnet sln" command. The workflow is like this:
dotnet new sln -n mysolution
dotnet new classlib -n mylib
dotnet sln add mylib/mylib.csproj

Try dotnet sln --help for more information.

Tested with version 1.0.0-rc4-004771
February 11, 2017 14:54
@R. Bemrose
Truth is that the first 3 RC's from the visual studio department are not really RC. These are beta.
Stop using it! Save time. Wait until the release + next update. Safe your self a headache.

@Abel: Complete reinstall.

@Sayed: Kudos! but its just a matter of time MS going to drop it. Just like Dynamic Data etc etc.


Still on VS2015 which is not bad !


February 12, 2017 0:29
1. The example shows "dotnet new mvc --auth None --framework netcoreapp1.0" but the options above don't list --auth or --framework as an option. Why not list everything that's available?

2. If the [arguments] for dotnet new is always the template name, why not call it [template name] instead?

3. For xunit the target framework is netcoreapp1.0, but for a library it's netstandard1.4... what?! I probably need to educate myself on the difference, but as a first impression, it raises eyebrows.

Otherwise it looks good. I would have liked something more descriptive than ItemGroup/PropertyGroup, but if I suppose we'll have to hold our noses. Being able to modify the file from the command line helps.
February 12, 2017 17:53
Hi All,
Does VS2017 RC co-exists nicely with VS2015, or should I continue to running it in a VM?

February 13, 2017 14:18
@Vitoc - you're right, I can't be bothered learning all these new commands when I have paid for VS2015/17 (I lease it on Azure) etc...

What's wrong with GUIDs as well?
February 14, 2017 7:15
Does it annoy anyone else that Microsoft are now in the habit of calling releases "Release Candidate" when they are, in fact, early Beta releases? A Release Candidate should be just that - A CANDIDATE FOR RELEASE - i.e. feature-complete and all known show-stopping bugs have been eliminated.
February 20, 2017 18:30
I look forward to the new template system if its easier to create templates. I have been playing with the original vstemplate system for the last month or so and find there are some gotchas, and not so good docs. I may have to check it out soon. I think I may have seen some talk about the new templates under the ligershark github org, when searching for old template stuff.
February 21, 2017 23:18
@AdamWright
"I look forward to the new template system if its easier to create templates. I have been playing with the original vstemplate system for the last month or so and find there are some gotchas, and not so good docs."
Yes, creating templates with the Template Engine is very easy. Much easier than using .vstemplate.

"I think I may have seen some talk about the new templates under the ligershark github org, when searching for old template stuff."
Yes. I'm the owner of SideWaffle (existing popular template pack in LigerShark repo). We (SideWaffle) will be migrating to this new template engine for new templates. I'm working with my team (at Microsoft) on enabling invoking the Template Engine from Visual Studio. When we do that you can author a template and light it up in the command line with dotnet new and VS with a custom wizard in a .vstemplate.

Comments are closed.

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.