templating icon indicating copy to clipboard operation
templating copied to clipboard

Improved `dotnet new` ITEM templates context support

Open bekir-ozturk opened this issue 3 years ago • 1 comments

Background

This is about better item templates context support in dotnet new (evaluate MSBuild properties and namespace). Allows to get details of the project item template should be added to. Details are:

  • namespace,
  • MSBuild properties. Limited to C# projects only.

Problem Summary

Item templates are limited now due to following reason:

No project context information when creating item templates

Though some item templates exist at the moment, they are barely usable as they are missing the context of the project they are being adding to. As the result, the simplest item template for class still cannot be implement as it is not possible to evaluate project namespace.

Design thoughts

  • introduce component-based way to evaluate project and its context with separate implementation for CLI and Visual Studio
  • supplement CLI to evaluate project context and feed the MS Build properties in addition to Visual Studio

Justification

  • ITEM templates relevance depends on context.
  • Restricting listing ITEM templates as available templates for given project context and being run if project context restrictions are not met.

Customer impact:

  1. Template authors will have a possibility to create item templates knowing key information about the project: namespace, tfm, etc.
  2. In addition to above, template authors will have a possibility to restrict item templates to be run only in certain type of projects (using project capability MSBuild property + constraints)
  3. Users will need to specify less information about item templates (now namespace needs to be specified etc).

Requirements

  1. Create a component-based approach to evaluate project context for item templates.
  2. Implement a way to determine project context for CLI templates.
  3. Implement constraint allowing to restrict templates based on ProjectCapabilities.
  4. Adapt dotnet new CLI to handle item templates
    1. Show only applicable item templates in dotnet new list
    2. Restrict non-applicable item templates for instantiation
    3. Provide a way to select the project to add item template to in case of ambiguity.

Prerequisite to...

  • [ ] https://github.com/dotnet/templating/issues/1713
  • [ ] https://github.com/dotnet/test-templates/issues/169

Also fixes

  • https://github.com/dotnet/templating/issues/338

Subtasks

  • [x] PoC and design
  • [x] implement project context find and MSBuild evaluation
  • [x] implement constraint based on MSBuild ProjectCapability item
  • [x] implement a way to pass through MSBuild properties as symbols
  • [x] add a way to force using of the prefix to the source
  • [ ] pass-through output location - via dotnet/sdk
  • [x] implement an option to specify the project in case of multiple options (?) - via dotnet/sdk
  • [x] expression evaluation for capabilities
  • [ ] cleanup localization

bekir-ozturk avatar Sep 01 '21 11:09 bekir-ozturk

Also fixes:

  • https://github.com/dotnet/templating/issues/338

donJoseLuis avatar Apr 05 '22 20:04 donJoseLuis

The work is completed in RC1

vlada-shubina avatar Aug 15 '22 16:08 vlada-shubina

@vlada-shubina where are the docs for this? Been looking about these changes for days and I can't seem to find any references as the docs I can find are all messy and dispersed.

CesarD avatar Mar 05 '24 01:03 CesarD

@CesarD take a look at the in-repo docs here: https://github.com/dotnet/templating/blob/main/docs%2FBinding-and-project-context-evaluation.md

baronfel avatar Mar 05 '24 01:03 baronfel

Thanks @baronfel. So for the solution name it's only possible to retrieve it if running the item template from Visual Studio? There's no chance to get the solution name from the dotnet new CLI?

CesarD avatar Mar 05 '24 09:03 CesarD