@@ -25,7 +25,9 @@ namespace GitReleaseManager.Cli
25
25
26
26
public static class Program
27
27
{
28
- private static StringBuilder log = new StringBuilder ( ) ;
28
+ private static StringBuilder _log = new StringBuilder ( ) ;
29
+ private static FileSystem _fileSystem ;
30
+ private static Config _configuration ;
29
31
30
32
[ System . Diagnostics . CodeAnalysis . SuppressMessage ( "Microsoft.Reliability" , "CA2000:Dispose objects before losing scope" , Justification = "Not required" ) ]
31
33
private static int Main ( string [ ] args )
@@ -34,29 +36,29 @@ private static int Main(string[] args)
34
36
// we've upgraded to latest Octokit.
35
37
ServicePointManager . SecurityProtocol |= SecurityProtocolType . Tls12 ;
36
38
37
- var fileSystem = new FileSystem ( ) ;
39
+ _fileSystem = new FileSystem ( ) ;
38
40
39
41
return Parser . Default . ParseArguments < CreateSubOptions , AddAssetSubOptions , CloseSubOptions , PublishSubOptions , ExportSubOptions , InitSubOptions , ShowConfigSubOptions , LabelSubOptions > ( args )
40
42
. MapResult (
41
- ( CreateSubOptions opts ) => CreateReleaseAsync ( opts , fileSystem ) . Result ,
43
+ ( CreateSubOptions opts ) => CreateReleaseAsync ( opts ) . Result ,
42
44
( AddAssetSubOptions opts ) => AddAssetsAsync ( opts ) . Result ,
43
45
( CloseSubOptions opts ) => CloseMilestoneAsync ( opts ) . Result ,
44
46
( PublishSubOptions opts ) => PublishReleaseAsync ( opts ) . Result ,
45
- ( ExportSubOptions opts ) => ExportReleasesAsync ( opts , fileSystem ) . Result ,
46
- ( InitSubOptions opts ) => CreateSampleConfigFile ( opts , fileSystem ) ,
47
- ( ShowConfigSubOptions opts ) => ShowConfig ( opts , fileSystem ) ,
47
+ ( ExportSubOptions opts ) => ExportReleasesAsync ( opts ) . Result ,
48
+ ( InitSubOptions opts ) => CreateSampleConfigFile ( opts ) ,
49
+ ( ShowConfigSubOptions opts ) => ShowConfig ( opts ) ,
48
50
( LabelSubOptions opts ) => CreateLabelsAsync ( opts ) . Result ,
49
51
errs => 1 ) ;
50
52
}
51
53
52
- private static async Task < int > CreateReleaseAsync ( CreateSubOptions subOptions , IFileSystem fileSystem )
54
+ private static async Task < int > CreateReleaseAsync ( CreateSubOptions subOptions )
53
55
{
54
56
try
55
57
{
56
58
ConfigureLogging ( subOptions . LogFilePath ) ;
57
59
58
60
var github = subOptions . CreateGitHubClient ( ) ;
59
- var configuration = ConfigurationProvider . Provide ( subOptions . TargetDirectory ?? Environment . CurrentDirectory , fileSystem ) ;
61
+ _configuration = ConfigurationProvider . Provide ( subOptions . TargetDirectory ?? Environment . CurrentDirectory , _fileSystem ) ;
60
62
61
63
Release release ;
62
64
if ( ! string . IsNullOrEmpty ( subOptions . Milestone ) )
@@ -67,7 +69,7 @@ private static async Task<int> CreateReleaseAsync(CreateSubOptions subOptions, I
67
69
releaseName = subOptions . Milestone ;
68
70
}
69
71
70
- release = await CreateReleaseFromMilestone ( github , subOptions . RepositoryOwner , subOptions . RepositoryName , subOptions . Milestone , releaseName , subOptions . TargetCommitish , subOptions . AssetPaths , subOptions . Prerelease , configuration ) ;
72
+ release = await CreateReleaseFromMilestone ( github , subOptions . RepositoryOwner , subOptions . RepositoryName , subOptions . Milestone , releaseName , subOptions . TargetCommitish , subOptions . AssetPaths , subOptions . Prerelease ) ;
71
73
}
72
74
else
73
75
{
@@ -92,6 +94,7 @@ private static async Task<int> AddAssetsAsync(AddAssetSubOptions subOptions)
92
94
ConfigureLogging ( subOptions . LogFilePath ) ;
93
95
94
96
var github = subOptions . CreateGitHubClient ( ) ;
97
+ _configuration = ConfigurationProvider . Provide ( subOptions . TargetDirectory ?? Environment . CurrentDirectory , _fileSystem ) ;
95
98
96
99
await AddAssets ( github , subOptions . RepositoryOwner , subOptions . RepositoryName , subOptions . TagName , subOptions . AssetPaths ) ;
97
100
@@ -112,6 +115,7 @@ private static async Task<int> CloseMilestoneAsync(CloseSubOptions subOptions)
112
115
ConfigureLogging ( subOptions . LogFilePath ) ;
113
116
114
117
var github = subOptions . CreateGitHubClient ( ) ;
118
+ _configuration = ConfigurationProvider . Provide ( subOptions . TargetDirectory ?? Environment . CurrentDirectory , _fileSystem ) ;
115
119
116
120
await CloseMilestone ( github , subOptions . RepositoryOwner , subOptions . RepositoryName , subOptions . Milestone ) ;
117
121
@@ -132,6 +136,7 @@ private static async Task<int> PublishReleaseAsync(PublishSubOptions subOptions)
132
136
ConfigureLogging ( subOptions . LogFilePath ) ;
133
137
134
138
var github = subOptions . CreateGitHubClient ( ) ;
139
+ _configuration = ConfigurationProvider . Provide ( subOptions . TargetDirectory ?? Environment . CurrentDirectory , _fileSystem ) ;
135
140
136
141
await PublishRelease ( github , subOptions . RepositoryOwner , subOptions . RepositoryName , subOptions . TagName ) ;
137
142
@@ -145,16 +150,16 @@ private static async Task<int> PublishReleaseAsync(PublishSubOptions subOptions)
145
150
}
146
151
}
147
152
148
- private static async Task < int > ExportReleasesAsync ( ExportSubOptions subOptions , IFileSystem fileSystem )
153
+ private static async Task < int > ExportReleasesAsync ( ExportSubOptions subOptions )
149
154
{
150
155
try
151
156
{
152
157
ConfigureLogging ( subOptions . LogFilePath ) ;
153
158
154
159
var github = subOptions . CreateGitHubClient ( ) ;
155
- var configuration = ConfigurationProvider . Provide ( subOptions . TargetDirectory ?? Environment . CurrentDirectory , fileSystem ) ;
160
+ _configuration = ConfigurationProvider . Provide ( subOptions . TargetDirectory ?? Environment . CurrentDirectory , _fileSystem ) ;
156
161
157
- var releasesMarkdown = await ExportReleases ( github , subOptions . RepositoryOwner , subOptions . RepositoryName , subOptions . TagName , configuration ) ;
162
+ var releasesMarkdown = await ExportReleases ( github , subOptions . RepositoryOwner , subOptions . RepositoryName , subOptions . TagName ) ;
158
163
159
164
using ( var sw = new StreamWriter ( File . Open ( subOptions . FileOutputPath , FileMode . OpenOrCreate ) ) )
160
165
{
@@ -171,19 +176,19 @@ private static async Task<int> ExportReleasesAsync(ExportSubOptions subOptions,
171
176
}
172
177
}
173
178
174
- private static int CreateSampleConfigFile ( InitSubOptions subOptions , IFileSystem fileSystem )
179
+ private static int CreateSampleConfigFile ( InitSubOptions subOptions )
175
180
{
176
181
ConfigureLogging ( subOptions . LogFilePath ) ;
177
182
178
- ConfigurationProvider . WriteSample ( subOptions . TargetDirectory ?? Environment . CurrentDirectory , fileSystem ) ;
183
+ ConfigurationProvider . WriteSample ( subOptions . TargetDirectory ?? Environment . CurrentDirectory , _fileSystem ) ;
179
184
return 0 ;
180
185
}
181
186
182
- private static int ShowConfig ( ShowConfigSubOptions subOptions , IFileSystem fileSystem )
187
+ private static int ShowConfig ( ShowConfigSubOptions subOptions )
183
188
{
184
189
ConfigureLogging ( subOptions . LogFilePath ) ;
185
190
186
- Console . WriteLine ( ConfigurationProvider . GetEffectiveConfigAsString ( subOptions . TargetDirectory ?? Environment . CurrentDirectory , fileSystem ) ) ;
191
+ Console . WriteLine ( ConfigurationProvider . GetEffectiveConfigAsString ( subOptions . TargetDirectory ?? Environment . CurrentDirectory , _fileSystem ) ) ;
187
192
return 0 ;
188
193
}
189
194
@@ -205,6 +210,7 @@ private static async Task<int> CreateLabelsAsync(LabelSubOptions subOptions)
205
210
newLabels . Add ( new NewLabel ( "help wanted" , "33aa3f" ) ) ;
206
211
207
212
var github = subOptions . CreateGitHubClient ( ) ;
213
+ _configuration = ConfigurationProvider . Provide ( subOptions . TargetDirectory ?? Environment . CurrentDirectory , _fileSystem ) ;
208
214
209
215
var labels = await github . Issue . Labels . GetAllForRepository ( subOptions . RepositoryOwner , subOptions . RepositoryName ) ;
210
216
@@ -227,9 +233,10 @@ private static async Task<int> CreateLabelsAsync(LabelSubOptions subOptions)
227
233
return 1 ;
228
234
}
229
235
}
230
- private static async Task < Release > CreateReleaseFromMilestone ( GitHubClient github , string owner , string repository , string milestone , string releaseName , string targetCommitish , IList < string > assets , bool prerelease , Config configuration )
236
+
237
+ private static async Task < Release > CreateReleaseFromMilestone ( GitHubClient github , string owner , string repository , string milestone , string releaseName , string targetCommitish , IList < string > assets , bool prerelease )
231
238
{
232
- var releaseNotesBuilder = new ReleaseNotesBuilder ( new DefaultGitHubClient ( github , owner , repository ) , owner , repository , milestone , configuration ) ;
239
+ var releaseNotesBuilder = new ReleaseNotesBuilder ( new DefaultGitHubClient ( github , owner , repository ) , owner , repository , milestone , _configuration ) ;
233
240
234
241
var result = await releaseNotesBuilder . BuildReleaseNotes ( ) ;
235
242
@@ -275,9 +282,9 @@ private static async Task AddAssets(GitHubClient github, string owner, string re
275
282
await AddAssets ( github , owner , repository , assets , release ) ;
276
283
}
277
284
278
- private static async Task < string > ExportReleases ( GitHubClient github , string owner , string repository , string tagName , Config configuration )
285
+ private static async Task < string > ExportReleases ( GitHubClient github , string owner , string repository , string tagName )
279
286
{
280
- var releaseNotesExporter = new ReleaseNotesExporter ( new DefaultGitHubClient ( github , owner , repository ) , configuration ) ;
287
+ var releaseNotesExporter = new ReleaseNotesExporter ( new DefaultGitHubClient ( github , owner , repository ) , _configuration ) ;
281
288
282
289
var result = await releaseNotesExporter . ExportReleaseNotes ( tagName ) ;
283
290
@@ -343,11 +350,14 @@ private static async Task AddAssets(GitHubClient github, string owner, string re
343
350
344
351
private static async Task AddAssetsSha256 ( GitHubClient github , string owner , string repository , IList < string > assets , Release release )
345
352
{
346
- if ( assets != null && assets . Any ( ) )
353
+ if ( assets != null && assets . Any ( ) && _configuration . Create . IncludeShaSection )
347
354
{
348
355
var stringBuilder = new StringBuilder ( release . Body ) ;
349
356
350
- stringBuilder . AppendLine ( "__SHA256 Hashes of the release artifacts__" ) ;
357
+ if ( ! release . Body . Contains ( _configuration . Create . ShaSectionHeading ) )
358
+ {
359
+ stringBuilder . AppendLine ( string . Format ( "### {0}" , _configuration . Create . ShaSectionHeading ) ) ;
360
+ }
351
361
352
362
foreach ( var asset in assets )
353
363
{
@@ -358,8 +368,8 @@ private static async Task AddAssetsSha256(GitHubClient github, string owner, str
358
368
continue ;
359
369
}
360
370
361
- stringBuilder . AppendFormat ( "- `{0}` \r \n " , file . Name ) ;
362
- stringBuilder . AppendFormat ( " \t - {0} \r \n " , ComputeSha256Hash ( asset ) ) ;
371
+ stringBuilder . AppendFormat ( _configuration . Create . ShaSectionLineFormat , file . Name , ComputeSha256Hash ( asset ) ) ;
372
+ stringBuilder . AppendLine ( ) ;
363
373
}
364
374
365
375
stringBuilder . AppendLine ( ) ;
@@ -394,7 +404,7 @@ private static void ConfigureLogging(string logFilePath)
394
404
{
395
405
var writeActions = new List < Action < string > >
396
406
{
397
- s => log . AppendLine ( s )
407
+ s => _log . AppendLine ( s )
398
408
} ;
399
409
400
410
if ( ! string . IsNullOrEmpty ( logFilePath ) )
@@ -433,17 +443,17 @@ private static void WriteLogEntry(string logFilePath, string s)
433
443
File . AppendAllText ( logFilePath , contents ) ;
434
444
}
435
445
436
- private static string ComputeSha256Hash ( string asset )
446
+ private static string ComputeSha256Hash ( string asset )
437
447
{
438
- // Create a SHA256
448
+ // Create a SHA256
439
449
using ( var sha256Hash = SHA256 . Create ( ) )
440
450
{
441
451
using ( var fileStream = File . Open ( asset , FileMode . Open , FileAccess . Read , FileShare . ReadWrite ) )
442
452
{
443
- // ComputeHash - returns byte array
453
+ // ComputeHash - returns byte array
444
454
var bytes = sha256Hash . ComputeHash ( fileStream ) ;
445
455
446
- // Convert byte array to a string
456
+ // Convert byte array to a string
447
457
var builder = new StringBuilder ( ) ;
448
458
449
459
foreach ( var t in bytes )
@@ -454,6 +464,6 @@ private static string ComputeSha256Hash(string asset)
454
464
return builder . ToString ( ) ;
455
465
}
456
466
}
457
- }
467
+ }
458
468
}
459
469
}
0 commit comments