dotnet / dotnet-template-samples

Samples showing how to create templates using the Template Engine for dotnet new and Visual Studio

Home Page:https://docs.microsoft.com/dotnet/articles/core/tools/dotnet-new

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Multi project with solution file

sayedihashimi opened this issue · comments

Create a template which is https://github.com/dotnet/dotnet-template-samples/tree/master/05-multi-project plus a solution file.

Since the unit test is optional this will also show optional content in the .sln file.

@seancpeters can you post a sample .sln file which has a conditional project entry?

At this point, to create conditions in a solution file so that the .sln file remains syntactically correct requires setting up a custom operation configuration in the template.json file. But this PR dotnet/templating#979 will provide better built in support for .sln files. Once this is merged, it'll become easier.

Current Situation
This section explains how to setup conditions in .sln files which will work right now. Note that this will remain backwards compatible after the PR, but it's a bit more complicated and syntactically different.

A custom conditional setup must be configured in the template.json, the following can be copied as-is:

  "SpecialCustomOperations": {
    "*.sln" : {
      "Operations": [
        {
          "type": "conditional",
          "configuration": {
            "style": "line",
            "token": "#"
          }
        }
      ]
    }
  },

The key “SpecialCustomOperations” is a top-level key, and so should appear at the same level as things such as “identity” & “tags”.

This configuration defines the syntax of conditional operations in “*.sln” files. The style is “line” to indicate we’re defining line-comments (as opposed to block comments, such as “” comments). The token value of “#” indicates what the conditional keywords will be preceded with. Template engine conditional operations come in two styles, “regular” and ”actionable”. Both styles operate in the same manner in terms of being conditional. The difference is that the “actionable” ones get a comment stripped from each line of their content if the content is emitted (this is the same as the built-in conditional operations). The format of the operations will be:

  • Regular:
    o ##if
    o ##elseif, ##elif
    o ##else
    o ##endif
  • Actionable:
    o ###if
    o ###elseif, ###elif
    o ###else

With this configuration, a .sln file can be conditioned similarly to any other file in a template. The following demonstrates optionally including various projects in a solution:

###if (CreateSharedProject)
#Project("{0828A3AA-EEE2-47CD-A744-86FE389AFD95}") = "BlankApp", "BlankApp\BlankApp.shproj", "{222CF9AB-CAD6-4608-BE59-DE5D3ECDC464}"
#EndProject
###else
#Project("{222CF9AB-CAD6-4608-BE59-DE5D3ECDC464}") = "BlankApp", "BlankApp\BlankApp.csproj", "{1C9BC7AB-9807-4F5E-9B07-4E82B2571A3B}"
#EndProject
##endif

##if (CreateiOSProject)
Project("{222CF9AB-CAD6-4608-BE59-DE5D3ECDC464}") = "BlankApp.iOS", "iOS\BlankApp.iOS.csproj", "{DBB686D5-B2FB-45C9-9A7F-62522D96764A}"
EndProject
##endif

###if (CreateAndroidProject)
#Project("{222CF9AB-CAD6-4608-BE59-DE5D3ECDC464}") = "BlankApp.Android", "Android\BlankApp.Android.csproj", "{4E5F77BC-F200-4335-8663-16006DC548EC}"
#EndProject
##endif

###if (CreateUWPProject)
#Project("{222CF9AB-CAD6-4608-BE59-DE5D3ECDC464}") = "BlankApp.UWP", "UWP\BlankApp.UWP.csproj", "{512666F3-34A0-46F7-A869-AD681CC33A9F}"
#EndProject
##endif

Near Future
PR dotnet/templating#979 provides built in support for conditionals in .sln files. Thus, there is no need to setup a custom configuration in template.json files. The syntax of the conditional operations will be slightly different than using the custom configuration, they will be:

  • Regular:
    o #if
    o #elseif, #elif
    o #else
    o #endif
  • Actionable:
    o ##if
    o ##elseif, ##elif
    o ##else
    The difference from the custom config setup is that each of these has one fewer '#' symbols. Thus, using them in a .sln file is very similar to the above:
