OSDN Git Service

na-get-lib,タスクセットの改修(NaGetTaskSet2)および、そのUnitTestを追加
[applistation/AppliStation.git] / na-get-lib / NaGet.SubCommands / NaGetUninstall.cs
1 using System;\r
2 using NaGet.Packages.Install;\r
3 using NaGet.Packages;\r
4 using NaGet.Net;\r
5 using NaGet.Tasks;\r
6 \r
7 namespace NaGet.SubCommands\r
8 {\r
9         public class NaGetUninstall : NaGetTaskSet\r
10         {\r
11                 private bool done = false;\r
12                 \r
13                 private int currentTaskSetIndex = -1;\r
14                 \r
15                 private PackageListsManager pkgListMan;\r
16                 \r
17                 /// <summary>\r
18                 /// \83A\83\93\83C\83\93\83X\83g\81[\83\8b\82·\82é\83p\83b\83P\81[\83W\r
19                 /// </summary>\r
20                 public Uninstallation[] Uninstallations;\r
21                 \r
22                 /// <summary>\r
23                 /// \83R\83\93\83X\83g\83\89\83N\83^\r
24                 /// </summary>\r
25                 /// <param name="pkgs">\83A\83\93\83C\83\93\83X\83g\81[\83\8b\82·\82é\83p\83b\83P\81[\83W</param>\r
26                 public NaGetUninstall(PackageListsManager pkgMan, InstalledPackage[] pkgs)\r
27                 {\r
28                         pkgListMan = pkgMan;\r
29                         \r
30                         Uninstallations = new Uninstallation[pkgs.Length];\r
31                         for (int i = 0; i < pkgs.Length; i++) {\r
32                                 Uninstallations[i] = new Uninstallation(pkgs[i]);\r
33                         }\r
34                         \r
35                         initializeMainTaskSetNames();\r
36                 }\r
37                 \r
38                 /// <summary>\r
39                 /// \83R\83\93\83X\83g\83\89\83N\83^\r
40                 /// </summary>\r
41                 /// <param name="uninstallations">\83A\83\93\83C\83\93\83X\83g\81[\83\8b\8f\88\97\9d\82Ì\94z\97ñ</param>\r
42                 public NaGetUninstall(PackageListsManager pkgMan, Uninstallation[] uninstallations)\r
43                 {\r
44                         pkgListMan = pkgMan;\r
45                         \r
46                         Uninstallations = uninstallations;\r
47                         initializeMainTaskSetNames();\r
48                 }\r
49                 \r
50                 private void initializeMainTaskSetNames()\r
51                 {\r
52                         taskSetNames = new System.Collections.Generic.List<string>();\r
53                         \r
54                         for (int i =0; i < Uninstallations.Length; i++) {\r
55                                 taskSetNames.Add(string.Format("\83A\83\93\83C\83\93\83X\83g\81[\83\8b: {0}", Uninstallations[i].ToString()));\r
56                         }\r
57                         taskSetNames.Add(string.Format("\83\8a\83X\83g\8dX\90V: {0}", NaGet.Env.ArchiveInstalledPackageListFile));\r
58                         taskSetNames.Add(string.Format("\83\8a\83X\83g\8dX\90V: {0}", NaGet.Env.SystemInstalledPackageListFile));\r
59                 }\r
60                 \r
61                 public override void Run()\r
62                 {\r
63                         currentTaskSetIndex ++;\r
64                         RaiseTaskSetEvent(TaskEventType.STARTED, "\83A\83\93\83C\83\93\83X\83g\81[\83\8b\8f\88\97\9d\8aJ\8en");\r
65                         \r
66                         foreach (Uninstallation uninst in Uninstallations) {\r
67                                 RaiseTaskSetEvent(TaskEventType.STARTED_SUBTASK, uninst.ToString());\r
68                                 \r
69                                 if (uninst.Installed) {\r
70                                         try {\r
71                                                 uninst.OutputDataReceived += this.ReceivedOutputData;\r
72                                                 uninst.ErrorDataReceived += this.ReceivedErrorData;\r
73                                                 int exitCode = uninst.Uninstall();\r
74                                                 if (exitCode != 0) {\r
75                                                         RaiseTaskSetEvent(TaskEventType.WARNING, "\83A\83\93\83C\83\93\83X\83g\81[\83\8b\82ª\90³\8fí\82É\8fI\82¦\82Ä\82¢\82È\82¢\89Â\94\\90«\82ª\82 \82è\82Ü\82·\81B\83v\83\8d\83Z\83X\82Ì\8fI\97¹\83R\81[\83h:"+exitCode);\r
76                                                 }\r
77                                         } catch (Exception e) {\r
78                                                 RaiseTaskSetEvent(TaskEventType.ERROR, e.Message);\r
79                                                 done = true;\r
80                                                 return;\r
81                                         }\r
82                                 } else {\r
83                                         RaiseTaskSetEvent(TaskEventType.WARNING, string.Format("{0}\82Í\8aù\82É\83A\83\93\83C\83\93\83X\83g\81[\83\8b\82³\82ê\82Ä\82¢\82é\82©\81A\83\\83t\83g\82Ì\91\8dÝ\82ð\8am\94F\82Å\82«\82Ü\82¹\82ñ\82Å\82µ\82½", uninst));\r
84                                 }\r
85                                 \r
86                                 RaiseTaskSetEvent(TaskEventType.COMPLETED_SUBTASK, uninst.ToString());\r
87                         }\r
88                         \r
89                         runLocalUpdate();\r
90                         \r
91                         done = true;\r
92                         \r
93                         RaiseTaskSetEvent(TaskEventType.COMPLETED, "\8fI\97¹", 100);\r
94                 }\r
95                 \r
96                 private void runLocalUpdate()\r
97                 {\r
98                         // \83C\83\93\83X\83g\81[\83\8b\83g\83\8a\83X\83g\82Ì\8dX\90V\r
99                         RaiseTaskSetEvent(TaskEventType.STARTED_SUBTASK, taskSetNames[currentTaskSetIndex]);\r
100                         pkgListMan.DetectInstalledPkgs();\r
101                         pkgListMan.SaveInstalledPackageList();\r
102                         RaiseTaskSetEvent(TaskEventType.COMPLETED_SUBTASK, taskSetNames[currentTaskSetIndex]);\r
103                         currentTaskSetIndex++;\r
104                 \r
105                         // \83V\83X\83e\83\80\82É\83C\83\93\83X\83g\81[\83\8b\82³\82ê\82Ä\82¢\82é\83\8a\83X\83g\82Ì\8dX\90V\r
106                         RaiseTaskSetEvent(TaskEventType.STARTED_SUBTASK, taskSetNames[currentTaskSetIndex]);\r
107                         pkgListMan.DetectSystemInstalledPkgs();\r
108                         pkgListMan.SaveSystemInstalledPackageList();\r
109                         RaiseTaskSetEvent(TaskEventType.COMPLETED_SUBTASK, taskSetNames[currentTaskSetIndex]);\r
110                         currentTaskSetIndex++;\r
111                 }\r
112                 \r
113                 public override bool Done {\r
114                         get { return done; }\r
115                 }\r
116                                 \r
117                 public override int CurrentTaskSetIndex {\r
118                         get { return currentTaskSetIndex; }\r
119                 }\r
120         }\r
121 }\r