-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathProgram.cs
165 lines (147 loc) · 5.33 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
/*
* Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the License);
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an AS IS BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using CommandLine;
using APITool.Dummy;
using APITool.Print;
using APITool.Ref;
namespace APITool
{
class Program
{
public int RunPrintAndReturnExitCode(PrintOptions options)
{
if (options.Verbose)
{
Log.Level = LogLevel.VERBOSE;
}
APIPrinter printer = new APIPrinter(options);
string absTarget = Path.GetFullPath(options.Target);
if (Directory.Exists(absTarget))
{
DirectoryInfo targetDir = new DirectoryInfo(absTarget);
var targetFiles = targetDir.GetFiles("*.dll").OrderBy(f => f.Name);
printer.EmitBegin();
foreach (var file in targetFiles)
{
printer.Run(file.FullName);
}
printer.EmitEnd();
}
else if (File.Exists(absTarget))
{
printer.EmitBegin();
printer.Run(absTarget);
printer.EmitEnd();
}
else
{
Log.Error($"No such file found: {absTarget}");
return 1;
}
return 0;
}
public int RunDummyAndReturnExitCode(DummyOptions options)
{
if (options.Verbose)
{
Log.Level = LogLevel.VERBOSE;
}
DummyGenerator dummyGenerator = new DummyGenerator();
// Convert to absolute path
if (!string.IsNullOrEmpty(options.InputPath))
{
options.InputPath = Path.GetFullPath(options.InputPath);
}
if (!string.IsNullOrEmpty(options.OutputPath))
{
options.OutputPath = Path.GetFullPath(options.OutputPath);
}
if (Directory.Exists(options.InputPath))
{
if (string.IsNullOrEmpty(options.OutputPath) || !Directory.Exists(options.OutputPath))
{
throw new DirectoryNotFoundException("Directory should be set as the output path.");
}
if (!options.OutputPath.EndsWith(Path.DirectorySeparatorChar))
{
options.OutputPath += Path.DirectorySeparatorChar;
}
DirectoryInfo inputDirInfo = new DirectoryInfo(options.InputPath);
FileInfo[] inputFiles = inputDirInfo.GetFiles("*.dll", SearchOption.AllDirectories);
Regex rgx = new Regex("^" + options.InputPath.Replace("\\", "\\\\").Replace("+", "\\+"));
foreach (var f in inputFiles)
{
Log.Info($"Processing {f.FullName} ...");
dummyGenerator.Run(f.FullName, rgx.Replace(f.FullName, options.OutputPath));
}
}
else
{
if (!File.Exists(options.InputPath))
{
throw new FileNotFoundException("Couldn't find the input file : " + options.InputPath);
}
dummyGenerator.Run(options.InputPath, options.OutputPath);
}
return 0;
}
public int RunRefAndReturnExitCode(RefOptions options)
{
if (options.Verbose)
{
Log.Level = LogLevel.VERBOSE;
}
if (!string.IsNullOrEmpty(options.TargetAssembly))
{
options.TargetAssembly = Path.GetFullPath(options.TargetAssembly);
}
if (File.Exists(options.TargetAssembly))
{
var printer = new RefPrinter(options);
printer.PrintReferences(options.TargetAssembly);
}
else
{
Log.Error($"No such file found: {options.TargetAssembly}");
return 1;
}
return 0;
}
static int Main(string[] args)
{
try
{
Program program = new Program();
return Parser.Default.ParseArguments<PrintOptions, DummyOptions, RefOptions>(args).MapResult(
(PrintOptions opts) => program.RunPrintAndReturnExitCode(opts),
(DummyOptions opts) => program.RunDummyAndReturnExitCode(opts),
(RefOptions opts) => program.RunRefAndReturnExitCode(opts),
errs => 1);
}
catch (Exception ex)
{
Log.Error(ex.Message);
Log.Error(ex.StackTrace);
Environment.Exit(1);
}
return 0;
}
}
}