OSDN Git Service

AppliStation-All,「キャッシュにダウンロード」する機能を追加
[applistation/AppliStation.git] / na-get-lib / NaGet.SubCommands / NaGetInstall.cs
1 using System;\r
2 using System.Collections.Generic;\r
3 using NaGet.Packages.Install;\r
4 using NaGet.Packages;\r
5 using NaGet.Net;\r
6 \r
7 namespace NaGet.SubCommands\r
8 {\r
9         public class NaGetInstall : 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                 /// ダウンロードに使うダウンローダオブジェクト\r
19                 /// </summary>\r
20                 public Downloader Downloader {\r
21                         get {\r
22                                 if (downloader == null) {\r
23                                         downloader = new Downloader();\r
24                                         downloader.DownloadEventRaised += delegate(object sender, DownloadEventArgs e) {\r
25                                                 if (e.Type == DownloadEventType.DOWNLOADING && e.TaskProgressPercent > 0) {\r
26                                                         RaiseTaskSetEvent(NaGetTaskSetEventType.PING, string.Empty, GetProgressPercent(NaGetTaskSetEventType.PING, e.TaskProgressPercent));\r
27                                                 }\r
28                                         };\r
29                                 }\r
30                                 return downloader;\r
31                         }\r
32                 }\r
33                 \r
34                 private Downloader downloader;\r
35                 \r
36                 private bool packageInstallerDownloaded = false;\r
37                 \r
38                 public override bool Cancelable {\r
39                         get { return ! done; }\r
40                 }\r
41                 \r
42                 /// <summary>\r
43                 /// インストールするパッケージ\r
44                 /// </summary>\r
45                 public Installation[] Installations;\r
46                 \r
47                 /// <summary>\r
48                 /// コンストラクタ\r
49                 /// </summary>\r
50                 /// <param name="pkgs">インストールするパッケージ</param>\r
51                 public NaGetInstall(PackageListsManager pkgListMan, Package[] pkgs)\r
52                         : this(pkgListMan, Installation.ConvertInstallations(pkgs))\r
53                 {\r
54                 }\r
55                 \r
56                 /// <summary>\r
57                 /// コンストラクタ\r
58                 /// </summary>\r
59                 /// <param name="installations">インストール処理の配列</param>\r
60                 public NaGetInstall(PackageListsManager pkgMan, Installation[] installations)\r
61                 {\r
62                         pkgListMan = pkgMan;\r
63                         \r
64                         Installations = installations;\r
65                         initializeMainTaskSetNames();\r
66                 }\r
67                 \r
68                 private void initializeMainTaskSetNames()\r
69                 {\r
70                         List<string> taskSetNames = new List<string>();\r
71                         \r
72                         for (int i =0; i < Installations.Length; i++) {\r
73                                 taskSetNames.Add(string.Format("取得: {0}", Installations[i].ToString()));\r
74                                 taskSetNames.Add(string.Format("ウイルススキャン: {0}", Installations[i].ToString()));\r
75                         }\r
76                         taskSetNames.Add("インストーラーの検証");\r
77                         for (int i =0; i < Installations.Length; i++) {\r
78                                 taskSetNames.Add(string.Format("インストール: {0}", Installations[i].ToString()));\r
79                         }\r
80                         taskSetNames.Add(string.Format("リスト更新: {0}", NaGet.Env.ArchiveInstalledPackageListFile));\r
81                         taskSetNames.Add(string.Format("リスト更新: {0}", NaGet.Env.SystemInstalledPackageListFile));\r
82                         \r
83                         TaskSetNames = taskSetNames.ToArray();\r
84                 }\r
85                                 \r
86                 public override void Run()\r
87                 {\r
88                         currentTaskSetIndex = 0;\r
89                         RaiseTaskSetEvent(NaGetTaskSetEventType.STARTED, "インストール処理開始");\r
90                         \r
91                         {\r
92                                 // ハッシュ非適合なインストーラの表\r
93                                 List<Installation> invalidInstallers = null;\r
94                                 \r
95                                 do {\r
96                                         currentTaskSetIndex = 0;\r
97                                         packageInstallerDownloaded = false;\r
98                                         \r
99                                         runDownloadAndVirusCheckInstallers();\r
100                                         if (done) return; // もしrunDownloadInstallers()内でエラー終了していたなら終了\r
101                                         \r
102                                         packageInstallerDownloaded = true;\r
103                                         \r
104                                         \r
105                                         RaiseTaskSetEvent(NaGetTaskSetEventType.STARTED_TASKSET, TaskSetNames[currentTaskSetIndex]);\r
106                                         \r
107                                         // ハッシュの壊れているインストーラーを取得\r
108                                         invalidInstallers = runCheckHashForInstaller();\r
109                                         \r
110                                         // ハッシュが壊れているときの対策\r
111                                         if (invalidInstallers.Count > 0) {\r
112                                                 System.Text.StringBuilder invalidInstallerNames = new System.Text.StringBuilder();\r
113                                                 foreach (Installation invalidInst in invalidInstallers) {\r
114                                                         invalidInstallerNames.AppendFormat(" - {0}\n", invalidInst.ToString());\r
115                                                 }\r
116                                                 \r
117                                                 string msg = string.Format("以下の{0}個のパッケージでファイルが壊れている可能性があります。\n{1}\n強制的にインストールを続行しますか?",\r
118                                                                            invalidInstallers.Count, invalidInstallerNames.ToString());\r
119                                                 NaGetTaskQueryResult result = NaGetTaskQueryResult.CANCEL;\r
120                                                 \r
121                                                 if (!cancelCalled) {\r
122                                                         result = RaiseTaskSetQueryEvent(msg, NaGetTaskQueryResult.CONTINUE\r
123                                                                                         | NaGetTaskQueryResult.RETRY\r
124                                                                                         | NaGetTaskQueryResult.CANCEL);\r
125                                                 }\r
126                                                 \r
127                                                 switch (result) {\r
128                                                         case NaGetTaskQueryResult.CONTINUE:\r
129                                                                 RaiseTaskSetEvent(NaGetTaskSetEventType.WARNING, "ハッシュの非整合を無視してインストールを継続");\r
130                                                                 invalidInstallers.Clear(); // ハッシュ非適合パッケージを強制的に抹消\r
131                                                                 break;\r
132                                                         case NaGetTaskQueryResult.RETRY:\r
133                                                                 RaiseTaskSetEvent(NaGetTaskSetEventType.INFO, "ダウンロード処理を再試行");\r
134                                                                 \r
135                                                                 foreach (Installation invalidInst in invalidInstallers) {\r
136                                                                         invalidInst.RemoveDownloadedFile();\r
137                                                                 }\r
138                                                                 \r
139                                                                 break;\r
140                                                         //case NaGetTaskQueryResult.CANCEL:\r
141                                                         default:\r
142                                                                 RaiseTaskSetEvent(NaGetTaskSetEventType.CANCELED, "パッケージのインストール処理がキャンセルされました");\r
143                                                                 done = true;\r
144                                                                 return;\r
145                                                 }\r
146                                         }\r
147                                         \r
148                                         // もしハッシュが不適合なソフトがあるならばダウンロード処理からやり直す\r
149                                 } while (invalidInstallers == null || invalidInstallers.Count > 0);\r
150                                 RaiseTaskSetEvent(NaGetTaskSetEventType.COMPLETED_TASKSET, TaskSetNames[currentTaskSetIndex]);\r
151                                 currentTaskSetIndex ++;\r
152                         }\r
153                         \r
154                         foreach (Installation inst in Installations) {\r
155                                 string installTaskMsg = TaskSetNames[currentTaskSetIndex];\r
156                                 if (inst.Silent && (!inst.SupportsSilentOnly)) {\r
157                                         installTaskMsg += " (サイレントインストール)";\r
158                                 }\r
159                                 \r
160                                 RaiseTaskSetEvent(NaGetTaskSetEventType.STARTED_TASKSET, installTaskMsg);\r
161                                 \r
162                                 try {\r
163                                         \r
164                                         inst.ErrorDataReceived += this.ReceivedErrorData;\r
165                                         inst.OutputDataReceived += this.ReceivedOutputData;\r
166                                         int exitCode = inst.Install();\r
167                                         if (exitCode != 0) {\r
168                                                 RaiseTaskSetEvent(NaGetTaskSetEventType.WARNING, "インストールが正常に終えていない可能性があります。プロセスの終了コード:"+exitCode);\r
169                                         }\r
170                                         \r
171                                         pkgListMan.WriteInstallationLog(inst);\r
172                                 } catch (Exception e) {\r
173                                         RaiseTaskSetEvent(NaGetTaskSetEventType.ERROR, e.Message);\r
174                                         done = true;\r
175                                         return;\r
176                                 }\r
177                                 RaiseTaskSetEvent(NaGetTaskSetEventType.COMPLETED_TASKSET, installTaskMsg);\r
178                                 currentTaskSetIndex ++;\r
179                                 \r
180                                 if (cancelCalled) {\r
181                                         RaiseTaskSetEvent(NaGetTaskSetEventType.CANCELED, "パッケージのインストール処理がキャンセルされました");\r
182                                         done = true;\r
183                                         return;\r
184                                 }\r
185                         }\r
186                         \r
187                         runLocalUpdate();\r
188                         \r
189                         done = true;\r
190                         \r
191                         RaiseTaskSetEvent(NaGetTaskSetEventType.COMPLETED, "終了", 100);\r
192                 }\r
193                 \r
194                 \r
195                 /// <summary>\r
196                 /// 処理内容のダウンロード・ウイルススキャン部分のサブルーチン\r
197                 /// </summary>\r
198                 private void runDownloadAndVirusCheckInstallers()\r
199                 {\r
200                         using (DownloadScanner scanner = new DownloadScanner()) {\r
201                                 scanner.Init();\r
202                                 foreach (Installation inst in Installations) {\r
203                                         if (! inst.IsInstallablePackage()) {\r
204                                                 string msg = string.Format("{0}はインストールすることができません", inst.ToString());\r
205                                                 \r
206                                                 RaiseTaskSetEvent(NaGetTaskSetEventType.ERROR, msg);\r
207                                                 done = true;\r
208                                                 return;\r
209                                         }\r
210                                         \r
211                                         RaiseTaskSetEvent(NaGetTaskSetEventType.STARTED_TASKSET, TaskSetNames[currentTaskSetIndex]);\r
212                                         \r
213                                         if (! inst.Downloaded) {\r
214                                                 try {\r
215                                                         inst.Download(Downloader);\r
216                                                 } catch (NaGetTaskCanceledException) {\r
217                                                         RaiseTaskSetEvent(NaGetTaskSetEventType.CANCELED, "インストーラーのダウンロード処理がキャンセルされました");\r
218                                                         done = true;\r
219                                                         return;\r
220                                                 } catch (System.Net.WebException e) {\r
221                                                         RaiseTaskSetEvent(NaGetTaskSetEventType.WARNING, e.Message);\r
222                                                         if (System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable()) {\r
223                                                                 RaiseTaskSetEvent(NaGetTaskSetEventType.ERROR, "ネットワークに接続されていません。");\r
224                                                         } else {\r
225                                                                 RaiseTaskSetEvent(NaGetTaskSetEventType.ERROR, "ネットワークに接続できませんでした。ネットワークが切断されているか、ファイアウォールによって遮断された可能性があります。");\r
226                                                         }\r
227                                                         done = true;\r
228                                                         return;\r
229                                                 } catch (Exception e) {\r
230                                                         RaiseTaskSetEvent(NaGetTaskSetEventType.ERROR, e.Message);\r
231                                                         done = true;\r
232                                                         return;\r
233                                                 }\r
234                                         }\r
235                                         \r
236                                         if (! inst.Downloaded) { // ダウンロードが完了せずに終わった=失敗=エラー\r
237                                                 RaiseTaskSetEvent(NaGetTaskSetEventType.ERROR, string.Format("{0}のインストーラーファイルを正常にダウンロードできませんでした", inst.ToString()));\r
238                                                 done = true;\r
239                                                 return;\r
240                                         }\r
241                                         RaiseTaskSetEvent(NaGetTaskSetEventType.COMPLETED_TASKSET, TaskSetNames[currentTaskSetIndex]);\r
242                                         currentTaskSetIndex ++;\r
243                                         \r
244                                         RaiseTaskSetEvent(NaGetTaskSetEventType.STARTED_TASKSET, TaskSetNames[currentTaskSetIndex]);\r
245                                         if (! NaGet.Env.EnableScanInstallerFile) {\r
246                                                 RaiseTaskSetEvent(NaGetTaskSetEventType.INFO, "ウイルススキャンを行わない設定のため、ダウンロードしたファイルはウイルススキャンされませんでした");\r
247                                         } else if (!scanner.HasScanner) {\r
248                                                 RaiseTaskSetEvent(NaGetTaskSetEventType.INFO, "ダウンロードしたファイルはウイルススキャンされませんでした(ウイルススキャンソフトが検出できませんでした)");\r
249                                         } else {\r
250                                                 try {\r
251                                                         DownloadScannerResult result = inst.ScanInstallerFile(scanner);\r
252                                                         \r
253                                                         switch (result) {\r
254                                                                 case DownloadScannerResult.ScannerNotFound:\r
255                                                                         RaiseTaskSetEvent(NaGetTaskSetEventType.INFO, "ダウンロードしたファイルはウイルススキャンされませんでした(ウイルススキャンソフトが検出できませんでした)");\r
256                                                                         break;\r
257                                                                 case DownloadScannerResult.InfectedAndCleaned:\r
258                                                                         RaiseTaskSetEvent(NaGetTaskSetEventType.ERROR,\r
259                                                                                   "インストーラーファイルからウイルス感染が検出されたため、削除されました。");\r
260                                                                         done = true;\r
261                                                                         return;\r
262                                                                 case DownloadScannerResult.InfectedButNotCleaned:\r
263                                                                         RaiseTaskSetEvent(NaGetTaskSetEventType.ERROR,\r
264                                                                                   "インストーラーファイルからウイルス感染が検出されました。");\r
265                                                                         done = true;\r
266                                                                         break;\r
267                                                                 case DownloadScannerResult.ErrorNotFound:\r
268                                                                         throw new System.IO.FileNotFoundException(string.Empty);\r
269                                                                         //break;\r
270                                                         }\r
271                                                         \r
272                                                 } catch (System.Runtime.InteropServices.COMException ex) {\r
273                                                         RaiseTaskSetEvent(NaGetTaskSetEventType.WARNING,\r
274                                                                           string.Format("{0} (E{1})", ex.Message, ex.ErrorCode));\r
275                                                 } catch (System.IO.FileNotFoundException ex) {\r
276                                                         if (ex.InnerException is System.Runtime.InteropServices.COMException) {\r
277                                                                 RaiseTaskSetEvent(NaGetTaskSetEventType.WARNING,\r
278                                                                                   string.Format("{0} (E{1})", ex.InnerException.Message, ((System.Runtime.InteropServices.COMException) ex.InnerException).ErrorCode));\r
279                                                         }\r
280                                                         RaiseTaskSetEvent(NaGetTaskSetEventType.ERROR, "インストーラーファイルがウイルススキャナーによって削除されました。");\r
281                                                         done = true;\r
282                                                         return;\r
283                                                 }\r
284                                         }\r
285                                         RaiseTaskSetEvent(NaGetTaskSetEventType.COMPLETED_TASKSET, TaskSetNames[currentTaskSetIndex]);\r
286                                         currentTaskSetIndex ++;\r
287                                         \r
288                                         if (cancelCalled) {\r
289                                                 RaiseTaskSetEvent(NaGetTaskSetEventType.CANCELED, "パッケージのインストール処理がキャンセルされました");\r
290                                                 done = true;\r
291                                                 return;\r
292                                         }\r
293                                 }\r
294                         }\r
295                 }\r
296                 \r
297                 /// <summary>\r
298                 /// ダウンロードしたパッケージが整合したか否かハッシュでチェック\r
299                 /// </summary>\r
300                 /// <returns>整合しなかったインストーラーのリスト</returns>\r
301                 private List<Installation> runCheckHashForInstaller()\r
302                 {\r
303                         List<Installation> invalidInstallers = new List<Installation>();\r
304                         \r
305                         int i = 0;\r
306                         foreach (Installation inst in Installations) {\r
307                                 float percent = (CurrentTaskSetIndex+((float)i / Installations.Length))*100f/TaskSetNames.Length;\r
308                                 \r
309                                 if (inst.GetRegisteredHashCount() > 0) {\r
310                                         RaiseTaskSetEvent(NaGetTaskSetEventType.INFO, "検証: "+inst.ToString(), percent);\r
311                                         \r
312                                         if (inst.IsInstallablePackage() && inst.VerifyHashValues() == false) {\r
313                                                 invalidInstallers.Add(inst);\r
314                                                 RaiseTaskSetEvent(NaGetTaskSetEventType.WARNING, "検証: "+inst.ToString() + " 非整合", percent);\r
315                                         } else {\r
316                                                 RaiseTaskSetEvent(NaGetTaskSetEventType.INFO, "検証: "+inst.ToString() + " OK", percent);\r
317                                         }\r
318                                 }\r
319                                 i++;\r
320                         }\r
321                         \r
322                         return invalidInstallers;\r
323                 }\r
324                 \r
325                 private void runLocalUpdate()\r
326                 {\r
327                         // インストールトリストの更新\r
328                         RaiseTaskSetEvent(NaGetTaskSetEventType.STARTED_TASKSET, TaskSetNames[currentTaskSetIndex]);\r
329                         pkgListMan.DetectInstalledPkgs();\r
330                         pkgListMan.SaveInstalledPackageList();\r
331                         RaiseTaskSetEvent(NaGetTaskSetEventType.COMPLETED_TASKSET, TaskSetNames[currentTaskSetIndex]);\r
332                         currentTaskSetIndex++;\r
333                 \r
334                         // システムにインストールされているリストの更新\r
335                         RaiseTaskSetEvent(NaGetTaskSetEventType.STARTED_TASKSET, TaskSetNames[currentTaskSetIndex]);\r
336                         pkgListMan.DetectSystemInstalledPkgs();\r
337                         pkgListMan.SaveSystemInstalledPackageList();\r
338                         RaiseTaskSetEvent(NaGetTaskSetEventType.COMPLETED_TASKSET, TaskSetNames[currentTaskSetIndex]);\r
339                         currentTaskSetIndex++;\r
340                 }\r
341                 \r
342                 public override bool Done {\r
343                         get { return done; }\r
344                 }\r
345                 \r
346                 public override int CurrentTaskSetIndex {\r
347                         get { return currentTaskSetIndex; }\r
348                 }\r
349                 \r
350                 private bool cancelCalled = false;\r
351                 \r
352                 public override bool Cancel()\r
353                 {\r
354                         cancelCalled = true;\r
355                         if (! packageInstallerDownloaded) {\r
356                                 return Downloader.Cancel();\r
357                         } else return true;\r
358                 }\r
359         }\r
360 }\r