OSDN Git Service

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