public void Shutdown(CommandHostContext context)
        {
            try {
                if (context.CommandHost != null) {
                    LogInfo(context, "Shutting down Coevery session...");
                    context.CommandHost.StopSession(_input, _output);
                }
            }
            catch (AppDomainUnloadedException) {
                LogInfo(context, "   (AppDomain already unloaded)");
            }

            if (context.CommandHost != null) {
                LogInfo(context, "Shutting down ASP.NET AppDomain...");
                ApplicationManager.GetApplicationManager().ShutdownAll();
            }
        }
 public CommandReturnCodes 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 CommandReturnCodes RunCommandInSession(CommandHostContext context, string command)
 {
     try {
         var args = new CoeveryParametersParser().Parse(new CommandParametersParser().Parse(new CommandLineParser().Parse(command)));
         return context.CommandHost.RunCommandInSession(_input, _output, context.Logger, args);
     }
     catch (AppDomainUnloadedException) {
         _output.WriteLine("AppDomain of Coevery session has been unloaded. (Retrying...)");
         return context.RetryResult;
     }
 }
        private CommandHostContext RunCommand(CommandHostContext context, string command)
        {
            if (string.IsNullOrWhiteSpace(command))
                return context;

            CommandReturnCodes result = RunCommandInSession(context, command);
            if (result == context.RetryResult) {
                _commandHostContextProvider.Shutdown(context);
                context = CommandHostContext();
                result = RunCommandInSession(context, command);
                if (result != CommandReturnCodes.Ok)
                    _output.WriteLine("Command returned non-zero result: {0}", result);
            }
            return context;
        }
 private string ReadCommand(CommandHostContext context)
 {
     _output.WriteLine();
     _output.Write("Coevery> ");
     return _input.ReadLine();
 }
 private CommandReturnCodes ExecuteSingleCommand(CommandHostContext context)
 {
     return context.CommandHost.RunCommand(_input, _output, context.Logger, context.Arguments);
 }
 private CommandReturnCodes ExecuteResponseFiles(CommandHostContext context)
 {
     var responseLines = new ResponseFiles.ResponseFiles().ReadFiles(context.Arguments.ResponseFiles);
     return context.CommandHost.RunCommands(_input, _output, context.Logger, responseLines.ToArray());
 }
 public CommandHostContext CreateContext()
 {
     var context = new CommandHostContext { RetryResult = CommandReturnCodes.Retry };
     Initialize(context);
     return context;
 }
 private void LogInfo(CommandHostContext context, string format, params object[] args)
 {
     if (context.Logger != null)
         context.Logger.LogInfo(format, args);
 }
        private void Initialize(CommandHostContext context)
        {
            context.Arguments = new CoeveryParametersParser().Parse(new CommandParametersParser().Parse(_args));
            context.Logger = new Logger(context.Arguments.Verbose, _output);

            // Perform some argument validation and display usage if something is incorrect
            context.DisplayUsageHelp = context.Arguments.Switches.ContainsKey("?");
            if (context.DisplayUsageHelp)
                return;

            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;
            }

            if (string.IsNullOrEmpty(context.Arguments.VirtualPath))
                context.Arguments.VirtualPath = "/";
            LogInfo(context, "Virtual path: \"{0}\"", context.Arguments.VirtualPath);

            if (string.IsNullOrEmpty(context.Arguments.WorkingDirectory))
                context.Arguments.WorkingDirectory = Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);

            LogInfo(context, "Working directory: \"{0}\"", context.Arguments.WorkingDirectory);

            LogInfo(context, "Detecting Coevery installation root directory...");
            context.CoeveryDirectory = GetCoeveryDirectory(context.Arguments.WorkingDirectory);
            LogInfo(context, "Coevery root directory: \"{0}\"", context.CoeveryDirectory.FullName);

            LogInfo(context, "Creating ASP.NET AppDomain for command agent...");
            context.CommandHost = CreateWorkerAppDomainWithHost(context.Arguments.VirtualPath, context.CoeveryDirectory.FullName, typeof(CommandHost));

            LogInfo(context, "Starting Coevery session");
            context.StartSessionResult = context.CommandHost.StartSession(_input, _output);
        }