OSDN Git Service

9b84cc8736712b9e70c7618764df19d24e8a3821
[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                         registSubTask("セキュリティプロトコル設定の確認",
44                                       new SecurityProtocolConfigSubTask());
45                         if (this.downloadPackageLists) {
46                                 // repos.list.xmlがあるとき、そこからよみとる。
47                                 repoList = NaGet.Utils.GetDeserializedObject<RepositoriesList>(NaGet.Env.RepositoriesListFile);
48                                 tempRepoFiles = new List<string>();
49                                 
50                                 foreach (RepositoryInfo repo in repoList.EnabledRepositories) {
51                                         string filepath = Path.GetTempFileName();
52                                         
53                                         tempRepoFiles.Add(filepath);
54                                         registSubTask(string.Format("リスト取得: {0}", repo.Url.Href),
55                                                       new DownloadSubTask(repo.Url.Href, filepath));
56                                 }
57                                 registSubTask(string.Format("リスト更新: {0}", NaGet.Env.PackageListFile),
58                                               new FunctionalSubTask<object>(runUpdatePackageListFile, null));
59                         }
60                         registSubTask("インストール済みのソフトリスト更新",
61                                       new LocalUpdateSubTask(this.pkgListMan));
62                 }
63                 
64                 public override void Run()
65                 {
66                         NotifyStarted();
67                         RaiseTaskSetEvent(TaskEventType.STARTED, string.Empty);
68                         
69                         try {
70                                 while (hasMoreSubTask) {
71                                         RaiseTaskSetEvent(TaskEventType.STARTED_SUBTASK, currentSubTaskName);
72                                         currentSubTask.Run();
73                                         RaiseTaskSetEvent(TaskEventType.COMPLETED_SUBTASK, currentSubTaskName);
74                                         
75                                         NotifyGoToNextSubTask();
76                                 }
77                         } catch (TaskCanceledException) {
78                                 cancelCalled = true;
79                         } catch (Exception e) {
80                                 RaiseTaskSetEvent(TaskEventType.ERROR, e.Message);
81                         } finally {
82                                 runDeleteTempFiles();
83                         }
84                         
85                         if (cancelCalled) {
86                                 NotifyCancelled();
87                                 RaiseTaskSetEvent(TaskEventType.CANCELED, "キャンセルされました");
88                         } else {
89                                 NotifyCompleted();
90                                 RaiseTaskSetEvent(TaskEventType.COMPLETED, string.Empty);
91                         }
92                 }
93                 
94                 private void runUpdatePackageListFile(object dummy)
95                 {
96                         int i = 0;
97                         PackageList<Package> avaiablePackageList = new PackageList<Package>();
98                         
99                         // かならず常にrepositoryリストに書き込む。
100                         NaGet.Utils.PutSerializeObject(NaGet.Env.RepositoriesListFile, repoList);
101                         
102                         foreach (RepositoryInfo repo in repoList.EnabledRepositories) {
103                                 if (repo.Type == RepositoryType.APPLISTATION_NATIVE_XML_1_0) {
104                                         try {
105                                                 string tmpfileName = tempRepoFiles[i];
106                                                 PackageList<Package> pkgList = NaGet.Utils.GetDeserializedObject<PackageList<Package>>(tmpfileName);
107                                                 pkgList.FixPackageListName(); // PackageListNameとの紐付けを行う
108                                                 
109                                                 // RepositoryReferenceの名前を読み込む
110                                                 repo.Name = (string.IsNullOrEmpty(pkgList.Name))? repo.Name : pkgList.Name;
111                                                 
112                                                 avaiablePackageList.AddPackages(pkgList);
113                                         } catch (InvalidOperationException) {
114                                                 RaiseTaskSetEvent(TaskEventType.ERROR, string.Format("レポジトリ'{0}'はAppliStation Native XML softlist形式ではありません。", repo.Name ?? repo.Url.Href));
115                                         }
116                                 } else {
117                                         RaiseTaskSetEvent(TaskEventType.WARNING, string.Format("レポジトリ'{0}'の設定が不正です。", repo.Name ?? repo.Url.Href));
118                                 }
119                                 i++;
120                         }
121                         
122                         pkgListMan.availablePkgList = avaiablePackageList; // Mediatorのリストを更新
123                         pkgListMan.SaveAvailablePackageList();
124                 }
125                 
126                 private void runDeleteTempFiles()
127                 {
128                         if (tempRepoFiles != null) {
129                                 foreach (string file in tempRepoFiles) {
130                                         if (File.Exists(file)) {
131                                                 File.Delete(file);
132                                         }
133                                 }
134                         }
135                 }
136                 
137                 public override bool Cancelable {
138                         get {
139                                 return !cancelCalled && Running && isDuringDownloading;
140                         }
141                 }
142                 
143                 private bool isDuringDownloading {
144                         get {
145                                 return Running && (currentSubTask is DownloadSubTask);
146                         }
147                 }
148                 
149         }
150 }