mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-15 03:25:23 +08:00
Removing some magic numbers
--HG-- branch : dev
This commit is contained in:
@@ -18,7 +18,7 @@ namespace Orchard.Specs.Bindings {
|
||||
var agent = new CommandHostAgent();
|
||||
var input = new StringReader("");
|
||||
var output = new StringWriter();
|
||||
details.StatusCode = agent.RunSingleCommand(
|
||||
details.StatusCode = (int)agent.RunSingleCommand(
|
||||
input,
|
||||
output,
|
||||
"Default",
|
||||
|
@@ -1,8 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using HtmlAgilityPack;
|
||||
using Orchard.Commands;
|
||||
|
||||
namespace Orchard.Specs.Hosting {
|
||||
[Serializable]
|
||||
|
@@ -7,6 +7,7 @@ using System.Text;
|
||||
using System.Web;
|
||||
using System.Web.Hosting;
|
||||
using HtmlAgilityPack;
|
||||
using Orchard.Commands;
|
||||
using Orchard.Specs.Util;
|
||||
|
||||
namespace Orchard.Specs.Hosting {
|
||||
|
@@ -16,6 +16,17 @@ using Orchard.Logging;
|
||||
using Orchard.Tasks;
|
||||
|
||||
namespace Orchard.Commands {
|
||||
|
||||
/// <summary>
|
||||
/// Different return codes for a command execution.
|
||||
/// </summary>
|
||||
public enum CommandReturnCodes
|
||||
{
|
||||
Ok = 0,
|
||||
Fail = 5,
|
||||
Retry = 240
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This is the guy instantiated by the orchard.exe host. It is reponsible for
|
||||
/// executing a single command.
|
||||
@@ -32,19 +43,19 @@ namespace Orchard.Commands {
|
||||
public ILogger Logger { get; set; }
|
||||
|
||||
|
||||
public int RunSingleCommand(TextReader input, TextWriter output, string tenant, string[] args, Dictionary<string, string> switches) {
|
||||
int result = StartHost(input, output);
|
||||
if (result != 0)
|
||||
public CommandReturnCodes RunSingleCommand(TextReader input, TextWriter output, string tenant, string[] args, Dictionary<string, string> switches) {
|
||||
CommandReturnCodes result = StartHost(input, output);
|
||||
if (result != CommandReturnCodes.Ok)
|
||||
return result;
|
||||
|
||||
result = RunCommand(input, output, tenant, args, switches);
|
||||
if (result != 0)
|
||||
if (result != CommandReturnCodes.Ok)
|
||||
return result;
|
||||
|
||||
return StopHost(input, output);
|
||||
}
|
||||
|
||||
public int RunCommand(TextReader input, TextWriter output, string tenant, string[] args, Dictionary<string, string> switches) {
|
||||
public CommandReturnCodes RunCommand(TextReader input, TextWriter output, string tenant, string[] args, Dictionary<string, string> switches) {
|
||||
try {
|
||||
tenant = tenant ?? "Default";
|
||||
|
||||
@@ -80,12 +91,12 @@ namespace Orchard.Commands {
|
||||
while (processingEngine.AreTasksPending())
|
||||
processingEngine.ExecuteNextTask();
|
||||
|
||||
return 0;
|
||||
return CommandReturnCodes.Ok;
|
||||
}
|
||||
catch (OrchardCommandHostRetryException e) {
|
||||
// Special "Retry" return code for our host
|
||||
output.WriteLine("{0} (Retrying...)", e.Message);
|
||||
return 240;
|
||||
return CommandReturnCodes.Retry;
|
||||
}
|
||||
catch (Exception e) {
|
||||
for (int i = 0; e != null; e = e.InnerException, i++) {
|
||||
@@ -95,43 +106,43 @@ namespace Orchard.Commands {
|
||||
output.WriteLine("Error: {0}", e.Message);
|
||||
output.WriteLine("{0}", e.StackTrace);
|
||||
}
|
||||
return 5;
|
||||
return CommandReturnCodes.Fail;
|
||||
}
|
||||
}
|
||||
|
||||
public int StartHost(TextReader input, TextWriter output) {
|
||||
public CommandReturnCodes StartHost(TextReader input, TextWriter output) {
|
||||
try {
|
||||
_hostContainer = CreateHostContainer();
|
||||
return 0;
|
||||
return CommandReturnCodes.Ok;
|
||||
}
|
||||
catch (OrchardCommandHostRetryException e) {
|
||||
// Special "Retry" return code for our host
|
||||
output.WriteLine("{0} (Retrying...)", e.Message);
|
||||
return 240;
|
||||
return CommandReturnCodes.Retry;
|
||||
}
|
||||
catch (Exception e) {
|
||||
for (; e != null; e = e.InnerException) {
|
||||
output.WriteLine("Error: {0}", e.Message);
|
||||
output.WriteLine("{0}", e.StackTrace);
|
||||
}
|
||||
return 5;
|
||||
return CommandReturnCodes.Fail;
|
||||
}
|
||||
}
|
||||
|
||||
public int StopHost(TextReader input, TextWriter output) {
|
||||
public CommandReturnCodes StopHost(TextReader input, TextWriter output) {
|
||||
try {
|
||||
if (_hostContainer != null) {
|
||||
_hostContainer.Dispose();
|
||||
_hostContainer = null;
|
||||
}
|
||||
return 0;
|
||||
return CommandReturnCodes.Ok;
|
||||
}
|
||||
catch (Exception e) {
|
||||
for (; e != null; e = e.InnerException) {
|
||||
output.WriteLine("Error: {0}", e.Message);
|
||||
output.WriteLine("{0}", e.StackTrace);
|
||||
}
|
||||
return 5;
|
||||
return CommandReturnCodes.Fail;
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -8,6 +8,16 @@ using Orchard.Parameters;
|
||||
using Orchard.ResponseFiles;
|
||||
|
||||
namespace Orchard.Host {
|
||||
|
||||
/// <summary>
|
||||
/// Different return codes for a command execution.
|
||||
/// </summary>
|
||||
public enum CommandReturnCodes {
|
||||
Ok = 0,
|
||||
Fail = 5,
|
||||
Retry = 240
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The CommandHost runs inside the ASP.NET AppDomain and serves as an intermediate
|
||||
/// between the command line and the CommandHostAgent, which is known to the Orchard
|
||||
@@ -31,7 +41,7 @@ namespace Orchard.Host {
|
||||
HostingEnvironment.UnregisterObject(this);
|
||||
}
|
||||
|
||||
public int StartSession(TextReader input, TextWriter output) {
|
||||
public CommandReturnCodes StartSession(TextReader input, TextWriter output) {
|
||||
_agent = CreateAgent();
|
||||
return StartHost(_agent, input, output);
|
||||
}
|
||||
@@ -43,9 +53,9 @@ namespace Orchard.Host {
|
||||
}
|
||||
}
|
||||
|
||||
public int RunCommand(TextReader input, TextWriter output, Logger logger, OrchardParameters args) {
|
||||
public CommandReturnCodes RunCommand(TextReader input, TextWriter output, Logger logger, OrchardParameters args) {
|
||||
var agent = CreateAgent();
|
||||
int result = (int)agent.GetType().GetMethod("RunSingleCommand").Invoke(agent, new object[] {
|
||||
CommandReturnCodes result = (CommandReturnCodes)agent.GetType().GetMethod("RunSingleCommand").Invoke(agent, new object[] {
|
||||
input,
|
||||
output,
|
||||
args.Tenant,
|
||||
@@ -55,8 +65,8 @@ namespace Orchard.Host {
|
||||
return result;
|
||||
}
|
||||
|
||||
public int RunCommandInSession(TextReader input, TextWriter output, Logger logger, OrchardParameters args) {
|
||||
int result = (int)_agent.GetType().GetMethod("RunCommand").Invoke(_agent, new object[] {
|
||||
public CommandReturnCodes RunCommandInSession(TextReader input, TextWriter output, Logger logger, OrchardParameters args) {
|
||||
CommandReturnCodes result = (CommandReturnCodes)_agent.GetType().GetMethod("RunCommand").Invoke(_agent, new object[] {
|
||||
input,
|
||||
output,
|
||||
args.Tenant,
|
||||
@@ -66,11 +76,11 @@ namespace Orchard.Host {
|
||||
return result;
|
||||
}
|
||||
|
||||
public int RunCommands(TextReader input, TextWriter output, Logger logger, IEnumerable<ResponseLine> responseLines) {
|
||||
public CommandReturnCodes RunCommands(TextReader input, TextWriter output, Logger logger, IEnumerable<ResponseLine> responseLines) {
|
||||
var agent = CreateAgent();
|
||||
|
||||
int result = StartHost(agent, input, output);
|
||||
if (result != 0)
|
||||
CommandReturnCodes result = StartHost(agent, input, output);
|
||||
if (result != CommandReturnCodes.Ok)
|
||||
return result;
|
||||
|
||||
foreach (var line in responseLines) {
|
||||
@@ -78,14 +88,14 @@ namespace Orchard.Host {
|
||||
|
||||
var args = new OrchardParametersParser().Parse(new CommandParametersParser().Parse(line.Args));
|
||||
|
||||
result = (int)agent.GetType().GetMethod("RunCommand").Invoke(agent, new object[] {
|
||||
result = (CommandReturnCodes)agent.GetType().GetMethod("RunCommand").Invoke(agent, new object[] {
|
||||
input,
|
||||
output,
|
||||
args.Tenant,
|
||||
args.Arguments.ToArray(),
|
||||
args.Switches});
|
||||
|
||||
if (result != 0) {
|
||||
if (result != CommandReturnCodes.Ok) {
|
||||
output.WriteLine("{0} ({1}): Command returned error ({2})", line.Filename, line.LineNumber, result);
|
||||
return result;
|
||||
}
|
||||
@@ -99,12 +109,12 @@ namespace Orchard.Host {
|
||||
return Activator.CreateInstance("Orchard.Framework", "Orchard.Commands.CommandHostAgent").Unwrap();
|
||||
}
|
||||
|
||||
private int StopHost(object agent, TextReader input, TextWriter output) {
|
||||
return (int)agent.GetType().GetMethod("StopHost").Invoke(agent, new object[] { input, output });
|
||||
private CommandReturnCodes StopHost(object agent, TextReader input, TextWriter output) {
|
||||
return (CommandReturnCodes)agent.GetType().GetMethod("StopHost").Invoke(agent, new object[] { input, output });
|
||||
}
|
||||
|
||||
private int StartHost(object agent, TextReader input, TextWriter output) {
|
||||
return (int)agent.GetType().GetMethod("StartHost").Invoke(agent, new object[] { input, output });
|
||||
private CommandReturnCodes StartHost(object agent, TextReader input, TextWriter output) {
|
||||
return (CommandReturnCodes)agent.GetType().GetMethod("StartHost").Invoke(agent, new object[] { input, output });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,13 +1,13 @@
|
||||
using System.IO;
|
||||
using System.Web.Hosting;
|
||||
using Orchard.Host;
|
||||
|
||||
namespace Orchard.HostContext {
|
||||
public class CommandHostContext {
|
||||
public int RetryResult { get; set; }
|
||||
public CommandReturnCodes StartSessionResult { get; set; }
|
||||
public CommandReturnCodes RetryResult { get; set; }
|
||||
|
||||
public OrchardParameters Arguments { get; set; }
|
||||
public DirectoryInfo OrchardDirectory { get; set; }
|
||||
public int StartSessionResult { get; set; }
|
||||
public bool DisplayUsageHelp { get; set; }
|
||||
public CommandHost CommandHost { get; set; }
|
||||
public Logger Logger { get; set; }
|
||||
|
@@ -24,8 +24,7 @@ namespace Orchard.HostContext {
|
||||
|
||||
[SecurityCritical]
|
||||
public CommandHostContext CreateContext() {
|
||||
var context = new CommandHostContext();
|
||||
context.RetryResult = 240;/*special return code for "Retry"*/
|
||||
var context = new CommandHostContext { RetryResult = CommandReturnCodes.Retry };
|
||||
Initialize(context);
|
||||
return context;
|
||||
}
|
||||
|
@@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.IO;
|
||||
using Orchard.Host;
|
||||
using Orchard.HostContext;
|
||||
using Orchard.Parameters;
|
||||
|
||||
@@ -16,7 +17,7 @@ namespace Orchard {
|
||||
_commandHostContextProvider = new CommandHostContextProvider(args);
|
||||
}
|
||||
|
||||
public int Run() {
|
||||
public CommandReturnCodes Run() {
|
||||
try {
|
||||
return DoRun();
|
||||
}
|
||||
@@ -25,18 +26,18 @@ namespace Orchard {
|
||||
for (; e != null; e = e.InnerException) {
|
||||
_output.WriteLine(" {0}", e.Message);
|
||||
}
|
||||
return -1;
|
||||
return CommandReturnCodes.Fail;
|
||||
}
|
||||
}
|
||||
|
||||
private int DoRun() {
|
||||
private CommandReturnCodes DoRun() {
|
||||
var context = CommandHostContext();
|
||||
if (context.DisplayUsageHelp) {
|
||||
DisplayUsageHelp();
|
||||
return 0;
|
||||
return CommandReturnCodes.Ok;
|
||||
}
|
||||
|
||||
int result;
|
||||
CommandReturnCodes result;
|
||||
if (context.Arguments.Arguments.Any())
|
||||
result = ExecuteSingleCommand(context);
|
||||
else if (context.Arguments.ResponseFiles.Any())
|
||||
@@ -58,17 +59,17 @@ namespace Orchard {
|
||||
return result;
|
||||
}
|
||||
|
||||
private int ExecuteSingleCommand(CommandHostContext context) {
|
||||
private CommandReturnCodes ExecuteSingleCommand(CommandHostContext context) {
|
||||
return context.CommandHost.RunCommand(_input, _output, context.Logger, context.Arguments);
|
||||
}
|
||||
|
||||
|
||||
private int ExecuteResponseFiles(CommandHostContext context) {
|
||||
private CommandReturnCodes ExecuteResponseFiles(CommandHostContext context) {
|
||||
var responseLines = new ResponseFiles.ResponseFiles().ReadFiles(context.Arguments.ResponseFiles);
|
||||
return context.CommandHost.RunCommands(_input, _output, context.Logger, responseLines.ToArray());
|
||||
}
|
||||
|
||||
public int ExecuteInteractive(CommandHostContext context) {
|
||||
public CommandReturnCodes ExecuteInteractive(CommandHostContext context) {
|
||||
_output.WriteLine("Type \"?\" for help, \"exit\" to exit, \"cls\" to clear screen");
|
||||
while (true) {
|
||||
var command = ReadCommand(context);
|
||||
@@ -102,18 +103,18 @@ namespace Orchard {
|
||||
if (string.IsNullOrWhiteSpace(command))
|
||||
return context;
|
||||
|
||||
int result = RunCommandInSession(context, command);
|
||||
CommandReturnCodes result = RunCommandInSession(context, command);
|
||||
if (result == context.RetryResult) {
|
||||
_commandHostContextProvider.Shutdown(context);
|
||||
context = CommandHostContext();
|
||||
result = RunCommandInSession(context, command);
|
||||
if (result != 0)
|
||||
if (result != CommandReturnCodes.Ok)
|
||||
_output.WriteLine("Command returned non-zero result: {0}", result);
|
||||
}
|
||||
return context;
|
||||
}
|
||||
|
||||
private int RunCommandInSession(CommandHostContext context, string command) {
|
||||
private CommandReturnCodes RunCommandInSession(CommandHostContext context, string command) {
|
||||
try {
|
||||
var args = new OrchardParametersParser().Parse(new CommandParametersParser().Parse(new CommandLineParser().Parse(command)));
|
||||
return context.CommandHost.RunCommandInSession(_input, _output, context.Logger, args);
|
||||
|
@@ -3,7 +3,7 @@
|
||||
namespace Orchard {
|
||||
public class Program {
|
||||
public static int Main(string[] args) {
|
||||
return new OrchardHost(Console.In, Console.Out, args).Run();
|
||||
return (int)new OrchardHost(Console.In, Console.Out, args).Run();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user