-
Notifications
You must be signed in to change notification settings - Fork 9
/
Program.cs
209 lines (172 loc) · 7.99 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
using System.CommandLine;
using Serilog;
using Serilog.Debugging;
using Serilog.Sinks.SystemConsole.Themes;
// dotnet publish --self-contained false --output ./publish
// ./publish/CreateMatrix matrix --update --matrix ./JSON/Matrix.json --version ./JSON/Version.json
// echo $?
namespace CreateMatrix;
public static class Program
{
private static async Task<int> Main(string[] args)
{
// Configure logger
ConfigureLogger();
// Create command line options
var rootCommand = CreateCommandLine();
// Run
// 0 == ok, 1 == error
return await rootCommand.InvokeAsync(args);
}
private static RootCommand CreateCommandLine()
{
var versionOption = new Option<string>(
name: "--version",
description: "Version JSON file.",
getDefaultValue: () => "./Make/Version.json");
var matrixOption = new Option<string>(
name: "--matrix",
description: "Matrix JSON file.",
getDefaultValue: () => "./Make/Matrix.json");
var schemaVersionOption = new Option<string>(
name: "--schemaversion",
description: "Version JSON schema file.",
getDefaultValue: () => "./JSON/Version.schema.json");
var schemaMatrixOption = new Option<string>(
name: "--schemamatrix",
description: "Matrix JSON schema file.",
getDefaultValue: () => "./JSON/Matrix.schema.json");
var updateOption = new Option<bool>(
name: "--update",
description: "Update version information",
getDefaultValue: () => false);
var makeOption = new Option<string>(
name: "--make",
description: "Make directory.",
getDefaultValue: () => "./Make");
var dockerOption = new Option<string>(
name: "--docker",
description: "Docker directory.",
getDefaultValue: () => "./Docker");
var labelOption = new Option<VersionInfo.LabelType>(
name: "--label",
description: "Version label.",
getDefaultValue: () => VersionInfo.LabelType.Latest);
var versionCommand = new Command("version", "Create version information file")
{
versionOption
};
versionCommand.SetHandler(VersionHandler, versionOption);
var matrixCommand = new Command("matrix", "Create matrix information file")
{
versionOption,
matrixOption,
updateOption
};
matrixCommand.SetHandler(MatrixHandler, versionOption, matrixOption, updateOption);
var schemaCommand = new Command("schema", "Write Version and Matrix JSON schema files")
{
schemaVersionOption,
schemaMatrixOption
};
schemaCommand.SetHandler(SchemaHandler, schemaVersionOption, schemaMatrixOption);
var makeCommand = new Command("make", "Create Docker and Compose files from Version file")
{
versionOption,
makeOption,
dockerOption,
labelOption
};
makeCommand.SetHandler(MakeHandler, versionOption, makeOption, dockerOption, labelOption);
var rootCommand = new RootCommand("CreateMatrix utility to create a matrix of builds from product versions")
{
versionCommand,
matrixCommand,
schemaCommand,
makeCommand
};
return rootCommand;
}
private static void ConfigureLogger()
{
// Configure serilog console logging
SelfLog.Enable(Console.Error);
LoggerConfiguration loggerConfiguration = new();
loggerConfiguration.WriteTo.Console(theme: AnsiConsoleTheme.Code, outputTemplate: "{Timestamp:HH:mm:ss} [{Level:u3}] {Message}{NewLine}{Exception}");
Log.Logger = loggerConfiguration.CreateLogger();
}
private static Task<int> SchemaHandler(string schemaVersionPath, string schemaMatrixPath)
{
Log.Logger.Information("Writing schema to file : Version Path: {VersionPath}, Matrix Path: {MatrixPath}", schemaVersionPath, schemaMatrixPath);
VersionJsonSchema.GenerateSchema(schemaVersionPath);
MatrixJsonSchema.GenerateSchema(schemaMatrixPath);
return Task.FromResult(0);
}
private static Task<int> VersionHandler(string versionPath)
{
// Get versions for all products using releases API
VersionJsonSchema versionSchema = new();
Log.Logger.Information("Getting version information online...");
versionSchema.Products = ProductInfo.GetProducts();
versionSchema.Products.ForEach(item => item.GetVersions());
versionSchema.Products.ForEach(item => item.LogInformation());
versionSchema.Products.ForEach(item => item.VerifyUrls());
// Write to file
Log.Logger.Information("Writing version information to {Path}", versionPath);
VersionJsonSchema.ToFile(versionPath, versionSchema);
return Task.FromResult(0);
}
private static Task<int> MatrixHandler(string versionPath, string matrixPath, bool updateVersion)
{
// Load version info from file
Log.Logger.Information("Reading version information from {Path}", versionPath);
var fileSchema = VersionJsonSchema.FromFile(versionPath);
// Re-verify as rules may have changed after file was written
fileSchema.Products.ForEach(item => item.Verify());
fileSchema.Products.ForEach(item => item.LogInformation());
// Update version information
if (updateVersion)
{
// Get versions for all products using releases API
VersionJsonSchema onlineSchema = new();
Log.Logger.Information("Getting version information online...");
onlineSchema.Products = ProductInfo.GetProducts();
onlineSchema.Products.ForEach(item => item.GetVersions());
onlineSchema.Products.ForEach(item => item.LogInformation());
// Make sure the labelled version numbers do not regress
ReleaseVersionForward.Verify(fileSchema.Products, onlineSchema.Products);
// Verify URL's
onlineSchema.Products.ForEach(item => item.VerifyUrls());
// Update the file version with the online version
Log.Logger.Information("Writing version information to {Path}", versionPath);
VersionJsonSchema.ToFile(versionPath, onlineSchema);
fileSchema = onlineSchema;
}
else
{
// Verify URL's
fileSchema.Products.ForEach(item => item.VerifyUrls());
}
// Create matrix
Log.Logger.Information("Creating Matrix from versions");
MatrixJsonSchema matrixSchema = new() { Images = ImageInfo.CreateImages(fileSchema.Products) };
Log.Logger.Information("Created {Count} images in matrix", matrixSchema.Images.Count);
// Log info
matrixSchema.Images.ForEach(item => item.LogInformation());
// Write matrix
Log.Logger.Information("Writing matrix information to {Path}", matrixPath);
MatrixJsonSchema.ToFile(matrixPath, matrixSchema);
return Task.FromResult(0);
}
private static Task<int> MakeHandler(string versionPath, string makePath, string dockerPath, VersionInfo.LabelType label)
{
// Load version info from file
Log.Logger.Information("Reading version information from {Path}", versionPath);
var versionSchema = VersionJsonSchema.FromFile(versionPath);
// Create Compose files
ComposeFile.Create(makePath);
// Create Docker files
DockerFile.Create(versionSchema.Products, dockerPath, label);
return Task.FromResult(0);
}
}