-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathProgram.cs
255 lines (224 loc) · 11.2 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
using System;
using System.Net;
using System.Reflection;
using System.Threading.Tasks;
using CommandLine;
using GitReleaseManager.Cli.Logging;
using GitReleaseManager.Core;
using GitReleaseManager.Core.Commands;
using GitReleaseManager.Core.Configuration;
using GitReleaseManager.Core.Helpers;
using GitReleaseManager.Core.Model;
using GitReleaseManager.Core.Options;
using GitReleaseManager.Core.Provider;
using GitReleaseManager.Core.ReleaseNotes;
using GitReleaseManager.Core.Templates;
using Microsoft.Extensions.DependencyInjection;
using NGitLab;
using Octokit;
using Serilog;
namespace GitReleaseManager.Cli
{
public static class Program
{
private static IServiceProvider _serviceProvider;
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE1006:Naming Styles", Justification = "The main entry point can not be named with an Async Suffix")]
private static async Task<int> Main(string[] args)
{
// Just add the TLS 1.2 protocol to the Service Point manager until
// we've upgraded to latest Octokit.
ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12;
try
{
return await Parser.Default.ParseArguments<CreateSubOptions, DiscardSubOptions, AddAssetSubOptions, CloseSubOptions, OpenSubOptions, PublishSubOptions, ExportSubOptions, InitSubOptions, ShowConfigSubOptions, LabelSubOptions>(args)
.WithParsed<BaseSubOptions>(LogConfiguration.ConfigureLogging)
.WithParsed<BaseSubOptions>(CreateFiglet)
.WithParsed<BaseSubOptions>(LogOptions)
.WithParsed<BaseSubOptions>(RegisterServices)
.MapResult(
(CreateSubOptions opts) => ExecuteCommand(opts),
(DiscardSubOptions opts) => ExecuteCommand(opts),
(AddAssetSubOptions opts) => ExecuteCommand(opts),
(CloseSubOptions opts) => ExecuteCommand(opts),
(OpenSubOptions opts) => ExecuteCommand(opts),
(PublishSubOptions opts) => ExecuteCommand(opts),
(ExportSubOptions opts) => ExecuteCommand(opts),
(InitSubOptions opts) => ExecuteCommand(opts),
(ShowConfigSubOptions opts) => ExecuteCommand(opts),
(LabelSubOptions opts) => ExecuteCommand(opts),
errs => Task.FromResult(1)).ConfigureAwait(false);
}
catch (AggregateException ex)
{
Log.Fatal("{Message}", ex.Message);
foreach (var innerException in ex.InnerExceptions)
{
Log.Fatal(innerException, "{Message}", innerException.Message);
}
return 1;
}
catch (Exception ex)
{
Log.Fatal(ex, "{Message}", ex.Message);
return 1;
}
finally
{
Log.CloseAndFlush();
DisposeServices();
}
}
private static void RegisterServices(BaseSubOptions options)
{
var fileSystem = new FileSystem(options);
var logger = Log.ForContext<VcsService>();
var mapper = AutoMapperConfiguration.Configure();
var configuration = ConfigurationProvider.Provide(options.TargetDirectory ?? Environment.CurrentDirectory, fileSystem);
var serviceCollection = new ServiceCollection()
.AddSingleton(logger)
.AddSingleton(mapper)
.AddSingleton(configuration)
.AddSingleton(configuration.Export)
.AddSingleton<ICommand<AddAssetSubOptions>, AddAssetsCommand>()
.AddSingleton<ICommand<CloseSubOptions>, CloseCommand>()
.AddSingleton<ICommand<CreateSubOptions>, CreateCommand>()
.AddSingleton<ICommand<DiscardSubOptions>, DiscardCommand>()
.AddSingleton<ICommand<ExportSubOptions>, ExportCommand>()
.AddSingleton<ICommand<InitSubOptions>, InitCommand>()
.AddSingleton<ICommand<LabelSubOptions>, LabelCommand>()
.AddSingleton<ICommand<OpenSubOptions>, OpenCommand>()
.AddSingleton<ICommand<PublishSubOptions>, PublishCommand>()
.AddSingleton<ICommand<ShowConfigSubOptions>, ShowConfigCommand>()
.AddSingleton<IFileSystem>(fileSystem)
.AddSingleton<IReleaseNotesExporter, ReleaseNotesExporter>()
.AddSingleton<IReleaseNotesBuilder, ReleaseNotesBuilder>()
.AddSingleton<IVcsService, VcsService>();
if (options is BaseVcsOptions vcsOptions)
{
if (string.IsNullOrWhiteSpace(vcsOptions.Token))
{
throw new Exception("The token option is not defined");
}
RegisterVcsProvider(vcsOptions, serviceCollection);
}
if (options is CreateSubOptions createOptions && !string.IsNullOrEmpty(createOptions.OutputPath))
{
configuration.Create.AllowUpdateToPublishedRelease = false;
}
serviceCollection = serviceCollection
.AddTransient((services) => new TemplateFactory(services.GetRequiredService<IFileSystem>(), services.GetRequiredService<Config>(), TemplateKind.Create));
_serviceProvider = serviceCollection.BuildServiceProvider();
}
private static void DisposeServices()
{
if (_serviceProvider is IDisposable serviceProvider)
{
serviceProvider.Dispose();
}
}
private static void CreateFiglet(BaseSubOptions options)
{
if (options.NoLogo)
{
return;
}
var version = Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion;
#pragma warning disable CA1307 // Specify StringComparison ; Reason, we can't do this because of targeting .NET Framework
if (version.Contains("+"))
#pragma warning restore CA1307 // Specify StringComparison
{
version = version.Substring(0, version.IndexOf("+", StringComparison.Ordinal));
}
// The following ugly formats is to prevent incorrect indentation
// detected by editorconfig formatters.
const string shortFormat = "\n ____ ____ __ __\n"
+ " / ___| _ \\| \\/ |\n"
+ " | | _| |_) | |\\/| |\n"
+ " | |_| | _ <| | | |\n"
+ " \\____|_| \\_\\_| |_|\n"
+ "{0,21}\n";
const string longFormat = "\n ____ _ _ ____ _ __ __\n"
+ " / ___(_) |_| _ \\ ___| | ___ __ _ ___ ___| \\/ | __ _ _ __ __ _ __ _ ___ _ __\n"
+ " | | _| | __| |_) / _ \\ |/ _ \\/ _` / __|/ _ \\ |\\/| |/ _` | '_ \\ / _` |/ _` |/ _ \\ '__|\n"
+ " | |_| | | |_| _ < __/ | __/ (_| \\__ \\ __/ | | | (_| | | | | (_| | (_| | __/ |\n"
+ " \\____|_|\\__|_| \\_\\___|_|\\___|\\__,_|___/\\___|_| |_|\\__,_|_| |_|\\__,_|\\__, |\\___|_|\n"
+ " |___/\n"
+ "{0,87}\n";
if (GetConsoleWidth() > 87)
{
Log.Information(longFormat, version);
}
else
{
Log.Information(shortFormat, version);
}
}
private static int GetConsoleWidth()
{
try
{
return Console.WindowWidth;
}
catch
{
Log.Verbose("Unable to get the width of the console.");
}
try
{
return Console.BufferWidth;
}
catch
{
Log.Verbose("Unable to get the width of the buffer");
return int.MaxValue;
}
}
private static Task<int> ExecuteCommand<TOptions>(TOptions options)
where TOptions : BaseSubOptions
{
var command = _serviceProvider.GetRequiredService<ICommand<TOptions>>();
return command.ExecuteAsync(options);
}
private static void LogOptions(BaseSubOptions options)
=> Log.Debug("{@Options}", options);
private static void RegisterKeyedVcsProvider<TVcsImplementation>(object provider, IServiceCollection serviceCollection)
where TVcsImplementation : class, IVcsProvider
{
static IVcsProvider ResolveService(IServiceProvider service, object key) => service.GetRequiredKeyedService<IVcsProvider>(key);
provider ??= "null";
Log.Debug("Registering {Type} with Service Key {Key}", typeof(IVcsProvider), provider);
if (typeof(TVcsImplementation) != typeof(NullReleasesProvider))
{
serviceCollection.AddKeyedSingleton<IVcsProvider, TVcsImplementation>(provider);
}
serviceCollection
.AddKeyedTransient<IAssetsProvider>(provider, ResolveService)
.AddKeyedTransient<ICommitsProvider>(provider, ResolveService)
.AddKeyedTransient<IIssuesProvider>(provider, ResolveService)
.AddKeyedTransient<IMilestonesProvider>(provider, ResolveService)
.AddKeyedTransient<IReleasesProvider>(provider, ResolveService);
}
private static void RegisterVcsProvider(BaseVcsOptions vcsOptions, IServiceCollection serviceCollection)
{
Log.Information("Using {Provider} as VCS Provider", vcsOptions.Provider);
serviceCollection.AddKeyedSingleton<IVcsProvider>("null", (service, _) => new NullReleasesProvider(vcsOptions.Provider.ToString(), service.GetRequiredService<ILogger>()));
RegisterKeyedVcsProvider<NullReleasesProvider>(null, serviceCollection);
RegisterKeyedVcsProvider<GitHubProvider>(VcsProvider.GitHub, serviceCollection);
serviceCollection
.AddSingleton<IGitLabClient>((_) => new GitLabClient("https://gitlab.com", vcsOptions.Token));
serviceCollection
.AddSingleton<IGitHubClient>((_) => new GitHubClient(new ProductHeaderValue("GitReleaseManager")) { Credentials = new Credentials(vcsOptions.Token) });
serviceCollection.AddTransient((service) => service.GetKeyedService<IVcsProvider>(vcsOptions.Provider) ?? service.GetRequiredKeyedService<IVcsProvider>("null"));
if (vcsOptions is CreateSubOptions createOptions && !string.IsNullOrEmpty(createOptions.OutputPath))
{
serviceCollection.AddSingleton<IReleasesProvider>((service) => new LocalProvider(
service.GetRequiredService<IFileSystem>(),
createOptions.OutputPath));
}
else
{
serviceCollection.AddTransient((service) => service.GetKeyedService<IReleasesProvider>(vcsOptions.Provider) ?? service.GetRequiredKeyedService<IReleasesProvider>("null"));
}
}
}
}