-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathProgramArguments.cs
More file actions
251 lines (229 loc) · 7.14 KB
/
ProgramArguments.cs
File metadata and controls
251 lines (229 loc) · 7.14 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
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Xml.Serialization;
using BitDiffer.Common.Configuration;
using BitDiffer.Common.Exceptions;
namespace BitDiffer.Common.Misc
{
public class ProgramArguments
{
private bool _help;
private string _reportFile;
private string _rawFile;
private string _logFile;
private ComparisonSet _set = new ComparisonSet();
public ProgramArguments()
{
_reportFile = GetFilePath("comparison.xml");
_set.Filter.ChangedItemsOnly = true;
}
public bool Help
{
get { return _help; }
}
public string ReportFile
{
get { return _reportFile; }
}
public string RawFile
{
get { return _rawFile; }
}
public string LogFile
{
get { return _logFile; }
}
public ComparisonSet ComparisonSet
{
get { return _set; }
}
public void Parse(bool required, string[] args)
{
for (int i = 0; i < args.Length; i++)
{
if (args[i].StartsWith("-"))
{
ParseOption(args, ref i);
}
else
{
string fileName = args[i];
if (i == 0 && fileName == Assembly.GetEntryAssembly().Location)
{
continue;
}
else if (Path.GetExtension(fileName).ToLower() == ".cset")
{
using (FileStream fs = File.Open(fileName, FileMode.Open))
{
XmlSerializer xs = new XmlSerializer(typeof(ComparisonSet));
_set = (ComparisonSet)xs.Deserialize(fs);
_set.FileName = fileName;
}
}
else
{
_set.Items.Add(fileName);
}
}
}
Validate(required);
}
private void ParseOption(string[] args, ref int i)
{
switch (args[i])
{
case "-?":
_help = true;
break;
case "-out":
_reportFile = GetFilePath(args[++i]);
break;
case "-raw":
_rawFile = GetFilePath(args[++i]);
break;
case "-log":
_logFile = GetFilePath(args[++i]);
break;
case "-dirs":
_set.Mode = ComparisonMode.CompareDirectories;
break;
case "-recurse":
_set.RecurseSubdirectories = true;
break;
case "-all":
_set.Filter.ChangedItemsOnly = false;
break;
case "-publiconly":
_set.Filter.IncludePublic = true;
_set.Filter.IncludeProtected = false;
_set.Filter.IncludeInternal = false;
_set.Filter.IncludePrivate = false;
break;
case "-xpublic":
_set.Filter.IncludePublic = false;
break;
case "-xprotected":
_set.Filter.IncludeProtected = false;
break;
case "-xinternal":
_set.Filter.IncludeInternal = false;
break;
case "-xprivate":
_set.Filter.IncludePrivate = false;
break;
case "-noattrs":
_set.Filter.IgnoreAssemblyAttributeChanges = true;
break;
case "-noimpl":
_set.Filter.CompareMethodImplementations = false;
break;
case "-nomulti":
_set.Config.Multithread = false;
break;
case "-gacfirst":
_set.Config.TryResolveGACFirst = true;
break;
case "-isolation":
ParseIsolationLevel(args[++i]);
break;
case "-execution":
_set.Config.UseReflectionOnlyContext = false;
break;
case "-ref":
_set.Config.ReferenceDirectories = GetMultiPath(args[++i]);
break;
case "-refdirs":
_set.Config.ReferenceDirectories = args[++i];
break;
default:
throw new ArgumentParserException("Unknown option {0}", args[i]);
}
}
private void ParseIsolationLevel(string level)
{
// Dont use Enum.Parse, obfuscation will break it
switch (level.ToLower())
{
case "low":
_set.Config.IsolationLevel = AppDomainIsolationLevel.Low;
break;
case "medium":
_set.Config.IsolationLevel = AppDomainIsolationLevel.Medium;
break;
case "high":
_set.Config.IsolationLevel = AppDomainIsolationLevel.High;
break;
default:
throw new ArgumentParserException("Unknown isolation level {0}", level);
}
}
private string GetMultiPath(string path)
{
if (!File.Exists(path))
{
throw new ArgumentParserException("File " + path + " was not found");
}
string[] paths = File.ReadAllLines(path);
List<string> trimPaths = new List<string>();
foreach (string p in paths)
{
if (!string.IsNullOrEmpty(p))
{
trimPaths.Add(p.Trim());
}
}
return string.Join(";", trimPaths.ToArray());
}
private string GetFilePath(string fileName)
{
return Path.GetFullPath(Path.Combine(Directory.GetCurrentDirectory(), fileName));
}
private void Validate(bool required)
{
if (required && _set.Items.Count == 0)
{
throw new ArgumentParserException("No filenames specified");
}
if (_reportFile != null)
{
if (!Directory.Exists(Path.GetDirectoryName(_reportFile)))
{
throw new ArgumentParserException("Directory for report does not exist: '{0}'", Path.GetDirectoryName(_reportFile));
}
}
if (_rawFile != null)
{
if (!Directory.Exists(Path.GetDirectoryName(_rawFile)))
{
throw new ArgumentParserException("Directory for report does not exist: '{0}'", Path.GetDirectoryName(_rawFile));
}
}
if (_set.Mode == ComparisonMode.CompareDirectories)
{
foreach (string dir in _set.Items)
{
if (!Directory.Exists(dir))
{
throw new ArgumentParserException("Directory does not exist or is not a directory: '{0}'", dir);
}
}
}
else
{
if (_set.RecurseSubdirectories)
{
throw new ArgumentParserException("Cannot specify to recurse subdirectories if not in directory mode");
}
foreach (string file in _set.Items)
{
if (!File.Exists(file))
{
throw new ArgumentParserException("File does not exist: '{0}'", file);
}
}
}
}
}
}