OSDN Git Service

na-get,インストール時AppliStation,ハッシュが非整合の時にダウンロード処理から「再試行」をすることをできるようにした
[applistation/AppliStation.git] / na-get-lib / NaGet.SubCommands / NaGetInstall.cs
1 using System;\r
2 using NaGet.Packages.Install;\r
3 using NaGet.Packages;\r
4 using NaGet.Net;\r
5 \r
6 namespace NaGet.SubCommands\r
7 {\r
8         public class NaGetInstall : NaGetTaskSet\r
9         {\r
10                 private bool done = false;\r
11                 \r
12                 private int currentTaskSetIndex = -1;\r
13                 \r
14                 private PackageListsManager pkgListMan;\r
15                 \r
16                 public Downloader Downloader; // TODO \8eQ\8fÆ\82ð\82µ\82Ä\83Z\83b\83g\82·\82é\82æ\82¤\82É\r
17                 \r
18                 private bool packageInstallerDownloaded = false;\r
19                 \r
20                 public override bool Cancelable {\r
21                         get { return ! done; }\r
22                 }\r
23                 \r
24                 /// <summary>\r
25                 /// \83C\83\93\83X\83g\81[\83\8b\82·\82é\83p\83b\83P\81[\83W\r
26                 /// </summary>\r
27                 public Installation[] Installations;\r
28                 \r
29                 /// <summary>\r
30                 /// \83R\83\93\83X\83g\83\89\83N\83^\r
31                 /// </summary>\r
32                 /// <param name="pkgs">\83C\83\93\83X\83g\81[\83\8b\82·\82é\83p\83b\83P\81[\83W</param>\r
33                 public NaGetInstall(PackageListsManager pkgListMan, Package[] pkgs)\r
34                         : this(pkgListMan, Installation.ConvertInstallations(pkgs))\r
35                 {\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="installations">\83C\83\93\83X\83g\81[\83\8b\8f\88\97\9d\82Ì\94z\97ñ</param>\r
42                 public NaGetInstall(PackageListsManager pkgMan, Installation[] installations)\r
43                 {\r
44                         pkgListMan = pkgMan;\r
45                         \r
46                         Installations = installations;\r
47                         initializeMainTaskSetNames();\r
48                 }\r
49                 \r
50                 private void initializeMainTaskSetNames()\r
51                 {\r
52                         System.Collections.Generic.List<string> taskSetNames = new System.Collections.Generic.List<string>();\r
53                         \r
54                         for (int i =0; i < Installations.Length; i++) {\r
55                                 taskSetNames.Add(string.Format("\8eæ\93¾: {0}", Installations[i].ToString()));\r
56                         }\r
57                         taskSetNames.Add("\83C\83\93\83X\83g\81[\83\89\82Ì\8c\9f\8fØ");\r
58                         for (int i =0; i < Installations.Length; i++) {\r
59                                 taskSetNames.Add(string.Format("\83C\83\93\83X\83g\81[\83\8b: {0}", Installations[i].ToString()));\r
60                         }\r
61                         taskSetNames.Add(string.Format("\83\8a\83X\83g\8dX\90V: {0}", NaGet.Env.ArchiveInstalledPackageListFile));\r
62                         taskSetNames.Add(string.Format("\83\8a\83X\83g\8dX\90V: {0}", NaGet.Env.SystemInstalledPackageListFile));\r
63                         \r
64                         TaskSetNames = taskSetNames.ToArray();\r
65                 }\r
66                                 \r
67                 public override void Run()\r
68                 {\r
69                         currentTaskSetIndex = 0;\r
70                         RaiseTaskSetEvent(NaGetTaskSetEventType.STARTED, "\83C\83\93\83X\83g\81[\83\8b\8f\88\97\9d\8aJ\8en");\r
71                         \r
72                         {\r
73                                 // \83n\83b\83V\83\85\94ñ\93K\8d\87\82È\83C\83\93\83X\83g\81[\83\89\82Ì\95\\r
74                                 System.Collections.Generic.List<Installation> invalidInstallers = null;\r
75                                 \r
76                                 while (invalidInstallers == null || invalidInstallers.Count > 0) {\r
77                                         currentTaskSetIndex = 0;\r
78                                         packageInstallerDownloaded = false;\r
79                                         \r
80                                         runDownloadInstallers();\r
81                                         if (done) return; // \82à\82µrunDownloadInstallers()\93à\82Å\83G\83\89\81[\8fI\97¹\82µ\82Ä\82¢\82½\82È\82ç\8fI\97¹\r
82                                         \r
83                                         packageInstallerDownloaded = true;\r
84                                         \r
85                                         // \83n\83b\83V\83\85\82Ì\89ó\82ê\82Ä\82¢\82é\83C\83\93\83X\83g\81[\83\89\82ð\8eæ\93¾\r
86                                         invalidInstallers = runCheckHashForInstaller();\r
87                                         \r
88                                         // \83n\83b\83V\83\85\82ª\89ó\82ê\82Ä\82¢\82é\82Æ\82«\82Ì\91Î\8dô\r
89                                         if (invalidInstallers.Count >= 0) {\r
90                                                 string msg = string.Format("{0}\8cÂ\82Ì\83p\83b\83P\81[\83W\82Å\83t\83@\83C\83\8b\82ª\89ó\82ê\82Ä\82¢\82é\89Â\94\\90«\82ª\82 \82è\82Ü\82·\n\8b­\90§\93I\82É\83C\83\93\83X\83g\81[\83\8b\82ð\91±\8ds\82µ\82Ü\82·\82©?",\r
91                                                                            invalidInstallers.Count);\r
92                                                 NaGetTaskQueryResult result = RaiseTaskSetQueryEvent(msg, NaGetTaskQueryResult.CONTINUE\r
93                                                                                                      | NaGetTaskQueryResult.RETRY\r
94                                                                                                      | NaGetTaskQueryResult.CANCEL);\r
95                                                 \r
96                                                 switch (result) {\r
97                                                         case NaGetTaskQueryResult.CONTINUE:\r
98                                                                 RaiseTaskSetEvent(NaGetTaskSetEventType.WARNING, "\83n\83b\83V\83\85\82Ì\94ñ\90®\8d\87\82ð\96³\8e\8b\82µ\82Ä\83C\83\93\83X\83g\81[\83\8b\82ð\8cp\91±");\r
99                                                                 invalidInstallers.Clear(); // \83n\83b\83V\83\85\94ñ\93K\8d\87\83p\83b\83P\81[\83W\82ð\8b­\90§\93I\82É\96\95\8fÁ\r
100                                                                 break;\r
101                                                         case NaGetTaskQueryResult.RETRY:\r
102                                                                 RaiseTaskSetEvent(NaGetTaskSetEventType.INFO, "\83_\83E\83\93\83\8d\81[\83h\8f\88\97\9d\82ð\8dÄ\8e\8e\8ds");\r
103                                                                 \r
104                                                                 foreach (Installation invalidInst in invalidInstallers) {\r
105                                                                         invalidInst.RemoveDownloadedFile();\r
106                                                                 }\r
107                                                                 \r
108                                                                 break;\r
109                                                         //case NaGetTaskQueryResult.CANCEL:\r
110                                                         default:\r
111                                                                 RaiseTaskSetEvent(NaGetTaskSetEventType.CANCELED, "\83p\83b\83P\81[\83W\82Ì\83C\83\93\83X\83g\81[\83\8b\8f\88\97\9d\82ª\83L\83\83\83\93\83Z\83\8b\82³\82ê\82Ü\82µ\82½");\r
112                                                                 done = true;\r
113                                                                 return;\r
114                                                 }\r
115                                         }\r
116                                         currentTaskSetIndex ++;\r
117                                 }\r
118                         }\r
119                         \r
120                         foreach (Installation inst in Installations) {\r
121                                 RaiseTaskSetEvent(NaGetTaskSetEventType.STARTED_TASKSET, inst.ToString());\r
122                                 \r
123                                 try {\r
124                                         inst.ErrorDataReceived += this.ReceivedErrorData;\r
125                                         inst.OutputDataReceived += this.ReceivedOutputData;\r
126                                         int exitCode = inst.Install();\r
127                                         if (exitCode != 0) {\r
128                                                 RaiseTaskSetEvent(NaGetTaskSetEventType.WARNING, "\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\83C\83\93\83X\83g\81[\83\89\82Ì\8fI\97¹\83R\81[\83h:"+exitCode);\r
129                                         }\r
130                                         \r
131                                         pkgListMan.WriteInstallationLog(inst);\r
132                                 } catch (Exception e) {\r
133                                         RaiseTaskSetEvent(NaGetTaskSetEventType.ERROR, e.Message);\r
134                                         done = true;\r
135                                         return;\r
136                                 }\r
137                                 currentTaskSetIndex ++;\r
138                                 \r
139                                 RaiseTaskSetEvent(NaGetTaskSetEventType.COMPLETED_TASKSET, inst.ToString());\r
140                                 \r
141                                 if (cancelCalled) {\r
142                                         RaiseTaskSetEvent(NaGetTaskSetEventType.CANCELED, "\83p\83b\83P\81[\83W\82Ì\83C\83\93\83X\83g\81[\83\8b\8f\88\97\9d\82ª\83L\83\83\83\93\83Z\83\8b\82³\82ê\82Ü\82µ\82½");\r
143                                         done = true;\r
144                                         return;\r
145                                 }\r
146                         }\r
147                         pkgListMan.SaveSystemInstalledLogList(); // \83\8d\83O\82Ì\83R\83~\83b\83g\r
148                         \r
149                         runLocalUpdate();\r
150                         \r
151                         done = true;\r
152                         \r
153                         RaiseTaskSetEvent(NaGetTaskSetEventType.COMPLETED, "\8fI\97¹", 100);\r
154                 }\r
155                 \r
156                 \r
157                 /// <summary>\r
158                 /// \8f\88\97\9d\93à\97e\82Ì\83_\83E\83\93\83\8d\81[\83h\95\94\95ª\82Ì\83T\83u\83\8b\81[\83`\83\93\r
159                 /// </summary>\r
160                 private void runDownloadInstallers()\r
161                 {\r
162                         foreach (Installation inst in Installations) {\r
163                                 if (! inst.IsInstallablePackage()) {\r
164                                         string msg = string.Format("{0}\82Í\83C\83\93\83X\83g\81[\83\8b\82·\82é\82±\82Æ\82ª\82Å\82«\82Ü\82¹\82ñ", inst.ToString());\r
165                                         \r
166                                         RaiseTaskSetEvent(NaGetTaskSetEventType.ERROR, msg);\r
167                                         done = true;\r
168                                         return;\r
169                                 }\r
170                                 \r
171                                 RaiseTaskSetEvent(NaGetTaskSetEventType.STARTED_TASKSET, inst.ToString());\r
172                                 \r
173                                 if (! inst.Downloaded) {\r
174                                         try {\r
175                                                 inst.Download(Downloader);\r
176                                         } catch (NaGetTaskCanceledException) {\r
177                                                 RaiseTaskSetEvent(NaGetTaskSetEventType.CANCELED, "\83C\83\93\83X\83g\81[\83\89\82Ì\83_\83E\83\93\83\8d\81[\83h\8f\88\97\9d\82ª\83L\83\83\83\93\83Z\83\8b\82³\82ê\82Ü\82µ\82½");\r
178                                                 done = true;\r
179                                                 return;\r
180                                         } catch (System.Net.WebException e) {\r
181                                                 RaiseTaskSetEvent(NaGetTaskSetEventType.WARNING, e.Message);\r
182                                                 if (System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable()) {\r
183                                                         RaiseTaskSetEvent(NaGetTaskSetEventType.ERROR, "\83l\83b\83g\83\8f\81[\83N\82É\90Ú\91±\82³\82ê\82Ä\82¢\82Ü\82¹\82ñ\81B");\r
184                                                 } else {\r
185                                                         RaiseTaskSetEvent(NaGetTaskSetEventType.ERROR, "\83l\83b\83g\83\8f\81[\83N\82É\90Ú\91±\82Å\82«\82Ü\82¹\82ñ\82Å\82µ\82½\81B\83l\83b\83g\83\8f\81[\83N\82ª\90Ø\92f\82³\82ê\82Ä\82¢\82é\82©\81A\83t\83@\83C\83A\83E\83H\81[\83\8b\82É\82æ\82Á\82Ä\8eÕ\92f\82³\82ê\82½\89Â\94\\90«\82ª\82 \82è\82Ü\82·\81B");\r
186                                                 }\r
187                                                 done = true;\r
188                                                 return;\r
189                                         } catch (Exception e) {\r
190                                                 RaiseTaskSetEvent(NaGetTaskSetEventType.ERROR, e.Message);\r
191                                                 done = true;\r
192                                                 return;\r
193                                         }\r
194                                 }\r
195                                 currentTaskSetIndex ++;\r
196                                 \r
197                                 if (inst.Downloaded) { // \90³\8fí\8fI\97¹\r
198                                         RaiseTaskSetEvent(NaGetTaskSetEventType.COMPLETED_TASKSET, inst.ToString());\r
199                                 } else { // \83C\83\93\83X\83g\81[\83\8b\82ª\8a®\97¹\82¹\82¸\82É\8fI\82í\82Á\82½=\8e¸\94s=\83G\83\89\81[\r
200                                         RaiseTaskSetEvent(NaGetTaskSetEventType.ERROR, string.Format("{0}\82Ì\83C\83\93\83X\83g\81[\83\89\82ð\90³\8fí\82É\83_\83E\83\93\83\8d\81[\83h\82Å\82«\82Ü\82¹\82ñ\82Å\82µ\82½", inst.ToString()));\r
201                                 }\r
202                         }\r
203                 }\r
204                 \r
205                 /// <summary>\r
206                 /// \83_\83E\83\93\83\8d\81[\83h\82µ\82½\83p\83b\83P\81[\83W\82ª\90®\8d\87\82µ\82½\82©\94Û\82©\83n\83b\83V\83\85\82Å\83`\83F\83b\83N\r
207                 /// </summary>\r
208                 /// <returns>\90®\8d\87\82µ\82È\82©\82Á\82½\83C\83\93\83X\83g\81[\83\89\82Ì\83\8a\83X\83g</returns>\r
209                 private System.Collections.Generic.List<Installation> runCheckHashForInstaller()\r
210                 {\r
211                         System.Collections.Generic.List<Installation> invalidInstallers = new System.Collections.Generic.List<Installation>();\r
212                         \r
213                         int i = 0;\r
214                         foreach (Installation inst in Installations) {\r
215                                 float percent = (CurrentTaskSetIndex+((float)i / Installations.Length))*100f/TaskSetNames.Length;\r
216                                 \r
217                                 if (inst.GetRegisteredHashCount() > 0) {\r
218                                         RaiseTaskSetEvent(NaGetTaskSetEventType.INFO, "\8c\9f\8fØ: "+inst.ToString(), percent);\r
219                                         \r
220                                         if (inst.IsInstallablePackage() && inst.VerifyHashValues() == false) {\r
221                                                 invalidInstallers.Add(inst);\r
222                                                 RaiseTaskSetEvent(NaGetTaskSetEventType.WARNING, "\8c\9f\8fØ: "+inst.ToString() + " \94ñ\90®\8d\87", percent);\r
223                                         } else {\r
224                                                 RaiseTaskSetEvent(NaGetTaskSetEventType.INFO, "\8c\9f\8fØ: "+inst.ToString() + " OK", percent);\r
225                                         }\r
226                                 }\r
227                                 i++;\r
228                         }\r
229                         \r
230                         return invalidInstallers;\r
231                 }\r
232                 \r
233                 private void runLocalUpdate()\r
234                 {\r
235                         // \83C\83\93\83X\83g\81[\83\8b\83g\83\8a\83X\83g\82Ì\8dX\90V\r
236                         RaiseTaskSetEvent(NaGetTaskSetEventType.STARTED_TASKSET, TaskSetNames[currentTaskSetIndex]);\r
237                         pkgListMan.DetectInstalledPkgs();\r
238                         pkgListMan.SaveInstalledPackageList();\r
239                         currentTaskSetIndex++;\r
240                         RaiseTaskSetEvent(NaGetTaskSetEventType.COMPLETED_TASKSET, TaskSetNames[currentTaskSetIndex-1]);\r
241                 \r
242                         // \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
243                         RaiseTaskSetEvent(NaGetTaskSetEventType.STARTED_TASKSET, TaskSetNames[currentTaskSetIndex]);\r
244                         pkgListMan.DetectSystemInstalledPkgs();\r
245                         pkgListMan.SaveSystemInstalledPackageList();\r
246                         currentTaskSetIndex++;\r
247                         RaiseTaskSetEvent(NaGetTaskSetEventType.COMPLETED_TASKSET, TaskSetNames[currentTaskSetIndex-1]);\r
248                 }\r
249                 \r
250                 public override bool Done {\r
251                         get { return done; }\r
252                 }\r
253                 \r
254                 public override int CurrentTaskSetIndex {\r
255                         get { return currentTaskSetIndex; }\r
256                 }\r
257                 \r
258                 private bool cancelCalled = false;\r
259                 \r
260                 public override bool Cancel()\r
261                 {\r
262                         cancelCalled = true;\r
263                         if (! packageInstallerDownloaded) {\r
264                                 return Downloader.Cancel();\r
265                         } else return true;\r
266                 }\r
267         }\r
268 }\r