dotnet / dotnet/templating

Inline conditional processing

Open
#7,991 0 comments 3 reactions 0 assignees View on GitHub
triage:transfer-to-sdk
Dominant language
No language data
Stars
1.7k
Forks
399
Avg merge
3d 5h
Merged PRs (30d)
39

Description

### Is your feature request related to a problem? Please describe.

Hello,

I am migrating a project from T4 templates. I often meet inline conditionals like the following:
```
add(a<# if(cond) { #>, b<# } #>);
```

This is how it can be done with templates today. Looks simple.
```csharp
#if(cond)
add(a);
#else
add(a,b);
#endif
```

But this grows exponentially with every condition on the same line:
```csharp
#if(cond_b)
#if(cond_c)
add(a,b,c);
#else
add(a,b);
#endif
#else
#if(cond_c)
add(a,c);
#else
add(a);
#endif
#endif
```

With method chains, the colon often jumps lines. I'd like to somehow append the colon to the last line written.
```csharp
var array = list.Where(...)
#if(a)
.Select(...)
.Where(...);
#else
.Select(...);
#endif
```

### Describe the solution you'd like.

Best case -- conditionals similar to T4 templates:

```csharp
add(a/*#if(cond)*/, b/*#endif*/)
```

A workaround solution could be made throguh a Multi-line Regex Replace Post Action (json copied from #6187):

```csharp
add(a
#if (cond)
/*concat*/, b
#endif
/*concat*/)
#if(...)
.chain()
#endline
/*concat*/;
```

```json
"postActions": [{
"condition": "(useCentralPackageManagement)",
"description": "Removes newlines marked by /*concat*/ comment.",
"args": {
"files": "**\\*.cs",
"pattern": "\\n/\\*concat\\*/",
"replace": ""
},
"manualInstructions": [{
"text": "Concat lines marked by /*concat*/ comment."
}],
"actionId": "Some Id",
"continueOnError": false
}]
```

### Additional context

_No response_

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.