-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathProgram.cs
214 lines (183 loc) · 7.87 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
210
211
212
213
214
using ElectronSharp.CLI.Commands;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace ElectronSharp.CLI
{
class Program
{
static async Task Main(string[] args)
{
if (args.Length == 0)
{
PrintUsageHeader();
PrintUsage();
Environment.Exit(-1);
}
Console.CancelKeyPress += (s, e) =>
{
ProcessHelper.KillActive();
Environment.Exit(-1);
};
ICommand command = null;
switch (args[0])
{
case StartElectronCommand.COMMAND_NAME:
command = new StartElectronCommand(args.Skip(1).ToArray());
break;
case BuildCommand.COMMAND_NAME:
command = new BuildCommand(args.Skip(1).ToArray());
break;
case InitCommand.COMMAND_NAME:
command = new InitCommand(args.Skip(1).ToArray());
break;
case AddCommand.COMMAND_NAME:
command = new AddCommand(args.Skip(1).ToArray());
break;
case VersionCommand.COMMAND_NAME:
command = new VersionCommand();
break;
case "--help":
case "--h":
case "help":
PrintUsageHeader();
if (args.Length > 1)
PrintUsage(args[1]);
else
PrintUsage();
break;
default:
Console.Error.WriteLine($"Unknown command {args[0]}");
PrintUsage();
Environment.Exit(-1);
break;
}
if (command != null)
{
var success = await command.ExecuteAsync();
if (!success)
{
Environment.Exit(-1);
}
}
}
private static void PrintUsageHeader()
{
var sb = new StringBuilder("ElectronSharp Tools");
var version = Version;
if (!string.IsNullOrEmpty(version))
{
sb.Append($" ({version})");
}
Console.WriteLine(sb.ToString());
Console.WriteLine("Project Home: https://github.com/ElectronSharp/ElectronSharp");
Console.WriteLine("\t");
}
private static void PrintUsage()
{
const int NAME_WIDTH = 23;
Console.WriteLine("\t");
Console.WriteLine("Commands to start the Electron Application:");
Console.WriteLine("\t");
Console.WriteLine($"\t{StartElectronCommand.COMMAND_NAME,-NAME_WIDTH} {StartElectronCommand.COMMAND_DESCRIPTION}");
Console.WriteLine("\t");
Console.WriteLine("Command to build the Electron Application:");
Console.WriteLine("\t");
Console.WriteLine($"\t{BuildCommand.COMMAND_NAME,-NAME_WIDTH} {BuildCommand.COMMAND_DESCRIPTION}");
Console.WriteLine("\t");
Console.WriteLine("Command to init the Electron Application:");
Console.WriteLine("\t");
Console.WriteLine($"\t{InitCommand.COMMAND_NAME,-NAME_WIDTH} {InitCommand.COMMAND_DESCRIPTION}");
Console.WriteLine("\t");
Console.WriteLine("Command to add a custom npm packages to the Electron Application:");
Console.WriteLine("\t");
Console.WriteLine($"\t{AddCommand.COMMAND_NAME,-NAME_WIDTH} {AddCommand.COMMAND_DESCRIPTION}");
Console.WriteLine("\t");
Console.WriteLine("Commands to see the current ElectronSharp version number:");
Console.WriteLine("\t");
Console.WriteLine($"\t{VersionCommand.COMMAND_NAME,-NAME_WIDTH} {VersionCommand.COMMAND_DESCRIPTION}");
Console.WriteLine("\t");
Console.WriteLine("\t");
Console.WriteLine("To get help on individual commands execute:");
Console.WriteLine("\telectron-sharp help <command>");
}
private static void PrintUsage(string command)
{
switch (command)
{
case StartElectronCommand.COMMAND_NAME:
PrintUsage(StartElectronCommand.COMMAND_NAME, StartElectronCommand.COMMAND_DESCRIPTION, StartElectronCommand.CommandOptions, StartElectronCommand.COMMAND_ARGUMENTS);
break;
case BuildCommand.COMMAND_NAME:
PrintUsage(BuildCommand.COMMAND_NAME, BuildCommand.COMMAND_DESCRIPTION, BuildCommand.CommandOptions, BuildCommand.COMMAND_ARGUMENTS);
break;
case InitCommand.COMMAND_NAME:
PrintUsage(InitCommand.COMMAND_NAME, InitCommand.COMMAND_DESCRIPTION, InitCommand.CommandOptions, InitCommand.COMMAND_ARGUMENTS);
break;
case AddCommand.COMMAND_NAME:
PrintUsage(AddCommand.COMMAND_NAME, AddCommand.COMMAND_DESCRIPTION, AddCommand.CommandOptions, AddCommand.COMMAND_ARGUMENTS);
break;
case VersionCommand.COMMAND_NAME:
PrintUsage(VersionCommand.COMMAND_NAME, VersionCommand.COMMAND_DESCRIPTION, VersionCommand.CommandOptions, VersionCommand.COMMAND_ARGUMENTS);
break;
default:
Console.Error.WriteLine($"Unknown command {command}");
PrintUsage();
break;
}
}
private static void PrintUsage(string command, string description, IList<CommandOption> options, string arguments)
{
const int INDENT = 3;
Console.WriteLine($"{command}: ");
Console.WriteLine($"{new string(' ', INDENT)}{description}");
Console.WriteLine("\t");
if (!string.IsNullOrEmpty(arguments))
{
Console.WriteLine($"{new string(' ', INDENT)}electron-sharp {command} [arguments] [options]");
Console.WriteLine($"{new string(' ', INDENT)}Arguments:");
Console.WriteLine($"{new string(' ', INDENT * 2)}{arguments}");
}
else
{
Console.WriteLine($"{new string(' ', INDENT)}electron-sharp {command} [options]");
}
const int SWITCH_COLUMN_WIDTH = 40;
Console.WriteLine($"{new string(' ', INDENT)}Options:");
foreach (var option in options)
{
StringBuilder stringBuilder = new StringBuilder();
if (option.ShortSwitch != null)
{
stringBuilder.Append($"{option.ShortSwitch,-6} | ");
}
stringBuilder.Append($"{option.Switch}");
if (stringBuilder.Length < SWITCH_COLUMN_WIDTH)
{
stringBuilder.Append(new string(' ', SWITCH_COLUMN_WIDTH - stringBuilder.Length));
}
stringBuilder.Append(option.Description);
Console.WriteLine($"{new string(' ', INDENT * 2)}{stringBuilder}");
}
}
private static string Version
{
get
{
AssemblyInformationalVersionAttribute attribute = null;
try
{
attribute = Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyInformationalVersionAttribute>();
}
catch (AmbiguousMatchException)
{
// Catch exception and continue if multiple attributes are found.
}
return attribute?.InformationalVersion;
}
}
}
}