-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathProgram.cs
203 lines (184 loc) · 8.42 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
//-----------------------------------------------------------------------
// <copyright file="Program.cs" company="GitTools Contributors">
// Copyright (c) 2015 - Present - GitTools Contributors
// </copyright>
//-----------------------------------------------------------------------
namespace GitReleaseManager.Cli
{
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.Options;
using GitReleaseManager.Core.Provider;
using GitReleaseManager.Core.ReleaseNotes;
using Microsoft.Extensions.DependencyInjection;
using Octokit;
using Serilog;
public static class Program
{
private static IServiceProvider _serviceProvider;
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<BaseVcsOptions>(ReportUsernamePasswordDeprecation)
.WithParsed<BaseVcsOptions>(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(BaseVcsOptions options)
{
var fileSystem = new FileSystem();
var logger = Log.ForContext<VcsService>();
var mapper = AutoMapperConfiguration.Configure();
var configuration = ConfigurationProvider.Provide(options.TargetDirectory ?? Environment.CurrentDirectory, fileSystem);
var credentials = string.IsNullOrWhiteSpace(options.Token)
? new Credentials(options.UserName, options.Password)
: new Credentials(options.Token);
var gitHubClient = new GitHubClient(new ProductHeaderValue("GitReleaseManager")) { Credentials = credentials };
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<IGitHubClient>(gitHubClient)
.AddSingleton<IVcsProvider, GitHubProvider>()
.AddSingleton<IVcsService, VcsService>();
_serviceProvider = serviceCollection.BuildServiceProvider();
}
private static void DisposeServices()
{
if (_serviceProvider is IDisposable serviceProvider)
{
serviceProvider.Dispose();
}
}
private static void ReportUsernamePasswordDeprecation(BaseVcsOptions options)
{
if (!string.IsNullOrEmpty(options.UserName) || !string.IsNullOrEmpty(options.Password))
{
Log.Warning(BaseVcsOptions.OBSOLETE_MESSAGE);
}
}
private static void CreateFiglet(BaseSubOptions options)
{
if (options.NoLogo)
{
return;
}
var version = Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion;
if (version.IndexOf('+') >= 0)
{
version = version.Substring(0, version.IndexOf('+'));
}
// 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.Execute(options);
}
private static void LogOptions(BaseSubOptions options)
=> Log.Debug("{@Options}", options);
}
}