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