/
Program.cs
531 lines (450 loc) · 17.5 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
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
namespace TicketPackageSyncTool
{
class Program
{
/// <summary>
/// 当前的根路径
/// </summary>
static string _root;
static string _chromePath;
static string _output;
static string[] _updates;
static string _pluginVersion;
static void Main(string[] args)
{
Console.Title = "12306订票助手文件版本同步工具";
Console.WriteLine("12306订票助手文件版本同步工具 by iFish(木鱼)");
Console.WriteLine(" VER. " + System.Diagnostics.FileVersionInfo.GetVersionInfo(System.Reflection.Assembly.GetEntryAssembly().Location).FileVersion);
Console.WriteLine("====================================================");
Console.WriteLine();
_root = GetContainingFolder();
Console.WriteLine("[INFO] 项目根目录是 " + _root);
//获得插件版本
_pluginVersion = GetVersion();
//创建输出目录
_output = CreateOutputDirectory();
//获得谷歌路径
_chromePath = GetChromeExePath();
//复制用户脚本
GenerateUserScript();
//谷歌原生扩展
GenerateChromePack();
//谷歌应用程序商店
GenerateWebStorePack();
//淘宝浏览器专版
GenerateTaobaoPack(_pluginVersion);
//猎豹浏览器专版
GenerateLieBaoPack(_pluginVersion);
//遨游浏览器扩展
GenerateMaxthonAddon(_pluginVersion);
//搜狗浏览器
GenerateSogouExtension(_pluginVersion);
//修正更新文件版本号
FixUpdateXmlVersion(_pluginVersion);
//更新版本信息js文件
FixUpdateJsVersion(_pluginVersion);
//修正说明文件
FixReadme();
Console.WriteLine();
Console.WriteLine("====================================================");
Console.WriteLine("按任意键退出.....");
Console.ReadKey();
}
/// <summary>
/// 谷歌应用商店
/// </summary>
static void GenerateWebStorePack()
{
//搜狗文件目录
var src = System.IO.Path.Combine(_root, "webstore");
System.IO.File.Copy(GetOriginalScript(), System.IO.Path.Combine(src, "12306_ticket_helper.user.js"), true);
Console.WriteLine("[INFO] 正在同步谷歌商店manifest.json版本");
ModifyManifestVersion(System.IO.Path.Combine(src, "manifest.json"), _pluginVersion);
//打包
Console.WriteLine("[INFO] 正在打包应用商店扩展....");
var tool = System.IO.Path.Combine(_root, "tools\\7z.exe");
System.Diagnostics.Process.Start(tool, " a -tZIP \"" + System.IO.Path.Combine(_output, "12306订票助手.zip") + "\" -y " + src + "\\*").WaitForExit();
}
/// <summary>
/// 修正说明文件
/// </summary>
static void FixReadme()
{
Console.WriteLine("[INFO] 正在修正更新记录.....");
var readme = System.IO.Path.Combine(_root, "readme.md");
var lines = System.IO.File.ReadAllLines(readme, System.Text.Encoding.UTF8);
if (lines.FirstOrDefault(s => s.StartsWith("**版本:" + _pluginVersion)) != null)
{
Console.WriteLine("[INFO] 已是最新,不需要修正");
return;
}
//修正
var dest = new List<string>(lines.Length + _updates.Length + 5);
var index = 0;
while (index < lines.Length && lines[index] != "官方网站")
{
dest.Add(lines[index++]);
}
//加入记录
dest.Add("**版本:" + _pluginVersion + "**");
foreach (var item in _updates)
{
var u = item.Trim(new[] { ' ', '\t', '\'', '"', ';', ',' });
if (u.StartsWith("//")) continue;
//移除html
u = System.Text.RegularExpressions.Regex.Replace(u, "<[^>]*>", "");
dest.Add("* " + u);
}
dest.Add(Environment.NewLine);
while (index < lines.Length)
{
dest.Add(lines[index++]);
}
System.IO.File.WriteAllLines(readme, dest.ToArray(), System.Text.Encoding.UTF8);
Console.WriteLine("[SUCC] 修正成功");
}
/// <summary>
/// 修正更新文件版本号
/// </summary>
/// <param name="version"></param>
static void FixUpdateXmlVersion(string version)
{
var xmls = System.IO.Directory.GetFiles(_root, "update*.xml");
foreach (var f in xmls)
{
Console.WriteLine("[INFO] 修正 " + System.IO.Path.GetFileName(f) + " 中的版本号....");
ModifyXmlVersion(f, version);
}
}
/// <summary>
/// 更新版本信息js文件
/// </summary>
/// <param name="version"></param>
static void FixUpdateJsVersion(string version)
{
var xmls = System.IO.Directory.GetFiles(_root, "version*.js");
var isbeta = version.Count(s => s == '.') == 3;
foreach (var f in xmls)
{
var fn = System.IO.Path.GetFileName(f);
if (isbeta ^ fn.IndexOf("_beta", StringComparison.OrdinalIgnoreCase) == 0)
{
continue;
}
Console.WriteLine("[INFO] 修正 " + System.IO.Path.GetFileName(f) + " 中的版本号....");
ModifyVersionJs(f, version);
}
}
/// <summary>
/// 搜狗浏览器扩展
/// </summary>
/// <param name="version"></param>
static void GenerateSogouExtension(string version)
{
//搜狗文件目录
var src = System.IO.Path.Combine(_root, "sogou");
System.IO.File.Copy(GetOriginalScript(), System.IO.Path.Combine(src, "12306_ticket_helper.user.js"), true);
Console.WriteLine("[INFO] 正在同步搜狗浏览器扩展版本号....");
//同步版本
var manifest = System.IO.Path.Combine(src, "manifest.xml");
System.IO.File.WriteAllText(manifest,
System.Text.RegularExpressions.Regex.Replace(
System.IO.File.ReadAllText(manifest, System.Text.Encoding.UTF8),
@"<version>\s*[\d\.]+\s*</version>",
"<version>" + version + "</version>")
, new System.Text.UTF8Encoding(false, true));
//打包
Console.WriteLine("[INFO] 正在打包搜狗浏览器扩展....");
var tool = System.IO.Path.Combine(_root, "tools\\7z.exe");
System.Diagnostics.Process.Start(tool, " a -tZIP \"" + System.IO.Path.Combine(_output, "12306订票助手.sext") + "\" -y " + src + "\\*").WaitForExit();
}
/// <summary>
/// 遨游浏览器扩展
/// </summary>
/// <param name="version"></param>
static void GenerateMaxthonAddon(string version)
{
//源文件目录
var src = System.IO.Path.Combine(_root, "src");
Console.WriteLine("[INFO] 正在同步遨游扩展版本号");
ModifyManifestVersion(System.IO.Path.Combine(src, "def.json"), version, new System.Text.UnicodeEncoding(false, true));
//工具目录
var tool = System.IO.Path.Combine(_root, "tools\\mxpacker.exe");
Console.WriteLine("[INFO] 正在打包遨游扩展....");
System.Diagnostics.Process.Start(tool, "\"" + src + "\"").WaitForExit();
System.IO.File.Move(src + ".mxaddon", System.IO.Path.Combine(_output, "12306订票助手.mxaddon"));
Console.WriteLine("[INFO] 遨游扩展生成完毕.");
}
/// <summary>
/// 生成淘宝定制版
/// </summary>
static void GenerateLieBaoPack(string version)
{
//源文件目录
var src = System.IO.Path.Combine(_root, "src_lb");
if (!System.IO.Directory.Exists(src)) return;
//查找KEY文件
var pem = System.IO.Directory.GetFiles(_root, "*.pem").FirstOrDefault() ?? "";
Console.WriteLine("[TIP!] 准备生成猎豹浏览器专版,请手动合并区别。完成后,按任意键继续....");
OpenLocationInExplorer(GetOriginalScript());
OpenLocationInExplorer(System.IO.Path.Combine(src, "12306_ticket_helper.user.js"));
Console.ReadKey();
Console.WriteLine("[INFO] 正在同步manifest.json版本");
ModifyManifestVersion(System.IO.Path.Combine(src, "manifest.json"), version);
GenerateChromePack("猎豹浏览器专版", src, pem);
}
/// <summary>
/// 生成淘宝定制版
/// </summary>
static void GenerateTaobaoPack(string version)
{
//源文件目录
var src = System.IO.Path.Combine(_root, "src_tb");
if (!System.IO.Directory.Exists(src)) return;
//查找KEY文件
var pem = System.IO.Directory.GetFiles(_root, "*.pem").FirstOrDefault() ?? "";
Console.WriteLine("[TIP!] 准备生成淘宝浏览器专版,请手动合并区别。完成后,按任意键继续....");
OpenLocationInExplorer(GetOriginalScript());
OpenLocationInExplorer(System.IO.Path.Combine(src, "12306_ticket_helper.user.js"));
Console.ReadKey();
Console.WriteLine("[INFO] 正在同步manifest.json版本");
ModifyManifestVersion(System.IO.Path.Combine(src, "manifest.json"), version);
GenerateChromePack("淘宝浏览器专版", src, pem);
}
/// <summary>
/// 创建谷歌扩展包
/// </summary>
/// <returns></returns>
static void GenerateChromePack()
{
//源文件目录
var src = System.IO.Path.Combine(_root, "src");
//查找KEY文件
var pem = System.IO.Directory.GetFiles(_root, "*.pem").FirstOrDefault() ?? "";
GenerateChromePack("谷歌浏览器", src, pem);
}
/// <summary>
/// 创建谷歌扩展包
/// </summary>
/// <returns></returns>
static void GenerateChromePack(string type, string src, string pem)
{
Console.WriteLine("[INFO] 正在打包[" + type + "]扩展....");
var psi = new System.Diagnostics.ProcessStartInfo(_chromePath, "\"--pack-extension=" + src + "\"" + (string.IsNullOrEmpty(pem) ? "" : " \"--pack-extension-key=" + pem + "\""));
Console.WriteLine("[INFO] 命令行为 " + psi.Arguments);
Console.Write("[INFO] 打包中....");
System.Diagnostics.Process.Start(psi).WaitForExit();
Console.WriteLine("已完成....");
var name = System.IO.Path.GetFileName(src);
System.IO.File.Move(src + ".crx", System.IO.Path.Combine(_output, "12306订票助手(" + type + ").crx"));
Console.WriteLine("[INFO] 已生成 " + name + ".crx");
}
/// <summary>
/// 复制用户脚本
/// </summary>
static void GenerateUserScript()
{
var src = GetOriginalScript();
var dest = System.IO.Path.Combine(_output, "12306_ticket_helper.user.js");
System.IO.File.Copy(src, dest);
Console.WriteLine("[INFO] 已经复制用户脚本 12306_ticket_helper.user.js 到目标目录");
}
/// <summary>
/// 创建输出目录
/// </summary>
/// <returns></returns>
static string CreateOutputDirectory()
{
var output = System.IO.Path.Combine(_root, "Publish");
if (System.IO.Directory.Exists(output)) System.IO.Directory.Delete(output, true);
System.IO.Directory.CreateDirectory(output);
Console.WriteLine("[INFO] 已经创建输出目录");
return output;
}
/// <summary>
/// 获得谷歌程序路径
/// </summary>
/// <returns></returns>
static string GetChromeExePath()
{
var is64bit = System.Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE") == "AMD64";
var chromePath = System.IO.Path.Combine(System.Environment.GetEnvironmentVariable(is64bit ? "programfiles(x86)" : "programfiles"), @"Google\Chrome\Application\chrome.exe");
if (!System.IO.File.Exists(chromePath))
{
chromePath = Environment.ExpandEnvironmentVariables(@"%LocalAppData%\Google\Chrome\Application\chrome.exe");
}
if (!System.IO.File.Exists(chromePath))
{
chromePath = System.Configuration.ConfigurationManager.AppSettings["chromepath"];
}
if (string.IsNullOrEmpty(chromePath) || !System.IO.File.Exists(chromePath))
throw new ApplicationException("[ERROR] 无法获得Chrome的路径!");
Console.WriteLine("[INFO] Chrome位于 " + chromePath);
return chromePath;
}
/// <summary>
/// 查找当前项目的根目录
/// </summary>
/// <returns></returns>
static string GetContainingFolder()
{
var root = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
if (System.IO.Directory.Exists(System.IO.Path.Combine(root, ".git"))) return root;
//当前没有.git目录,尝试查找项目根目录
var tempRoot = root;
var lastIndex = 0;
while ((lastIndex = tempRoot.LastIndexOf("\\")) != -1)
{
tempRoot = tempRoot.Substring(0, lastIndex);
if (System.IO.Directory.Exists(System.IO.Path.Combine(tempRoot, ".git"))) return tempRoot;
}
return root;
}
/// <summary>
/// 读取助手的版本,并更新助手信息中的版本号
/// </summary>
/// <returns></returns>
static string GetVersion()
{
Console.WriteLine("[INFO] 正在获得助手版本号……");
var txt = System.IO.File.ReadAllLines(GetOriginalScript(), System.Text.Encoding.UTF8);
System.Text.RegularExpressions.Match match = null;
var versionReg = new System.Text.RegularExpressions.Regex(@"var\s+version\s*=\s*['""]([\.\d]+)['""]");
txt.FirstOrDefault(s => (match = versionReg.Match(s)).Success);
if (match == null)
{
throw new Exception("[ERROR] 无法找到版本标记");
}
var version = match.Groups[1].Value;
Console.WriteLine("[INFO] 版本号为 " + version);
Console.WriteLine("[INFO] 正在修改助手META区版本号");
var metaVersionReg = new System.Text.RegularExpressions.Regex(@"^//\s*@version\s*([\d\.]+)");
for (int i = 0; i < txt.Length; i++)
{
match = metaVersionReg.Match(txt[i]);
if (!match.Success) continue;
if (match.Groups[1].Value == version)
{
Console.WriteLine("[INFO] META区是同步的,不需要修改");
}
else
{
Console.WriteLine("[SUCC] 已同步版本号");
txt[i] = "// @version " + version;
System.IO.File.WriteAllLines(GetOriginalScript(), txt, System.Text.Encoding.UTF8);
}
break;
}
Console.WriteLine("[INFO] 正在同步manifest.json中的文件版本");
ModifyManifestVersion(System.IO.Path.Combine(_root, "src\\manifest.json"), version);
//查找更新
var start = 0;
var end = 0;
while (start < txt.Length && txt[start] != "var updates = [")
{
start++;
}
if (txt[start] != "var updates = [") throw new ApplicationException("未能找到更新开始标记!");
end = start + 1;
while (end < txt.Length && txt[end] != "];")
{
if (txt[end][0] != '\t' && txt[end][0] != ' ') throw new ApplicationException("查找更新时发生错误,更新区内容缩进不是空格或tab。");
end++;
}
if (txt[end] != "];") throw new ApplicationException("未能找到更新结束标记!");
_updates = txt.Skip(start + 1).Take(end - start - 1).ToArray();
Console.WriteLine("============更新如下===========");
Console.WriteLine(string.Join(Environment.NewLine, _updates));
Console.WriteLine("==============================");
return version;
}
static string GetOriginalScript()
{
return System.IO.Path.Combine(_root, "src\\12306_ticket_helper.user.js");
}
/// <summary>
/// 在资源管理器中打开指定位置
/// </summary>
/// <param name="path">路径</param>
public static void OpenLocationInExplorer(string path)
{
System.Diagnostics.Process.Start("explorer.exe", "/select,\"" + path + "\"");
}
/// <summary>
/// 修改指定清单的版本
/// </summary>
/// <param name="filePath"></param>
/// <param name="version"></param>
public static void ModifyManifestVersion(string filePath, string version)
{
ModifyManifestVersion(filePath, version, System.Text.Encoding.UTF8);
}
/// <summary>
/// 修改指定清单的版本
/// </summary>
/// <param name="filePath"></param>
/// <param name="version"></param>
public static void ModifyManifestVersion(string filePath, string version, System.Text.Encoding encoding)
{
var manifest = System.IO.File.ReadAllText(filePath, encoding);
manifest = System.Text.RegularExpressions.Regex.Replace(
manifest,
@"[""']version[""']\s*:\s*['""][\.\d+]+['""]",
"\"version\": \"" + version + "\""
);
System.IO.File.WriteAllText(filePath, manifest, encoding);
}
/// <summary>
/// 修改指定清单的版本
/// </summary>
/// <param name="filePath"></param>
/// <param name="version"></param>
public static void ModifyXmlVersion(string filePath, string version)
{
ModifyXmlVersion(filePath, version, System.Text.Encoding.UTF8);
}
/// <summary>
/// 修改指定清单的版本
/// </summary>
/// <param name="filePath"></param>
/// <param name="version"></param>
public static void ModifyXmlVersion(string filePath, string version, System.Text.Encoding encoding)
{
var manifest = System.IO.File.ReadAllText(filePath, encoding);
manifest = System.Text.RegularExpressions.Regex.Replace(
manifest,
@"version=\s*['""][\.\d+]+['""]",
"version=\"" + version + "\""
);
System.IO.File.WriteAllText(filePath, manifest, encoding);
}
/// <summary>
/// 更新版本JS文件
/// </summary>
/// <param name="filepath"></param>
/// <param name="version"></param>
static void ModifyVersionJs(string filepath, string version)
{
var result = new List<string>();
if (version.Count(s => s == '.') == 3)
{
result.Add("var version_12306_helper = \"" + version.Substring(0, version.LastIndexOf(".")) + "\";");
result.Add("var version_12306_helper_beta = \"" + version + "\";");
}
else
{
result.Add("var version_12306_helper = \"" + version + "\";");
result.Add("var version_12306_helper_beta = \"" + version + "\";");
}
result.Add("var version_updater = [");
result.AddRange(_updates);
result.Add("];");
result.AddRange(System.IO.File.ReadAllLines(System.IO.Path.Combine(_root, "sysmessage.js")));
System.IO.File.WriteAllLines(filepath, result.ToArray());
}
}
}