mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-15 11:44:58 +08:00
Slightly refactor orchard.exe
Allows calling command execution in asp.net host, passing right set of parameters --HG-- branch : dev rename : src/Tools/Orchard/OrchardArguments.cs => src/Tools/Orchard/OrchardParameters.cs rename : src/Tools/Orchard/OrchardArgumentsParser.cs => src/Tools/Orchard/OrchardParametersParser.cs rename : src/Tools/Orchard/Arguments/Switch.cs => src/Tools/Orchard/Parameters/CommandSwitch.cs
This commit is contained in:
@@ -19,13 +19,14 @@ namespace Orchard.Commands {
|
|||||||
try {
|
try {
|
||||||
var hostContainer = OrchardStarter.CreateHostContainer(MvcSingletons);
|
var hostContainer = OrchardStarter.CreateHostContainer(MvcSingletons);
|
||||||
var host = hostContainer.Resolve<IOrchardHost>();
|
var host = hostContainer.Resolve<IOrchardHost>();
|
||||||
var tenantManager = hostContainer.Resolve<ITenantManager>();
|
|
||||||
|
|
||||||
host.Initialize();
|
host.Initialize();
|
||||||
|
|
||||||
|
// Find tenant (or default)
|
||||||
tenant = tenant ?? "default";
|
tenant = tenant ?? "default";
|
||||||
|
var tenantManager = hostContainer.Resolve<ITenantManager>();
|
||||||
var tenantSettings = tenantManager.LoadSettings().Single(s => String.Equals(s.Name, tenant, StringComparison.OrdinalIgnoreCase));
|
var tenantSettings = tenantManager.LoadSettings().Single(s => String.Equals(s.Name, tenant, StringComparison.OrdinalIgnoreCase));
|
||||||
|
|
||||||
|
// Disptach command execution to ICommandManager
|
||||||
using (var env = host.CreateStandaloneEnvironment(tenantSettings)) {
|
using (var env = host.CreateStandaloneEnvironment(tenantSettings)) {
|
||||||
env.Resolve<ICommandManager>().Execute(new CommandParameters {Arguments = args, Switches = switches});
|
env.Resolve<ICommandManager>().Execute(new CommandParameters {Arguments = args, Switches = switches});
|
||||||
}
|
}
|
||||||
|
@@ -1,7 +0,0 @@
|
|||||||
using System.Collections.Generic;
|
|
||||||
|
|
||||||
namespace Orchard.Arguments {
|
|
||||||
public interface IParser {
|
|
||||||
ParserResult Parse(IEnumerable<string> args);
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,34 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
|
|
||||||
namespace Orchard.Arguments {
|
|
||||||
public class Parser : IParser {
|
|
||||||
public ParserResult Parse(IEnumerable<string> args) {
|
|
||||||
ParserResult result = new ParserResult();
|
|
||||||
|
|
||||||
IEnumerator<string> e = args.GetEnumerator();
|
|
||||||
while (e.MoveNext()) {
|
|
||||||
if (e.Current[0] == '/') {
|
|
||||||
var s = ParseSwitch(e);
|
|
||||||
result.Switches.Add(s);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
result.Arguments.Add(e.Current);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
private Switch ParseSwitch(IEnumerator<string> enumerator) {
|
|
||||||
string sw = enumerator.Current.Substring(1);
|
|
||||||
string[] args = sw.Split(':');
|
|
||||||
return new Switch {
|
|
||||||
Name = args[0],
|
|
||||||
Value = args.Length >= 2 ? args[1] : string.Empty
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,13 +0,0 @@
|
|||||||
using System.Collections.Generic;
|
|
||||||
|
|
||||||
namespace Orchard.Arguments {
|
|
||||||
public class ParserResult {
|
|
||||||
public ParserResult() {
|
|
||||||
this.Arguments = new List<string>();
|
|
||||||
this.Switches = new List<Switch>();
|
|
||||||
}
|
|
||||||
|
|
||||||
public IList<string> Arguments { get; set; }
|
|
||||||
public IList<Switch> Switches { get; set; }
|
|
||||||
}
|
|
||||||
}
|
|
@@ -19,9 +19,12 @@ namespace Orchard.Host {
|
|||||||
//TODO
|
//TODO
|
||||||
}
|
}
|
||||||
|
|
||||||
public void RunCommand(string[] args) {
|
public void RunCommand(OrchardParameters args) {
|
||||||
var agent = Activator.CreateInstance("Orchard.Framework", "Orchard.Commands.CommandHostAgent").Unwrap();
|
var agent = Activator.CreateInstance("Orchard.Framework", "Orchard.Commands.CommandHostAgent").Unwrap();
|
||||||
agent.GetType().GetMethod("RunSingleCommand").Invoke(agent, new object[] { args });
|
agent.GetType().GetMethod("RunSingleCommand").Invoke(agent, new object[] {
|
||||||
|
args.Tenant,
|
||||||
|
args.Arguments.ToArray(),
|
||||||
|
args.Switches});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
7
src/Tools/Orchard/IOrchardParametersParser.cs
Normal file
7
src/Tools/Orchard/IOrchardParametersParser.cs
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
using Orchard.Parameters;
|
||||||
|
|
||||||
|
namespace Orchard {
|
||||||
|
public interface IOrchardParametersParser {
|
||||||
|
OrchardParameters Parse(CommandParameters parameters);
|
||||||
|
}
|
||||||
|
}
|
@@ -48,15 +48,16 @@
|
|||||||
<Reference Include="System.Xml" />
|
<Reference Include="System.Xml" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="Arguments\IParser.cs" />
|
<Compile Include="Parameters\ICommandParametersParser.cs" />
|
||||||
<Compile Include="OrchardArguments.cs" />
|
<Compile Include="IOrchardParametersParser.cs" />
|
||||||
<Compile Include="Arguments\Parser.cs" />
|
<Compile Include="OrchardParameters.cs" />
|
||||||
<Compile Include="Arguments\ParserResult.cs" />
|
<Compile Include="Parameters\CommandParametersParser.cs" />
|
||||||
|
<Compile Include="Parameters\CommandParameters.cs" />
|
||||||
<Compile Include="Host\CommandHost.cs" />
|
<Compile Include="Host\CommandHost.cs" />
|
||||||
<Compile Include="OrchardArgumentsParser.cs" />
|
<Compile Include="OrchardParametersParser.cs" />
|
||||||
<Compile Include="Program.cs" />
|
<Compile Include="Program.cs" />
|
||||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
<Compile Include="Arguments\Switch.cs" />
|
<Compile Include="Parameters\CommandSwitch.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||||
|
@@ -4,10 +4,13 @@ using System.Linq;
|
|||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
namespace Orchard {
|
namespace Orchard {
|
||||||
public class OrchardArguments {
|
public class OrchardParameters : MarshalByRefObject {
|
||||||
public bool Verbose { get; set; }
|
public bool Verbose { get; set; }
|
||||||
public string VirtualPath { get; set; }
|
public string VirtualPath { get; set; }
|
||||||
public string WorkingDirectory { get; set; }
|
public string WorkingDirectory { get; set; }
|
||||||
public string Tenant { get; set; }
|
public string Tenant { get; set; }
|
||||||
|
public IEnumerable<string> Arguments { get; set; }
|
||||||
|
public IDictionary<string, string> Switches { get; set; }
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@@ -1,21 +1,18 @@
|
|||||||
using System;
|
using System.Collections.Generic;
|
||||||
using System.Collections.Generic;
|
using Orchard.Parameters;
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using Orchard.Arguments;
|
|
||||||
|
|
||||||
namespace Orchard {
|
namespace Orchard {
|
||||||
public interface IOrchardArgumentsParser {
|
public class OrchardParametersParser : IOrchardParametersParser {
|
||||||
OrchardArguments Parse(ParserResult arguments);
|
|
||||||
}
|
|
||||||
|
|
||||||
public class OrchardArgumentsParser : IOrchardArgumentsParser {
|
public OrchardParameters Parse(CommandParameters parameters) {
|
||||||
|
|
||||||
public OrchardArguments Parse(ParserResult arguments) {
|
var result = new OrchardParameters {
|
||||||
var result = new OrchardArguments();
|
Arguments = parameters.Arguments,
|
||||||
|
Switches = new Dictionary<string, string>()
|
||||||
|
};
|
||||||
|
|
||||||
foreach (var sw in arguments.Switches) {
|
foreach (var sw in parameters.Switches) {
|
||||||
switch (sw.Name.ToLowerInvariant()) {
|
switch (sw.Key.ToLowerInvariant()) {
|
||||||
case "wd":
|
case "wd":
|
||||||
case "workingdirectory":
|
case "workingdirectory":
|
||||||
result.WorkingDirectory = sw.Value;
|
result.WorkingDirectory = sw.Value;
|
||||||
@@ -35,6 +32,10 @@ namespace Orchard {
|
|||||||
case "tenant":
|
case "tenant":
|
||||||
result.Tenant = sw.Value;
|
result.Tenant = sw.Value;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
result.Switches.Add(sw.Key, sw.Value);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
8
src/Tools/Orchard/Parameters/CommandParameters.cs
Normal file
8
src/Tools/Orchard/Parameters/CommandParameters.cs
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace Orchard.Parameters {
|
||||||
|
public class CommandParameters {
|
||||||
|
public IList<string> Arguments { get; set; }
|
||||||
|
public IDictionary<string, string> Switches { get; set; }
|
||||||
|
}
|
||||||
|
}
|
27
src/Tools/Orchard/Parameters/CommandParametersParser.cs
Normal file
27
src/Tools/Orchard/Parameters/CommandParametersParser.cs
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace Orchard.Parameters {
|
||||||
|
public class CommandParametersParser : ICommandParametersParser {
|
||||||
|
public CommandParameters Parse(IEnumerable<string> args) {
|
||||||
|
var result = new CommandParameters {
|
||||||
|
Arguments = new List<string>(),
|
||||||
|
Switches = new Dictionary<string, string>()
|
||||||
|
};
|
||||||
|
|
||||||
|
foreach (var arg in args) {
|
||||||
|
if (arg[0] == '/') {
|
||||||
|
var switchKeyValue = arg.Substring(1).Split(':');
|
||||||
|
result.Switches.Add(switchKeyValue[0], switchKeyValue.Length >= 2 ? switchKeyValue[1] : string.Empty);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
result.Arguments.Add(arg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -1,5 +1,5 @@
|
|||||||
namespace Orchard.Arguments {
|
namespace Orchard.Parameters {
|
||||||
public class Switch {
|
public class CommandSwitch {
|
||||||
public string Name { get; set; }
|
public string Name { get; set; }
|
||||||
public string Value { get; set; }
|
public string Value { get; set; }
|
||||||
}
|
}
|
7
src/Tools/Orchard/Parameters/ICommandParametersParser.cs
Normal file
7
src/Tools/Orchard/Parameters/ICommandParametersParser.cs
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace Orchard.Parameters {
|
||||||
|
public interface ICommandParametersParser {
|
||||||
|
CommandParameters Parse(IEnumerable<string> args);
|
||||||
|
}
|
||||||
|
}
|
@@ -8,6 +8,7 @@ using System.Text;
|
|||||||
using System.Web;
|
using System.Web;
|
||||||
using System.Web.Hosting;
|
using System.Web.Hosting;
|
||||||
using Orchard.Host;
|
using Orchard.Host;
|
||||||
|
using Orchard.Parameters;
|
||||||
|
|
||||||
namespace Orchard {
|
namespace Orchard {
|
||||||
class Program {
|
class Program {
|
||||||
@@ -23,7 +24,7 @@ namespace Orchard {
|
|||||||
|
|
||||||
public void Run() {
|
public void Run() {
|
||||||
// Parse command line arguments
|
// Parse command line arguments
|
||||||
var arguments = new OrchardArgumentsParser().Parse(new Orchard.Arguments.Parser().Parse(_args));
|
var arguments = new OrchardParametersParser().Parse(new CommandParametersParser().Parse(_args));
|
||||||
|
|
||||||
if (string.IsNullOrEmpty(arguments.VirtualPath))
|
if (string.IsNullOrEmpty(arguments.VirtualPath))
|
||||||
arguments.VirtualPath = "/";
|
arguments.VirtualPath = "/";
|
||||||
@@ -50,7 +51,7 @@ namespace Orchard {
|
|||||||
if (arguments.Verbose) {
|
if (arguments.Verbose) {
|
||||||
Console.WriteLine("Executing command in ASP.NET AppDomain");
|
Console.WriteLine("Executing command in ASP.NET AppDomain");
|
||||||
}
|
}
|
||||||
host.RunCommand(_args);
|
host.RunCommand(arguments);
|
||||||
}
|
}
|
||||||
|
|
||||||
private DirectoryInfo GetOrchardDirectory(string directory) {
|
private DirectoryInfo GetOrchardDirectory(string directory) {
|
||||||
@@ -74,7 +75,8 @@ namespace Orchard {
|
|||||||
return directoryInfo;
|
return directoryInfo;
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new ApplicationException("Directory \"{0}\" doesn't seem to contain an Orchard installation");
|
throw new ApplicationException(
|
||||||
|
string.Format("Directory \"{0}\" doesn't seem to contain an Orchard installation", directory));
|
||||||
}
|
}
|
||||||
|
|
||||||
public object CreateWorkerAppDomainWithHost(string virtualPath, string physicalPath, Type hostType) {
|
public object CreateWorkerAppDomainWithHost(string virtualPath, string physicalPath, Type hostType) {
|
||||||
|
Reference in New Issue
Block a user