Merge OrchardCLI.exe with Orchard.exe

--HG--
branch : dev
This commit is contained in:
Renaud Paquay
2010-07-28 14:11:28 -07:00
parent dcda153286
commit 89a3186cef
11 changed files with 150 additions and 393 deletions

View File

@@ -1,16 +1,14 @@
using System;
using System.IO;
using System.Web.Hosting;
using Orchard.Host;
namespace Orchard.HostContext {
public class CommandHostContext {
public bool Interactive { get; set; }
public int RetryResult { get; set; }
public OrchardParameters Arguments { get; set; }
public DirectoryInfo OrchardDirectory { get; set; }
public int StartSessionResult { get; set; }
public bool ShowHelp { get; set; }
public bool DisplayUsageHelp { get; set; }
public ApplicationManager AppManager { get; set; }
public ApplicationObject AppObject { get; set; }
public CommandHost CommandHost { get; set; }

View File

@@ -20,9 +20,8 @@ namespace Orchard.HostContext {
_args = args;
}
public CommandHostContext CreateContext(bool interactive) {
public CommandHostContext CreateContext() {
var context = new CommandHostContext();
context.Interactive = interactive;
context.RetryResult = 240;/*special return code for "Retry"*/
Initialize(context);
return context;
@@ -50,26 +49,13 @@ namespace Orchard.HostContext {
context.Logger = new Logger(context.Arguments.Verbose, _output);
// Perform some argument validation and display usage if something is incorrect
bool showHelp = context.Arguments.Switches.ContainsKey("?");
if (!showHelp) {
// If not interactive CLI, we need at least some arguments...
if (!context.Interactive) {
showHelp = (!context.Arguments.Arguments.Any() && !context.Arguments.ResponseFiles.Any());
}
}
context.DisplayUsageHelp = context.Arguments.Switches.ContainsKey("?");
if (context.DisplayUsageHelp)
return;
if (!showHelp) {
// If not interactive CLI, we need
if (!context.Interactive) {
showHelp = (context.Arguments.Arguments.Any() && context.Arguments.ResponseFiles.Any());
if (showHelp) {
_output.WriteLine("Incorrect syntax: Response files cannot be used in conjunction with commands");
}
}
}
if (showHelp) {
context.ShowHelp = true;
context.DisplayUsageHelp = (context.Arguments.Arguments.Any() && context.Arguments.ResponseFiles.Any());
if (context.DisplayUsageHelp) {
_output.WriteLine("Incorrect syntax: Response files cannot be used in conjunction with commands");
return;
}

View File

@@ -1,6 +1,6 @@
namespace Orchard.HostContext {
public interface ICommandHostContextProvider {
CommandHostContext CreateContext(bool interactive);
CommandHostContext CreateContext();
void Shutdown(CommandHostContext context);
}
}

View File

@@ -2,6 +2,7 @@
using System.Linq;
using System.IO;
using Orchard.HostContext;
using Orchard.Parameters;
namespace Orchard {
class OrchardHost {
@@ -20,7 +21,7 @@ namespace Orchard {
return DoRun();
}
catch (Exception e) {
_output.WriteLine("Error running command:");
_output.WriteLine("Error:");
for (; e != null; e = e.InnerException) {
_output.WriteLine(" {0}", e.Message);
}
@@ -30,44 +31,165 @@ namespace Orchard {
private int DoRun() {
var context = CommandHostContext();
if (context.ShowHelp) {
DisplayHelp();
if (context.DisplayUsageHelp) {
DisplayUsageHelp();
return 0;
}
var result = Execute(context);
int result;
if (context.Arguments.Arguments.Any())
result = ExecuteSingleCommand(context);
else if (context.Arguments.ResponseFiles.Any())
result = ExecuteResponseFiles(context);
else {
result = ExecuteInteractive(context);
}
_commandHostContextProvider.Shutdown(context);
return result;
}
private CommandHostContext CommandHostContext() {
_output.WriteLine("Initializing Orchard host. (This might take a few seconds...)");
var result = _commandHostContextProvider.CreateContext(false/*interactive*/);
_output.WriteLine("Initializing Orchard session. (This might take a few seconds...)");
var result = _commandHostContextProvider.CreateContext();
if (result.StartSessionResult == result.RetryResult) {
result = _commandHostContextProvider.CreateContext(false/*interactive*/);
result = _commandHostContextProvider.CreateContext();
}
return result;
}
private int Execute(CommandHostContext context) {
if (context.Arguments.ResponseFiles.Any()) {
var responseLines = new ResponseFiles.ResponseFiles().ReadFiles(context.Arguments.ResponseFiles);
return context.CommandHost.RunCommands(_input, _output, context.Logger, responseLines.ToArray());
}
else {
return context.CommandHost.RunCommand(_input, _output, context.Logger, context.Arguments);
private int ExecuteSingleCommand(CommandHostContext context) {
return context.CommandHost.RunCommand(_input, _output, context.Logger, context.Arguments);
}
private int 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) {
_output.WriteLine("Type \"?\" for help, \"exit\" to exit, \"cls\" to clear screen");
while (true) {
var command = ReadCommand(context);
switch (command.ToLowerInvariant()) {
case "quit":
case "q":
case "exit":
case "e":
return 0;
case "help":
case "?":
DisplayInteractiveHelp();
break;
case "cls":
Console.Clear();
break;
default:
context = RunCommand(context, command);
break;
}
}
}
private void DisplayHelp() {
private string ReadCommand(CommandHostContext context) {
_output.WriteLine();
_output.Write("orchard> ");
return _input.ReadLine();
}
private CommandHostContext RunCommand(CommandHostContext context, string command) {
if (string.IsNullOrWhiteSpace(command))
return context;
int result = RunCommandInSession(context, command);
if (result == context.RetryResult) {
_commandHostContextProvider.Shutdown(context);
context = CommandHostContext();
result = RunCommandInSession(context, command);
if (result != 0)
_output.WriteLine("Command returned non-zero result: {0}", result);
}
return context;
}
private int 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);
}
catch (AppDomainUnloadedException) {
_output.WriteLine("AppDomain of Orchard session has been unloaded. (Retrying...)");
return context.RetryResult;
}
}
private void DisplayInteractiveHelp() {
_output.WriteLine("The Orchard command interpreter supports running a few built-in commands");
_output.WriteLine("as well as specific commands from enabled features of an Orchard installation.");
_output.WriteLine("");
_output.WriteLine("The general syntax of commands is");
_output.WriteLine("");
_output.WriteLine(" <command-name> [arg1] ... [argn] [/switch1[:value1]] ... [/switchn[:valuen]]");
_output.WriteLine("");
_output.WriteLine(" <command-name>");
_output.WriteLine(" Specifies the command to execute (the command name can be multiple words separated by spaces");
_output.WriteLine("");
_output.WriteLine(" [arg1] ... [argn]");
_output.WriteLine(" Specifies additional arguments for the command");
_output.WriteLine("");
_output.WriteLine(" [/switch1[:value1]] ... [/switchn[:valuen]]");
_output.WriteLine(" Specifies switches to apply to the command. Available switches generally ");
_output.WriteLine(" depend on the command executed, with the exception of a few built-in ones.");
_output.WriteLine("");
_output.WriteLine(" Built-in commands");
_output.WriteLine(" =================");
_output.WriteLine("");
_output.WriteLine(" help|h|?");
_output.WriteLine(" Displays this message");
_output.WriteLine("");
_output.WriteLine(" exit|quit|e|q");
_output.WriteLine(" Terminates the interactive session");
_output.WriteLine("");
_output.WriteLine(" cls");
_output.WriteLine(" Clears the console screen");
_output.WriteLine("");
_output.WriteLine(" help commands");
_output.WriteLine(" Displays the list of available commands");
_output.WriteLine("");
_output.WriteLine(" help <command-name>");
_output.WriteLine(" Display help for a given command");
_output.WriteLine("");
_output.WriteLine(" Built-in switches");
_output.WriteLine(" =================");
_output.WriteLine("");
_output.WriteLine(" /Verbose");
_output.WriteLine(" /v");
_output.WriteLine(" Turns on verbose output");
_output.WriteLine("");
_output.WriteLine(" /Tenant:tenant-name");
_output.WriteLine(" /t:tenant-name");
_output.WriteLine(" Specifies which tenant to run the command into. \"Default\" tenant by default.");
_output.WriteLine("");
}
private void DisplayUsageHelp() {
_output.WriteLine("Executes Orchard commands from a Orchard installation directory.");
_output.WriteLine("");
_output.WriteLine("Usage:");
_output.WriteLine(" orchard.exe command [arg1] ... [argn] [/switch1[:value1]] ... [/switchn[:valuen]]");
_output.WriteLine(" orchard.exe @response-file1 ... [@response-filen] [/switch1[:value1]] ... [/switchn[:valuen]]");
_output.WriteLine(" orchard.exe");
_output.WriteLine(" Starts the Orchard command interpreter");
_output.WriteLine("");
_output.WriteLine(" command");
_output.WriteLine(" Specify the command to execute");
_output.WriteLine(" orchard.exe <command-name> [arg1] ... [argn] [/switch1[:value1]] ... [/switchn[:valuen]]");
_output.WriteLine(" Executes a single command");
_output.WriteLine("");
_output.WriteLine(" orchard.exe @response-file1 ... [@response-filen] [/switch1[:value1]] ... [/switchn[:valuen]]");
_output.WriteLine(" Executes multiples commands from response files");
_output.WriteLine("");
_output.WriteLine(" where");
_output.WriteLine("");
_output.WriteLine(" <command-name>");
_output.WriteLine(" Specifies the command to execute (the command name can be multiple words separated by spaces");
_output.WriteLine("");
_output.WriteLine(" [arg1] ... [argn]");
_output.WriteLine(" Specify additional arguments for the command");

View File

@@ -1,131 +0,0 @@
using System;
using System.IO;
using Orchard;
using Orchard.HostContext;
using Orchard.Parameters;
using Orchard.ResponseFiles;
namespace OrchardCLI {
class CLIHost {
private readonly TextWriter _output;
private readonly TextReader _input;
private readonly ICommandHostContextProvider _commandHostContextProvider;
public CLIHost(TextReader input, TextWriter output, string[] args) {
_input = input;
_output = output;
_commandHostContextProvider = new CommandHostContextProvider(args);
}
public int Run() {
try {
return DoRun();
}
catch (Exception e) {
_output.WriteLine("Error:");
for (; e != null; e = e.InnerException) {
_output.WriteLine(" {0}", e.Message);
}
return -1;
}
}
public int DoRun() {
var context = CommandHostContext();
if (context.ShowHelp) {
DisplayHelp();
return 0;
}
_output.WriteLine("Type \"help commands\" for help, \"exit\" to exit, \"cls\" to clear screen");
while (true) {
var command = ReadCommand(context);
switch (command.ToLowerInvariant()) {
case "quit":
case "q":
case "exit":
case "e":
_commandHostContextProvider.Shutdown(context);
return 0;
case "cls":
Console.Clear();
break;
default:
context = RunCommand(context, command);
break;
}
}
}
private string ReadCommand(CommandHostContext context) {
_output.WriteLine();
_output.Write("orchard> ");
return _input.ReadLine();
}
private CommandHostContext CommandHostContext() {
_output.WriteLine("Initializing Orchard session. (This might take a few seconds...)");
var result = _commandHostContextProvider.CreateContext(true/*interactive*/);
if (result.StartSessionResult == result.RetryResult) {
result = _commandHostContextProvider.CreateContext(true/*interactive*/);
}
return result;
}
private CommandHostContext RunCommand(CommandHostContext context, string command) {
if (string.IsNullOrWhiteSpace(command))
return context;
int result = RunCommandInSession(context, command);
if (result == context.RetryResult) {
_commandHostContextProvider.Shutdown(context);
context = CommandHostContext();
result = RunCommandInSession(context, command);
if (result != 0)
_output.WriteLine("Command returned non-zero result: {0}", result);
}
return context;
}
private int 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);
}
catch (AppDomainUnloadedException) {
_output.WriteLine("AppDomain of Orchard session has been unloaded. (Retrying...)");
return context.RetryResult;
}
}
private void DisplayHelp() {
_output.WriteLine("Executes the Orchard interactive command line from a Orchard installation directory.");
_output.WriteLine("");
_output.WriteLine("Usage:");
_output.WriteLine(" orchardcli.exe [/switch1[:value1]] ... [/switchn[:valuen]]");
_output.WriteLine("");
_output.WriteLine(" Built-in switches");
_output.WriteLine(" =================");
_output.WriteLine("");
_output.WriteLine(" /WorkingDirectory:<physical-path>");
_output.WriteLine(" /wd:<physical-path>");
_output.WriteLine(" Specifies the orchard installation directory. The current directory is the default.");
_output.WriteLine("");
_output.WriteLine(" /Verbose");
_output.WriteLine(" /v");
_output.WriteLine(" Turn on verbose output");
_output.WriteLine("");
_output.WriteLine(" /VirtualPath:<virtual-path>");
_output.WriteLine(" /vp:<virtual-path>");
_output.WriteLine(" Virtual path to pass to the WebHost. Empty (i.e. root path) by default.");
_output.WriteLine("");
_output.WriteLine(" /Tenant:tenant-name");
_output.WriteLine(" /t:tenant-name");
_output.WriteLine(" Specifies which tenant to run the command into. \"Default\" tenant by default.");
_output.WriteLine("");
}
}
}

View File

@@ -1,156 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>9.0.30729</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{71A006E0-85BD-4CC4-ADF9-B548D5CA72A7}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>OrchardCLI</RootNamespace>
<AssemblyName>OrchardCLI</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<FileUpgradeFlags>
</FileUpgradeFlags>
<OldToolsVersion>3.5</OldToolsVersion>
<UpgradeBackupLocation />
<IsWebBootstrapper>false</IsWebBootstrapper>
<TargetFrameworkProfile />
<PublishUrl>publish\</PublishUrl>
<Install>true</Install>
<InstallFrom>Disk</InstallFrom>
<UpdateEnabled>false</UpdateEnabled>
<UpdateMode>Foreground</UpdateMode>
<UpdateInterval>7</UpdateInterval>
<UpdateIntervalUnits>Days</UpdateIntervalUnits>
<UpdatePeriodically>false</UpdatePeriodically>
<UpdateRequired>false</UpdateRequired>
<MapFileExtensions>true</MapFileExtensions>
<ApplicationRevision>0</ApplicationRevision>
<ApplicationVersion>1.0.0.%2a</ApplicationVersion>
<UseApplicationTrust>false</UseApplicationTrust>
<BootstrapperEnabled>true</BootstrapperEnabled>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<UseVSHostingProcess>true</UseVSHostingProcess>
<PlatformTarget>AnyCPU</PlatformTarget>
<CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<PlatformTarget>AnyCPU</PlatformTarget>
<CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Web" />
<Reference Include="System.Xml.Linq">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Data.DataSetExtensions">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="..\Orchard\ApplicationObject.cs">
<Link>ApplicationObject.cs</Link>
</Compile>
<Compile Include="..\Orchard\HostContext\CommandHostContext.cs">
<Link>HostContext\CommandHostContext.cs</Link>
</Compile>
<Compile Include="..\Orchard\HostContext\CommandHostContextProvider.cs">
<Link>HostContext\CommandHostContextProvider.cs</Link>
</Compile>
<Compile Include="..\Orchard\HostContext\ICommandHostContextProvider.cs">
<Link>HostContext\ICommandHostContextProvider.cs</Link>
</Compile>
<Compile Include="..\Orchard\Host\CommandHost.cs">
<Link>Host\CommandHost.cs</Link>
</Compile>
<Compile Include="..\Orchard\IOrchardParametersParser.cs">
<Link>IOrchardParametersParser.cs</Link>
</Compile>
<Compile Include="..\Orchard\Logger.cs">
<Link>Logger.cs</Link>
</Compile>
<Compile Include="..\Orchard\OrchardParameters.cs">
<Link>OrchardParameters.cs</Link>
</Compile>
<Compile Include="..\Orchard\OrchardParametersParser.cs">
<Link>OrchardParametersParser.cs</Link>
</Compile>
<Compile Include="..\Orchard\Parameters\CommandLineParser.cs">
<Link>Parameters\CommandLineParser.cs</Link>
</Compile>
<Compile Include="..\Orchard\Parameters\CommandParameters.cs">
<Link>Parameters\CommandParameters.cs</Link>
</Compile>
<Compile Include="..\Orchard\Parameters\CommandParametersParser.cs">
<Link>Parameters\CommandParametersParser.cs</Link>
</Compile>
<Compile Include="..\Orchard\Parameters\CommandSwitch.cs">
<Link>Parameters\CommandSwitch.cs</Link>
</Compile>
<Compile Include="..\Orchard\Parameters\ICommandParametersParser.cs">
<Link>Parameters\ICommandParametersParser.cs</Link>
</Compile>
<Compile Include="..\Orchard\ResponseFiles\ResponseFileReader.cs">
<Link>ResponseFiles\ResponseFileReader.cs</Link>
</Compile>
<Compile Include="..\Orchard\ResponseFiles\ResponseFiles.cs">
<Link>ResponseFiles\ResponseFiles.cs</Link>
</Compile>
<Compile Include="CLIHost.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<BootstrapperPackage Include="Microsoft.Net.Client.3.5">
<Visible>False</Visible>
<ProductName>.NET Framework 3.5 SP1 Client Profile</ProductName>
<Install>false</Install>
</BootstrapperPackage>
<BootstrapperPackage Include="Microsoft.Net.Framework.3.5.SP1">
<Visible>False</Visible>
<ProductName>.NET Framework 3.5 SP1</ProductName>
<Install>true</Install>
</BootstrapperPackage>
<BootstrapperPackage Include="Microsoft.Windows.Installer.3.1">
<Visible>False</Visible>
<ProductName>Windows Installer 3.1</ProductName>
<Install>true</Install>
</BootstrapperPackage>
</ItemGroup>
<ItemGroup>
<None Include="app.config">
<SubType>Designer</SubType>
</None>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>

View File

@@ -1,9 +0,0 @@
using System;
namespace OrchardCLI {
public class Program {
public static int Main(string[] args) {
return new CLIHost(Console.In, Console.Out, args).Run();
}
}
}

View File

@@ -1,36 +0,0 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("OrchardCLI")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Orchard")]
[assembly: AssemblyCopyright("Copyright © CodePlex Foundation 2009")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("5ee627dd-b767-441e-b8d0-ec7d26faac49")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("0.5.0")]
[assembly: AssemblyFileVersion("0.5.0")]

View File

@@ -1,6 +0,0 @@
<?xml version="1.0"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
</configuration>