OSDN Git Service

na-get-lib,チケット #36130 に関連して、更新パッケージから非対応パッケージを除外するようにした
[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<Uninstallation>(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(Uninstallation uninst)
69                 {
70                         if (uninst.Installed) {
71                                 try {
72                                         uninst.OutputDataReceived += this.ReceivedOutputData;
73                                         uninst.ErrorDataReceived += this.ReceivedErrorData;
74                                         int exitCode = uninst.Uninstall();
75                                         if (exitCode != 0) {
76                                                 RaiseTaskSetEvent(TaskEventType.WARNING, "アンインストールが正常に終えていない可能性があります。プロセスの終了コード:"+exitCode);
77                                         }
78                                 } catch (Exception e) {
79                                         RaiseTaskSetEvent(TaskEventType.ERROR, e.Message);
80                                         throw e;
81                                 }
82                         } else {
83                                 RaiseTaskSetEvent(TaskEventType.WARNING, string.Format("{0}は既にアンインストールされているか、ソフトの存在を確認できませんでした", uninst));
84                         }
85                 }
86                 
87                 public override bool Cancelable {
88                         get {
89                                 return !cancelCalled && Running && isDuringUninstalling;
90                         }
91                 }
92                 
93                 private bool isDuringUninstalling {
94                         get {
95                                 return currentSubTask is LocalUpdateSubTask;
96                         }
97                 }
98         }
99 }