OSDN Git Service

na-get-lib,パッケージコレクションのパッケージが既にインストール済みの場合には処理しない処理が正常動作していなかった不具合を修正
[applistation/AppliStation.git] / na-get-lib / NaGet.SubCommands / NaGetUpdate.cs
1 using System;\r
2 using System.IO;\r
3 using NaGet.Packages;\r
4 using NaGet.Packages.Install;\r
5 using NaGet.Net;\r
6 using NaGet.SubCommands;\r
7 \r
8 namespace NaGet.SubCommands\r
9 {\r
10         public class NaGetUpdate : NaGetTaskSet\r
11         {\r
12                 private RepositoriesList repoList;\r
13                 \r
14                 private PackageListsManager pkgListMan;\r
15                 \r
16                 private bool downloadPackageLists = false;\r
17                 \r
18                 private bool packageListsDownloaded = true;\r
19                 \r
20                 \r
21                 /// <summary>\r
22                 /// ダウンロードに使うダウンローダオブジェクト\r
23                 /// </summary>\r
24                 public Downloader Downloader {\r
25                         get {\r
26                                 if (downloader == null) {\r
27                                         downloader = new Downloader();\r
28                                         downloader.DownloadEventRaised += delegate(object sender, DownloadEventArgs e) {\r
29                                                 if (e.Type == DownloadEventType.DOWNLOADING && e.TaskProgressPercent > 0) {\r
30                                                         RaiseTaskSetEvent(NaGetTaskSetEventType.PING, string.Empty, GetProgressPercent(NaGetTaskSetEventType.PING, e.TaskProgressPercent));\r
31                                                 }\r
32                                         };\r
33                                 }\r
34                                 return downloader;\r
35                         }\r
36                 }\r
37                 \r
38                 private Downloader downloader;\r
39                 \r
40                 private int currentTaskSetIndex = -1;\r
41                 \r
42                 private bool done = false;\r
43                 \r
44                 public override int CurrentTaskSetIndex {\r
45                         get { return currentTaskSetIndex; }\r
46                 }\r
47                 \r
48                 public override bool Cancelable {\r
49                         get { return ! packageListsDownloaded; }\r
50                 }\r
51                 \r
52                 public NaGetUpdate(PackageListsManager pkgListMan)\r
53                         : this(pkgListMan, true)\r
54                 {\r
55                 }\r
56                 \r
57                 public NaGetUpdate(PackageListsManager pkgMan, bool downloadPackageListsFlag)\r
58                 {\r
59                         pkgListMan = pkgMan;\r
60                         downloadPackageLists = downloadPackageListsFlag;\r
61                         \r
62                         System.Collections.Generic.List<string> taskSetNames = new System.Collections.Generic.List<string>();\r
63                         if (downloadPackageLists) {\r
64                                 if ((! File.Exists(NaGet.Env.RepositoriesListFile)) && File.Exists(NaGet.Env.ProviderListFile)) {\r
65                                         // repos.list.xmlがなくてprovider.list.txtがあるとき、マイグレートする。\r
66                                         repoList = RepositoriesList.MigrateFromProviderListTxt(NaGet.Env.ProviderListFile);\r
67                                         NaGet.Utils.PutSerializeObject(NaGet.Env.RepositoriesListFile, repoList);\r
68                                 } else {\r
69                                         // repos.list.xmlがあるとき、そこからよみとる。\r
70                                         repoList = NaGet.Utils.GetDeserializedObject<RepositoriesList>(NaGet.Env.RepositoriesListFile);\r
71                                 }\r
72                                 \r
73                                 foreach (RepositoryInfo repo in repoList.EnabledRepositories) {\r
74                                         taskSetNames.Add(string.Format("リスト取得: {0}", repo.Url.Href));\r
75                                 }\r
76                                 taskSetNames.Add(string.Format("リスト更新: {0}", NaGet.Env.PackageListFile));\r
77                         }\r
78                         taskSetNames.Add(string.Format("リスト更新: {0}", NaGet.Env.ArchiveInstalledPackageListFile));\r
79                         taskSetNames.Add(string.Format("リスト更新: {0}", NaGet.Env.SystemInstalledPackageListFile));\r
80                         \r
81                         TaskSetNames = taskSetNames.ToArray();\r
82                 }\r
83 \r
84                 public override void Run()\r
85                 {\r
86                         currentTaskSetIndex ++;\r
87                         RaiseTaskSetEvent(NaGetTaskSetEventType.STARTED, "リスト更新処理開始");\r
88                         \r
89                         try {\r
90                                 // リストのダウンロード\r
91                                 if (downloadPackageLists) {\r
92                                         packageListsDownloaded = false;\r
93                                         try {\r
94                                                 runDownloadPackageLists();\r
95                                         } catch (NaGetTaskCanceledException) {\r
96                                                 RaiseTaskSetEvent(NaGetTaskSetEventType.WARNING, "リストのダウンロード処理がキャンセルされました");\r
97                                                 pkgListMan.LoadPackageLists();\r
98                                         } catch (System.Net.WebException e) {\r
99                                                 RaiseTaskSetEvent(NaGetTaskSetEventType.WARNING, e.Message);\r
100                                                 if (System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable()) {\r
101                                                         RaiseTaskSetEvent(NaGetTaskSetEventType.WARNING, "ネットワークに接続されていません。");\r
102                                                 } else {\r
103                                                         RaiseTaskSetEvent(NaGetTaskSetEventType.WARNING, "ネットワークに接続できませんでした。ネットワークが切断されているか、ファイアウォールによって遮断された可能性があります。");\r
104                                                 }\r
105                                                 pkgListMan.LoadPackageLists();\r
106                                         } finally {\r
107                                                 int numOfEnabledRepos = NaGet.Utils.IEnumerable2Array(repoList.EnabledRepositories).Length;\r
108                                                 currentTaskSetIndex = numOfEnabledRepos + 1;\r
109                                         }\r
110                                         packageListsDownloaded = true;\r
111                                 }\r
112                                 \r
113                                 runLocalUpdate();\r
114                         } finally {\r
115                                 done = true;\r
116                         }\r
117                         \r
118                         RaiseTaskSetEvent(NaGetTaskSetEventType.COMPLETED, "終了", 100);\r
119                 }\r
120                 \r
121                 private void runDownloadPackageLists()\r
122                 {\r
123                         PackageList<Package> avaiablePackageList = new PackageList<Package>();\r
124                         foreach(RepositoryInfo repo in repoList.EnabledRepositories) {\r
125                                 RaiseTaskSetEvent(NaGetTaskSetEventType.STARTED_TASKSET, TaskSetNames[currentTaskSetIndex]);\r
126                                 \r
127                                 if (Uri.IsWellFormedUriString(repo.Url.Href, UriKind.Absolute)) {\r
128                                         string tmpfileName = Path.GetTempFileName();\r
129                                         try {\r
130                                                 Downloader.Download(repo.Url.Href, tmpfileName);\r
131                                                 if (repo.Type == RepositoryType.APPLISTATION_NATIVE_XML_1_0) {\r
132                                                         try {\r
133                                                                 PackageList<Package> pkgList = NaGet.Utils.GetDeserializedObject<PackageList<Package>>(tmpfileName);\r
134                                                                 pkgList.FixPackageListName(); // PackageListNameとの紐付けを行う\r
135                                                                 \r
136                                                                 // RepositoryReferenceの名前を読み込む // TODO RepositoryReferenceの名前を読み込む処理はここでいいのか?\r
137                                                                 repo.Name = (string.IsNullOrEmpty(pkgList.Name))? repo.Name : pkgList.Name;\r
138                                                                 \r
139                                                                 avaiablePackageList.AddPackages(pkgList);\r
140                                                         } catch (InvalidOperationException) {\r
141                                                                 RaiseTaskSetEvent(NaGetTaskSetEventType.ERROR, string.Format("レポジトリ'{0}'はAppliStation Native XML softlist形式ではありません。", repo.Name ?? repo.Url.Href));\r
142                                                         }\r
143                                                 } else {\r
144                                                         RaiseTaskSetEvent(NaGetTaskSetEventType.WARNING, string.Format("レポジトリ'{0}'の設定が不正です。", repo.Name ?? repo.Url.Href));\r
145                                                 }\r
146                                         } finally {\r
147                                                 if (File.Exists(tmpfileName)) {\r
148                                                         File.Delete(tmpfileName);\r
149                                                 }\r
150                                         }\r
151                                 } else {\r
152                                         RaiseTaskSetEvent(NaGetTaskSetEventType.WARNING, string.Format("レポジトリ'{0}'のURLが不正なため、ソフトリストは取得できませんでした。", repo.Name ?? repo.Url.Href));\r
153                                 }\r
154                                 \r
155                                 RaiseTaskSetEvent(NaGetTaskSetEventType.COMPLETED_TASKSET, TaskSetNames[currentTaskSetIndex]);\r
156                                 currentTaskSetIndex ++;\r
157                         }\r
158                         \r
159                         // TODO 暫定的にかならず常にrepositoryリストに書き込む。\r
160                         if ( true ) {\r
161                                 NaGet.Utils.PutSerializeObject(NaGet.Env.RepositoriesListFile, repoList);\r
162                         }\r
163                         \r
164                         RaiseTaskSetEvent(NaGetTaskSetEventType.STARTED_TASKSET, TaskSetNames[currentTaskSetIndex]);\r
165                         pkgListMan.availablePkgList = avaiablePackageList; // Mediatorのリストを更新\r
166                         pkgListMan.SaveAvailablePackageList();\r
167                         RaiseTaskSetEvent(NaGetTaskSetEventType.COMPLETED_TASKSET, TaskSetNames[currentTaskSetIndex]);\r
168                         currentTaskSetIndex ++;\r
169                 }\r
170                 \r
171                 private void runLocalUpdate()\r
172                 {\r
173                         // インストールトリストの更新\r
174                         RaiseTaskSetEvent(NaGetTaskSetEventType.STARTED_TASKSET, TaskSetNames[currentTaskSetIndex]);\r
175                         pkgListMan.DetectInstalledPkgs();\r
176                         pkgListMan.SaveInstalledPackageList();\r
177                         RaiseTaskSetEvent(NaGetTaskSetEventType.COMPLETED_TASKSET, TaskSetNames[currentTaskSetIndex]);\r
178                         currentTaskSetIndex++;\r
179                 \r
180                         // システムにインストールされているリストの更新\r
181                         RaiseTaskSetEvent(NaGetTaskSetEventType.STARTED_TASKSET, TaskSetNames[currentTaskSetIndex]);\r
182                         pkgListMan.DetectSystemInstalledPkgs();\r
183                         pkgListMan.SaveSystemInstalledPackageList();\r
184                         RaiseTaskSetEvent(NaGetTaskSetEventType.COMPLETED_TASKSET, TaskSetNames[currentTaskSetIndex]);\r
185                         currentTaskSetIndex++;\r
186                 }\r
187                 \r
188                 public override bool Cancel()\r
189                 {\r
190                         return Downloader.Cancel();\r
191                 }\r
192 \r
193                 public override bool Done {\r
194                         get { return done; }\r
195                 }\r
196         }\r
197 }\r