-
Notifications
You must be signed in to change notification settings - Fork 0
/
Options.cs
346 lines (286 loc) · 12 KB
/
Options.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
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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using CommandLine;
using CommandLine.Text;
using Newtonsoft.Json;
namespace Pocoyo
{
public class Options
{
private static string _errorMessage;
/// <summary>
/// Files or Folders to parse
/// </summary>
[ValueList(typeof(List<string>))]
public IList<string> Files { get; set; }
/// <summary>
/// Additional Typescript Files to include
/// </summary>
[Option('t', "AdditionalFiles", Required = false, HelpText = "Additional typescript files to include")]
public string AdditionalFiles { get; set; }
public string[] TypescriptFiles => AdditionalFiles?.Split(',').Select(item => Path.GetFullPath(item.Trim())).ToArray();
[Option('o', "output", Required = false, HelpText = "(Required) - Output file or folder. If folder, then uses input file name for output file name within output folder.")]
public string OutputFile { get; set; }
/// <summary>
/// Namespace to use in typescript definitions
/// </summary>
[Option('n', "namespace", Required = false, HelpText = "Alternate namespace to use in typescript definitions. Defaults c# file's namespace.")]
public string Namespace { get; set; }
[OptionList('e', "Excluded", Separator = ',', HelpText = "List of types that should be exclude (comma seperated). For example MyClass,MyEnum.")]
public IList<string> Excluded { get; set; }
[OptionList('f', "ExcludedAttributes", Separator = ',', HelpText = "List of Attributes that should be exclude (comma seperated). For example JsonIgnore,NotMapped.")]
public IList<string> ExcludedAttributes { get; set; }
[OptionList('k', "Known", Separator = ',', HelpText = "List of known types which are not found in c# files (comma seperated). For example T as in MyClass<T>.")]
public IList<string> KnownTypes { get; set; }
[Option('c', "commands", Required = false, HelpText = "Read command line args in from specified file.")]
public string CommandFile { get; set; }
[Option('d', "PocoyoDataFilePath", Required = false, HelpText = "Pocoyo Data file path")]
public string PocoyoDataFilePath { get; set; }
[Option('p', "SkipPreprocess", DefaultValue = false, HelpText = "Skips pre-processing files for types.")]
public bool SkipPreprocess { get; set; }
[Option('v', "verbose", DefaultValue = false, HelpText = "Prints all messages.")]
public bool Verbose { get; set; }
[Option('s', "Silent", DefaultValue = false, HelpText = "Turns off all console messages.")]
public bool Silent { get; set; }
[JsonIgnore]
public List<string> InputFiles { get; set; } = new List<string>();
[JsonIgnore]
public string OutputFolder { get; set; }
[JsonIgnore]
public string OutputFilePath => string.IsNullOrEmpty(OutputFolder) ? OutputFile : null;
[JsonIgnore]
public bool PreProcess => !SkipPreprocess;
[ParserState]
[JsonIgnore]
public IParserState LastParserState { get; set; }
private void LogError(string errorMessage)
{
_errorMessage += errorMessage + "\r\n";
}
[HelpOption]
public string GetUsage()
{
var helpText = new HelpText
{
Heading = $@"{Utility.AssemblyName} [c# file(s) / folder(s)] ... [options]:
Generates typescript definition files from files or folder containing c# files.
Options:
[c# file(s) / folder(s)] (Required) c# source files or folder containing c# files
Specifies c# file(s) and/or c# folder(s) to convert to typescript definition files.
Separate files/folders by spaces.",
};
helpText.AddOptions(this);
var parseErrors = new HelpText().RenderParsingErrorsText(this, 3);
var errorMessage = string.IsNullOrEmpty(parseErrors) && string.IsNullOrEmpty(_errorMessage) ? "" : $@"
========================================================
========================================================
========================================================
PARSE ERRRORS
{parseErrors}{_errorMessage}
========================================================
========================================================
";
return $@"
{helpText}
Produced by Carlos Gomes ([email protected])
Examples:
{Utility.AssemblyName} Sample.cs -o Sample.d.ts
{Utility.AssemblyName} Sample.cs SampleFolder --output=Combined.d.ts --verbose --excluded=MyClass,MyEnum --excludedAttributes=JsonIgnore,NotMapped
{Utility.AssemblyName} Sample.cs SampleFolder --output=Combined.d.ts --Known=T
{Utility.AssemblyName} --commands=command_line_args_file.txt
{errorMessage}
";
}
private bool ProcessArgs(bool ignoreCommandFile = false)
{
if (!string.IsNullOrEmpty(PocoyoDataFilePath))
{
if (!File.Exists(PocoyoDataFilePath))
{
LogError($"Missing file: {PocoyoDataFilePath}");
return false;
}
PocoyoData.LoadFromData(PocoyoDataFilePath);
}
if (!ignoreCommandFile)
{
if (!string.IsNullOrEmpty(CommandFile))
{
if (!File.Exists(CommandFile))
{
LogError($"Missing --commands file: {CommandFile}");
return false;
}
return false;
}
}
if (Files.Count == 0)
{
LogError($"Missing input files / folders");
return false;
}
foreach (var filePath in Files)
{
var inputFile = Path.GetFullPath(filePath);
if (Directory.Exists(inputFile))
{
foreach (var csharpFilePath in Directory.EnumerateFiles(inputFile, "*.cs", SearchOption.AllDirectories))
{
var found = InputFiles.FirstOrDefault(item => string.Equals(item, csharpFilePath));
if (found == null)
InputFiles.Add(csharpFilePath);
}
}
else if (File.Exists(inputFile))
{
var found = InputFiles.FirstOrDefault(item => string.Equals(item, inputFile));
if (found == null)
InputFiles.Add(inputFile);
}
else
{
LogError($"Invalid file: {inputFile}");
return false;
}
}
if (string.IsNullOrEmpty(OutputFile))
{
LogError($"Missing --outputFile");
return false;
}
var outputFilePath = Path.GetFullPath(OutputFile);
var outputFolder = Path.GetDirectoryName(outputFilePath);
Directory.CreateDirectory(outputFolder);
if (Directory.Exists(outputFilePath))
{
OutputFolder = outputFilePath;
}
else
{
if (File.Exists(outputFilePath))
{
File.Delete(outputFilePath);
}
OutputFile = outputFilePath;
}
if (InputFiles.Count == 0)
{
LogError($"Missing input files / folders");
return false;
}
if (string.IsNullOrEmpty(CommandFile) || !CommandFile.ToLower().EndsWith(".json"))
SerializeToJson();
return true;
}
private void SerializeToJson()
{
try
{
var options = (Options)this.MemberwiseClone();
var outputFolder = !string.IsNullOrEmpty(OutputFolder) ? OutputFolder : Path.GetDirectoryName(OutputFilePath);
var outputFileName = !string.IsNullOrEmpty(OutputFolder) ? "pocoyo.json" : Path.GetFileNameWithoutExtension(OutputFilePath) + ".json";
#if !SkipMakeRelative
var relativePath = Environment.CurrentDirectory;
options.OutputFile = Utility.MakeRelativePath(options.OutputFile, relativePath);
options.CommandFile = Utility.MakeRelativePath(options.CommandFile, relativePath);
var paths = new List<string>();
foreach (var filePath in options.Files)
{
paths.Add(Utility.MakeRelativePath(filePath, relativePath));
}
options.Files = paths;
#endif
var optionsJsonPath = Path.Combine(outputFolder, outputFileName);
if (!string.Equals(CommandFile, optionsJsonPath, StringComparison.OrdinalIgnoreCase))
{
Json.SerializeToFile(options, optionsJsonPath);
if (!options.Silent)
Log.Info($"Generated --commands file: {optionsJsonPath}");
}
}
catch (Exception ex)
{
Log.Exception($"{nameof(SerializeToJson)}", ex);
}
}
/// <summary>
/// Gets options from command line file - text or json
/// </summary>
public Options GetOptionsFromCommandFile()
{
if (string.IsNullOrEmpty(CommandFile))
return null;
CommandFile = Path.GetFullPath(CommandFile);
if (!File.Exists(CommandFile))
{
LogError($"Missing --commands file: {CommandFile}");
return null;
}
Options options = null;
if (Path.GetExtension(CommandFile) == ".json")
{
// Get from json
options = Json.TryDeserializeFromFile<Options>(CommandFile);
}
else
{
// Re-parse with command file
var commandArgs = GetCommandFileArgs(CommandFile);
options = new Options();
if (!Parser.Default.ParseArguments(commandArgs, options))
options = null;
}
if (options != null)
{
if (string.IsNullOrEmpty(options.CommandFile))
options.CommandFile = CommandFile;
if (Verbose)
options.Verbose = true;
if (Silent)
options.Silent = true;
if (options.ProcessArgs(true))
return options;
}
return null;
}
public static Options ParseArgs(string[] args)
{
try
{
var options = new Options();
if (Parser.Default.ParseArguments(args, options))
{
if (options.ProcessArgs())
return options;
// Try options from command line file
var commandLineOptions = options.GetOptionsFromCommandFile();
if (commandLineOptions != null)
return commandLineOptions;
Log.Error(options.GetUsage());
}
}
catch (Exception ex)
{
Log.Error($"Parse error {ex.Message}");
}
return null;
}
private static string[] GetCommandFileArgs(string commandFile)
{
var commandArgs = File.ReadAllLines(commandFile);
var trimmed = new List<string>();
foreach (var arg in commandArgs)
{
trimmed.Add(arg.Trim());
}
return trimmed.ToArray();
}
public override string ToString()
{
return Json.Serialize(this);
}
}
}