forked from siteserver/cms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
195 lines (176 loc) · 6.88 KB
/
Copy pathProgram.cs
File metadata and controls
195 lines (176 loc) · 6.88 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
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using NDesk.Options;
using Quartz;
using Quartz.Impl;
using SiteServer.Cli.Core;
using SiteServer.Cli.Jobs;
using SiteServer.CMS.Plugin;
using SiteServer.Plugin;
using SiteServer.Utils;
namespace SiteServer.Cli
{
internal static class Program
{
private static bool IsHelp { get; set; }
private static string Repeat { get; set; }
private static Dictionary<string, Func<IJobContext, Task>> Jobs { get; set; }
public static string CommandName { get; private set; }
public static string[] CommandArgs { get; private set; }
private static readonly OptionSet Options = new OptionSet {
{ "r|repeat=", "schedule CRON expression",
v => Repeat = v },
{ "h|help", "命令说明",
v => IsHelp = v != null }
};
private static void Main(string[] args)
{
//Console.OutputEncoding = Encoding.UTF8;
Console.OutputEncoding = Encoding.GetEncoding(936);
if (!CliUtils.ParseArgs(Options, args)) return;
var commandNames = new List<string>();
var commandArgs = new List<string>();
if (args.Length >= 1)
{
var isCommand = true;
foreach (var arg in args)
{
if (isCommand && !StringUtils.StartsWith(arg, "-"))
{
commandNames.Add(StringUtils.Trim(arg));
}
else
{
isCommand = false;
commandArgs.Add(StringUtils.Trim(arg));
}
}
}
CommandName = string.Join(" ", commandNames);
CommandArgs = commandArgs.ToArray();
Console.WriteLine("欢迎使用 SiteServer Cli 命令行工具");
Console.WriteLine();
Jobs = new Dictionary<string, Func<IJobContext, Task>>(StringComparer.CurrentCultureIgnoreCase)
{
{BackupJob.CommandName, BackupJob.Execute},
{InstallJob.CommandName, InstallJob.Execute},
{RestoreJob.CommandName, RestoreJob.Execute},
{UpdateJob.CommandName, UpdateJob.Execute},
{VersionJob.CommandName, VersionJob.Execute},
{TestJob.CommandName, TestJob.Execute}
};
PluginManager.LoadPlugins(CliUtils.PhysicalApplicationPath);
var pluginJobs = PluginJobManager.GetJobs();
if (pluginJobs != null && pluginJobs.Count > 0)
{
foreach (var command in pluginJobs.Keys)
{
if (!Jobs.ContainsKey(command))
{
Jobs.Add(command, pluginJobs[command]);
}
}
}
if (!Jobs.ContainsKey(CommandName))
{
RunHelpAsync(IsHelp, CommandName).GetAwaiter().GetResult();
}
else if (!string.IsNullOrEmpty(Repeat))
{
RunRepeatAsync(Repeat).GetAwaiter().GetResult();
}
else
{
RunExecuteAsync(CommandName, CommandArgs, null).GetAwaiter().GetResult();
}
}
private static async Task RunHelpAsync(bool isHelp, string commandName)
{
if (isHelp || string.IsNullOrEmpty(commandName))
{
var version = Assembly.GetExecutingAssembly().GetName().Version.ToString();
await Console.Out.WriteLineAsync($"Cli 命令行版本: {version.Substring(0, version.Length - 2)}");
await Console.Out.WriteLineAsync($"当前文件夹: {CliUtils.PhysicalApplicationPath}");
await Console.Out.WriteLineAsync($"Cli 命令行文件夹: {Assembly.GetExecutingAssembly().Location}");
await Console.Out.WriteLineAsync();
await CliUtils.PrintRowLine();
await CliUtils.PrintRow("Usage");
await CliUtils.PrintRowLine();
BackupJob.PrintUsage();
InstallJob.PrintUsage();
RestoreJob.PrintUsage();
UpdateJob.PrintUsage();
VersionJob.PrintUsage();
await CliUtils.PrintRowLine();
await CliUtils.PrintRow("https://www.siteserver.cn/docs/cli");
await CliUtils.PrintRowLine();
Console.ReadLine();
}
else
{
Console.WriteLine($"'{commandName}' is not a siteserver command. See 'sitserver --help'");
}
}
private static async Task RunRepeatAsync(string schedule)
{
try
{
var factory = new StdSchedulerFactory(new NameValueCollection
{
{ "quartz.serializer.type", "binary" }
});
var sched = await factory.GetScheduler();
await sched.Start();
var job = JobBuilder.Create<SchedJob>()
.WithIdentity("job1", "group1")
.Build();
var trigger = TriggerBuilder.Create()
.WithIdentity("trigger1", "group1")
.StartNow()
.WithCronSchedule(schedule)
.WithPriority(1)
.Build();
await sched.ScheduleJob(job, trigger);
await Task.Delay(-1);
await sched.Shutdown();
}
catch (Exception ex)
{
await CliUtils.PrintErrorAsync(ex.Message);
}
}
public static async Task RunExecuteAsync(string commandName, string[] commandArgs, IJobExecutionContext jobContext)
{
try
{
Func<IJobContext, Task> job;
if (Jobs.TryGetValue(commandName, out job))
{
if (job != null)
{
var context = new JobContextImpl(commandName, commandArgs, jobContext);
await job(context);
}
}
}
catch (Exception ex)
{
await CliUtils.PrintErrorAsync(ex.Message);
var errorLogFilePath = CliUtils.CreateErrorLogFile("siteserver");
await CliUtils.AppendErrorLogsAsync(errorLogFilePath, new List<TextLogInfo>
{
new TextLogInfo
{
DateTime = DateTime.Now,
Detail = "Console Error",
Exception = ex
}
});
}
}
}
}