Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multi Project Template folder structure incorrect

So i am trying to build a Multi Project template and when i set it up the folder structure is coming out incorrectly (Not how microsoft does it when creating projects) and it's messing things up like the Packages folder and References folder.

This is current Structure:

Solution Folder
-Solution File
-Folder (Solution Name)
 --Packages
 --References
 --Project1 Folder
 --Project2 Folder

I am wanting it to have the same structure that .NET does automatically:

Solution Folder
-Solution File
-References Folder
-Packages Folder
-Project1 Folder
-Project2 Folder

Here is my vstemplate:

<VSTemplate Version="3.0.0" xmlns="http://schemas.microsoft.com/developer/vstemplate/2005" Type="ProjectGroup">
  <TemplateData>
    <Name>ASP Solution Template</Name>
    <Description>This is the Solution Template for ASP Applications</Description>
    <Icon>__TemplateIcon.ico</Icon>
    <ProjectType>CSharp</ProjectType>
  </TemplateData>
  <TemplateContent BuildOnLoad="true">
    <ProjectCollection>
      <SolutionFolder Name="References">
      </SolutionFolder>
      <SolutionFolder Name="packages">
      </SolutionFolder>
      <ProjectTemplateLink ProjectName="$safeprojectname$">
        ASPTemplate\MyTemplate.vstemplate
      </ProjectTemplateLink>
      <ProjectTemplateLink ProjectName="$safeprojectname$.ClassLibrary">
        ClassLibrary\MyTemplate.vstemplate
      </ProjectTemplateLink>
    </ProjectCollection>
  </TemplateContent>
</VSTemplate>
like image 992
Andy Xufuris Avatar asked Oct 04 '22 09:10

Andy Xufuris


2 Answers

Take a look at dotnet new custom templates. I had the same problem and this was a nice easy solution.

https://docs.microsoft.com/en-us/dotnet/core/tutorials/create-custom-template

  • Create your solution and put your files and/or projects where you like, I'll call the solution MySolution
  • Add folders, projects, whatever you like
  • Create a folder at the top level by the solution called .template.config
  • Create a json file in that .template.config called template.json

The template.json attributes can be found here https://docs.microsoft.com/en-us/dotnet/core/tools/custom-templates

Example template.json:

{
    "$schema": "http://json.schemastore.org/template",
    "author": "Your goodself",
    "classifications": [ "C#", "Awesome", "etc" ],
    "identity": "MySolution",
    "name": "Empty MySolution",
    "shortName": "mysolution",
    "sourceName": "MySolution",
    "tags": {
        "language": "C#"
    },
}

sourceName is important, that is used to name your project new project and rename everything.

To see what templates you already have do:

dotnet new -l

To add your new template the above do:

dotnet new -i <fully qualified solution folder>

eg. dotnet new -i c:\dev\MySolution

You should then see your name in a:

dotnet new -l

That's it, ready to use!

Go to a fresh folder and type:

dotnet new mysolution -n MySolutionFromTemplate

You will then see all the folders, files and everything named as expected


Finally if you want to delete the template do:

dotnet new -u c:\dev\MySolution

NOTE: You will probably need this in your nuget repo. There are instructions how to do this here https://docs.microsoft.com/en-us/dotnet/core/tutorials/create-custom-template

like image 81
ozzy Avatar answered Oct 05 '22 23:10

ozzy


I don't think it's possible to add solution folders in the parent solution folder with vstemplates.

However, you could try adding a wizard to your template that enables you to run custom code when a user creates a project from a template.

Follow the instructions here and here, but basically you do something like this:

  1. Implement the IWizard interface in a ClassLibrary-project and use EnvDTE80 to create the folders:

    public class MyWizard : IWizard
    {
        public void RunStarted(object automationObject, Dictionary<string, string> replacementsDictionary, WizardRunKind runKind, object[] customParams)
        {
            // Pseudo-code
            var dte = (DTE2)automationObject;
            var solution = (Solution2)dte.Solution;
    
            solution.AddSolutionFolder("References");
        }
    
        // Default implementations of IWizard here (return true's and do nothing's)
    }
    
  2. Modify your vstemplate to use the wizard:

    <VSTemplate Version="3.0.0" xmlns="http://schemas.microsoft.com/developer/vstemplate/2005" Type="ProjectGroup">
      <TemplateData />
      <TemplateContent BuildOnLoad="true" /> <!-- Remove the SolutionFolder elements -->
    
      <WizardExtension>
        <Assembly>CustomWizard, Version=1.0.0.0, Culture=Neutral, PublicKeyToken=fa3902f409bb6a3b</Assembly>
        <FullClassName>CustomWizard.MyWizard</FullClassName>
      </WizardExtension>
    
    </VSTemplate>
    

Then the code should be running when you create a project with your new template.

Hope this helps you somehow.

like image 37
Mario S Avatar answered Oct 05 '22 23:10

Mario S