OSDN Git Service

AppliStation-All,アンインストール処理を新方式(NaGetUninstall2)に変更。
authorttp <ttp@users.sourceforge.jp>
Mon, 9 May 2011 15:00:16 +0000 (00:00 +0900)
committerttp <ttp@users.sourceforge.jp>
Mon, 9 May 2011 15:00:16 +0000 (00:00 +0900)
AppliStation/PackageListViewForm.cs
all-get/Main.cs
na-get-lib/NaGet.SubCommands/NaGetUninstall2.cs [new file with mode: 0644]
na-get-lib/na-get-lib.csproj

index 773738c..af829a1 100644 (file)
@@ -486,7 +486,7 @@ namespace AppliStation
                {\r
                        AppliStation.Util.ExecutionProgressViewer prog = new AppliStation.Util.ExecutionProgressViewer();\r
                        prog.Shown += delegate(object sender2, EventArgs e2) {\r
-                               NaGet.SubCommands.NaGetUninstall tasks = new NaGet.SubCommands.NaGetUninstall(pkgListsMan, pkgs);\r
+                               NaGet.SubCommands.NaGetUninstall2 tasks = new NaGet.SubCommands.NaGetUninstall2(pkgListsMan, pkgs);\r
                                prog.SetTaskSet(tasks);\r
                                prog.Refresh();\r
                                prog.StartTaskSet();\r
index a3f210d..bc608b2 100644 (file)
@@ -63,6 +63,65 @@ namespace AllGet
                }\r
        }\r
 \r
