dotnet / docs

This repository contains .NET Documentation.
https://learn.microsoft.com/dotnet
Creative Commons Attribution 4.0 International
4.26k stars 5.89k forks source link

When I use a template created after following this tutorial, the projects are always created in a /template subfolder and not where I ran the `dotnet new` command #23485

Closed StingyJack closed 3 years ago

StingyJack commented 3 years ago

the template package csproj file says that the projects go into the /working/templates folder as subfolders, so when I run dotnet new to use the template a /templates subfolder is created in the folder where I ran the command, and the project folders in that as subfolders.

I looked at a few of the examples in the https://github.com/dotnet/dotnet-template-samples/ repo but those dont look like this tutorial.

if I unzip the nuget package created it looks like this. image

If I install that nuget package by using dotnet new -i \<path to nupkg file\>, and then run the dotnet new \<alias\> command to create the project set from the template in the C:\temp\TemplateTests\MyNewProject\ folder, it executes without error but all of the projects are at C:\temp\TemplateTests\MyNewProject\templates instead of the C:\temp\TemplateTests\MyNewProject\ folder image

How do I avoid having the extra /template directory in the created project's path?


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

adegeo commented 3 years ago

Hi @StingyJack I'll run through the docs and see what happens. Which SDK are you using?

StingyJack commented 3 years ago

dotnet --version reports 5.0.201 and is using MSBuild version 16.9.0+57a23d249

The projects all target 3.1. This is the template packaging project's csproj and template json. Maybe there is a conflicting setting creating this but I went over it a few times.

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <PackageType>Template</PackageType>
    <!--Do not set a project version here as it will override the one specified when dotnet pack is executed -->
    <!--<PackageVersion>0.1.0</PackageVersion>-->
    <PackageId>Redacted.Template.ApiProjects</PackageId>
    <Title>Redacted templates for WebApi based service projects</Title>
    <Authors>Redacted</Authors>
    <Description>Redacted project templates to use when creating a new project set that uses Web API</Description>
    <PackageTags>dotnet-new;templates;redacted</PackageTags>
    <TargetFramework>netstandard2.0</TargetFramework>
    <IncludeContentInPack>true</IncludeContentInPack>
    <IncludeBuildOutput>false</IncludeBuildOutput>
    <ContentTargetFolders>content</ContentTargetFolders>
    <NoWarn>$(NoWarn);NU5128</NoWarn>
    <PackageOutputPath>.\Packaged</PackageOutputPath>
    <NoDefaultExcludes>true</NoDefaultExcludes> <!--  I added this to allow for including .editorconfig and .gitignore files, This settings presence didnt change the subfoldering-->
  </PropertyGroup>
  <ItemGroup>
    <Content Include="templates\**\*" Exclude="templates\**\bin\**;templates\**\obj\**" Link=".\"/>
    <Compile Remove="**\*" />
  </ItemGroup>
  <ItemGroup>
    <Content Include=".template.config\template.json" />
  </ItemGroup>
</Project>
{
  "$schema": "http://json.schemastore.org/template",
  "author": "Redacted",
  "classifications": [ "Redacted", "WebApi" ],
  "identity": "Redacted.Template.Api",
  "name": "Redacted project template for Web Api project types",
  "shortName": "redacted",
  "tags": {
    "language": "C#",
    "type": "project"
  },
  "sourceName": "$safeprojectname$"
}
adegeo commented 3 years ago

Hi! Sorry I was in the middle of appending my comment with more information after running through the template article. I was going to ask for the info you just provided. I'll check it out.

adegeo commented 3 years ago

How many ./template.config/template.json items do you have? I see that you have loose .sln and .md files in the template folder. Are you trying to do one giant single template that outputs all the individual folders+projects?

StingyJack commented 3 years ago

Just the one templateconfig/template.json referenced in the package csproj.

This first template is a "half stack" with DAL, WebApi, and a few other projects. When we onboard a new customer project this would be the starting point that takes care of most of the boilerplate and making sure that some the usual controls are in place. We have a few other variations with different project types, but this has to work first.

There are also item templates that will get built from the same sources. If I can't get this new template format to work, we revert to using the Export Template (previous style) project templates (for which there is little to no possible automation with VS in the toolchain).

adegeo commented 3 years ago

Ahhhhh OK that is your problem. You need to move that \.template.config\ folder into the templates directory. That folder should be at the level of your template content itself.

Take a look at https://docs.microsoft.com/en-us/dotnet/core/tutorials/cli-templates-create-project-template#create-the-template-config where it shows the print out of the folder structure for that single project template.

image

consoleasync is designated a template because it has the .template.config\ folder as a subfolder and the config file inside that. When the template is invoked, it lays out all the files contained in the consoleasync folder, including any subfolders and files (besides the .template.config\ folder). Since you had this up in working, it thinks that the template starts there and it sees that templates subfolder and

The way the tutorial designs the folder structure allows you to easily have multiple templates and to pack them as content easily. However, if you want only a single template, then that .template.config\ folder should be up 1 level from the screenshot, in the templates folder.

StingyJack commented 3 years ago

@adegeo - that was it. Since the tutorials didnt actually cover the multiple project packing, I had been basing the folder structure off of the multi project sample which has...

package.csproj
---.template.config
------template.json
---templates
------project1
------project2

... like I had been trying to do.

Thanks for the assistance and the extra set of eyes.

adegeo commented 3 years ago

Sure thing! It might be good in this article to showcase the folder structure with the two templates created in the previous tutorial steps.

StingyJack commented 3 years ago

... or get those samples fixed. =D

adegeo commented 3 years ago

I believe the template should be OK. If you take that 05-multi-project folder and put it in the templates folder for the tutorial project, everything works fine.