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