Skip to content

Commit 91c3cd4

Browse files
committed
added conditional processing for C# language features based on languageVersion / framework
1 parent 6875aa5 commit 91c3cd4

File tree

9 files changed

+523
-29
lines changed

9 files changed

+523
-29
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
{
2+
"$schema": "http://json.schemastore.org/template",
3+
"symbols": {
4+
"csharp10orlater": {
5+
"type": "generated",
6+
"generator": "regexMatch",
7+
"datatype": "bool",
8+
"parameters": {
9+
"pattern": "10.0|10|preview|latest|default|latestMajor",
10+
"source": "langVersion"
11+
}
12+
},
13+
"csharp9orlater": {
14+
"type": "generated",
15+
"generator": "regexMatch",
16+
"datatype": "bool",
17+
"parameters": {
18+
"pattern": "9|9.0|10.0|10|preview|latest|default|latestMajor",
19+
"source": "langVersion"
20+
}
21+
},
22+
"csharp8orlater": {
23+
"type": "generated",
24+
"generator": "regexMatch",
25+
"datatype": "bool",
26+
"parameters": {
27+
"pattern": "8|8.0|9|9.0|10.0|10|preview|latest|default|latestMajor",
28+
"source": "langVersion"
29+
}
30+
},
31+
"csharpFeature_GlobalUsings_Supported": {
32+
"type": "generated",
33+
"generator": "switch",
34+
"parameters": {
35+
"datatype": "bool",
36+
"cases": [
37+
{
38+
//supported only in net6.0
39+
"condition": "Framework != '' && Framework != 'net6.0'",
40+
"value": "false"
41+
},
42+
{
43+
//supported only in net6.0
44+
"condition": "TargetFrameworkOverride != '' && TargetFrameworkOverride != 'net6.0'",
45+
"value": "false"
46+
},
47+
//supported only for C# version 10.0
48+
{
49+
"condition": "csharp10orlater == 'true'",
50+
"value": "true"
51+
},
52+
{
53+
"condition": "true",
54+
"value": "false"
55+
}
56+
]
57+
}
58+
},
59+
"csharpFeature_FileScopedNamespaces_Supported": {
60+
"type": "generated",
61+
"generator": "switch",
62+
"parameters": {
63+
"datatype": "bool",
64+
"cases": [
65+
{
66+
//supported only in net6.0
67+
"condition": "Framework != '' && Framework != 'net6.0'",
68+
"value": "false"
69+
},
70+
{
71+
//supported only in net6.0
72+
"condition": "TargetFrameworkOverride != '' && TargetFrameworkOverride != 'net6.0'",
73+
"value": "false"
74+
},
75+
//supported only for C# version 10.0
76+
{
77+
"condition": "csharp10orlater == 'true'",
78+
"value": "true"
79+
}
80+
]
81+
}
82+
},
83+
"csharpFeature_Nullable_Supported": {
84+
"type": "generated",
85+
"generator": "switch",
86+
"parameters": {
87+
"datatype": "bool",
88+
"cases": [
89+
{
90+
//supported by default for net6.0, net5.0, netstandard2.1, netcoreapp3.1
91+
"condition": "(TargetFrameworkOverride == '' || TargetFrameworkOverride == 'net6.0' || TargetFrameworkOverride == 'net5.0'|| TargetFrameworkOverride == 'netstandard2.1' || TargetFrameworkOverride == 'netcoreapp3.1') && langVersion == ''",
92+
"value": "true"
93+
},
94+
{
95+
//supported not supported by default for other frameworks
96+
"condition": "TargetFrameworkOverride != '' && langVersion == ''",
97+
"value": "false"
98+
},
99+
{
100+
//supported not supported by default for netstandard2.0
101+
"condition": "Framework == 'netstandard2.0' && langVersion == ''",
102+
"value": "false"
103+
},
104+
//supported since C# 8.0
105+
{
106+
"condition": "csharp8orlater == 'true'",
107+
"value": "true"
108+
}
109+
]
110+
}
111+
}
112+
}
113+
}

