/
Program.cs
80 lines (67 loc) · 1.82 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
using System.CommandLine;
using System.CommandLine.Builder;
using System.CommandLine.Parsing;
using RunScript;
var workingDirectory = Environment.CurrentDirectory;
Environment.SetEnvironmentVariable("INIT_CWD", workingDirectory);
Project? project;
Dictionary<string, string?> scripts;
try
{
(project, scripts, workingDirectory) = await new ProjectLoader().Load(workingDirectory);
}
catch (Exception ex)
{
logError(ex.Message);
return 1;
}
var rootCommand = new RootCommand();
rootCommand.TreatUnmatchedTokensAsErrors = false;
rootCommand.AddGlobalOption(GlobalOptions.IfPresent);
rootCommand.AddGlobalOption(GlobalOptions.ScriptShell);
rootCommand.AddGlobalOption(GlobalOptions.Verbose);
foreach (var (name, script) in scripts.OrderBy(s => s.Key))
{
var runScript = new RunScriptCommand(
name,
script,
project.ScriptShell,
scripts,
workingDirectory);
rootCommand.AddCommand(runScript);
}
if (!scripts.ContainsKey("env"))
{
rootCommand.AddCommand(new EnvironmentCommand());
}
var parser = new CommandLineBuilder(rootCommand)
.UseVersionOption()
.UseHelp()
.UseEnvironmentVariableDirective()
.UseParseDirective()
.UseSuggestDirective()
.RegisterWithDotnetSuggest()
.RunScriptsIfPrsent()
.UseParseErrorReporting()
.UseExceptionHandler((ex, ctx) =>
{
if (ctx.ParseResult.HasOption(GlobalOptions.Verbose))
{
logError(ex.ToString());
}
else
{
logError(ex.Message);
}
})
.CancelOnProcessTermination()
.EnableLegacyDoubleDashBehavior()
.Build();
var parseResult = parser.Parse(args);
return await parseResult.InvokeAsync();
static void logError(string message)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(message);
Console.ResetColor();
}