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