OSDN Git Service

na-get-lib,複数ソフトインストールかつハッシュ検証失敗したとき、ハッシュ検証成功したソフトの状態がおかしくなる不具合を修正
[applistation/AppliStation.git] / na-get-lib / NaGet.SubCommands / NaGetUpdate2.cs
1 using System;
2 using System.IO;
3 using System.Collections.Generic;
4 using System.Net;
5 using NaGet.Packages;
6 using NaGet.Packages.Install;
7 using NaGet.SubCommands;
8 using NaGet.SubCommands.SubTask;
9 using NaGet.Tasks;
10
11 namespace NaGet.SubCommands
12 {
13         /// <summary>
14         /// ソフトリストなどのアップデート処理
15         /// </summary>
16         public class NaGetUpdate2 : NaGetTaskSet2
17         {
18                 private PackageListsManager pkgListMan = null;
19                 
20                 private bool downloadPackageLists = true;
21                 
22                 private RepositoriesList repoList = null;
23                 
24                 private IList<string> tempRepoFiles = null;
25                 
26                 public NaGetUpdate2(PackageListsManager pkgListMan)
27                         : this(pkgListMan, true)
28                 {
29                 }
30                 
31                 /// <summary>
32                 /// コンストラクタ
33                 /// </summary>
34                 /// <param name="pkgMan">参照パッケージマネージャ</param>
35                 /// <param name="downloadPackageListsFlag">リストをダウンロードするか?</param>
36                 public NaGetUpdate2(PackageListsManager pkgMan, bool downloadPackageListsFlag)
37                 {
38                         this.pkgListMan = pkgMan;
39                         this.downloadPackageLists = downloadPackageListsFlag;
40                         
41                         // taskセットの初期化
42                         initSubTask();
43                         if (this.downloadPackageLists) {
44                                 // repos.list.xmlがあるとき、そこからよみとる。
45                                 repoList = NaGet.Utils.GetDeserializedObject<RepositoriesList>(NaGet.Env.RepositoriesListFile);
46                                 tempRepoFiles = new List<string>();
47                                 
48                                 foreach (RepositoryInfo repo in repoList.EnabledRepositories) {
49                                         string filepath = Path.GetTempFileName();
50                                         
51                                         tempRepoFiles.Add(filepath);
52                                         registSubTask(string.Format("リスト取得: {0}", repo.Url.Href),
53                                                       new DownloadSubTask(repo.Url.Href, filepath));
54                                 }
55                                 registSubTask(string.Format("リスト更新: {0}", NaGet.Env.PackageListFile),
56                                               new FunctionalSubTask<object>(runUpdatePackageListFile, null));
57                         }
58                         registSubTask("インストール済みのソフトリスト更新",
59                                       new LocalUpdateSubTask(this.pkgListMan));
60                 }
61                 
62                 public override void Run()
63                 {
64                         NotifyStarted();
65                         RaiseTaskSetEvent(TaskEventType.STARTED, string.Empty);
66                         
67                         try {
68                                 while (hasMoreSubTask) {
69                                         RaiseTaskSetEvent(TaskEventType.STARTED_SUBTASK, currentSubTaskName);
70                                         currentSubTask.Run();
71                                         RaiseTaskSetEvent(TaskEventType.COMPLETED_SUBTASK, currentSubTaskName);
72                                         
73                                         NotifyGoToNextSubTask();
74                                 }
75                         } catch (TaskCanceledException) {
76                                 cancelCalled = true;
77                         } catch (Exception e) {
78                                 RaiseTaskSetEvent(TaskEventType.ERROR, e.Message);
79                         } finally {
80                                 runDeleteTempFiles();
81                         }
82                         
83                         if (cancelCalled) {
84                                 NotifyCancelled();
85                                 RaiseTaskSetEvent(TaskEventType.CANCELED, "キャンセルされました");
86                         } else {
87                                 NotifyCompleted();
88                                 RaiseTaskSetEvent(TaskEventType.COMPLETED, string.Empty);
89                         }
90                 }
91                 
92                 private void runUpdatePackageListFile(object dummy)
93                 {
94                         int i = 0;
95                         PackageList<Package> avaiablePackageList = new PackageList<Package>();
96                         
97                         // かならず常にrepositoryリストに書き込む。
98                         NaGet.Utils.PutSerializeObject(NaGet.Env.RepositoriesListFile, repoList);
99                         
100                         foreach (RepositoryInfo repo in repoList.EnabledRepositories) {
101                                 if (repo.Type == RepositoryType.APPLISTATION_NATIVE_XML_1_0) {
102                                         try {
103                                                 string tmpfileName = tempRepoFiles[i];
104                                                 PackageList<Package> pkgList = NaGet.Utils.GetDeserializedObject<PackageList<Package>>(tmpfileName);
105                                                 pkgList.FixPackageListName(); // PackageListNameとの紐付けを行う
106                                                 
107                                                 // RepositoryReferenceの名前を読み込む
108                                                 repo.Name = (string.IsNullOrEmpty(pkgList.Name))? repo.Name : pkgList.Name;
109                                                 
110                                                 avaiablePackageList.AddPackages(pkgList);
111                                         } catch (InvalidOperationException) {
112                                                 RaiseTaskSetEvent(TaskEventType.ERROR, string.Format("レポジトリ'{0}'はAppliStation Native XML softlist形式ではありません。", repo.Name ?? repo.Url.Href));
113                                         }
114                                 } else {
115                                         RaiseTaskSetEvent(TaskEventType.WARNING, string.Format("レポジトリ'{0}'の設定が不正です。", repo.Name ?? repo.Url.Href));
116                                 }
117                                 i++;
118                         }
119                         
120                         pkgListMan.availablePkgList = avaiablePackageList; // Mediatorのリストを更新
121                         pkgListMan.SaveAvailablePackageList();
122                 }
123                 
124                 private void runDeleteTempFiles()
125                 {
126                         if (tempRepoFiles != null) {
127                                 foreach (string file in tempRepoFiles) {
128                                         if (File.Exists(file)) {
129                                                 File.Delete(file);
130                                         }
131                                 }
132                         }
133                 }
134                 
135                 public override bool Cancelable {
136                         get {
137                                 return !cancelCalled && Running && isDuringDownloading;
138                         }
139                 }
140                 
141                 private bool isDuringDownloading {
142                         get {
143                                 return Running && (currentSubTask is DownloadSubTask);
144                         }
145                 }
146                 
147         }
148 }