7
7
namespace GitHubReleaseManager . Cli
8
8
{
9
9
using System ;
10
+ using System . Collections . Generic ;
11
+ using System . Globalization ;
10
12
using System . IO ;
11
13
using System . Linq ;
14
+ using System . Text ;
12
15
using System . Threading . Tasks ;
13
16
using CommandLine ;
14
17
using GitHubReleaseManager . Configuration ;
@@ -18,6 +21,8 @@ namespace GitHubReleaseManager.Cli
18
21
19
22
public static class Program
20
23
{
24
+ private static StringBuilder log = new StringBuilder ( ) ;
25
+
21
26
[ System . Diagnostics . CodeAnalysis . SuppressMessage ( "Microsoft.Reliability" , "CA2000:Dispose objects before losing scope" , Justification = "Not required" ) ]
22
27
private static int Main ( string [ ] args )
23
28
{
@@ -30,13 +35,21 @@ private static int Main(string[] args)
30
35
options ,
31
36
( verb , subOptions ) =>
32
37
{
38
+ if ( subOptions != null )
39
+ {
40
+ if ( string . IsNullOrEmpty ( ( ( BaseSubOptions ) subOptions ) . TargetPath ) )
41
+ {
42
+ ( ( BaseSubOptions ) subOptions ) . TargetPath = Environment . CurrentDirectory ;
43
+ }
44
+
45
+ ConfigureLogging ( ( ( BaseSubOptions ) subOptions ) . LogFilePath ) ;
46
+ }
47
+
33
48
var fileSystem = new FileSystem ( ) ;
34
- var currentFilePath = System . Reflection . Assembly . GetExecutingAssembly ( ) . Location ;
35
- var currentDirectory = Path . GetDirectoryName ( currentFilePath ) ;
36
49
37
50
if ( verb == "create" )
38
51
{
39
- result = CreateReleaseAsync ( ( CreateSubOptions ) subOptions ) . Result ;
52
+ result = CreateReleaseAsync ( ( CreateSubOptions ) subOptions , fileSystem ) . Result ;
40
53
}
41
54
42
55
if ( verb == "close" )
@@ -56,13 +69,13 @@ private static int Main(string[] args)
56
69
57
70
if ( verb == "init" )
58
71
{
59
- ConfigurationProvider . WriteSample ( currentDirectory , fileSystem ) ;
72
+ ConfigurationProvider . WriteSample ( ( ( InitSubOptions ) subOptions ) . TargetPath , fileSystem ) ;
60
73
result = 0 ;
61
74
}
62
75
63
76
if ( verb == "showconfig" )
64
77
{
65
- Console . WriteLine ( ConfigurationProvider . GetEffectiveConfigAsString ( currentDirectory , fileSystem ) ) ;
78
+ Console . WriteLine ( ConfigurationProvider . GetEffectiveConfigAsString ( ( ( ShowConfigSubOptions ) subOptions ) . TargetPath , fileSystem ) ) ;
66
79
result = 0 ;
67
80
}
68
81
} ) )
@@ -73,13 +86,14 @@ private static int Main(string[] args)
73
86
return result ;
74
87
}
75
88
76
- private static async Task < int > CreateReleaseAsync ( CreateSubOptions options )
89
+ private static async Task < int > CreateReleaseAsync ( CreateSubOptions subOptions , IFileSystem fileSystem )
77
90
{
78
91
try
79
92
{
80
- var github = options . CreateGitHubClient ( ) ;
93
+ var github = subOptions . CreateGitHubClient ( ) ;
94
+ var configuration = ConfigurationProvider . Provide ( subOptions . TargetPath , fileSystem ) ;
81
95
82
- await CreateRelease ( github , options . RepositoryOwner , options . RepositoryName , options . Milestone , options . TargetCommitish , options . AssetPath ) ;
96
+ await CreateRelease ( github , subOptions . RepositoryOwner , subOptions . RepositoryName , subOptions . Milestone , subOptions . TargetCommitish , subOptions . AssetPath , configuration ) ;
83
97
84
98
return 0 ;
85
99
}
@@ -91,13 +105,13 @@ private static async Task<int> CreateReleaseAsync(CreateSubOptions options)
91
105
}
92
106
}
93
107
94
- private static async Task < int > CloseMilestoneAsync ( CloseSubOptions options )
108
+ private static async Task < int > CloseMilestoneAsync ( CloseSubOptions subOptions )
95
109
{
96
110
try
97
111
{
98
- var github = options . CreateGitHubClient ( ) ;
112
+ var github = subOptions . CreateGitHubClient ( ) ;
99
113
100
- await CloseMilestone ( github , options . RepositoryOwner , options . RepositoryName , options . Milestone ) ;
114
+ await CloseMilestone ( github , subOptions . RepositoryOwner , subOptions . RepositoryName , subOptions . Milestone ) ;
101
115
102
116
return 0 ;
103
117
}
@@ -109,15 +123,15 @@ private static async Task<int> CloseMilestoneAsync(CloseSubOptions options)
109
123
}
110
124
}
111
125
112
- private static async Task < int > CloseAndPublishReleaseAsync ( PublishSubOptions options )
126
+ private static async Task < int > CloseAndPublishReleaseAsync ( PublishSubOptions subOptions )
113
127
{
114
128
try
115
129
{
116
- var github = options . CreateGitHubClient ( ) ;
130
+ var github = subOptions . CreateGitHubClient ( ) ;
117
131
118
- await CloseMilestone ( github , options . RepositoryOwner , options . RepositoryName , options . Milestone ) ;
132
+ await CloseMilestone ( github , subOptions . RepositoryOwner , subOptions . RepositoryName , subOptions . Milestone ) ;
119
133
120
- await PublishRelease ( github , options . RepositoryOwner , options . RepositoryName , options . Milestone ) ;
134
+ await PublishRelease ( github , subOptions . RepositoryOwner , subOptions . RepositoryName , subOptions . Milestone ) ;
121
135
122
136
return 0 ;
123
137
}
@@ -129,15 +143,15 @@ private static async Task<int> CloseAndPublishReleaseAsync(PublishSubOptions opt
129
143
}
130
144
}
131
145
132
- private static async Task < int > ExportReleasesAsync ( ExportSubOptions options )
146
+ private static async Task < int > ExportReleasesAsync ( ExportSubOptions subOptions )
133
147
{
134
148
try
135
149
{
136
- var github = options . CreateGitHubClient ( ) ;
150
+ var github = subOptions . CreateGitHubClient ( ) ;
137
151
138
- var releasesMarkdown = await ExportReleases ( github , options . RepositoryOwner , options . RepositoryName ) ;
152
+ var releasesMarkdown = await ExportReleases ( github , subOptions . RepositoryOwner , subOptions . RepositoryName ) ;
139
153
140
- using ( var sw = new StreamWriter ( File . Open ( options . FileOutputPath , FileMode . OpenOrCreate ) ) )
154
+ using ( var sw = new StreamWriter ( File . Open ( subOptions . FileOutputPath , FileMode . OpenOrCreate ) ) )
141
155
{
142
156
sw . Write ( releasesMarkdown ) ;
143
157
}
@@ -152,9 +166,9 @@ private static async Task<int> ExportReleasesAsync(ExportSubOptions options)
152
166
}
153
167
}
154
168
155
- private static async Task CreateRelease ( GitHubClient github , string owner , string repository , string milestone , string targetCommitish , string asset )
169
+ private static async Task CreateRelease ( GitHubClient github , string owner , string repository , string milestone , string targetCommitish , string asset , Config configuration )
156
170
{
157
- var releaseNotesBuilder = new ReleaseNotesBuilder ( new DefaultGitHubClient ( github , owner , repository ) , owner , repository , milestone ) ;
171
+ var releaseNotesBuilder = new ReleaseNotesBuilder ( new DefaultGitHubClient ( github , owner , repository ) , owner , repository , milestone , configuration ) ;
158
172
159
173
var result = await releaseNotesBuilder . BuildReleaseNotes ( ) ;
160
174
@@ -219,5 +233,48 @@ private static async Task PublishRelease(GitHubClient github, string owner, stri
219
233
220
234
await github . Release . Edit ( owner , repository , release . Id , releaseUpdate ) ;
221
235
}
236
+
237
+ [ System . Diagnostics . CodeAnalysis . SuppressMessage ( "Microsoft.Design" , "CA1031:DoNotCatchGeneralExceptionTypes" , Justification = "This is required here" ) ]
238
+ private static void ConfigureLogging ( string logFilePath )
239
+ {
240
+ var writeActions = new List < Action < string > >
241
+ {
242
+ s => log . AppendLine ( s )
243
+ } ;
244
+
245
+ if ( logFilePath == "console" )
246
+ {
247
+ writeActions . Add ( Console . WriteLine ) ;
248
+ }
249
+ else if ( ! string . IsNullOrEmpty ( logFilePath ) )
250
+ {
251
+ try
252
+ {
253
+ Directory . CreateDirectory ( Path . GetDirectoryName ( logFilePath ) ) ;
254
+ if ( File . Exists ( logFilePath ) )
255
+ {
256
+ using ( File . CreateText ( logFilePath ) )
257
+ {
258
+ }
259
+ }
260
+
261
+ writeActions . Add ( x => WriteLogEntry ( logFilePath , x ) ) ;
262
+ }
263
+ catch ( Exception ex )
264
+ {
265
+ Console . WriteLine ( "Failed to configure logging: " + ex . Message ) ;
266
+ }
267
+ }
268
+
269
+ Logger . WriteInfo = s => writeActions . ForEach ( a => a ( s ) ) ;
270
+ Logger . WriteWarning = s => writeActions . ForEach ( a => a ( s ) ) ;
271
+ Logger . WriteError = s => writeActions . ForEach ( a => a ( s ) ) ;
272
+ }
273
+
274
+ private static void WriteLogEntry ( string logFilePath , string s )
275
+ {
276
+ var contents = string . Format ( CultureInfo . InvariantCulture , "{0}\t \t {1}\r \n " , DateTime . Now . ToString ( "yyyy-MM-dd HH:mm:ss" , CultureInfo . InvariantCulture ) , s ) ;
277
+ File . AppendAllText ( logFilePath , contents ) ;
278
+ }
222
279
}
223
280
}
0 commit comments