Skip to content

Commit 8955000

Browse files
CopilotPureWeen
andcommitted
Add template semantic markers for platform-specific directives and document template conventions
Co-authored-by: PureWeen <5375137+PureWeen@users.noreply.github.com>
1 parent b1e5115 commit 8955000

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed

.github/copilot-instructions.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,92 @@ All PRs are required to have this at the top of the description:
266266

267267
Always put that at the top, without the block quotes. Without it, the users will NOT be able to try the PR and your work will have been in vain!
268268

269+
## Working with Templates
270+
271+
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.
272+
273+
### Template Conditional Compilation Directives
274+
275+
Templates use special comment markers to control how preprocessor directives are processed during template instantiation:
276+
277+
#### Platform-Specific Directives (Build-Time)
278+
279+
Platform-specific `#if` directives (like `#if WINDOWS`, `#if ANDROID`, `#if IOS`, `#if MACCATALYST`) must be wrapped with `//-:cnd:noEmit` and `//+:cnd:noEmit` markers:
280+
281+
```csharp
282+
//-:cnd:noEmit
283+
#if WINDOWS
284+
// Windows-specific code
285+
#endif
286+
//+:cnd:noEmit
287+
```
288+
289+
**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.
290+
291+
**Examples:**
292+
```csharp
293+
//-:cnd:noEmit
294+
#if DEBUG
295+
builder.Logging.AddDebug();
296+
#endif
297+
//+:cnd:noEmit
298+
299+
//-:cnd:noEmit
300+
#if IOS || MACCATALYST
301+
handlers.AddHandler<CollectionView, CollectionViewHandler2>();
302+
#endif
303+
//+:cnd:noEmit
304+
305+
//-:cnd:noEmit
306+
#if WINDOWS
307+
Microsoft.Maui.Controls.Handlers.Items.CollectionViewHandler.Mapper.AppendToMapping(
308+
"KeyboardAccessibleCollectionView",
309+
(handler, view) => { /* ... */ });
310+
#endif
311+
//+:cnd:noEmit
312+
```
313+
314+
#### Template Parameter Directives (Template-Time)
315+
316+
Template parameter directives (like `#if (IncludeSampleContent)`) do NOT use the `//-:cnd:noEmit` markers:
317+
318+
```csharp
319+
#if (IncludeSampleContent)
320+
using CommunityToolkit.Maui;
321+
#endif
322+
```
323+
324+
**Why?** These directives are evaluated when the template is instantiated (when user runs `dotnet new maui`), not when the code is compiled.
325+
326+
### Template Naming Conventions
327+
328+
- Template project names use placeholders like `MauiApp._1` which get replaced with the user's actual project name
329+
- Namespaces follow the same pattern: `namespace MauiApp._1;`
330+
- These will be transformed to the user's chosen project name during template instantiation
331+
332+
### Files to Exclude from Template Changes
333+
334+
Never modify auto-generated files in templates:
335+
- `cgmanifest.json` - Auto-generated component governance manifest
336+
- `templatestrings.json` - Auto-generated localization file
337+
338+
These files are regenerated during the build process and should not be manually edited.
339+
340+
### Template Testing
341+
342+
When making changes to templates:
343+
1. Build the template project: `dotnet build src/Templates/src/Microsoft.Maui.Templates.csproj`
344+
2. For comprehensive testing, use the `build.ps1` script in the Templates directory to pack, install, and test the template
345+
3. Verify the generated project compiles for all target platforms
346+
347+
### Quick Reference
348+
349+
| Directive Type | Wrapper Needed | Example |
350+
|---|---|---|
351+
| Platform-specific (`#if WINDOWS`, `#if ANDROID`, etc.) | ✅ Yes - use `//-:cnd:noEmit` | Build-time platform detection |
352+
| Debug mode (`#if DEBUG`) | ✅ Yes - use `//-:cnd:noEmit` | Build configuration |
353+
| Template parameters (`#if (IncludeSampleContent)`) | ❌ No | Template instantiation options |
354+
269355
## Additional Resources
270356

271357
- [Development Guide](.github/DEVELOPMENT.md)

src/Templates/src/templates/maui-mobile/MauiProgram.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public static MauiApp CreateMauiApp()
2121
#endif
2222
.ConfigureMauiHandlers(handlers =>
2323
{
24+
//-:cnd:noEmit
2425
#if WINDOWS
2526
Microsoft.Maui.Controls.Handlers.Items.CollectionViewHandler.Mapper.AppendToMapping("KeyboardAccessibleCollectionView", (handler, view) =>
2627
{
@@ -35,6 +36,7 @@ public static MauiApp CreateMauiApp()
3536
}
3637
});
3738
#endif
39+
//+:cnd:noEmit
3840
//-:cnd:noEmit
3941
#if IOS || MACCATALYST
4042
handlers.AddHandler<Microsoft.Maui.Controls.CollectionView, Microsoft.Maui.Controls.Handlers.Items2.CollectionViewHandler2>();

0 commit comments

Comments
 (0)