2 using System.Collections.Generic;
4 using NaGet.Packages.Install;
5 using NaGet.SubCommands;
6 using NaGet.SubCommands.SubTask;
8 using NaGet.InteropServices;
10 namespace NaGet.SubCommands
13 /// キャッシュへのインストーラファイルのダウンロード処理
15 public class NaGetDownloadToCache2 : NaGetTaskSet2
17 private IList<Installation> installations;
19 private DownloadScannerService scanner;
24 /// <param name="pkgs">インストールするパッケージ</param>
25 public NaGetDownloadToCache2(PackageListsManager pkgListMan, Package[] pkgs)
26 : this(pkgListMan, Installation.ConvertInstallations(pkgs))
33 /// <param name="installations">インストール処理の配列</param>
34 public NaGetDownloadToCache2(PackageListsManager pkgMan, IList<Installation> insts)
36 installations = insts;
38 scanner = new DownloadScannerService();
43 foreach (Installation inst in installations) {
44 DownloadSubTask dlSTask = new DownloadSubTask(inst.InstallerURL, inst.InstallerFile);
45 VirusScanSubTask scanSTask = new VirusScanSubTask(scanner, inst.InstallerFile, inst.InstallerURL);
47 dlSTask.EnableChangeFileName = true;
48 dlSTask.TaskEventRaised += delegate(object sender, TaskEventArgs e) {
49 if (e.Type == TaskEventType.COMPLETED) {
50 scanSTask.TargetFilePath = inst.InstallerFile = dlSTask.Filepath;
54 registSubTask(string.Format("ダウンロード: {0}", inst),
56 registSubTask(string.Format("ウイルススキャン: {0}", inst),
59 registSubTask("インストーラーの検証",
60 new VerifyInstallerFileSubTask(insts));
61 registSubTask("インストール済みのソフトリスト更新",
62 new LocalUpdateSubTask(pkgMan));
65 public override void Run()
68 RaiseTaskSetEvent(TaskEventType.STARTED, string.Empty);
71 while (hasMoreSubTask) {
72 bool canGoToNextSubTask = true;
74 RaiseTaskSetEvent(TaskEventType.STARTED_SUBTASK, currentSubTaskName);
76 RaiseTaskSetEvent(TaskEventType.COMPLETED_SUBTASK, currentSubTaskName);
78 if (runCheckVerify() == false) {
79 canGoToNextSubTask = false;
80 NotifyGoToSubTask(0); // 最初からやり直し。
83 throw new TaskCanceledException("cancel is called");
86 if (canGoToNextSubTask) {
87 NotifyGoToNextSubTask();
90 } catch (TaskCanceledException) {
92 } catch (Exception e) {
93 RaiseTaskSetEvent(TaskEventType.ERROR, e.Message);
98 RaiseTaskSetEvent(TaskEventType.CANCELED, "キャンセルされました");
101 RaiseTaskSetEvent(TaskEventType.COMPLETED, string.Empty);
105 private bool runCheckVerify()
109 if (currentSubTask is VerifyInstallerFileSubTask) {
110 VerifyInstallerFileSubTask verifySTask = currentSubTask as VerifyInstallerFileSubTask;
111 if (verifySTask.InvalidInstallers != null && verifySTask.InvalidInstallers.Count > 0) {
112 System.Text.StringBuilder invalidInstallerNames = new System.Text.StringBuilder();
113 foreach (Installation invalidInst in verifySTask.InvalidInstallers) {
114 invalidInstallerNames.AppendFormat(" - {0}\n", invalidInst.ToString());
117 string msg = string.Format("以下の{0}個のパッケージでファイルが壊れている可能性があります。\n{1}\nダウンロードし直しますか?",
118 verifySTask.InvalidInstallers.Count, invalidInstallerNames.ToString());
119 NaGetTaskQueryResult result = NaGetTaskQueryResult.CANCEL;
122 result = RaiseTaskSetQueryEvent(msg, NaGetTaskQueryResult.CONTINUE | NaGetTaskQueryResult.CANCEL);
126 case NaGetTaskQueryResult.CONTINUE:
127 RaiseTaskSetEvent(TaskEventType.INFO, "ダウンロード処理を再試行");
129 foreach (Installation invalidInst in verifySTask.InvalidInstallers) {
130 invalidInst.RemoveDownloadedFile();
135 case NaGetTaskQueryResult.CANCEL:
136 case NaGetTaskQueryResult.CANCELED_AUTOMATICALLY:
139 throw new TaskCanceledException("処理の継続のキャンセルが選択されました");
147 public override bool Cancelable {
149 return !cancelCalled && Running && isDuringDownloading;
153 private bool isDuringDownloading {
155 return Running && (currentSubTask is DownloadSubTask);