+       sealed class TaskSetEventHandlers\r
+       {\r
+               public static void OnTaskSetEvent(object sender, TaskEventArgs e) { \r
+                       switch (e.Type) {\r
+//                             case NaGetTaskSetEventType.COMPLETED_TASKSET\r
+//                                     break;\r
+                               case TaskEventType.COMPLETED:\r
+                                       Console.WriteLine("Done.");\r
+                                       break;\r
+                               case TaskEventType.STARTED_SUBTASK:\r
+                                       Console.Write("  " + e.TaskMessage);\r
+                                       break;\r
+                               case TaskEventType.COMPLETED_SUBTASK:\r
+                                       Console.WriteLine(" ... Done. [{0}%]", (int) e.ProgressPercent);\r
+                                       break;\r
+                               case TaskEventType.INFO:\r
+                                       Console.WriteLine("  " + e.TaskMessage);\r
+                                       break;\r
+                               case TaskEventType.ERROR:\r
+                               case TaskEventType.WARNING:\r
+                                       Console.WriteLine("  [Error]" + e.TaskMessage);\r
+                                       break;\r
+                       }\r
+               }\r
+               \r
+               public static void OnDownloadSubTaskEvent(object sender, TaskEventArgs e)\r
+               {\r
+                       if (sender is NaGet.SubCommands.SubTask.DownloadSubTask) {\r
+                               if (e.Type == TaskEventType.STARTED) {\r
+                                       Console.WriteLine();\r
+                               }\r
+                               \r
+                               int origPosX = Console.CursorLeft;\r
+                               for (int i = 0; i < Console.WindowWidth - 1; i++) {\r
+                                       Console.Write(' ');\r
+                               }\r
+                               Console.CursorLeft = origPosX;\r
+                               \r
+                               switch (e.Type) {\r
+                                       case TaskEventType.PING:\r
+                                               {\r
+                                                       int origPos = Console.CursorLeft;\r
+                                                       Console.Write(e.TaskMessage);\r
+                                                       Console.CursorLeft = origPos;\r
+                                               }\r
+                                               break;\r
+                                       case TaskEventType.WARNING:\r
+                                       case TaskEventType.ERROR:\r
+                                               Console.WriteLine("  [Error]" + e.TaskMessage);\r
+                                               break;\r
+                                       default:\r
+                                               Console.WriteLine(e.TaskMessage);\r
+                                               break;\r
+                               }\r
+                       }\r
+               }\r
+       }\r
+       \r
+       \r
        class MainClass\r
        {\r
                private Downloader downloader;\r
@@ -97,58 +156,8 @@ namespace AllGet
                public void update(bool downloadPackageListFlag)\r
                {\r
                        NaGet.SubCommands.NaGetUpdate2 tasks = new NaGet.SubCommands.NaGetUpdate2(pkgListMan, downloadPackageListFlag);\r
-                       tasks.TaskEventRaised += delegate(object sender, TaskEventArgs e) { \r
-                               switch (e.Type) {\r
-//                                     case NaGetTaskSetEventType.COMPLETED_TASKSET\r
-//                                             break;\r
-                                       case TaskEventType.COMPLETED:\r
-                                               Console.WriteLine("Done.");\r
-                                               break;\r
-                                       case TaskEventType.STARTED_SUBTASK:\r
-                                               Console.Write("  " + e.TaskMessage);\r
-                                               break;\r
-                                       case TaskEventType.COMPLETED_SUBTASK:\r
-                                               Console.WriteLine(" ... Done. [{0}%]", (int) e.ProgressPercent);\r
-                                               break;\r
-                                       case TaskEventType.INFO:\r
-                                               Console.WriteLine("  " + e.TaskMessage);\r
-                                               break;\r
-                                       case TaskEventType.ERROR:\r
-                                       case TaskEventType.WARNING:\r
-                                               Console.WriteLine("  [Error]" + e.TaskMessage);\r
-                                               break;\r
-                               }\r
-                       };\r
-                       tasks.SubTaskEventRaised += delegate (object sender, TaskEventArgs e) {\r
-                               if (sender is NaGet.SubCommands.SubTask.DownloadSubTask) {\r
-                                       if (e.Type == TaskEventType.STARTED) {\r
-                                               Console.WriteLine();\r
-                                       }\r
-                                       \r
-                                       int origPosX = Console.CursorLeft;\r
-                                       for (int i = 0; i < Console.WindowWidth - 1; i++) {\r
-                                               Console.Write(' ');\r
-                                       }\r
-                                       Console.CursorLeft = origPosX;\r
-                                       \r
-                                       switch (e.Type) {\r
-                                               case TaskEventType.PING:\r
-                                                       {\r
-                                                               int origPos = Console.CursorLeft;\r
-                                                               Console.Write(e.TaskMessage);\r
-                                                               Console.CursorLeft = origPos;\r
-                                                       }\r
-                                                       break;\r
-                                               case TaskEventType.WARNING:\r
-                                               case TaskEventType.ERROR:\r
-                                                       Console.WriteLine("  [Error]" + e.TaskMessage);\r
-                                                       break;\r
-                                               default:\r
-                                                       Console.WriteLine(e.TaskMessage);\r
-                                                       break;\r
-                                       }\r
-                               }\r
-                       };\r
+                       tasks.TaskEventRaised += TaskSetEventHandlers.OnTaskSetEvent;\r
+                       tasks.SubTaskEventRaised += TaskSetEventHandlers.OnDownloadSubTaskEvent;\r
                        \r
                        tasks.Run();\r
                }\r
@@ -407,16 +416,14 @@ namespace AllGet
                        PackageList<InstalledPackage> installedPkgList = new PackageList<InstalledPackage>();\r
                        installedPkgList.AddPackages(pkgListMan.GetAllInstalledPackages());\r
                        \r
-                       Uninstallation[] uninstallations = new Uninstallation[packagenames.Length];\r
+                       InstalledPackage[] insPkgs = new InstalledPackage[packagenames.Length];\r
                        for (int i = 0; i < packagenames.Length; i++) {\r
-                               InstalledPackage pkg = installedPkgList.GetPackageForName(packagenames[i]);\r
+                               insPkgs[i] = installedPkgList.GetPackageForName(packagenames[i]);\r
                                \r
-                               if (pkg == null) {\r
+                               if (insPkgs[i] == null) {\r
                                        Console.WriteLine("E: could not found package " + packagenames[i]);\r
                                        Environment.Exit(1);\r
                                }\r
-                               \r
-                               uninstallations[i] = new Uninstallation(pkg);\r
                        }\r
                        \r
 //                     if (AllGet.Util.Confirm("Do you want to continue [Y/n]?", true) == false) {\r
@@ -424,29 +431,8 @@ namespace AllGet
 //                             Environment.Exit(0);\r
 //                     }\r
                        \r
-                       NaGet.SubCommands.NaGetUninstall tasks = new NaGet.SubCommands.NaGetUninstall(pkgListMan, uninstallations);\r
-                       tasks.TaskEventRaised += delegate(object sender, TaskEventArgs e) { \r
-                               switch (e.Type) {\r
-//                                     case NaGetTaskSetEventType.COMPLETED_TASKSET\r
-//                                             break;\r
-                                       case TaskEventType.COMPLETED:\r
-                                               Console.WriteLine("Done.");\r
-                                               break;\r
-                                       case TaskEventType.STARTED_SUBTASK:\r
-                                               Console.Write("  " + e.TaskMessage);\r
-                                               break;\r
-                                       case TaskEventType.COMPLETED_SUBTASK:\r
-                                               Console.WriteLine(" ... Done. [{0}%]", (int) e.ProgressPercent);\r
-                                               break;\r
-                                       case TaskEventType.INFO:\r
-                                               Console.WriteLine("  " + e.TaskMessage);\r
-                                               break;\r
-                                       case TaskEventType.ERROR:\r
-                                       case TaskEventType.WARNING:\r
-                                               Console.WriteLine("  [Error] " + e.TaskMessage);\r
-                                               break;\r
-                               }\r
-                       };\r
+                       NaGet.SubCommands.NaGetUninstall2 tasks = new NaGet.SubCommands.NaGetUninstall2(pkgListMan, insPkgs);\r
+                       tasks.TaskEventRaised += TaskSetEventHandlers.OnTaskSetEvent;\r
                        \r
                        tasks.Run();\r
                }\r
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));
+                       }
+               }
+       }
+}
index fd356a1..6ed8621 100644 (file)
@@ -77,6 +77,7 @@
     <Compile Include="NaGet.SubCommands\NaGetTaskSet.cs" />\r
     <Compile Include="NaGet.SubCommands\NaGetTaskSet2.cs" />\r
     <Compile Include="NaGet.SubCommands\NaGetUninstall.cs" />\r
+    <Compile Include="NaGet.SubCommands\NaGetUninstall2.cs" />\r
     <Compile Include="NaGet.SubCommands\NaGetUpdate2.cs" />\r
     <Compile Include="NaGet.Tasks\Task.cs" />\r
     <Compile Include="NaGet.Tasks\TaskCanceledException.cs" />\r