4

I'm trying to create a simple dotnet new template containing a 'default' .editorconfig and .gitconfig, which my team uses.

Unfortunately the .files will not be inclueded during dotnet pack.

Here's part of my .csproj:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <PackageType>Template</PackageType>
    <TargetFramework>net6.0</TargetFramework>
    
    <IncludeContentInPack>true</IncludeContentInPack>
    <IncludeBuildOutput>false</IncludeBuildOutput>
    <ContentTargetFolders>content</ContentTargetFolders>
  </PropertyGroup>

  <PropertyGroup>
    <Description>dotnet new templates</Description>
    <PackageTags>template dotnet console</PackageTags>
    <Version>1.0.0</Version>   
  </PropertyGroup>

  <ItemGroup>
    <Compile Remove="**\*" />
    <Content Include="templates\**\*" Exclude="templates\**\bin\**;templates\**\obj\**;templates\**\.vs\**" />
    <Content Include="templates\SolutionTemplate\.editorconfig;templates\SolutionTemplate\.gitignore" Pack="true" />
  </ItemGroup>

</Project>

Despite being explicitly added, the .editorconfig and .gitignore will be ignored. Has anyone experience with this?

TGlatzer
  • 5,815
  • 2
  • 25
  • 46

1 Answers1

9

There are multiple solutions, the easiest is adding the NoDefaultExcludes ("Prevents default exclusion of NuGet package files and files and folders starting with a dot, such as .svn and .gitignore.") to your csproj like this:

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <NoDefaultExcludes>true</NoDefaultExcludes>
        ...
    </PropertyGroup>
</Project>

Using this option the .editorconfig and .gitignore files will be included, but other files starting with a dot or package files might also be included.

I used another option to name the files in the template repository editorconfig and gitignore and rename the files to .editorconfig and .gitignore in the .template.config/template.json (which is executed during template installation) like this:

{
  ...
  "sources": [
    {
      "modifiers": [
        {
          "rename": {
            "editorconfig": ".editorconfig",
            "gitignore": ".gitignore"
          }
        }
      ]
    }
  ]
}
dotarj
  • 418
  • 2
  • 8