templating
templating copied to clipboard
Improved `dotnet new` ITEM templates context support
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:
- Template authors will have a possibility to create item templates knowing key information about the project: namespace, tfm, etc.
- 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)
- Users will need to specify less information about item templates (now namespace needs to be specified etc).
Requirements
- Create a component-based approach to evaluate project context for item templates.
- Implement a way to determine project context for CLI templates.
- Implement constraint allowing to restrict templates based on
ProjectCapabilities
. - Adapt
dotnet new
CLI to handle item templates- Show only applicable item templates in
dotnet new list
- Restrict non-applicable item templates for instantiation
- Provide a way to select the project to add item template to in case of ambiguity.
- Show only applicable item templates in
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
Also fixes:
- https://github.com/dotnet/templating/issues/338
The work is completed in RC1
@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 take a look at the in-repo docs here: https://github.com/dotnet/templating/blob/main/docs%2FBinding-and-project-context-evaluation.md
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?