Skip to content

Commit d5cea5a

Browse files
committed
Adds global usings to generated code
Adds a global usings file to the generated code. This reduces the amount of code needed in the generated files, making them more readable and maintainable.
1 parent b6bebbc commit d5cea5a

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

new-cli/GitVersion.Cli.Generator.Tests/SystemCommandlineGeneratorTests.cs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
using System.CommandLine;
1+
using System.CommandLine;
22
using GitVersion.Infrastructure;
33
using Microsoft.CodeAnalysis;
4+
using Microsoft.CodeAnalysis.CSharp;
45
using Microsoft.CodeAnalysis.CSharp.Testing;
56
using Microsoft.CodeAnalysis.Testing;
67

@@ -11,8 +12,6 @@ public class SystemCommandlineGeneratorTests
1112
/*language=cs*/
1213
private const string TestCommandSourceCode =
1314
"""
14-
using System.Threading;
15-
using System.Threading.Tasks;
1615
using GitVersion.Infrastructure;
1716
1817
namespace GitVersion.Commands;
@@ -32,6 +31,19 @@ public Task<int> InvokeAsync(TestCommandSettings settings, CancellationToken can
3231
}
3332
}
3433
34+
""";
35+
36+
/*language=cs*/
37+
private const string GlobalUsingsCode =
38+
"""
39+
// <autogenerated />
40+
global using global::System;
41+
global using global::System.Collections.Generic;
42+
global using global::System.IO;
43+
global using global::System.Linq;
44+
global using global::System.Net.Http;
45+
global using global::System.Threading;
46+
global using global::System.Threading.Tasks;
3547
""";
3648

3749
/*language=cs*/
@@ -40,8 +52,6 @@ public Task<int> InvokeAsync(TestCommandSettings settings, CancellationToken can
4052
{{Content.GeneratedHeader}}
4153
using System.CommandLine;
4254
using System.CommandLine.Binding;
43-
using System.Threading;
44-
using System.Threading.Tasks;
4555
4656
using GitVersion.Commands;
4757
@@ -105,9 +115,7 @@ public void RegisterTypes(IContainerRegistrar services)
105115
private const string ExpectedRootCommandImplText =
106116
$$"""
107117
{{Content.GeneratedHeader}}
108-
using System.Collections.Generic;
109118
using System.CommandLine;
110-
using System.Linq;
111119
112120
using GitVersion;
113121
namespace GitVersion.Generated;
@@ -147,6 +155,7 @@ public async Task ValidateGeneratedCommandImplementation()
147155
{
148156
Sources =
149157
{
158+
(generatorType, "GlobalUsings.cs", GlobalUsingsCode),
150159
(generatorType, "TestCommand.cs", TestCommandSourceCode)
151160
},
152161
GeneratedSources =

new-cli/GitVersion.Cli.Generator/Content.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ public static class Content
2020
{{{GeneratedHeader}}}
2121
using System.CommandLine;
2222
using System.CommandLine.Binding;
23-
using System.Threading;
24-
using System.Threading.Tasks;
2523
2624
using {{Model.CommandTypeNamespace}};
2725
{{- if Model.SettingsTypeNamespace != Model.CommandTypeNamespace }}
@@ -78,9 +76,7 @@ Task<int> Run(ParseResult parseResult, CancellationToken cancellationToken)
7876
/*language=cs*/
7977
public const string RootCommandImplContent = $$$"""
8078
{{{GeneratedHeader}}}
81-
using System.Collections.Generic;
8279
using System.CommandLine;
83-
using System.Linq;
8480
8581
using {{InfraNamespaceName}};
8682
namespace {{Namespace}};

0 commit comments

Comments
 (0)