1
1
// Licensed to the .NET Foundation under one or more agreements.
2
2
// The .NET Foundation licenses this file to you under the MIT license.
3
3
4
- using System ;
5
- using System . Collections . Generic ;
6
4
using System . Diagnostics ;
7
- using System . IO ;
8
5
using System . Linq ;
9
6
using System . Net ;
10
7
using System . Net . Http ;
11
8
using System . Reflection ;
12
9
using System . Security . Cryptography ;
13
10
using System . Text . Json ;
14
- using System . Threading ;
15
- using System . Threading . Tasks ;
16
11
using Microsoft . Build . Evaluation ;
17
12
using Microsoft . DotNet . Openapi . Tools ;
18
13
using Microsoft . DotNet . Openapi . Tools . Internal ;
@@ -46,9 +41,9 @@ public BaseCommand(CommandLineApplication parent, string name, IHttpClientWrappe
46
41
47
42
ProjectFileOption = Option ( "-p|--updateProject" , "The project file update." , CommandOptionType . SingleValue ) ;
48
43
49
- if ( Parent is Application )
44
+ if ( Parent is Application application )
50
45
{
51
- WorkingDirectory = ( ( Application ) Parent ) . WorkingDirectory ;
46
+ WorkingDirectory = application . WorkingDirectory ;
52
47
}
53
48
else
54
49
{
@@ -89,10 +84,11 @@ private async Task<int> ExecuteAsync()
89
84
private Application GetApplication ( )
90
85
{
91
86
var parent = Parent ;
92
- while ( ! ( parent is Application ) )
87
+ while ( parent is not Application )
93
88
{
94
89
parent = parent . Parent ;
95
90
}
91
+
96
92
return ( Application ) parent ;
97
93
}
98
94
@@ -126,7 +122,7 @@ internal FileInfo ResolveProjectFile(CommandOption projectOption)
126
122
return new FileInfo ( project ) ;
127
123
}
128
124
129
- protected Project LoadProject ( FileInfo projectFile )
125
+ protected static Project LoadProject ( FileInfo projectFile )
130
126
{
131
127
var project = ProjectCollection . GlobalProjectCollection . LoadProject (
132
128
projectFile . FullName ,
@@ -136,12 +132,12 @@ protected Project LoadProject(FileInfo projectFile)
136
132
return project ;
137
133
}
138
134
139
- internal bool IsProjectFile ( string file )
135
+ internal static bool IsProjectFile ( string file )
140
136
{
141
137
return File . Exists ( Path . GetFullPath ( file ) ) && file . EndsWith ( ".csproj" , StringComparison . Ordinal ) ;
142
138
}
143
139
144
- internal bool IsUrl ( string file )
140
+ internal static bool IsUrl ( string file )
145
141
{
146
142
return Uri . TryCreate ( file , UriKind . Absolute , out var _ ) && file . StartsWith ( "http" , StringComparison . Ordinal ) ;
147
143
}
@@ -167,7 +163,8 @@ internal async Task AddOpenAPIReference(
167
163
168
164
if ( sourceUrl != null )
169
165
{
170
- if ( items . Any ( i => string . Equals ( i . GetMetadataValue ( SourceUrlAttrName ) , sourceUrl ) ) )
166
+ if ( items . Any (
167
+ i => string . Equals ( i . GetMetadataValue ( SourceUrlAttrName ) , sourceUrl , StringComparison . Ordinal ) ) )
171
168
{
172
169
Warning . Write ( $ "A reference to '{ sourceUrl } ' already exists in '{ project . FullPath } '.") ;
173
170
return ;
@@ -299,8 +296,8 @@ internal async Task<string> DownloadGivenOption(string url, CommandOption fileOp
299
296
/// <param name="retryCount"></param>
300
297
private static async Task < IHttpResponseMessageWrapper > RetryRequest (
301
298
Func < Task < IHttpResponseMessageWrapper > > retryBlock ,
302
- CancellationToken cancellationToken = default ,
303
- int retryCount = 60 )
299
+ int retryCount = 60 ,
300
+ CancellationToken cancellationToken = default )
304
301
{
305
302
for ( var retry = 0 ; retry < retryCount ; retry ++ )
306
303
{
@@ -331,7 +328,7 @@ private static async Task<IHttpResponseMessageWrapper> RetryRequest(
331
328
{
332
329
if ( exception is HttpRequestException || exception is WebException )
333
330
{
334
- await Task . Delay ( 1 * 1000 ) ; //Wait for a while before retry.
331
+ await Task . Delay ( 1 * 1000 , cancellationToken ) ; // Wait for a while before retry.
335
332
}
336
333
}
337
334
}
@@ -340,7 +337,7 @@ private static async Task<IHttpResponseMessageWrapper> RetryRequest(
340
337
throw new OperationCanceledException ( "Failed to connect, retry limit exceeded." ) ;
341
338
}
342
339
343
- private string GetUniqueFileName ( string directory , string fileName , string extension )
340
+ private static string GetUniqueFileName ( string directory , string fileName , string extension )
344
341
{
345
342
var uniqueName = fileName ;
346
343
@@ -366,7 +363,7 @@ private string GetUniqueFileName(string directory, string fileName, string exten
366
363
return uniqueName + extension ;
367
364
}
368
365
369
- private string GetFileNameFromResponse ( IHttpResponseMessageWrapper response , string url )
366
+ private static string GetFileNameFromResponse ( IHttpResponseMessageWrapper response , string url )
370
367
{
371
368
var contentDisposition = response . ContentDisposition ( ) ;
372
369
string result ;
@@ -396,22 +393,12 @@ private string GetFileNameFromResponse(IHttpResponseMessageWrapper response, str
396
393
else
397
394
{
398
395
var parts = uri . Host . Split ( '.' ) ;
399
-
400
- // There's no segment, use the domain name.
401
- string domain ;
402
- switch ( parts . Length )
396
+ var domain = parts . Length switch
403
397
{
404
- case 1 :
405
- case 2 :
406
- // It's localhost if 1, no www if 2
407
- domain = parts . First ( ) ;
408
- break ;
409
- case 3 :
410
- domain = parts [ 1 ] ;
411
- break ;
412
- default :
413
- throw new NotImplementedException ( "We don't handle the case that the Host has more than three segments" ) ;
414
- }
398
+ 1 or 2 => parts . First ( ) , // It's localhost or somewhere in an Intranet if 1; no www if 2.
399
+ 3 => parts [ 1 ] , // Grab XYZ in www.XYZ.domain.com or similar.
400
+ _ => throw new NotImplementedException ( "We don't handle the case that the Host has more than three segments" ) ,
401
+ } ;
415
402
416
403
result = domain + DefaultExtension ;
417
404
}
@@ -420,7 +407,7 @@ private string GetFileNameFromResponse(IHttpResponseMessageWrapper response, str
420
407
return result ;
421
408
}
422
409
423
- internal CodeGenerator ? GetCodeGenerator ( CommandOption codeGeneratorOption )
410
+ internal static CodeGenerator ? GetCodeGenerator ( CommandOption codeGeneratorOption )
424
411
{
425
412
CodeGenerator ? codeGenerator ;
426
413
if ( codeGeneratorOption . HasValue ( ) )
@@ -435,7 +422,7 @@ private string GetFileNameFromResponse(IHttpResponseMessageWrapper response, str
435
422
return codeGenerator ;
436
423
}
437
424
438
- internal void ValidateCodeGenerator ( CommandOption codeGeneratorOption )
425
+ internal static void ValidateCodeGenerator ( CommandOption codeGeneratorOption )
439
426
{
440
427
if ( codeGeneratorOption . HasValue ( ) )
441
428
{
@@ -494,7 +481,7 @@ private async Task<IDictionary<string, string>> LoadPackageVersionsFromURLAsync(
494
481
495
482
private static IDictionary < string , string > GetServicePackages ( CodeGenerator ? type )
496
483
{
497
- CodeGenerator generator = type ?? CodeGenerator . NSwagCSharp ;
484
+ var generator = type ?? CodeGenerator . NSwagCSharp ;
498
485
var name = Enum . GetName ( typeof ( CodeGenerator ) , generator ) ;
499
486
var attributes = typeof ( Program ) . Assembly . GetCustomAttributes < OpenApiDependencyAttribute > ( ) ;
500
487
@@ -513,10 +500,8 @@ private static IDictionary<string, string> GetServicePackages(CodeGenerator? typ
513
500
514
501
private static byte [ ] GetHash ( Stream stream )
515
502
{
516
- using ( var algorithm = SHA256 . Create ( ) )
517
- {
518
- return algorithm . ComputeHash ( stream ) ;
519
- }
503
+ using var algorithm = SHA256 . Create ( ) ;
504
+ return algorithm . ComputeHash ( stream ) ;
520
505
}
521
506
522
507
private async Task WriteToFileAsync ( Stream content , string destinationPath , bool overwrite )
@@ -572,12 +557,13 @@ private async Task WriteToFileAsync(Stream content, string destinationPath, bool
572
557
573
558
// Create or overwrite the destination file.
574
559
reachedCopy = true ;
575
- using var fileStream = new FileStream ( destinationPath , FileMode . OpenOrCreate , FileAccess . Write ) ;
560
+ using var fileStream = new FileStream ( destinationPath , FileMode . Create , FileAccess . Write ) ;
576
561
fileStream . Seek ( 0 , SeekOrigin . Begin ) ;
577
562
if ( content . CanSeek )
578
563
{
579
564
content . Seek ( 0 , SeekOrigin . Begin ) ;
580
565
}
566
+
581
567
await content . CopyToAsync ( fileStream ) ;
582
568
}
583
569
catch ( Exception ex )
0 commit comments