OSDN Git Service

na-get-lib,ダウンロード後にファイル名が引き継がれずインストールなどが動作していなかったのを修正
[applistation/AppliStation.git] / na-get-lib / NaGet.SubCommands / NaGetDownloadToCache2.cs
1 using System;
2 using System.Collections.Generic;
3 using NaGet.Packages;
4 using NaGet.Packages.Install;
5 using NaGet.SubCommands;
6 using NaGet.SubCommands.SubTask;
7 using NaGet.Tasks;
8 using NaGet.InteropServices;
9
10 namespace NaGet.SubCommands
11 {
12         /// <summary>
13         /// キャッシュへのインストーラファイルのダウンロード処理
14         /// </summary>
15         public class NaGetDownloadToCache2 : NaGetTaskSet2
16         {
17                 private IList<Installation> installations;
18                 
19                 private DownloadScannerService scanner;
20                 
21                 /// <summary>
22                 /// コンストラクタ
23                 /// </summary>
24                 /// <param name="pkgs">インストールするパッケージ</param>
25                 public NaGetDownloadToCache2(PackageListsManager pkgListMan, Package[] pkgs)
26                         : this(pkgListMan, Installation.ConvertInstallations(pkgs))
27                 {
28                 }
29                 
30                 /// <summary>
31                 /// コンストラクタ
32                 /// </summary>
33                 /// <param name="installations">インストール処理の配列</param>
34                 public NaGetDownloadToCache2(PackageListsManager pkgMan, IList<Installation> insts)
35                 {
36                         installations = insts;
37                         
38                         scanner = new DownloadScannerService();
39                         scanner.Init();
40                         
41                         // taskセットの初期化
42                         initSubTask();
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);
46                                 
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;
51                                         }
52                                 };
53                                 
54                                 registSubTask(string.Format("ダウンロード: {0}", inst),
55                                               dlSTask);
56                                 registSubTask(string.Format("ウイルススキャン: {0}", inst),
57                                               scanSTask);
58                         }
59                         registSubTask("インストーラーの検証",
60                                       new VerifyInstallerFileSubTask(insts));
61                         registSubTask("インストール済みのソフトリスト更新",
62                                       new LocalUpdateSubTask(pkgMan));
63                 }
64                 
65                 public override void Run()
66                 {
67                         NotifyStarted();
68                         RaiseTaskSetEvent(TaskEventType.STARTED, string.Empty);
69                         
70                         try {
71                                 while (hasMoreSubTask) {
72                                         bool canGoToNextSubTask = true;
73                                         
74                                         RaiseTaskSetEvent(TaskEventType.STARTED_SUBTASK, currentSubTaskName);
75                                         currentSubTask.Run();
76                                         RaiseTaskSetEvent(TaskEventType.COMPLETED_SUBTASK, currentSubTaskName);
77                                         
78                                         if (runCheckVerify() == false) {
79                                                 canGoToNextSubTask = false;
80                                                 NotifyGoToSubTask(0); // 最初からやり直し。
81                                         }
82                                         if (cancelCalled) {
83                                                 throw new TaskCanceledException("cancel is called");
84                                         }
85                                         
86                                         if (canGoToNextSubTask) {
87                                                 NotifyGoToNextSubTask();
88                                         }
89                                 }
90                         } catch (TaskCanceledException) {
91                                 cancelCalled = true;
92                         } catch (Exception e) {
93                                 RaiseTaskSetEvent(TaskEventType.ERROR, e.Message);
94                         }
95                         
96                         if (cancelCalled) {
97                                 NotifyCancelled();
98                                 RaiseTaskSetEvent(TaskEventType.CANCELED, "キャンセルされました");
99                         } else {
100                                 NotifyCompleted();
101                                 RaiseTaskSetEvent(TaskEventType.COMPLETED, string.Empty);
102                         }
103                 }
104                 
105                 private bool runCheckVerify()
106                 {
107                         bool ret = true;
108                         
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());
115                                         }
116                                         
117                                         string msg = string.Format("以下の{0}個のパッケージでファイルが壊れている可能性があります。\n{1}\nダウンロードし直しますか?",
118                                                                    verifySTask.InvalidInstallers.Count, invalidInstallerNames.ToString());
119                                         NaGetTaskQueryResult result = NaGetTaskQueryResult.CANCEL;
120                                         
121                                         if (!cancelCalled) {
122                                                 result = RaiseTaskSetQueryEvent(msg, NaGetTaskQueryResult.CONTINUE | NaGetTaskQueryResult.CANCEL);
123                                         }
124                                         
125                                         switch (result) {
126                                                 case NaGetTaskQueryResult.CONTINUE:
127                                                         RaiseTaskSetEvent(TaskEventType.INFO, "ダウンロード処理を再試行");
128                                                         
129                                                         foreach (Installation invalidInst in verifySTask.InvalidInstallers) {
130                                                                 invalidInst.RemoveDownloadedFile();
131                                                         }
132                                                         ret = false;
133                                                         
134                                                         break;
135                                                 case NaGetTaskQueryResult.CANCEL:
136                                                 case NaGetTaskQueryResult.CANCELED_AUTOMATICALLY:
137                                                 default:
138                                                         ret = false;
139                                                         throw new TaskCanceledException("処理の継続のキャンセルが選択されました");
140                                         }
141                                 }
142                         }
143                         
144                         return ret;
145                 }
146                 
147                 public override bool Cancelable {
148                         get {
149                                 return !cancelCalled && Running && isDuringDownloading;
150                         }
151                 }
152                 
153                 private bool isDuringDownloading {
154                         get {
155                                 return Running && (currentSubTask is DownloadSubTask);
156                         }
157                 }
158         }
159 }