-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathProgram.cs
More file actions
185 lines (160 loc) · 6.85 KB
/
Program.cs
File metadata and controls
185 lines (160 loc) · 6.85 KB
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
using Serilog.Sinks.SystemConsole.Themes;
// dotnet publish --self-contained false --output ./publish
// ./publish/CreateMatrix matrix --updateversion --matrixpath ./JSON/Matrix.json --versionpath ./JSON/Version.json
// echo $?
namespace CreateMatrix;
internal sealed class Program(
CommandLine.Options commandLineOptions,
CancellationToken cancellationToken
)
{
internal static async Task<int> Main(string[] args)
{
try
{
// Parse commandline
CommandLine commandLine = new(args);
// Bypass startup for errors or help and version commands
if (CommandLine.BypassStartup(commandLine.Result))
{
return await commandLine.Result.InvokeAsync().ConfigureAwait(false);
}
// Log to the console
LoggerConfiguration loggerConfiguration = new LoggerConfiguration()
.Enrich.WithThreadId()
.WriteTo.Console(
theme: AnsiConsoleTheme.Code,
formatProvider: CultureInfo.InvariantCulture,
outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] [t:{ThreadId}{ThreadName}] {Message:lj}{NewLine}{Exception}"
);
Log.Logger = loggerConfiguration.CreateLogger();
// Invoke command
return await commandLine.Result.InvokeAsync().ConfigureAwait(false);
}
catch (OperationCanceledException)
{
Log.Logger.Warning("Operation was cancelled.");
return 130; // POSIX standard for SIGINT
}
catch (Exception ex) when (Log.Logger.LogAndHandle(ex))
{
return 1;
}
finally
{
await Log.CloseAndFlushAsync().ConfigureAwait(false);
}
}
internal async Task<int> ExecuteVersionAsync()
{
ArgumentNullException.ThrowIfNull(commandLineOptions.VersionPath);
// Get versions for all products using releases API
VersionJsonSchema versionSchema = new();
Log.Logger.Information("Getting version information online...");
versionSchema.Products.AddRange(ProductInfo.GetProducts());
foreach (ProductInfo productInfo in versionSchema.Products)
{
await productInfo.FetchVersionsAsync(cancellationToken).ConfigureAwait(false);
productInfo.LogInformation();
await productInfo.VerifyUrlsAsync(cancellationToken).ConfigureAwait(false);
}
// Write to file
Log.Logger.Information(
"Writing version information to {Path}",
commandLineOptions.VersionPath.FullName
);
VersionJsonSchema.ToFile(commandLineOptions.VersionPath.FullName, versionSchema);
return 0;
}
internal async Task<int> ExecuteMatrixAsync()
{
ArgumentNullException.ThrowIfNull(commandLineOptions.VersionPath);
ArgumentNullException.ThrowIfNull(commandLineOptions.MatrixPath);
// Load version info from file
Log.Logger.Information(
"Reading version information from {Path}",
commandLineOptions.VersionPath.FullName
);
VersionJsonSchema fileSchema = VersionJsonSchema.FromFile(
commandLineOptions.VersionPath.FullName
);
// Re-verify as rules may have changed after file was written
foreach (ProductInfo productInfo in fileSchema.Products)
{
productInfo.Verify();
productInfo.LogInformation();
}
// Update version information
if (commandLineOptions.UpdateVersion)
{
// Get versions for all products using releases API
VersionJsonSchema onlineSchema = new();
Log.Logger.Information("Getting version information online...");
onlineSchema.Products.AddRange(ProductInfo.GetProducts());
foreach (ProductInfo productInfo in onlineSchema.Products)
{
await productInfo.FetchVersionsAsync(cancellationToken).ConfigureAwait(false);
productInfo.LogInformation();
}
// Make sure the labelled version numbers do not regress
ReleaseVersionForward.Verify(fileSchema.Products, onlineSchema.Products);
// Verify URL's
foreach (ProductInfo productInfo in onlineSchema.Products)
{
await productInfo.VerifyUrlsAsync(cancellationToken).ConfigureAwait(false);
}
// Update the file version with the online version
Log.Logger.Information(
"Writing version information to {Path}",
commandLineOptions.VersionPath.FullName
);
VersionJsonSchema.ToFile(commandLineOptions.VersionPath.FullName, onlineSchema);
fileSchema = onlineSchema;
}
else
{
// Verify URL's
foreach (ProductInfo productInfo in fileSchema.Products)
{
await productInfo.VerifyUrlsAsync(cancellationToken).ConfigureAwait(false);
}
}
// Create matrix
Log.Logger.Information("Creating Matrix from versions");
MatrixJsonSchema matrixSchema = new();
matrixSchema.Images.AddRange(ImageInfo.CreateImages(fileSchema.Products));
Log.Logger.Information("Created {Count} images in matrix", matrixSchema.Images.Count);
matrixSchema.Images.ForEach(imageInfo => imageInfo.LogInformation());
// Write matrix
Log.Logger.Information(
"Writing matrix information to {Path}",
commandLineOptions.MatrixPath.FullName
);
MatrixJsonSchema.ToFile(commandLineOptions.MatrixPath.FullName, matrixSchema);
return 0;
}
internal Task<int> ExecuteMakeAsync()
{
ArgumentNullException.ThrowIfNull(commandLineOptions.VersionPath);
ArgumentNullException.ThrowIfNull(commandLineOptions.MakeDirectory);
ArgumentNullException.ThrowIfNull(commandLineOptions.DockerDirectory);
// Load version info from file
Log.Logger.Information(
"Reading version information from {Path}",
commandLineOptions.VersionPath.FullName
);
VersionJsonSchema versionSchema = VersionJsonSchema.FromFile(
commandLineOptions.VersionPath.FullName
);
// Create Compose files
ComposeFile.Create(commandLineOptions.MakeDirectory);
// Create Docker files
List<ProductInfo> products = [.. versionSchema.Products];
DockerFile.Create(
products,
commandLineOptions.DockerDirectory.FullName,
commandLineOptions.VersionLabel
);
return Task.FromResult(0);
}
}