Working on CommandManager

--HG--
branch : dev
This commit is contained in:
Renaud Paquay
2010-04-08 14:46:58 -07:00
parent 250d6fd474
commit f766320b79
8 changed files with 134 additions and 50 deletions

View File

@@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autofac;
using NUnit.Framework;
using Orchard.Commands;
namespace Orchard.Tests.Commands {
[TestFixture]
public class CommandManagerTests {
private ICommandManager _manager;
[SetUp]
public void Init() {
var builder = new ContainerBuilder();
builder.RegisterType<CommandManager>().As<ICommandManager>();
var container = builder.Build();
_manager = container.Resolve<ICommandManager>();
}
[Test]
public void ManagerCanRunACommand() {
_manager.Execute(new CommandContext { Command = "foo bar" });
}
}
}

View File

@@ -116,6 +116,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Commands\CommandHandlerTests.cs" />
<Compile Include="Commands\CommandManagerTests.cs" />
<Compile Include="ContentManagement\ContentQueryTests.cs">
<SubType>Code</SubType>
</Compile>

View File

@@ -1,41 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.Routing;
using Autofac;
using Orchard.Environment;
namespace Orchard.Commands {
/// <summary>
/// This is the guy instantiated by the orchard.exe host. It is reponsible for
/// executing a single command.
/// </summary>
public class CommandAgent {
public void RunSingleCommand(string[] args) {
try {
Console.WriteLine("Command being run!");
var host = OrchardStarter.CreateHost(MvcSingletons);
host.Initialize();
var shell = host.
}
catch (Exception e) {
while (e != null) {
Console.WriteLine("Error: {0}", e.Message);
e = e.InnerException;
}
}
}
protected void MvcSingletons(ContainerBuilder builder) {
builder.RegisterInstance(ControllerBuilder.Current);
builder.RegisterInstance(RouteTable.Routes);
builder.RegisterInstance(ModelBinders.Binders);
builder.RegisterInstance(ModelMetadataProviders.Current);
builder.RegisterInstance(ViewEngines.Engines);
}
}
}

View File

@@ -0,0 +1,72 @@
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.Routing;
using Autofac;
using Orchard.Environment;
using Orchard.Environment.Configuration;
namespace Orchard.Commands {
/// <summary>
/// This is the guy instantiated by the orchard.exe host. It is reponsible for
/// executing a single command.
/// </summary>
public class CommandHostAgent {
public void RunSingleCommand(string[] args) {
try {
var context = ParseArguments(args);
var hostContainer = OrchardStarter.CreateHostContainer(MvcSingletons);
var host = hostContainer.Resolve<IOrchardHost>();
var tenantManager = hostContainer.Resolve<ITenantManager>();
host.Initialize();
// Find the shell settings for the tenant...
// cretae the stand-alone env
// resolve a command
}
catch (Exception e) {
for(; e != null; e = e.InnerException) {
Console.WriteLine("Error: {0}", e.Message);
}
}
}
private static CommandContext ParseArguments(IEnumerable<string> args) {
var arguments = new List<string>();
var switches = new NameValueCollection();
foreach (string arg in args) {
if (arg[0] == '/') {
string[] split = arg.Substring(1).Split(':');
switches.Add(split[0], split.Length >= 2 ? split[1] : string.Empty);
}
else {
arguments.Add(arg);
}
}
return new CommandContext {
Input = "",
Output = "",
Command = arguments[0],
Arguments = arguments.Skip(1).ToArray(),
Switches = switches
};
}
protected void MvcSingletons(ContainerBuilder builder) {
builder.RegisterInstance(ControllerBuilder.Current);
builder.RegisterInstance(RouteTable.Routes);
builder.RegisterInstance(ModelBinders.Binders);
builder.RegisterInstance(ModelMetadataProviders.Current);
builder.RegisterInstance(ViewEngines.Engines);
}
}
}

View File

@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autofac.Features.Metadata;
namespace Orchard.Commands {
public interface ICommandManager : IDependency{
void Execute(CommandContext context);
}
public class CommandManager : ICommandManager {
private readonly IEnumerable<Meta<ICommandHandler>> _handlers;
public CommandManager(IEnumerable<Meta<ICommandHandler>> handlers) {
_handlers = handlers;
}
public void Execute(CommandContext context) {
}
}
}

View File

@@ -57,17 +57,18 @@ namespace Orchard.Environment {
if (File.Exists(optionalHostConfig))
builder.RegisterModule(new ConfigurationSettingsReader(ConfigurationSettingsReader.DefaultSectionName, optionalHostConfig));
return builder.Build();
var container = builder.Build();
var updater = new ContainerUpdater();
updater.RegisterInstance(container);
updater.Update(container);
return container;
}
public static IOrchardHost CreateHost(Action<ContainerBuilder> registrations) {
var container = CreateHostContainer(registrations);
var updater = new ContainerUpdater();
updater.RegisterInstance(container);
updater.Update(container);
var orchardHost = container.Resolve<IOrchardHost>();
return orchardHost;
return container.Resolve<IOrchardHost>();
}
}
}

View File

@@ -137,7 +137,8 @@
<Compile Include="Validation\Argument.cs" />
</ItemGroup>
<ItemGroup>
<Compile Include="Commands\CommandAgent.cs" />
<Compile Include="Commands\CommandHostAgent.cs" />
<Compile Include="Commands\CommandManager.cs" />
<Compile Include="Commands\OrchardCommandAttribute.cs" />
<Compile Include="Commands\CommandContext.cs" />
<Compile Include="Commands\DefaultOrchardCommandHandler.cs" />

View File

@@ -20,7 +20,7 @@ namespace Orchard.Host {
}
public void RunCommand(string[] args) {
var agent = Activator.CreateInstance("Orchard.Framework", "Orchard.Commands.CommandAgent").Unwrap();
var agent = Activator.CreateInstance("Orchard.Framework", "Orchard.Commands.CommandHostAgent").Unwrap();
agent.GetType().GetMethod("RunSingleCommand").Invoke(agent, new object[] { args });
}
}