Fix for http://orchard.codeplex.com/workitem/16600 Web command line doesn't support switches and quoted arguments.

--HG--
branch : dev
This commit is contained in:
Bertrand Le Roy
2010-08-30 16:34:19 -07:00
parent b7d809e8d0
commit 013f11319a

View File

@@ -1,4 +1,4 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web.Mvc;
@@ -27,12 +27,9 @@ namespace Orchard.DevTools.Controllers {
[HttpPost]
public ActionResult Execute(CommandsExecuteViewModel model) {
var writer = new StringWriter();
var parameters = new CommandParameters {
Arguments = model.CommandLine.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries),
Output = writer
};
var commandLine = model.CommandLine.Trim();
CommandParameters parameters = GetCommandParameters(commandLine, writer);
_commandManager.Execute(parameters);
model.History = (model.History ?? Enumerable.Empty<string>())
@@ -43,6 +40,63 @@ namespace Orchard.DevTools.Controllers {
return View("Execute", model);
}
private static CommandParameters GetCommandParameters(string commandLine, StringWriter writer) {
var arguments = new List<string>();
var switches = new Dictionary<string,string>();
var current = 0;
while (current < commandLine.Length) {
var nextSpace = commandLine.IndexOf(' ', current);
if (nextSpace == -1) nextSpace = commandLine.Length;
var arg = commandLine.Substring(current, nextSpace - current).Trim();
if (arg.Length == 0) {
current = nextSpace + 1;
continue;
}
if (arg[0] == '/') {
var colonIndex = arg.IndexOf(':');
if (colonIndex != -1) {
var switchName = arg.Substring(1, colonIndex - 1);
if (arg.Length > colonIndex + 1) {
if (arg[colonIndex + 1] == '"') {
var beginningOfSwitchValue = commandLine.IndexOf('"', current) + 1;
if (beginningOfSwitchValue != 0) {
var endOfSwitchValue = commandLine.IndexOf('"', beginningOfSwitchValue);
if (endOfSwitchValue != -1) {
switches.Add(switchName,
commandLine.Substring(beginningOfSwitchValue,
endOfSwitchValue - beginningOfSwitchValue));
current = endOfSwitchValue + 1;
continue;
}
}
}
else {
switches.Add(switchName, arg.Substring(colonIndex + 1));
current = nextSpace + 1;
continue;
}
}
}
}
else if (arg[0] == '"') {
var argumentStart = commandLine.IndexOf('"', current) + 1;
var argumentEnd = commandLine.IndexOf('"', argumentStart);
if (argumentEnd != -1) {
arguments.Add(commandLine.Substring(argumentStart, argumentEnd - argumentStart));
current = argumentEnd + 1;
continue;
}
}
arguments.Add(arg);
current = nextSpace + 1;
}
return new CommandParameters {
Arguments = arguments,
Switches = switches,
Output = writer
};
}
}
}