protected override async void Execute() { string message; var path = this.OutputDirectory; DirectoryInfo directory; // 出力先ディレクトリのディレクトリ名の検証 try { directory = System.IO.Directory.CreateDirectory(path); } catch (Exception) { await this._DialogService.ShowMessage(MessageBoxButton.OK, Resources.Msg_BackupFailed, null); return; } // 空かどうか検証 if (!this.IsOutputDirectoryEmpty(directory.FullName)) { var result = await this._DialogService.ShowMessage(MessageBoxButton.YesNo, Resources.Msg_DirectoryIsNotEmpty, null); if (result == MessageBoxResult.No) { return; } } var configuration = new BackupConfiguration { Database = this.Database, Files = this.Files, Plugins = this.Plugins, Themes = this.Themes }; try { var progressDialogService = new ProgressDialogService { IsAutoClose = true }; var engine = new BackupEngine(this._BitnamiRedmineService, this._BackupService, this._DispatcherService, this._LogService, configuration, this.Stack, this._OutputDirectory); var report = engine.PrepareBackup(); progressDialogService.Action = () => { engine.ExecuteBackup(); }; progressDialogService.Report = report; await progressDialogService.ShowMessage(null, null); if (progressDialogService.Result == MessageBoxResult.Cancel) { message = Resources.Msg_BackupCancel; await this._DialogService.ShowMessage(MessageBoxButton.OK, message, null); return; } else { message = Resources.Msg_BackupComplete; } } 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; await this._DialogService.ShowMessage(MessageBoxButton.OK, message, null); return; } // Update Setting RedmineSetting redmineSetting; var applicationSetting = this.GetApplicationSetting(out redmineSetting); redmineSetting.Backup.Database = configuration.Database; redmineSetting.Backup.Files = configuration.Files; redmineSetting.Backup.Plugins = configuration.Plugins; redmineSetting.Backup.Themes = configuration.Themes; redmineSetting.Backup.BaseDirectory = this.Directory; redmineSetting.Backup.DirectoryName = this.DirectoryName; var history = new BackupHistorySetting { DisplayVersion = this.Stack.DisplayVersion, DateTime = DateTime.UtcNow, OutputDirectory = path }; this._ApplicationSettingService.BackupHistories.Add(history); applicationSetting.BackupHistories.Add(history); this._ApplicationSettingService.UpdateApplicationSetting(applicationSetting); 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(); } }