Home > database >  How to specify the files included in a dotnet template
How to specify the files included in a dotnet template

Time:11-02

I have a Visual Studio solution with which I am trying to make a template. I have a .template.config directory containing a template.json file and I also have a SolutionTemplate.nuspec. I am trying to make a solution template i.e. the template creates a solution containing multiple projects. The directory structure is:

- .git
- .template.config
    - template.json
- template_content
    - proj_one_dir
        - files
    - proj_two_dir
        - files
    - Template.sln
- SolutionTemplate.nuspec
- pipeline.yml

The sources section of template.json is:

{
    "sources":[{
    "modifiers": [{
      "exclude":[
        "**/[Bb]in/**",
        "**/[Oo]bj/**",
        ".template.config/**/*",
        "**/*.filelist",
        "**/*.user",
        "**/*.lock.json",
        ".git/**"
      ]
    }],
    "source": "./template_content",
    "target": "./"
  }]
}

As you can see, I'm trying to stop things like the git and template config directories from being included in the output.

The .nuspec file content is:

<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd">
  <metadata>
    <id>Our.Microservice.Template</id>
    <version>1.0.0</version>
    <description>
      Blah blah blah.
    </description>
    <authors>Me</authors>
    <license type="expression">Apache-2.0</license>
    <packageTypes>
      <packageType name="Template" />
    </packageTypes>
  </metadata>
</package>

If I run nuget.exe pack "SolutionTemplate.nuspec" then the final output when I use the template just includes everything. I've seen instructions that suggest to control the output in the .nuspec file e.g.

<files>
    <file   src="template_content\**\*.*" 
            exclude="**\bin\**\*.*;**\obj\**\*.*;**\*.nuspec;**\*.nupkg;**\*.suo;docs\**;.git\**;**\.gitignore;.vs\**;.vscode\**;" 
            />
</files> 

This does seem to work. But if that's the intended way to control which files are included/excluded, then what's the sources section in template.json for?

What's the correct way to pick and choose the files that end up as the template content?

CodePudding user response:

I've realised the answer on my own.

I hadn't really appreciated the fact that there's two stages to this process.

  1. Packing the files up into the Nuget package
  2. Using the template (in a way, extracting the content of the Nuget)

To control what goes into the Nuget package, we use the <files> section in the YourNuspecFile.nuspec.

To control how the files in the Nuget package are used to make the end result, we use the "sources": section of template.json.

I hadn't found any documentation anywhere that pointed this out clearly. It may be obvious to many, but I'd managed to conflate the two in my mind.

I also realised my json didn't make sense.

Here's the files section of my .nuspec file now:

<files>
    <file   src=".template.config\**\*.*"
            target=".template.config" />
    <file   src="docs\**\*.*"
            target="docs" 
            exclude="docs\dotnet_templates\**\*.*;docs\images\**\*.*;" />
    <file   src="src\**\*.*"
            target="src"
            exclude="**\bin\**\*.*;**\obj\**\*.*;**\*.nuspec;**\*.nupkg;**\*.suo;**\*.sln;" />
    <file   src="test\**\*.*"
            target="test"
            exclude="**\bin\**\*.*;**\obj\**\*.*;**\*.nuspec;**\*.nupkg;**\*.suo;**\*.sln;" />
    <file   src="README_FOR_TEMPLATE.md" 
            target="README.md" />
    <file   src="Templates.Microservice.Template.sln" 
            target="Templates.Microservice.sln" />
    <file   src=".gitignore" 
            target=".gitignore" />
</files> 

And here's the sources section of my `template.json':

{"sources":[{
    "source": "./docs/",
    "include": [
      "**/*"
    ],
    "target": "./docs/"
  },
  {
    "source": "./src/",
    "include": [
      "**/*"
    ],
    "target": "./src/"
  },
  {
    "source": "./test/",
    "include": [
      "**/*"
    ],
    "target": "./test/"
  },
  {
    "source": "./",
    "include": [
      "README.md",
      "Templates.Microservice.sln",
      ".gitignore"
    ],
    "target": "./"
  }]
}

As you can see, I've done some renames in the .nuspec file. But I am aware that these could also have been done in the template.json file (with a bit more learning).

  • Related