-
-
Notifications
You must be signed in to change notification settings - Fork 345
/
Program.cs
227 lines (190 loc) · 7.27 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
215
216
217
218
219
220
221
222
223
224
225
226
227
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using CommandLine;
using log4net;
using log4net.Core;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using CKAN.Versioning;
using CKAN.NetKAN.Model;
using CKAN.NetKAN.Processors;
using CKAN.NetKAN.Transformers;
using CKAN.NetKAN.Extensions;
using YamlDotNet.RepresentationModel;
namespace CKAN.NetKAN
{
public static class Program
{
private const int ExitOk = 0;
private const int ExitBadOpt = 1;
private const int ExitError = 2;
private static readonly ILog Log = LogManager.GetLogger(typeof(Program));
private static CmdLineOptions Options { get; set; }
public static int Main(string[] args)
{
try
{
ProcessArgs(args);
// Force-allow TLS 1.2 for HTTPS URLs, because GitHub requires it.
// This is on by default in .NET 4.6, but not in 4.5.
ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12;
// If we see the --version flag, then display our build info
// and exit.
if (Options.Version)
{
Console.WriteLine(Meta.GetVersion(VersionFormat.Full));
return ExitOk;
}
var game = GameInstanceManager.GameByShortName(Options.Game);
if (!string.IsNullOrEmpty(Options.ValidateCkan))
{
var ckan = new Metadata(JObject.Parse(File.ReadAllText(Options.ValidateCkan)));
var inf = new Inflator(
Options.CacheDir,
Options.OverwriteCache,
Options.GitHubToken,
Options.GitLabToken,
Options.PreRelease,
game
);
inf.ValidateCkan(ckan);
Console.WriteLine(QueueHandler.serializeCkan(
PropertySortTransformer.SortProperties(ckan)));
return ExitOk;
}
if (!string.IsNullOrEmpty(Options.Queues))
{
var queues = Options.Queues.Split(new char[] { ',' }, 2);
var qh = new QueueHandler(
queues[0],
queues[1],
Options.CacheDir,
Options.OverwriteCache,
Options.GitHubToken,
Options.GitLabToken,
Options.PreRelease,
game
);
qh.Process();
return ExitOk;
}
if (Options.File != null)
{
Log.InfoFormat("Transforming {0}", Options.File);
var netkans = ReadNetkans();
Log.Info("Finished reading input");
var inf = new Inflator(
Options.CacheDir,
Options.OverwriteCache,
Options.GitHubToken,
Options.GitLabToken,
Options.PreRelease,
game
);
var ckans = inf.Inflate(
Options.File,
netkans,
new TransformOptions(
ParseReleases(Options.Releases),
ParseSkipReleases(Options.SkipReleases),
ParseHighestVersion(Options.HighestVersion),
netkans[0].Staged,
netkans[0].StagingReason))
.ToArray();
foreach (Metadata ckan in ckans)
{
WriteCkan(ckan.Json());
}
}
else
{
Log.Fatal(
"Usage: netkan [--verbose|--debug] [--debugger] [--prerelease] [--outputdir=...] <filename|URL>"
);
return ExitBadOpt;
}
}
catch (Exception e)
{
e = e.GetBaseException();
Log.Fatal(e.Message);
if (Options == null || Options.Debug)
{
Log.Fatal(e.StackTrace);
}
return ExitError;
}
return ExitOk;
}
private static int? ParseReleases(string val)
{
return val == "all" ? (int?)null : int.Parse(val);
}
private static int? ParseSkipReleases(string val)
{
return string.IsNullOrWhiteSpace(val) ? (int?)null : int.Parse(val);
}
private static ModuleVersion ParseHighestVersion(string val)
{
return val == null ? null : new ModuleVersion(val);
}
private static void ProcessArgs(string[] args)
{
if (args.Any(i => i == "--debugger"))
{
Debugger.Launch();
}
Options = new CmdLineOptions();
Parser.Default.ParseArgumentsStrict(args, Options);
Logging.Initialize();
LogManager.GetRepository().Threshold =
Options.Verbose ? Level.Info
: Options.Debug ? Level.Debug
: Level.Warn;
if (Options.NetUserAgent != null)
{
Net.UserAgentString = Options.NetUserAgent;
}
}
private static Metadata[] ReadNetkans()
{
if (!Options.File.EndsWith(".netkan"))
{
Log.WarnFormat("Input is not a .netkan file");
}
return ArgContents(Options.File).Select(ymap => new Metadata(ymap))
.ToArray();
}
private static YamlMappingNode[] ArgContents(string arg)
=> Uri.IsWellFormedUriString(arg, UriKind.Absolute)
? YamlExtensions.Parse(Net.DownloadText(new Uri(arg)))
: YamlExtensions.Parse(File.OpenText(arg));
internal static string CkanFileName(JObject json)
=> Path.Combine(
Options.OutputDir,
string.Format(
"{0}-{1}.ckan",
(string)json["identifier"],
((string)json["version"]).Replace(':', '-')));
private static void WriteCkan(JObject json)
{
var finalPath = CkanFileName(json);
var sb = new StringBuilder();
var sw = new StringWriter(sb);
using (var writer = new JsonTextWriter(sw))
{
writer.Formatting = Formatting.Indented;
writer.Indentation = 4;
writer.IndentChar = ' ';
var serializer = new JsonSerializer();
serializer.Serialize(writer, json);
}
File.WriteAllText(finalPath, sw + Environment.NewLine);
Log.InfoFormat("Transformation written to {0}", finalPath);
}
}
}