151 lines
4.8 KiB
C#
151 lines
4.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.Specialized;
|
|
using System.Threading;
|
|
using System.Windows.Forms;
|
|
|
|
namespace YLPublishTool
|
|
{
|
|
class Program
|
|
{
|
|
static int Main(string[] args)
|
|
{
|
|
if (Environment.UserInteractive && (args == null || args.Length < 1))
|
|
{
|
|
Console.WriteLine("启用配置模式");
|
|
AppDomain.CurrentDomain.UnhandledException += (sender, ex) =>
|
|
{
|
|
Console.WriteLine("发生错误:" + ex.ExceptionObject);
|
|
};
|
|
Application.EnableVisualStyles();
|
|
Application.SetCompatibleTextRenderingDefault(false);
|
|
var t = new Thread(() =>
|
|
{
|
|
new Forms.MainForm().ShowDialog();
|
|
});
|
|
t.SetApartmentState(ApartmentState.STA);
|
|
t.Start();
|
|
return 1;
|
|
}
|
|
|
|
AppDomain.CurrentDomain.UnhandledException += (sender, ex) =>
|
|
{
|
|
if (ex.ExceptionObject is Exception exp)
|
|
{
|
|
NLog.LogManager.GetLogger(nameof(Program)).Error(exp, "发生未处理错误");
|
|
}
|
|
else
|
|
{
|
|
NLog.LogManager.GetLogger(nameof(Program)).Error(ex.ExceptionObject);
|
|
}
|
|
};
|
|
|
|
var errcode = 0;
|
|
|
|
try
|
|
{
|
|
foreach (var arg in args)
|
|
{
|
|
NLog.LogManager.GetLogger("命令参数").Info(arg);
|
|
|
|
if (arg.StartsWith("/p:"))
|
|
{
|
|
var predefines = new NameValueCollection {
|
|
["当前目录"] = AppContext.BaseDirectory.TrimEnd('\\')
|
|
};
|
|
|
|
foreach (var kv in ParseEnume(arg.Substring(3).Trim(new char[] { '\'', '"' })))
|
|
{
|
|
if (kv.Key != "f")
|
|
{
|
|
predefines[kv.Key] = kv.Value;
|
|
NLog.LogManager.GetLogger("外部变量").Info($"{kv.Key} = {kv.Value}");
|
|
}
|
|
else if (!string.IsNullOrWhiteSpace(kv.Value))
|
|
{
|
|
try
|
|
{
|
|
new Runner().Run(kv.Value, predefines);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
errcode++;
|
|
NLog.LogManager.GetLogger("Runner").Error(ex, "运行脚本出错:" + kv.Value);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
errcode++;
|
|
NLog.LogManager.GetLogger(nameof(Program)).Error(ex, "运行脚本出错");
|
|
}
|
|
|
|
return errcode;
|
|
}
|
|
|
|
private static IEnumerable<KeyValuePair<string, string>> ParseEnume(string query)
|
|
{
|
|
var len = query.Length;
|
|
var buf = new char[len + 2];
|
|
query.CopyTo(0, buf, 0, len);
|
|
len += 2;
|
|
|
|
//在下面的循环中正好排除最后一个处理
|
|
buf[len - 2] = '&';
|
|
buf[len - 1] = 'm';
|
|
|
|
var start = 0;
|
|
var hasEscape = false;
|
|
|
|
|
|
for (var index = 0; index < len; index++)
|
|
{
|
|
if (buf[index] != '&')
|
|
{
|
|
continue;
|
|
}
|
|
|
|
if (buf[index + 1] == '&')
|
|
{
|
|
index++;
|
|
hasEscape = true;
|
|
continue;
|
|
}
|
|
|
|
//处理遇到单个&的情况
|
|
|
|
var substr = new string(buf, start, index - start).Trim();
|
|
|
|
start = index + 1;
|
|
|
|
if (substr.Length == 0)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
if (hasEscape)
|
|
{
|
|
hasEscape = false;
|
|
substr = substr.Replace("&&", "&");
|
|
}
|
|
|
|
var index2 = substr.IndexOf('=');
|
|
|
|
if (index2 > 0)
|
|
{
|
|
var key = substr.Substring(0, index2).Trim();
|
|
var value = substr.Substring(index2 + 1).Trim();
|
|
yield return new KeyValuePair<string, string>(key, value);
|
|
}
|
|
else
|
|
{
|
|
yield return new KeyValuePair<string, string>(substr.Trim(), string.Empty);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|