-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathCommandArgs.cs
234 lines (194 loc) · 9.65 KB
/
CommandArgs.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
using McMaster.Extensions.CommandLineUtils;
using McMaster.Extensions.CommandLineUtils.Abstractions;
using Microsoft.CodeAnalysis.CSharp;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.IO;
using System.Linq;
using System.Text;
namespace Meadow.SolCodeGen
{
[Flags]
public enum GenerateOutputType
{
Source = 1 << 0,
Assembly = 1 << 1
}
public class CommandArgs
{
public string SolSourceDirectory { get; set; }
public string Namespace { get; set; }
public GenerateOutputType Generate { get; set; }
public string LegacySolcPath { get; set; }
public Version SolcVersion { get; set; }
public int SolcOptimizer { get; set; }
public string OutputDirectory { get; set; }
public string AssemblyOutputDirectory { get; set; }
public const string MISSING_SOL_FILES = "missingsolfiles";
public static bool TryParse(string[] args, LoggerDelegate logger, out CommandArgs argResult, out Exception parseException)
{
try
{
var result = new CommandArgs();
var app = new CommandLineApplication();
app.HelpOption();
app.ThrowOnUnexpectedArgument = true;
var sourceOption = app
.Option("-s|--source <SOURCE>", "Path to the directory containing solidity source files", CommandOptionType.SingleValue)
.IsRequired()
.OnValidate(ctc =>
{
var opt = (ctc.ObjectInstance as CommandOption).Value();
opt = Path.GetFullPath(opt);
bool isDirExists = Directory.Exists(opt);
if (!isDirExists)
{
// Perform a case-insensitve search for the contracts directory.
if (Path.GetFileName(opt).Equals("contracts", StringComparison.OrdinalIgnoreCase))
{
var parentDirs = Directory.GetDirectories(Path.GetDirectoryName(opt), "*", SearchOption.TopDirectoryOnly);
var contractDir = parentDirs.FirstOrDefault(d => Path.GetFileName(d).Equals("contracts", StringComparison.OrdinalIgnoreCase));
if (contractDir != null)
{
opt = contractDir;
isDirExists = true;
}
}
}
if (!isDirExists)
{
return new ValidationResult("Solidity source file directory does not exist: " + opt);
}
var containsSolFiles = Directory.EnumerateFiles(opt, "*.sol", SearchOption.AllDirectories).Any();
if (!containsSolFiles)
{
return new ValidationResult("Solidity source file directory does not contain any .sol files: " + opt, new[] { MISSING_SOL_FILES });
}
result.SolSourceDirectory = opt;
logger("Solidity source directory specified: " + opt);
return ValidationResult.Success;
});
var namespaceOption = app
.Option("-n|--namespace", "The namespace for the generated code", CommandOptionType.SingleValue)
.OnValidate(ctc =>
{
var opt = (ctc.ObjectInstance as CommandOption).Value();
if (!SyntaxFacts.IsValidIdentifier(opt.Replace('.', '_')))
{
return new ValidationResult($"The specified namespace '{opt}' is not valid intenfier syntax");
}
result.Namespace = opt;
return ValidationResult.Success;
});
var generateOption = app
.Option<GenerateOutputType>("-g|--generate", "Generation output type, either 'source' or 'assembly'", CommandOptionType.MultipleValue)
.IsRequired()
.OnValidate(ctc =>
{
var opts = (ctc.ObjectInstance as CommandOption<GenerateOutputType>).ParsedValues;
result.Generate = opts.Aggregate(default(GenerateOutputType), (a, b) => a | b);
return ValidationResult.Success;
})
.Accepts().Values("source", "assembly");
var legacySolcOption = app
.Option("--legacysolc", "Path to .NET legacy solc native resolver lib, required if solcversion options is set", CommandOptionType.SingleValue)
.OnValidate(ctc =>
{
var opt = (ctc.ObjectInstance as CommandOption).Value();
if (!string.IsNullOrWhiteSpace(opt))
{
opt = Path.GetFullPath(opt);
if (!File.Exists(opt))
{
return new ValidationResult($"Legacy solc lib path is set but file does not exist: {opt}");
}
result.LegacySolcPath = opt;
logger($"Legacy solc lib set: {opt}");
}
return ValidationResult.Success;
});
var solcVersionOption = app
.Option("--solcversion", "Version of the libsolc compiler to use, requires the legacysolc option to be set", CommandOptionType.SingleValue)
.OnValidate(ctc =>
{
var opt = (ctc.ObjectInstance as CommandOption).Value();
if (!string.IsNullOrWhiteSpace(opt))
{
if (!Version.TryParse(opt, out var solcVersionParsed))
{
return new ValidationResult($"Could not parse specified solc version: {opt}");
}
result.SolcVersion = solcVersionParsed;
logger($"Solc version specified: {solcVersionParsed}");
}
return ValidationResult.Success;
});
var solcOptimizerOption = app
.Option("--solcoptimizer", "Enables the solc optimizer with the given run number", CommandOptionType.SingleValue)
.OnValidate(ctc =>
{
var opt = (ctc.ObjectInstance as CommandOption).Value();
if (!string.IsNullOrWhiteSpace(opt))
{
if (!int.TryParse(opt, out var solcOptimizerRuns))
{
return new ValidationResult($"Could not parse specified solc optimizer run setting: '{opt}'. Should be an integer of 1 or high, or set to 0 or empty string for disabling optimizer.");
}
if (solcOptimizerRuns > 0)
{
result.SolcOptimizer = solcOptimizerRuns;
logger($"Solc optimizer enabled with run count: {solcOptimizerRuns}");
}
}
return ValidationResult.Success;
});
var outputDirectoryOption = app
.Option("-o|--output", "Generated output directory", CommandOptionType.SingleValue)
.OnValidate(ctc =>
{
var opt = (ctc.ObjectInstance as CommandOption).Value();
result.OutputDirectory = opt;
return ValidationResult.Success;
});
var assemblyOutputDirectoryOption = app
.Option("--assembly-output", "Compilation output directory", CommandOptionType.SingleValue)
.OnValidate(ctc =>
{
var opt = (ctc.ObjectInstance as CommandOption).Value();
result.AssemblyOutputDirectory = opt;
return ValidationResult.Success;
});
ValidationResult validationError = null;
app.OnValidationError(v =>
{
validationError = v;
});
bool successfulExecute = false;
app.OnExecute(() =>
{
successfulExecute = true;
});
app.Execute(args);
if (!successfulExecute || validationError != null)
{
var exception = new Exception(validationError.ErrorMessage);
foreach (var memberName in validationError.MemberNames)
{
exception.Data[memberName] = true;
}
throw exception;
}
parseException = null;
argResult = result;
return true;
}
catch (Exception ex)
{
parseException = ex;
argResult = null;
return false;
}
}
}
}