template_feed/Microsoft.DotNet.Common.ProjectTemplates.6.0/content/ClassLibrary-CSharp/.template.config/template.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,5 +120,8 @@
120120
},
121121
"continueOnError": true
122122
}
123+
],
124+
"additionalConfigFiles": [
125+
"csharpFeatures.template.json"
123126
]
124127
}
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1-
namespace Company.ClassLibrary1
1+
#if (!csharpFeature_GlobalUsings_Supported)
2+
using System;
3+
4+
#endif
5+
6+
namespace Company.ClassLibrary1
27
{
38
public class Class1
49
{
10+
511
}
612
}

template_feed/Microsoft.DotNet.Common.ProjectTemplates.6.0/content/ClassLibrary-CSharp/Company.ClassLibrary1.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
<TargetFramework Condition="'$(TargetFrameworkOverride)' != ''">TargetFrameworkOverride</TargetFramework>
66
<RootNamespace Condition="'$(name)' != '$(name{-VALUE-FORMS-}safe_namespace)'">Company.ClassLibrary1</RootNamespace>
77
<LangVersion Condition="'$(langVersion)' != ''">$(ProjectLanguageVersion)</LangVersion>
8-
<Nullable Condition="('$(Framework)' != 'netstandard2.0')">enable</Nullable>
8+
<DisableImplicitNamespaceImports Condition="'$(csharpFeature_GlobalUsings_Supported)' != 'true'">true</DisableImplicitNamespaceImports>
9+
<Nullable Condition="'$(csharpFeature_Nullable_Supported)' == 'true'">enable</Nullable>
910
</PropertyGroup>
1011

1112
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
{
2+
"$schema": "http://json.schemastore.org/template",
3+
"symbols": {
4+
"csharp10orlater": {
5+
"type": "generated",
6+
"generator": "regexMatch",
7+
"datatype": "bool",
8+
"parameters": {
9+
"pattern": "10.0|10|preview|latest|default|latestMajor",
10+
"source": "langVersion"
11+
}
12+
},
13+
"csharp9orlater": {
14+
"type": "generated",
15+
"generator": "regexMatch",
16+
"datatype": "bool",
17+
"parameters": {
18+
"pattern": "9|9.0|10.0|10|preview|latest|default|latestMajor",
19+
"source": "langVersion"
20+
}
21+
},
22+
"csharp8orlater": {
23+
"type": "generated",
24+
"generator": "regexMatch",
25+
"datatype": "bool",
26+
"parameters": {
27+
"pattern": "8|8.0|9|9.0|10.0|10|preview|latest|default|latestMajor",
28+
"source": "langVersion"
29+
}
30+
},
31+
"csharpFeature_GlobalUsings_Supported": {
32+
"type": "generated",
33+
"generator": "switch",
34+
"parameters": {
35+
"datatype": "bool",
36+
"cases": [
37+
//no need to check Framework as the only possible value is 'net6.0'
38+
{
39+
//supported only in net6.0
40+
"condition": "TargetFrameworkOverride != '' && TargetFrameworkOverride != 'net6.0'",
41+
"value": "false"
42+
},
43+
//supported only for C# version 10.0
44+
{
45+
"condition": "csharp10orlater == 'true'",
46+
"value": "true"
47+
},
48+
{
49+
"condition": "true",
50+
"value": "false"
51+
}
52+
]
53+
}
54+
},
55+
"csharpFeature_Nullable_Supported": {
56+
"type": "generated",
57+
"generator": "switch",
58+
"parameters": {
59+
"datatype": "bool",
60+
"cases": [
61+
//no need to check Framework as the only possible value is 'net6.0'
62+
{
63+
//supported by default for net6.0, net5.0, netstandard2.1, netcoreapp3.1
64+
"condition": "(TargetFrameworkOverride == '' || TargetFrameworkOverride == 'net6.0' || TargetFrameworkOverride == 'net5.0'|| TargetFrameworkOverride == 'netstandard2.1' || TargetFrameworkOverride == 'netcoreapp3.1') && langVersion == ''",
65+
"value": "true"
66+
},
67+
{
68+
//supported not supported by default for other frameworks
69+
"condition": "TargetFrameworkOverride != '' && langVersion == ''",
70+
"value": "false"
71+
},
72+
//supported since C# 8.0
73+
{
74+
"condition": "csharp8orlater == 'true'",
75+
"value": "true"
76+
},
77+
{
78+
"condition": "true",
79+
"value": "false"
80+
}
81+
]
82+
}
83+
},
84+
"csharpFeature_TopLevelProgram_Supported": {
85+
"type": "generated",
86+
"generator": "switch",
87+
"parameters": {
88+
"datatype": "bool",
89+
"cases": [
90+
//no need to check Framework as the only possible value is 'net6.0'
91+
{
92+
//supported by default for net6.0, net5.0
93+
"condition": "(TargetFrameworkOverride == '' || TargetFrameworkOverride == 'net6.0' || TargetFrameworkOverride == 'net5.0') && langVersion == ''",
94+
"value": "true"
95+
},
96+
{
97+
//supported not supported by default for other frameworks
98+
"condition": "TargetFrameworkOverride != '' && langVersion == ''",
99+
"value": "false"
100+
},
101+
//supported since C# 9.0
102+
{
103+
"condition": "csharp9orlater == 'true'",
104+
"value": "true"
105+
},
106+
{
107+
"condition": "true",
108+
"value": "false"
109+
}
110+
]
111+
}
112+
}
113+
}
114+
}