##if (CreateSharedProject)
#Project("{0828A3AA-EEE2-47CD-A744-86FE389AFD95}") = "BlankApp", "BlankApp\BlankApp.shproj", "{222CF9AB-CAD6-4608-BE59-DE5D3ECDC464}"
#EndProject
##else
#Project("{222CF9AB-CAD6-4608-BE59-DE5D3ECDC464}") = "BlankApp", "BlankApp\BlankApp.csproj", "{1C9BC7AB-9807-4F5E-9B07-4E82B2571A3B}"
#EndProject
#endif

#if (CreateiOSProject)
Project("{222CF9AB-CAD6-4608-BE59-DE5D3ECDC464}") = "BlankApp.iOS", "iOS\BlankApp.iOS.csproj", "{DBB686D5-B2FB-45C9-9A7F-62522D96764A}"
EndProject
#endif

##if (CreateAndroidProject)
#Project("{222CF9AB-CAD6-4608-BE59-DE5D3ECDC464}") = "BlankApp.Android", "Android\BlankApp.Android.csproj", "{4E5F77BC-F200-4335-8663-16006DC548EC}"
#EndProject
#endif

##if (CreateUWPProject)
#Project("{222CF9AB-CAD6-4608-BE59-DE5D3ECDC464}") = "BlankApp.UWP", "UWP\BlankApp.UWP.csproj", "{512666F3-34A0-46F7-A869-AD681CC33A9F}"
#EndProject
#endif

any update?

@a-patel The "Near Future" scenario from my previous post has been merged, and is available in 2.1.0-preview1 or newer builds. The syntax for .sln file comments shown in the above post is built in, and doesn't need to be configured in template.json

Can I create the 'Multi project with solution file' (ASP.Net Core 2.0) using SidewaffleCreator2017

My Directory structure is like:

|- data
|-design
|-help
|-src
>|-.sln (VS solution file)
>|-Libraries
>>|-LIB1.csproj
>>|-LIB2.csproj
>>|-LIB3.csproj

|-Presentation
>>|-PRE1.csproj
>>|-PRE2.csproj
>|-Tests
>>|-TEST1.csproj
>>|-TEST2.csproj
>|-Plugins
>>|-Plugin1.csproj
>>|-Plugin2.csproj

Yes it should work. Let us know if you run into any issues.

Is there a tutorial that takes you through how to create a solution template step by step? thanks

Is there a tutorial that takes you through how to create a solution template step by step? thanks

Not that I'm aware of.

cc @KathleenDollard

I managed to get my project template to create the folder structure I want and a .sln file ie:

|-src

|-WebApp

|-WebApp.csproj
WebApp.sln

It works as I wish using dotnet new, but I use the same template in vsix with sidewaffle and I end up with 2 .sln files and a folder structure nested one level deeper than I want, ie I get:

|-WebApp

|-src

|-WebApp
|-WebApp.csproj
|-WebApp.sln
WebApp.sln

I know this is a little off topic since not directly an issue with dotnet new template, but hoping someone here might have some tips. I would like to prevent VS/vsix/sidewaffle from creating a .sln file and instead use the one already included in the template. If anyone here knows how to do that I would greatly appreciate the help.

Hi all

We're basically trying to build a Service Template solution for .net core "micro" services.
I have tried your suggested approach in the sln file with the conditionals and it works ok when the template is executed. The two files in the if-block get excluded in the final sln file.

However, ideally we would want to open the solution to continue to refine our service template over time. But every time we make a change resulting in changes to the solution file, once we save using Visual Studio (currently 15.9.3), it will remove the if-tags. We usually notice and revert the changes before commit, but it's somewhat of a hassle.

Is there a work-around for this or are we not using it correctly? I have also tried with double-#, but it doesn't affect the outcome.

Example:

Project("{fb8acff2-d9c2-4af7-bb7c-3adb281d1acb}") = "Solution Items", "Solution Items", "{282809d3-ba53-4273-ad3e-0ab728be1389}"

	ProjectSection(SolutionItems) = preProject
		.gitignore = .gitignore
		artifact-ci.yml = artifact-ci.yml
		Nuget.config = Nuget.config
#if (includeTemplateConfig)
		.template.config\template.json = .template.config\template.json
		readme.template.md = readme.template.md
#endif 
		StyleCop.ruleset = StyleCop.ruleset
	EndProjectSection

EndProject