Skip to content

Commit 329d788

Browse files
committed
fix: improve when UsingCodingRules is false for usings and nuget packages
1 parent b473e6c commit 329d788

File tree

17 files changed

+202
-67
lines changed

17 files changed

+202
-67
lines changed

src/Atc.Rest.ApiGenerator.Framework.Minimal/ProjectGenerator/ServerApiGenerator.cs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ public ServerApiGenerator(
4141
$"\"{ContentWriterConstants.ApiGeneratorName}\", \"{settings.Version}\"");
4242
}
4343

44-
public async Task ScaffoldProjectFile()
44+
public async Task ScaffoldProjectFile(
45+
bool usingCodingRules)
4546
{
4647
var packageReferences = await nugetPackageReferenceProvider.GetPackageReferencesForApiProjectForMinimalApi();
4748

@@ -67,7 +68,12 @@ public async Task ScaffoldProjectFile()
6768
],
6869
[
6970
new("DocumentationFile", Attributes: null, @$"bin\Debug\net9.0\{settings.ProjectName}.xml"),
70-
new("NoWarn", Attributes: null, "$(NoWarn);1573;1591;1701;1702;1712;8618;"),
71+
new(
72+
"NoWarn",
73+
Attributes: null,
74+
usingCodingRules
75+
? "$(NoWarn);1573;1591;1701;1702;1712;8618;"
76+
: "$(NoWarn);1573;1591;1701;1702;1712;8618;8632;"),
7177
],
7278
],
7379
[
@@ -326,7 +332,8 @@ public void MaintainApiSpecification(
326332
settings.ProjectPath);
327333

328334
public void MaintainGlobalUsings(
329-
bool removeNamespaceGroupSeparatorInGlobalUsings)
335+
bool removeNamespaceGroupSeparatorInGlobalUsings,
336+
bool usingCodingRules)
330337
{
331338
var requiredUsings = new List<string>
332339
{
@@ -339,6 +346,15 @@ public void MaintainGlobalUsings(
339346
"Microsoft.AspNetCore.Mvc",
340347
};
341348

349+
if (!usingCodingRules)
350+
{
351+
requiredUsings.Add("System");
352+
requiredUsings.Add("System.Collections.Generic");
353+
requiredUsings.Add("System.Linq");
354+
requiredUsings.Add("System.Threading");
355+
requiredUsings.Add("System.Threading.Tasks");
356+
}
357+
342358
if (openApiDocument.IsUsingRequiredForSystemTextJsonSerializationAndSystemRuntimeSerialization(settings.IncludeDeprecatedOperations))
343359
{
344360
requiredUsings.Add("System.Runtime.Serialization");

src/Atc.Rest.ApiGenerator.Framework.Minimal/ProjectGenerator/ServerDomainGenerator.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,8 @@ public void GenerateServiceCollectionEndpointHandlerExtensions()
248248
}
249249

250250
public void MaintainGlobalUsings(
251-
bool removeNamespaceGroupSeparatorInGlobalUsings)
251+
bool removeNamespaceGroupSeparatorInGlobalUsings,
252+
bool usingCodingRules)
252253
{
253254
var requiredUsings = new List<string>
254255
{
@@ -258,6 +259,13 @@ public void MaintainGlobalUsings(
258259
"Microsoft.Extensions.DependencyInjection",
259260
};
260261

262+
if (!usingCodingRules)
263+
{
264+
requiredUsings.Add("System");
265+
requiredUsings.Add("System.Threading");
266+
requiredUsings.Add("System.Threading.Tasks");
267+
}
268+
261269
var apiGroupNames = openApiDocument.GetApiGroupNames();
262270

263271
requiredUsings.AddRange(apiGroupNames.Select(x => NamespaceFactory.Create(apiProjectName, NamespaceFactory.CreateWithApiGroupName(x, settings.ContractsNamespace))));

src/Atc.Rest.ApiGenerator.Framework.Minimal/ProjectGenerator/ServerHostGenerator.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,8 @@ public void GenerateConfigureSwaggerDocOptions()
236236
}
237237

238238
public void MaintainGlobalUsings(
239-
bool removeNamespaceGroupSeparatorInGlobalUsings)
239+
bool removeNamespaceGroupSeparatorInGlobalUsings,
240+
bool usingCodingRules)
240241
{
241242
var requiredUsings = new List<string>
242243
{
@@ -263,6 +264,17 @@ public void MaintainGlobalUsings(
263264
$"{domainProjectName}.Extensions",
264265
};
265266

267+
if (!usingCodingRules)
268+
{
269+
requiredUsings.Add("System");
270+
requiredUsings.Add("System.IO");
271+
requiredUsings.Add("Microsoft.AspNetCore.Builder");
272+
requiredUsings.Add("Microsoft.AspNetCore.Hosting");
273+
requiredUsings.Add("Microsoft.Extensions.Hosting");
274+
requiredUsings.Add("Microsoft.Extensions.Configuration");
275+
requiredUsings.Add("Microsoft.Extensions.DependencyInjection");
276+
}
277+
266278
GlobalUsingsHelper.CreateOrUpdate(
267279
logger,
268280
ContentWriterArea.Src,

src/Atc.Rest.ApiGenerator.Framework.Minimal/ProjectGenerator/ServerHostTestGenerator.cs

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,11 @@ public ServerHostTestGenerator(
4848
.CreateGeneratedCode(settings.Version);
4949
}
5050

51-
public async Task ScaffoldProjectFile()
51+
public async Task ScaffoldProjectFile(
52+
bool usingCodingRules)
5253
{
53-
var packageReferences = await nugetPackageReferenceProvider.GetPackageReferencesForTestHostProjectForMinimalApi();
54+
var packageReferences = await nugetPackageReferenceProvider.GetPackageReferencesForTestHostProjectForMinimalApi(
55+
usingCodingRules);
5456

5557
var itemGroupPackageReferences = packageReferences
5658
.Select(packageReference => new ItemGroupParameter(
@@ -70,7 +72,12 @@ public async Task ScaffoldProjectFile()
7072
],
7173
[
7274
new("DocumentationFile", Attributes: null, @$"bin\Debug\net9.0\{settings.ProjectName}.xml"),
73-
new("NoWarn", Attributes: null, "$(NoWarn);1573;1591;1701;1702;1712;8618;NU1603;NU1608;"),
75+
new(
76+
"NoWarn",
77+
Attributes: null,
78+
usingCodingRules
79+
? "$(NoWarn);1573;1591;1701;1702;1712;8618;NU1603;NU1608;"
80+
: "$(NoWarn);1573;1591;1701;1702;1712;8618;8632;NU1603;NU1608;"),
7481
],
7582
],
7683
[
@@ -252,14 +259,24 @@ public void GenerateEndpointTests()
252259
}
253260

254261
public void MaintainGlobalUsings(
255-
bool usingCodingRules,
256-
bool removeNamespaceGroupSeparatorInGlobalUsings)
262+
bool removeNamespaceGroupSeparatorInGlobalUsings,
263+
bool usingCodingRules)
257264
{
258265
var requiredUsings = new List<string>
259-
{
260-
"System.CodeDom.Compiler",
261-
"AutoFixture",
262-
};
266+
{
267+
"System.CodeDom.Compiler",
268+
"AutoFixture",
269+
};
270+
271+
if (!usingCodingRules)
272+
{
273+
requiredUsings.Add("System");
274+
requiredUsings.Add("System.Threading");
275+
requiredUsings.Add("System.Threading.Tasks");
276+
requiredUsings.Add("System.Collections.Generic");
277+
requiredUsings.Add("System.IO");
278+
requiredUsings.Add("System.Net.Http");
279+
}
263280

264281
//// TODO: Maybe some is needed?
265282
////if (false)

src/Atc.Rest.ApiGenerator.Framework.Mvc/ProjectGenerator/ServerApiGenerator.cs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ public ServerApiGenerator(
4141
.CreateGeneratedCode(settings.Version);
4242
}
4343

44-
public async Task ScaffoldProjectFile()
44+
public async Task ScaffoldProjectFile(
45+
bool usingCodingRules)
4546
{
4647
var packageReferences = await nugetPackageReferenceProvider.GetPackageReferencesForApiProjectForMvc();
4748

@@ -67,7 +68,12 @@ public async Task ScaffoldProjectFile()
6768
],
6869
[
6970
new("DocumentationFile", Attributes: null, @$"bin\Debug\net9.0\{settings.ProjectName}.xml"),
70-
new("NoWarn", Attributes: null, "$(NoWarn);1573;1591;1701;1702;1712;8618;"),
71+
new(
72+
"NoWarn",
73+
Attributes: null,
74+
usingCodingRules
75+
? "$(NoWarn);1573;1591;1701;1702;1712;8618;"
76+
: "$(NoWarn);1573;1591;1701;1702;1712;8618;8632;"),
7177
],
7278
],
7379
[
@@ -342,7 +348,8 @@ public void MaintainApiSpecification(
342348
settings.ProjectPath);
343349

344350
public void MaintainGlobalUsings(
345-
bool removeNamespaceGroupSeparatorInGlobalUsings)
351+
bool removeNamespaceGroupSeparatorInGlobalUsings,
352+
bool usingCodingRules)
346353
{
347354
var requiredUsings = new List<string>
348355
{
@@ -353,6 +360,15 @@ public void MaintainGlobalUsings(
353360
"Atc.Rest.Results",
354361
};
355362

363+
if (!usingCodingRules)
364+
{
365+
requiredUsings.Add("System");
366+
requiredUsings.Add("System.Collections.Generic");
367+
requiredUsings.Add("System.Linq");
368+
requiredUsings.Add("System.Threading");
369+
requiredUsings.Add("System.Threading.Tasks");
370+
}
371+
356372
if (openApiDocument.IsUsingRequiredForSystemNet(settings.IncludeDeprecatedOperations))
357373
{
358374
requiredUsings.Add("System.Net");

src/Atc.Rest.ApiGenerator.Framework.Mvc/ProjectGenerator/ServerDomainGenerator.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,13 +150,21 @@ public void GenerateServiceCollectionEndpointHandlerExtensions()
150150
=> throw new NotSupportedException($"{nameof(GenerateServiceCollectionEndpointHandlerExtensions)} is not supported for MVC");
151151

152152
public void MaintainGlobalUsings(
153-
bool removeNamespaceGroupSeparatorInGlobalUsings)
153+
bool removeNamespaceGroupSeparatorInGlobalUsings,
154+
bool usingCodingRules)
154155
{
155156
var requiredUsings = new List<string>
156157
{
157158
"System.CodeDom.Compiler",
158159
};
159160

161+
if (!usingCodingRules)
162+
{
163+
requiredUsings.Add("System");
164+
requiredUsings.Add("System.Threading");
165+
requiredUsings.Add("System.Threading.Tasks");
166+
}
167+
160168
var apiGroupNames = openApiDocument.GetApiGroupNames();
161169

162170
requiredUsings.AddRange(apiGroupNames.Select(x => NamespaceFactory.Create(apiProjectName, NamespaceFactory.CreateWithApiGroupName(x, settings.ContractsNamespace))));

src/Atc.Rest.ApiGenerator.Framework.Mvc/ProjectGenerator/ServerHostGenerator.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,8 @@ public void GenerateConfigureSwaggerDocOptions()
189189
}
190190

191191
public void MaintainGlobalUsings(
192-
bool removeNamespaceGroupSeparatorInGlobalUsings)
192+
bool removeNamespaceGroupSeparatorInGlobalUsings,
193+
bool usingCodingRules)
193194
{
194195
var requiredUsings = new List<string>
195196
{
@@ -201,6 +202,17 @@ public void MaintainGlobalUsings(
201202
$"{settings.ProjectName}.Options",
202203
};
203204

205+
if (!usingCodingRules)
206+
{
207+
requiredUsings.Add("System");
208+
requiredUsings.Add("System.IO");
209+
requiredUsings.Add("Microsoft.AspNetCore.Builder");
210+
requiredUsings.Add("Microsoft.AspNetCore.Hosting");
211+
requiredUsings.Add("Microsoft.Extensions.Hosting");
212+
requiredUsings.Add("Microsoft.Extensions.Configuration");
213+
requiredUsings.Add("Microsoft.Extensions.DependencyInjection");
214+
}
215+
204216
if (UseRestExtended)
205217
{
206218
requiredUsings.Add("Asp.Versioning.ApiExplorer");

src/Atc.Rest.ApiGenerator.Framework.Mvc/ProjectGenerator/ServerHostTestGenerator.cs

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,11 @@ public ServerHostTestGenerator(
5050
.CreateGeneratedCode(settings.Version);
5151
}
5252

53-
public async Task ScaffoldProjectFile()
53+
public async Task ScaffoldProjectFile(
54+
bool usingCodingRules)
5455
{
55-
var packageReferences = await nugetPackageReferenceProvider.GetPackageReferencesForTestHostProjectForMvc();
56+
var packageReferences = await nugetPackageReferenceProvider.GetPackageReferencesForTestHostProjectForMvc(
57+
usingCodingRules);
5658

5759
var itemGroupPackageReferences = packageReferences
5860
.Select(packageReference => new ItemGroupParameter(
@@ -72,7 +74,12 @@ public async Task ScaffoldProjectFile()
7274
],
7375
[
7476
new("DocumentationFile", Attributes: null, @$"bin\Debug\net9.0\{settings.ProjectName}.xml"),
75-
new("NoWarn", Attributes: null, "$(NoWarn);1573;1591;1701;1702;1712;8618;NU1603;NU1608;"),
77+
new(
78+
"NoWarn",
79+
Attributes: null,
80+
usingCodingRules
81+
? "$(NoWarn);1573;1591;1701;1702;1712;8618;NU1603;NU1608;"
82+
: "$(NoWarn);1573;1591;1701;1702;1712;8618;8632;NU1603;NU1608;"),
7683
],
7784
],
7885
[
@@ -252,28 +259,38 @@ public void GenerateEndpointTests()
252259
}
253260

254261
public void MaintainGlobalUsings(
255-
bool usingCodingRules,
256-
bool removeNamespaceGroupSeparatorInGlobalUsings)
262+
bool removeNamespaceGroupSeparatorInGlobalUsings,
263+
bool usingCodingRules)
257264
{
258265
var requiredUsings = new List<string>
259-
{
260-
"System.CodeDom.Compiler",
261-
"System.Text",
262-
"System.Text.Json",
263-
"System.Text.Json.Serialization",
264-
"System.Reflection",
265-
"Atc.XUnit",
266-
"Atc.Rest.Options",
267-
"AutoFixture",
268-
"Microsoft.AspNetCore.Hosting",
269-
"Microsoft.AspNetCore.Http",
270-
"Microsoft.AspNetCore.TestHost",
271-
"Microsoft.AspNetCore.Mvc.Testing",
272-
"Microsoft.Extensions.Configuration",
273-
"Microsoft.Extensions.DependencyInjection",
274-
"Xunit",
275-
apiProjectName,
276-
};
266+
{
267+
"System.CodeDom.Compiler",
268+
"System.Text",
269+
"System.Text.Json",
270+
"System.Text.Json.Serialization",
271+
"System.Reflection",
272+
"Atc.XUnit",
273+
"Atc.Rest.Options",
274+
"AutoFixture",
275+
"Microsoft.AspNetCore.Hosting",
276+
"Microsoft.AspNetCore.Http",
277+
"Microsoft.AspNetCore.TestHost",
278+
"Microsoft.AspNetCore.Mvc.Testing",
279+
"Microsoft.Extensions.Configuration",
280+
"Microsoft.Extensions.DependencyInjection",
281+
"Xunit",
282+
apiProjectName,
283+
};
284+
285+
if (!usingCodingRules)
286+
{
287+
requiredUsings.Add("System");
288+
requiredUsings.Add("System.Threading");
289+
requiredUsings.Add("System.Threading.Tasks");
290+
requiredUsings.Add("System.Collections.Generic");
291+
requiredUsings.Add("System.IO");
292+
requiredUsings.Add("System.Net.Http");
293+
}
277294

278295
if (openApiDocument.IsUsingRequiredForAtcRestResults())
279296
{

src/Atc.Rest.ApiGenerator.Framework/ProjectGenerator/IServerApiGenerator.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ namespace Atc.Rest.ApiGenerator.Framework.ProjectGenerator;
1111
/// </remarks>
1212
public interface IServerApiGenerator
1313
{
14-
Task ScaffoldProjectFile();
14+
Task ScaffoldProjectFile(
15+
bool usingCodingRules);
1516

1617
void GenerateAssemblyMarker();
1718

@@ -29,5 +30,6 @@ void MaintainApiSpecification(
2930
FileInfo apiSpecificationFile);
3031

3132
void MaintainGlobalUsings(
32-
bool removeNamespaceGroupSeparatorInGlobalUsings);
33+
bool removeNamespaceGroupSeparatorInGlobalUsings,
34+
bool usingCodingRules);
3335
}

src/Atc.Rest.ApiGenerator.Framework/ProjectGenerator/IServerDomainGenerator.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@ public interface IServerDomainGenerator
2020
void GenerateServiceCollectionEndpointHandlerExtensions();
2121

2222
void MaintainGlobalUsings(
23-
bool removeNamespaceGroupSeparatorInGlobalUsings);
23+
bool removeNamespaceGroupSeparatorInGlobalUsings,
24+
bool usingCodingRules);
2425
}

0 commit comments

Comments
 (0)