template_feed/Microsoft.DotNet.Common.ProjectTemplates.6.0/content/ConsoleApplication-CSharp/.template.config/template.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,5 +96,8 @@
9696
},
9797
"continueOnError": true
9898
}
99+
],
100+
"additionalConfigFiles": [
101+
"csharpFeatures.template.json"
99102
]
100-
}
103+
}

template_feed/Microsoft.DotNet.Common.ProjectTemplates.6.0/content/ConsoleApplication-CSharp/Company.ConsoleApplication1.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
<OutputType>Exe</OutputType>
55
<TargetFramework Condition="'$(TargetFrameworkOverride)' == ''">net6.0</TargetFramework>
66
<TargetFramework Condition="'$(TargetFrameworkOverride)' != ''">TargetFrameworkOverride</TargetFramework>
7+
<RootNamespace Condition="'$(name)' != '$(name{-VALUE-FORMS-}safe_namespace)'">Company.ConsoleApplication1</RootNamespace>
78
<LangVersion Condition="'$(langVersion)' != ''">$(ProjectLanguageVersion)</LangVersion>
8-
<Nullable>enable</Nullable>
9+
<DisableImplicitNamespaceImports Condition="'$(csharpFeature_GlobalUsings_Supported)' != 'true'">true</DisableImplicitNamespaceImports>
10+
<Nullable Condition="'$(csharpFeature_Nullable_Supported)' == 'true'">enable</Nullable>
911
</PropertyGroup>
1012

1113
</Project>
Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,19 @@
1-
Console.WriteLine("Hello, World!");
1+
#if (!csharpFeature_GlobalUsings_Supported)
2+
using System;
3+
4+
#endif
5+
#if (!csharpFeature_TopLevelProgram_Supported)
6+
namespace Company.ConsoleApplication1
7+
{
8+
class Program
9+
{
10+
static void Main(string[] args)
11+
{
12+
Console.WriteLine("Hello, World!");
13+
}
14+
}
15+
}
16+
#else
17+
Console.WriteLine("Hello, World!");
218
// See https://aka.ms/new-console-template for more information
19+
#endif

0 commit comments

Comments
 (0)