protected override async void Execute() { var configuration = new BackupConfiguration { Database = this.HasDatabase && this.Database, Files = this.HasFile && this.Files, Plugins = this.HasPlugin && this.Plugins, Themes = this.HasTheme && this.Themes }; // リストア処理の実行 string message = null; try { var progressDialogService = new ProgressDialogService { IsAutoClose = true }; var engine = new RestoreEngine(this._BitnamiRedmineService, this._BackupService, this._DispatcherService, this._LogService, configuration, this.Stack, this.Directory); var report = engine.PrepareRestore(); progressDialogService.Action = () => { engine.ExecuteRestore(); }; progressDialogService.Report = report; await progressDialogService.ShowMessage(null, null); if (progressDialogService.Result == MessageBoxResult.Cancel) { message = Resources.Msg_RestoreCancel; } else { message = Resources.Msg_RestoreComplete; } } catch (Exception ex) { message = $"Exception is thown. Reason is {ex.Message}"; this._LogService.Error(message); message = $"StackTrace is {ex.StackTrace}"; this._LogService.Error(message); message = Resources.Msg_BackupFailed; } finally { await this._DialogService.ShowMessage(MessageBoxButton.OK, message, null); } }
private int ExecuteCommandLine(string mode, string version, BackupConfiguration configuration, string output, string name) { // インスタンス生成時点で IoC コンテナを生成し、必要なインジェクションが // 終了している var locator = new ViewModelLocator(); var bitnamiRedmineService = SimpleIoc.Default.GetInstance <IBitnamiRedmineService>(); try { // 自プロセスを親プロセスのコンソールにアタッチ // とりあえず失敗しない前提 SafeNativeMethods.AttachConsole(uint.MaxValue); // stdoutのストリームを取得 var defaultStdout = new IntPtr(7); var currentStdout = SafeNativeMethods.GetStdHandle(SafeNativeMethods.STD_OUTPUT_HANDLE); // リセット if (currentStdout != defaultStdout) { SafeNativeMethods.SetStdHandle(SafeNativeMethods.STD_OUTPUT_HANDLE, defaultStdout); } // これ以降は、普通に Console.WriteLine 等が使える var writer = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = true }; Console.SetOut(writer); var redmineStack = bitnamiRedmineService.GetBitnamiRedmineStacks(). FirstOrDefault(stack => stack.DisplayVersion.Equals(version)); // 指定した引数のバージョンの Redmine が見つからなかった if (redmineStack == null) { Console.WriteLine($"Specified Redmine version '{version}' is not found."); return(ArgumentError); } switch (mode.ToLowerInvariant()) { case "backup": { // フォルダが存在しない場合もあるので作成 output = Utility.GetSanitizedDirectoryPath(redmineStack, Path.Combine(output, name)); if (!Directory.Exists(output)) { Directory.CreateDirectory(output); } Console.WriteLine($"Start backup to '{output}'..."); var engine = new BackupEngine(bitnamiRedmineService, SimpleIoc.Default.GetInstance <IBackupService>(), null, SimpleIoc.Default.GetInstance <ILogService>(), configuration, redmineStack, output); var report = engine.PrepareBackup(); engine.ExecuteBackup(); // プログレスの変更のイベントをサブスクライブしてコンソールに進捗状況を表示する? //progressDialogService.Action = () => //{ //}; } return(Success); case "restore": { Console.WriteLine($"Start restore from '{output}'..."); var engine = new RestoreEngine(SimpleIoc.Default.GetInstance <IBitnamiRedmineService>(), SimpleIoc.Default.GetInstance <IBackupService>(), null, SimpleIoc.Default.GetInstance <ILogService>(), configuration, redmineStack, output); var report = engine.PrepareRestore(); engine.ExecuteRestore(); // プログレスの変更のイベントをサブスクライブしてコンソールに進捗状況を表示する? //progressDialogService.Action = () => //{ //}; } return(Success); default: Console.WriteLine($"Argument '{mode}' is invalid."); return(ArgumentError); } } finally { SafeNativeMethods.FreeConsole(); } }