Templating: Ignore file(s) option

Created on 17 Jan 2018  路  3Comments  路  Source: dotnet/templating

I would like a way to ignore files / folders when creating the template.

For example, when the template itself is added to source control there is a .git folder in the solution. When creating a new solution based on the template it also gets the contents of the template .git folder. This has the potential to cause some very strange effects.

Another example would be to exclude the .nuspec file when developing and installing the template locally (before package distribution).

Most helpful comment

Thanks for the info, works great!

Some minor syntax updates for anyone else wanting to use this solution:

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

All 3 comments

@brucewilkins - You can accomplish this using a custom "sources" top-level element in the template's template.json file. The "sources" section allows template authors to declare files to explicitly include, exclude, rename, or copy only (without any template processing).

If a template doesn't explicitly define any sources, one default source is implicitly defined for the template, with this configuration:

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

The default setup is generally a good starting point for template authors, and it can be modified without having to redefine the entire configuration. This is accomplished with a modifiers section under a source, like this:

{
...
  "sources": [
    "modifiers": [
      {
        "exclude": [
          "**/.git/**",
          "**/.nuspec"
        ]
      }
    ]
  ],
...
}

Using a modifiers section in a template source causes the source definition to be extended, instead of having pieces overridden. Using just the second sources configuration gives all the setup from the first configuration listed (the default), as well as the excludes listed in the modifiers in the second config.

Thanks for the info, works great!

Some minor syntax updates for anyone else wanting to use this solution:

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

Ah, sorry (and thanks), I didn't have that formatted quite correctly.

Was this page helpful?
0 / 5 - 0 ratings