-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Add path-specific GitHub Copilot instructions for .NET MAUI Templates #32112
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 7 commits
dd9a166
032560e
b1e5115
8955000
e81fd7a
221de0f
25df86b
4655b34
f5a6893
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
# Copilot instructions for working with .NET MAUI Templates | ||
# This file provides specific guidance when modifying template files in src/Templates/ | ||
|
||
# Apply these instructions only to files in the Templates directory | ||
applicability: | ||
paths: | ||
- 'src/Templates/**' | ||
|
||
instructions: | | ||
# Working with .NET MAUI Templates | ||
|
||
When modifying files in the `src/Templates/` directory, you must follow special template semantics and conventions to ensure the templates work correctly when users create new projects. | ||
|
||
## Template Conditional Compilation Directives | ||
|
||
Templates use special comment markers to control how preprocessor directives are processed during template instantiation: | ||
|
||
### Platform-Specific Directives (Build-Time) | ||
|
||
Platform-specific `#if` directives (like `#if WINDOWS`, `#if ANDROID`, `#if IOS`, `#if MACCATALYST`) must be wrapped with `//-:cnd:noEmit` and `//+:cnd:noEmit` markers: | ||
|
||
```csharp | ||
//-:cnd:noEmit | ||
#if WINDOWS | ||
// Windows-specific code | ||
#endif | ||
//+:cnd:noEmit | ||
``` | ||
|
||
**Why?** These markers tell the template engine to preserve these directives in the generated code exactly as-is, so they will be evaluated at compile-time when the user builds their project. | ||
|
||
**Examples:** | ||
```csharp | ||
//-:cnd:noEmit | ||
#if DEBUG | ||
builder.Logging.AddDebug(); | ||
#endif | ||
//+:cnd:noEmit | ||
|
||
//-:cnd:noEmit | ||
#if IOS || MACCATALYST | ||
handlers.AddHandler<CollectionView, CollectionViewHandler2>(); | ||
#endif | ||
//+:cnd:noEmit | ||
|
||
//-:cnd:noEmit | ||
#if WINDOWS | ||
Microsoft.Maui.Controls.Handlers.Items.CollectionViewHandler.Mapper.AppendToMapping( | ||
"KeyboardAccessibleCollectionView", | ||
(handler, view) => { /* ... */ }); | ||
#endif | ||
//+:cnd:noEmit | ||
``` | ||
|
||
### Template Parameter Directives (Template-Time) | ||
|
||
Template parameter directives (like `#if (IncludeSampleContent)`) do NOT use the `//-:cnd:noEmit` markers: | ||
|
||
```csharp | ||
#if (IncludeSampleContent) | ||
using CommunityToolkit.Maui; | ||
#endif | ||
``` | ||
|
||
**Why?** These directives are evaluated when the template is instantiated (when user runs `dotnet new maui`), not when the code is compiled. | ||
|
||
## Template Naming Conventions | ||
|
||
- Template project names use placeholders like `MauiApp._1` which get replaced with the user's actual project name | ||
- Namespaces follow the same pattern: `namespace MauiApp._1;` | ||
- These will be transformed to the user's chosen project name during template instantiation | ||
|
||
## Files to Exclude from Template Changes | ||
|
||
Never modify auto-generated files in templates: | ||
- `cgmanifest.json` - Auto-generated component governance manifest | ||
- `templatestrings.json` - Auto-generated localization file | ||
|
||
These files are regenerated during the build process and should not be manually edited. | ||
|
||
## Template Testing | ||
|
||
When making changes to templates: | ||
1. Build the template project: `dotnet build src/Templates/src/Microsoft.Maui.Templates.csproj` | ||
2. For comprehensive testing, use the `build.ps1` script in the Templates directory to pack, install, and test the template | ||
3. Verify the generated project compiles for all target platforms | ||
|
||
## Quick Reference | ||
|
||
| Directive Type | Wrapper Needed | Example | | ||
|---|---|---| | ||
| Platform-specific (`#if WINDOWS`, `#if ANDROID`, etc.) | ✅ Yes - use `//-:cnd:noEmit` | Build-time platform detection | | ||
| Debug mode (`#if DEBUG`) | ✅ Yes - use `//-:cnd:noEmit` | Build configuration | | ||
| Template parameters (`#if (IncludeSampleContent)`) | ❌ No | Template instantiation options | |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,14 +18,25 @@ public static MauiApp CreateMauiApp() | |
#if (IncludeSampleContent) | ||
.UseMauiCommunityToolkit() | ||
.ConfigureSyncfusionToolkit() | ||
//-:cnd:noEmit | ||
.ConfigureMauiHandlers(handlers => | ||
{ | ||
//-:cnd:noEmit | ||
#if IOS || MACCATALYST | ||
handlers.AddHandler<Microsoft.Maui.Controls.CollectionView, Microsoft.Maui.Controls.Handlers.Items2.CollectionViewHandler2>(); | ||
#if WINDOWS | ||
|
||
Microsoft.Maui.Controls.Handlers.Items.CollectionViewHandler.Mapper.AppendToMapping("KeyboardAccessibleCollectionView", (handler, view) => | ||
{ | ||
handler.PlatformView.SingleSelectionFollowsFocus = false; | ||
}); | ||
|
||
Microsoft.Maui.Handlers.ContentViewHandler.Mapper.AppendToMapping(nameof(Pages.Controls.CategoryChart), (handler, view) => | ||
{ | ||
if (view is Pages.Controls.CategoryChart && handler.PlatformView is ContentPanel contentPanel) | ||
{ | ||
contentPanel.IsTabStop = true; | ||
} | ||
}); | ||
#endif | ||
}) | ||
//+:cnd:noEmit | ||
}) | ||
#endif | ||
.ConfigureFonts(fonts => | ||
{ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot
follow the guidance here please for this file
https://docs.github.com/en/copilot/how-tos/configure-custom-instructions/add-repository-instructions#creating-path-specific-custom-instructions-2
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed path-specific custom instructions format following GitHub documentation. Changes:
templates.yml
totemplates.md
applies_to
fieldThe file now uses the correct format:
Commit 25df86b (will be new commit after push).