OSDN Git Service

na-get-lib,アンインストール処理の中途終了に対応。
[applistation/AppliStation.git] / na-get-lib / NaGet.SubCommands / NaGetUninstall2.cs
1 using System;
2 using System.Collections.Generic;
3 using NaGet.Packages;
4 using NaGet.Packages.Install;
5 using NaGet.SubCommands;
6 using NaGet.SubCommands.SubTask;
7 using NaGet.Tasks;
8
9 namespace NaGet.SubCommands
10 {
11         /// <summary>
12         /// ソフトのアンインストール処理
13         /// </summary>
14         public class NaGetUninstall2 : NaGetTaskSet2
15         {
16                 
17                 private IList<Uninstallation> uninsts;
18                 
19                 public NaGetUninstall2(PackageListsManager pkgMan, IList<InstalledPackage> pkgs)
20                 {
21                         uninsts = new List<Uninstallation>(pkgs.Count);
22                         foreach (InstalledPackage instPkg in pkgs) {
23                                 uninsts.Add(new Uninstallation(instPkg));
24                         }
25                         
26                         // taskセットの初期化
27                         initSubTask();
28                         foreach (Uninstallation uninst in uninsts) {
29                                 registSubTask(string.Format("アンインストール: {0}", uninst.ToString()),
30                                               new FunctionalSubTask(runUninstall, uninst));
31                         }
32                         registSubTask("インストール済みのソフトリスト更新",
33                                       new LocalUpdateSubTask(pkgMan));
34                 }
35                 
36                 public override void Run()
37                 {
38                         NotifyStarted();
39                         RaiseTaskSetEvent(TaskEventType.STARTED, string.Empty);
40                         
41                         try {
42                                 while (hasMoreSubTask) {
43                                         RaiseTaskSetEvent(TaskEventType.STARTED_SUBTASK, currentSubTaskName);
44                                         currentSubTask.Run();
45                                         RaiseTaskSetEvent(TaskEventType.COMPLETED_SUBTASK, currentSubTaskName);
46                                         
47                                         if (cancelCalled) {
48                                                 throw new TaskCanceledException("cancel is called");
49                                         }
50                                         
51                                         NotifyGoToNextSubTask();
52                                 }
53                         } catch (TaskCanceledException) {
54                                 cancelCalled = true;
55                         } catch (Exception e) {
56                                 RaiseTaskSetEvent(TaskEventType.ERROR, e.Message);
57                         }
58                         
59                         if (cancelCalled) {
60                                 NotifyCancelled();
61                                 RaiseTaskSetEvent(TaskEventType.CANCELED, "キャンセルされました");
62                         } else {
63                                 NotifyCompleted();
64                                 RaiseTaskSetEvent(TaskEventType.COMPLETED, string.Empty);
65                         }
66                 }
67                 
68                 private void runUninstall(object uninstObj)
69                 {
70                         Uninstallation uninst = (Uninstallation) uninstObj;
71                         
72                         if (uninst.Installed) {
73                                 try {
74                                         uninst.OutputDataReceived += this.ReceivedOutputData;
75                                         uninst.ErrorDataReceived += this.ReceivedErrorData;
76                                         int exitCode = uninst.Uninstall();
77                                         if (exitCode != 0) {
78                                                 RaiseTaskSetEvent(TaskEventType.WARNING, "アンインストールが正常に終えていない可能性があります。プロセスの終了コード:"+exitCode);
79                                         }
80                                 } catch (Exception e) {
81                                         RaiseTaskSetEvent(TaskEventType.ERROR, e.Message);
82                                         throw e;
83                                 }
84                         } else {
85                                 RaiseTaskSetEvent(TaskEventType.WARNING, string.Format("{0}は既にアンインストールされているか、ソフトの存在を確認できませんでした", uninst));
86                         }
87                 }
88                 
89                 public override bool Cancelable {
90                         get {
91                                 return !cancelCalled && Running && isDuringUninstalling;
92                         }
93                 }
94                 
95                 private bool isDuringUninstalling {
96                         get {
97                                 return currentSubTask is LocalUpdateSubTask;
98                         }
99                 }
100         }
101 }