-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
482 lines (404 loc) · 21.9 KB
/
Program.cs
File metadata and controls
482 lines (404 loc) · 21.9 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
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Spectre.Console;
using Spectre.Console.Cli;
using WebDev.Tool.Helper.Internal;
using WebDev.Tool.Helper.Internal.Config;
using WebDev.Tool.Classes;
using WebDev.Tool.Classes.Configuration;
using WebDev.Tool.Commands.Shell;
using WebDev.Tool.Commands.Apache;
using WebDev.Tool.Commands.Services;
using WebDev.Tool.Commands;
using WebDev.Tool.Commands.Config;
using WebDev.Tool.Commands.Info;
using WebDev.Tool.Commands.ModeJS;
using WebDev.Tool.Commands.NodeJS;
using WebDev.Tool.Commands.Php;
using WebDev.Tool.Commands.Project;
using WebDev.Tool.Commands.Restore;
using WebDev.Tool.Commands.Secrets;
using WebDev.Tool.Commands.tasks;
using WebDev.Tool.Commands.terminal;
using WebDev.Tool.Commands.workspaces;
using WebDev.Tool.Helper;
using WebDev.Tool.Helper.Internal.Config.Sections;
using WebDev.Tool.Commands.Mysql;
namespace WebDev.Tool
{
internal class Program
{
static void Main(string[] args)
{
var app = new CommandApp();
var version = UpdateHelper.CurrentVersion;
if (args.Contains("--not-main") || args.Contains("-n"))
{
PathHelper.IsMainWorkspace = false;
}
if (args.Contains("--no-header"))
{
EnvironmentHelper.DisableProgramHeader();
}
// Output the program name, version and info if the config file could not be read
OutputProgramHeader(version, args.Contains("--debug"));
// Load additional commands that are defined within shell scripts
var additionalCommands = new Dictionary<string, CustomBranch>();
try {
additionalCommands = CustomCommandsLoader.Load();
} catch (Exception e) {
AnsiConsole.MarkupLine("[red]Unable to load the custom commands[/] - [orange3]Append '--debug' to show more details[/]");
// With he debug arg, output the exception and exit
if (args.Contains("--debug")) {
AnsiConsole.WriteException(e);
return;
}
}
app.Configure(config =>
{
config.SetApplicationName("webdev");
config.SetApplicationVersion(version);
// Add Branches and their commands
if (EnvironmentHelper.IsRunningInDevContainer())
{
config.AddBranch("apache", branch => AddApacheCommandBranch(branch, additionalCommands));
}
config.AddBranch("services", branch => AddServicesCommandBranch(branch, additionalCommands));
config.AddBranch("config", branch => AddConfigCommandBranch(branch, additionalCommands));
if (!EnvironmentHelper.IsRunningInDevContainer())
{
config.AddBranch("project", branch => AddProjectCommandBranch(branch, additionalCommands));
config.AddBranch("secrets", branch => AddSecretsCommandBranch(branch, additionalCommands));
}
if (!EnvironmentHelper.IsRunningInDevContainer())
{
config.AddBranch("mysql", branch => AddMysqlCommandBranch(branch, additionalCommands));
}
if (EnvironmentHelper.IsRunningInDevContainer())
{
config.AddBranch("nodejs", branch => AddNodeJsCommandBranch(branch, additionalCommands));
config.AddBranch("php", branch => AddPhpCommandBranch(branch, additionalCommands));
config.AddBranch("restore", branch => AddRestoreCommandBranch(branch, additionalCommands));
}
if (TasksConfig.Tasks.Count > 0)
{
config.AddBranch("task", branch => AddTaskCommandBranch(branch, TasksConfig.Tasks));
}
config.AddBranch("tasks", branch => AddTasksCommandBranch(branch, additionalCommands));
if (!EnvironmentHelper.IsRunningInDevContainer())
{
config.AddCommand<OpenDevcontainerTerminalCommand>("terminal").WithDescription("Open a terminal to the running devcontainer");
}
config.AddCommand<SelfUpdateCommand>("update").WithDescription("Update this tool to the latest version");
// Commands that are executed for various actions, like project start, to show the user some infos and what he can do next etc.
config.AddCommand<ShowProjectInfoCommand>("info");
//config.AddCommand<ShowWebdevInstallSummaryCommand>("webdev-install-summary").IsHidden();
// Prepare and run workspaces
config.AddCommand<OnInitWorkspacesCommand>("workspaces-on-init").IsHidden();
config.AddCommand<PostStartWorkspacesCommand>("workspaces-post-start").IsHidden();
List<string> reservedBranches = new() { "default", "config", "php", "nodejs", "apache", "mysql", "services", "restore", "secrets", "tasks", "task", "tests" };
// Add Tests branch
if (TestsConfig.Tests.Count > 0)
{
config.AddBranch("tests", branch =>
{
branch.SetDescription("Run tests");
foreach (KeyValuePair<string, TestEntryConfiguration> entry in TestsConfig.Tests) {
branch.AddCommand<TestsCommand>(entry.Key)
.WithData(entry.Value)
.WithDescription(entry.Value.Name);
}
});
}
// Add branches that haven´t been added yet via custom commands
foreach (KeyValuePair<string, CustomBranch> entry in additionalCommands.Where(x => !reservedBranches.Contains(x.Key))) {
config.AddBranch(entry.Value.Name, branch =>
{
branch.SetDescription(entry.Value.Description);
foreach (CustomCommand cmd in entry.Value.Commands) {
branch.AddCommand<ShellFileCommand>(cmd.Command)
.WithData(cmd)
.WithDescription(cmd.Description);
}
});
}
// Add all commands without branches
if (additionalCommands.TryGetValue("default", out CustomBranch branch)) {
foreach (CustomCommand cmd in branch.Commands) {
config.AddCommand<ShellFileCommand>(cmd.Command)
.WithData(cmd)
.WithDescription(cmd.Description);
}
}
});
app.Run(args);
if (ConfigHelper.ConfigFileExists && ConfigHelper.IsConfigFileValid && ConfigHelper.ConfigUpdated) {
try {
// Save config file
ConfigHelper.SaveConfigFile();
} catch (Exception e) {
AnsiConsole.WriteLine("[red]Saving the config file failed[/] - [orange3]Append '--debug' to show more details[/]");
if (args.Contains("--debug")) {
AnsiConsole.WriteException(e);
}
}
}
}
private static void OutputProgramHeader(string programVersion, bool showException = false)
{
if (!EnvironmentHelper.IsProgramHeaderDisabled())
{
AnsiConsole.Write(new FigletText("WebDev"));
AnsiConsole.Markup("[deepskyblue3]WebDev Tool[/] - Version [green]" + programVersion + "[/]");
AnsiConsole.Write(EnvironmentHelper.IsRunningInDevContainer() ? " - DevContainer Mode" : " - Local Mode");
}
// Try to load the config file
ConfigHelper.ReadConfigFile();
// Try to load the app settings
AppSettingsHelper.LoadAppSettings(showException);
if (EnvironmentHelper.IsProgramHeaderDisabled())
{
return;
}
try {
// Check for updates
var latestVersion = UpdateHelper.GetLatestVersion().Result;
var isUpdateAvailable = UpdateHelper.IsUpdateAvailable();
if (isUpdateAvailable) {
AnsiConsole.MarkupLine(" - [orange3]Latest Version is " + latestVersion + ". Use 'webdev update' to update.[/]");
} else {
AnsiConsole.MarkupLine("");
}
} catch (Exception e) {
AnsiConsole.MarkupLine(" - [red]Check for update failed[/]");
if (showException) {
AnsiConsole.WriteException(e);
}
}
if (!ConfigHelper.ConfigFileExists) {
AnsiConsole.MarkupLine("[orange3]No config file found - falling back to default settings[/]");
} else if(!ConfigHelper.IsConfigFileValid) {
AnsiConsole.MarkupLine("[red]Config file is invalid - falling back to default settings[/] - [orange3]Append '--debug' to show more details[/]");
if (showException) {
try {
ConfigHelper.ReadConfigFile(true);
} catch (Exception e) {
AnsiConsole.WriteException(e);
}
}
}
}
private static void AddConfigCommandBranch(IConfigurator<CommandSettings> branch, Dictionary<string, CustomBranch> additionalCommands)
{
branch.SetDescription("Creates or verify the configuration file");
branch.AddCommand<VerifyConfigCommand>("verify")
.WithAlias("v")
.WithDescription(@"Tries to read the config file and shows it`s content");
if (additionalCommands.TryGetValue("config", out CustomBranch customBranch)) {
foreach (CustomCommand cmd in customBranch.Commands) {
branch.AddCommand<ShellFileCommand>(cmd.Command)
.WithData(cmd)
.WithDescription(cmd.Description);
}
}
}
private static void AddTaskCommandBranch(IConfigurator<CommandSettings> branch, Dictionary<string, TaskEntryConfiguration> Tasks)
{
branch.SetDescription("Run a specific task defined in the config file");
foreach (KeyValuePair<string, TaskEntryConfiguration> entry in Tasks) {
branch.AddCommand<RunTaskCommand>(entry.Key)
.WithData(entry.Key)
.WithDescription(entry.Value.Name);
}
}
private static void AddTasksCommandBranch(IConfigurator<CommandSettings> branch, Dictionary<string, CustomBranch> additionalCommands)
{
branch.SetDescription("Run a specific section of all tasks defined in the config file");
branch.AddCommand<RunTasksCommand>("prebuild")
.WithData("prebuild")
.WithAlias("p")
.WithDescription(@"Runs prebuild sections of all tasks");
branch.AddCommand<RunTasksCommand>("create")
.WithData("create")
.WithAlias("c")
.WithDescription(@"Runs create sections of all tasks");
branch.AddCommand<RunTasksCommand>("start")
.WithData("start")
.WithAlias("s")
.WithDescription(@"Runs start sections of all tasks");
branch.AddCommand<RunTasksCommand>("init")
.WithData("init")
.WithAlias("i")
.WithDescription(@"Runs init sections of all tasks");
if (additionalCommands.TryGetValue("tasks", out CustomBranch customBranch)) {
foreach (CustomCommand cmd in customBranch.Commands) {
branch.AddCommand<ShellFileCommand>(cmd.Command)
.WithData(cmd)
.WithDescription(cmd.Description);
}
}
}
private static void AddPhpCommandBranch(IConfigurator<CommandSettings> branch, Dictionary<string, CustomBranch> additionalCommands)
{
branch.SetDescription("Different commands to change active php version, ini settings etc.");
branch.AddCommand<PhpVersionCommand>("version")
.WithAlias("v")
.WithDescription("Shows or sets the currently used PHP Version");
branch.AddCommand<PhpIniCommand>("ini")
.WithAlias("i")
.WithDescription("Change the value of a PHP setting.");
branch.AddCommand<PhpRestoreCommand>("restore")
.WithAlias("r")
.WithDescription("Restores a previously set PHP version and their settings");
branch.AddCommand<PhpDebugCommand>("xdebug")
.WithAlias("d")
.WithDescription("Shows or sets the current xdebug mode");
branch.AddCommand<PhpPackageCommand>("packages")
.WithAlias("p")
.WithDescription("Shows installed php packages or install new ones");
if (additionalCommands.TryGetValue("php", out CustomBranch customBranch)) {
foreach (CustomCommand cmd in customBranch.Commands) {
branch.AddCommand<ShellFileCommand>(cmd.Command)
.WithData(cmd)
.WithDescription(cmd.Description);
}
}
}
private static void AddNodeJsCommandBranch(IConfigurator<CommandSettings> branch, Dictionary<string, CustomBranch> additionalCommands)
{
branch.SetDescription("Different commands to change active nodejs version, etc.");
branch.AddCommand<NodeJSVersionCommand>("version")
.WithAlias("v")
.WithDescription("Shows or sets the currently used NodeJS Version");
branch.AddCommand<NodeJSRestoreCommand>("restore")
.WithAlias("r")
.WithDescription("Restores a previously set NodeJS version");
if (additionalCommands.TryGetValue("nodejs", out CustomBranch customBranch)) {
foreach (CustomCommand cmd in customBranch.Commands) {
branch.AddCommand<ShellFileCommand>(cmd.Command)
.WithData(cmd)
.WithDescription(cmd.Description);
}
}
}
private static void AddApacheCommandBranch(IConfigurator<CommandSettings> branch, Dictionary<string, CustomBranch> additionalCommands)
{
branch.SetDescription("Some simple commands to start/stop/restart the apache webserver");
branch.AddCommand<ApacheStatusCommand>("status")
.WithDescription("Shows the current status of apache");
branch.AddCommand<ApacheStartCommand>("start")
.WithDescription("Starts apache");
branch.AddCommand<ApacheStopCommand>("stop")
.WithDescription("Stops apache");
branch.AddCommand<ApacheRestartCommand>("restart")
.WithDescription("Restarts apache");
if (additionalCommands.TryGetValue("apache", out CustomBranch customBranch)) {
foreach (CustomCommand cmd in customBranch.Commands) {
branch.AddCommand<ShellFileCommand>(cmd.Command)
.WithData(cmd)
.WithDescription(cmd.Description);
}
}
}
private static void AddMysqlCommandBranch(IConfigurator<CommandSettings> branch, Dictionary<string, CustomBranch> additionalCommands)
{
branch.SetDescription("Various commands to interact with the mysql database");
branch.AddCommand<MysqlPullCommand>("update")
.WithDescription("Pulls the latest version of the custom database image");
/* branch.AddCommand<NotYetImplementedCommand>("export")
.WithDescription("Exports the content of the database to a file [red]Not implemented yet[/]");
branch.AddCommand<NotYetImplementedCommand>("import")
.WithDescription("Imports database content from a file [red]Not implemented yet[/]");
branch.AddCommand<NotYetImplementedCommand>("snapshot")
.WithDescription("Create/Restore a snapshot of the database. Useful to make a backup before you test something and want to restore the old state fast if anything goes wrong [red]Not implemented yet[/]"); */
if (additionalCommands.TryGetValue("mysql", out CustomBranch customBranch)) {
foreach (CustomCommand cmd in customBranch.Commands) {
branch.AddCommand<ShellFileCommand>(cmd.Command)
.WithData(cmd)
.WithDescription(cmd.Description);
}
}
}
private static void AddServicesCommandBranch(IConfigurator<CommandSettings> branch, Dictionary<string, CustomBranch> additionalCommands)
{
branch.SetDescription("List, status of services and define which should be started");
branch.AddCommand<ListServicesCommand>("list")
.WithDescription("List available the services");
branch.AddCommand<StartServicesCommand>("start")
.WithDescription("Start the services that are marked as active");
branch.AddCommand<StopServicesCommand>("stop")
.WithDescription("Stops running services");
branch.AddCommand<SelectServicesCommand>("select")
.WithDescription("Select which services should be active");
if (additionalCommands.TryGetValue("services", out CustomBranch customBranch)) {
foreach (CustomCommand cmd in customBranch.Commands) {
branch.AddCommand<ShellFileCommand>(cmd.Command)
.WithData(cmd)
.WithDescription(cmd.Description);
}
}
}
private static void AddRestoreCommandBranch(IConfigurator<CommandSettings> branch, Dictionary<string, CustomBranch> additionalCommands)
{
branch.SetDescription("Restore settings separate for nodejs or php, or for all at once ");
branch.AddCommand<RestoreAllCommand>("all")
.WithAlias("a")
.WithDescription("Restore all settings");
branch.AddCommand<RestorePhpCommand>("php")
.WithAlias("p")
.WithDescription("Restore settings for php");
branch.AddCommand<RestoreNodeJsCommand>("nodejs")
.WithAlias("n")
.WithDescription("Restore settings for NodeJS");
branch.AddCommand<RestoreEnvCommand>("env")
.WithAlias("e")
.WithDescription("Restore environment variables");
if (additionalCommands.TryGetValue("restore", out CustomBranch customBranch)) {
foreach (CustomCommand cmd in customBranch.Commands) {
branch.AddCommand<ShellFileCommand>(cmd.Command)
.WithData(cmd)
.WithDescription(cmd.Description);
}
}
}
private static void AddSecretsCommandBranch(IConfigurator<CommandSettings> branch, Dictionary<string, CustomBranch> additionalCommands)
{
branch.SetDescription("Loads external secrets");
branch.AddCommand<LoadSecretsCommand>("load")
.WithAlias("l")
.WithDescription(@"Reads the config file and loads the defined secrets");
branch.AddCommand<ExportSecretsCommand>("export")
.WithAlias("e")
.WithDescription(@"Exports the secrets to the console");
if (additionalCommands.TryGetValue("secrets", out CustomBranch customBranch)) {
foreach (CustomCommand cmd in customBranch.Commands) {
branch.AddCommand<ShellFileCommand>(cmd.Command)
.WithData(cmd)
.WithDescription(cmd.Description);
}
}
}
private static void AddProjectCommandBranch(IConfigurator<CommandSettings> branch, Dictionary<string, CustomBranch> additionalCommands)
{
branch.SetDescription("Project commands");
/* branch.AddCommand<InitProjectCommand>("init")
.WithAlias("i")
.WithDescription(@"Creates a new project from a given Repo and sets up the necessary devcontainer"); */
branch.AddCommand<StartProjectCommand>("start")
.WithAlias("s")
.WithDescription(@"Checks if the current folder contains a devcontainer spec and starts it");
branch.AddCommand<StopProjectCommand>("stop")
.WithAlias("st")
.WithDescription(@"Checks if the current folder contains a devcontainer spec and stops it");
if (additionalCommands.TryGetValue("project", out CustomBranch customBranch)) {
foreach (CustomCommand cmd in customBranch.Commands) {
branch.AddCommand<ShellFileCommand>(cmd.Command)
.WithData(cmd)
.WithDescription(cmd.Description);
}
}
}
}
}