OSDN Git Service

AppliStation-All,アンインストール処理を新方式(NaGetUninstall2)に変更。
[applistation/AppliStation.git] / na-get-lib / NaGet.SubCommands / NaGetUninstall2.cs
diff --git a/na-get-lib/NaGet.SubCommands/NaGetUninstall2.cs b/na-get-lib/NaGet.SubCommands/NaGetUninstall2.cs
new file mode 100644 (file)
index 0000000..7778208
--- /dev/null
@@ -0,0 +1,89 @@
+using System;
+using System.Collections.Generic;
+using NaGet.Packages;
+using NaGet.Packages.Install;
+using NaGet.SubCommands;
+using NaGet.SubCommands.SubTask;
+using NaGet.Tasks;
+
+namespace NaGet.SubCommands
+{
+       /// <summary>
+       /// ソフトのアンインストール処理
+       /// </summary>
+       public class NaGetUninstall2 : NaGetTaskSet2
+       {
+               
+               private IList<Uninstallation> uninsts;
+               
+               public NaGetUninstall2(PackageListsManager pkgMan, IList<InstalledPackage> pkgs)
+               {
+                       uninsts = new List<Uninstallation>(pkgs.Count);
+                       foreach (InstalledPackage instPkg in pkgs) {
+                               uninsts.Add(new Uninstallation(instPkg));
+                       }
+                       
+                       // taskセットの初期化
+                       initSubTask();
+                       foreach (Uninstallation uninst in uninsts) {
+                               registSubTask(string.Format("アンインストール: {0}", uninst.ToString()),
+                                             new FunctionalSubTask(runUninstall, uninst));
+                       }
+                       registSubTask("インストール済みのソフトリスト更新",
+                                     new LocalUpdateSubTask(pkgMan));
+               }
+               
+               public override void Run()
+               {
+                       NotifyStarted();
+                       RaiseTaskSetEvent(TaskEventType.STARTED, string.Empty);
+                       
+                       try {
+                               while (hasMoreSubTask) {
+                                       RaiseTaskSetEvent(TaskEventType.STARTED_SUBTASK, currentSubTaskName);
+                                       currentSubTask.Run();
+                                       RaiseTaskSetEvent(TaskEventType.COMPLETED_SUBTASK, currentSubTaskName);
+                                       
+                                       if (cancelCalled) {
+                                               throw new TaskCanceledException("cancel is called");
+                                       }
+                                       
+                                       NotifyGoToNextSubTask();
+                               }
+                       } catch (TaskCanceledException) {
+                               cancelCalled = true;
+                       } catch (Exception e) {
+                               RaiseTaskSetEvent(TaskEventType.ERROR, e.Message);
+                       }
+                       
+                       if (cancelCalled) {
+                               NotifyCancelled();
+                               RaiseTaskSetEvent(TaskEventType.CANCELED, "キャンセルされました");
+                       } else {
+                               NotifyCompleted();
+                               RaiseTaskSetEvent(TaskEventType.COMPLETED, string.Empty);
+                       }
+               }
+               
+               private void runUninstall(object uninstObj)
+               {
+                       Uninstallation uninst = (Uninstallation) uninstObj;
+                       
+                       if (uninst.Installed) {
+                               try {
+                                       uninst.OutputDataReceived += this.ReceivedOutputData;
+                                       uninst.ErrorDataReceived += this.ReceivedErrorData;
+                                       int exitCode = uninst.Uninstall();
+                                       if (exitCode != 0) {
+                                               RaiseTaskSetEvent(TaskEventType.WARNING, "アンインストールが正常に終えていない可能性があります。プロセスの終了コード:"+exitCode);
+                                       }
+                               } catch (Exception e) {
+                                       RaiseTaskSetEvent(TaskEventType.ERROR, e.Message);
+                                       throw e;
+                               }
+                       } else {
+                               RaiseTaskSetEvent(TaskEventType.WARNING, string.Format("{0}は既にアンインストールされているか、ソフトの存在を確認できませんでした", uninst));
+                       }
+               }
+       }
+}