OSDN Git Service

6d2c8b6c859c607a65f1f525328f18e561e348b5
[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                                 // repos.list.xmlがあるとき、そこからよみとる。\r
65                                 repoList = NaGet.Utils.GetDeserializedObject<RepositoriesList>(NaGet.Env.RepositoriesListFile);\r
66                                 \r
67                                 foreach (RepositoryInfo repo in repoList.EnabledRepositories) {\r
68                                         taskSetNames.Add(string.Format("リスト取得: {0}", repo.Url.Href));\r
69                                 }\r
70                                 taskSetNames.Add(string.Format("リスト更新: {0}", NaGet.Env.PackageListFile));\r
71                         }\r
72                         taskSetNames.Add(string.Format("リスト更新: {0}", NaGet.Env.ArchiveInstalledPackageListFile));\r
73                         taskSetNames.Add(string.Format("リスト更新: {0}", NaGet.Env.SystemInstalledPackageListFile));\r
74                         \r
75                         TaskSetNames = taskSetNames.ToArray();\r
76                 }\r
77 \r
78                 public override void Run()\r
79                 {\r
80                         currentTaskSetIndex ++;\r
81                         RaiseTaskSetEvent(NaGetTaskSetEventType.STARTED, "リスト更新処理開始");\r
82                         \r
83                         try {\r
84                                 // リストのダウンロード\r
85                                 if (downloadPackageLists) {\r
86                                         packageListsDownloaded = false;\r
87                                         try {\r
88                                                 runDownloadPackageLists();\r
89                                         } catch (NaGetTaskCanceledException) {\r
90                                                 RaiseTaskSetEvent(NaGetTaskSetEventType.WARNING, "リストのダウンロード処理がキャンセルされました");\r
91                                                 pkgListMan.LoadPackageLists();\r
92                                         } catch (System.Net.WebException e) {\r
93                                                 RaiseTaskSetEvent(NaGetTaskSetEventType.WARNING, e.Message);\r
94                                                 if (System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable()) {\r
95                                                         RaiseTaskSetEvent(NaGetTaskSetEventType.WARNING, "ネットワークに接続されていません。");\r
96                                                 } else {\r
97                                                         RaiseTaskSetEvent(NaGetTaskSetEventType.WARNING, "ネットワークに接続できませんでした。ネットワークが切断されているか、ファイアウォールによって遮断された可能性があります。");\r
98                                                 }\r
99                                                 pkgListMan.LoadPackageLists();\r
100                                         } finally {\r
101                                                 int numOfEnabledRepos = NaGet.Utils.IEnumerable2Array(repoList.EnabledRepositories).Length;\r
102                                                 currentTaskSetIndex = numOfEnabledRepos + 1;\r
103                                         }\r
104                                         packageListsDownloaded = true;\r
105                                 }\r
106                                 \r
107                                 runLocalUpdate();\r
108                         } finally {\r
109                                 done = true;\r
110                         }\r
111                         \r
112                         RaiseTaskSetEvent(NaGetTaskSetEventType.COMPLETED, "終了", 100);\r
113                 }\r
114                 \r
115                 private void runDownloadPackageLists()\r
116                 {\r
117                         PackageList<Package> avaiablePackageList = new PackageList<Package>();\r
118                         foreach(RepositoryInfo repo in repoList.EnabledRepositories) {\r
119                                 RaiseTaskSetEvent(NaGetTaskSetEventType.STARTED_TASKSET, TaskSetNames[currentTaskSetIndex]);\r
120                                 \r
121                                 if (Uri.IsWellFormedUriString(repo.Url.Href, UriKind.Absolute)) {\r
122                                         string tmpfileName = Path.GetTempFileName();\r
123                                         try {\r
124                                                 Downloader.Download(repo.Url.Href, tmpfileName);\r
125                                                 if (repo.Type == RepositoryType.APPLISTATION_NATIVE_XML_1_0) {\r
126                                                         try {\r
127                                                                 PackageList<Package> pkgList = NaGet.Utils.GetDeserializedObject<PackageList<Package>>(tmpfileName);\r
128                                                                 pkgList.FixPackageListName(); // PackageListNameとの紐付けを行う\r
129                                                                 \r
130                                                                 // RepositoryReferenceの名前を読み込む // TODO RepositoryReferenceの名前を読み込む処理はここでいいのか?\r
131                                                                 repo.Name = (string.IsNullOrEmpty(pkgList.Name))? repo.Name : pkgList.Name;\r
132                                                                 \r
133                                                                 avaiablePackageList.AddPackages(pkgList);\r
134                                                         } catch (InvalidOperationException) {\r
135                                                                 RaiseTaskSetEvent(NaGetTaskSetEventType.ERROR, string.Format("レポジトリ'{0}'はAppliStation Native XML softlist形式ではありません。", repo.Name ?? repo.Url.Href));\r
136                                                         }\r
137                                                 } else {\r
138                                                         RaiseTaskSetEvent(NaGetTaskSetEventType.WARNING, string.Format("レポジトリ'{0}'の設定が不正です。", repo.Name ?? repo.Url.Href));\r
139                                                 }\r
140                                         } finally {\r
141                                                 if (File.Exists(tmpfileName)) {\r
142                                                         File.Delete(tmpfileName);\r
143                                                 }\r
144                                         }\r
145                                 } else {\r
146                                         RaiseTaskSetEvent(NaGetTaskSetEventType.WARNING, string.Format("レポジトリ'{0}'のURLが不正なため、ソフトリストは取得できませんでした。", repo.Name ?? repo.Url.Href));\r
147                                 }\r
148                                 \r
149                                 RaiseTaskSetEvent(NaGetTaskSetEventType.COMPLETED_TASKSET, TaskSetNames[currentTaskSetIndex]);\r
150                                 currentTaskSetIndex ++;\r
151                         }\r
152                         \r
153                         // TODO 暫定的にかならず常にrepositoryリストに書き込む。\r
154                         if ( true ) {\r
155                                 NaGet.Utils.PutSerializeObject(NaGet.Env.RepositoriesListFile, repoList);\r
156                         }\r
157                         \r
158                         RaiseTaskSetEvent(NaGetTaskSetEventType.STARTED_TASKSET, TaskSetNames[currentTaskSetIndex]);\r
159                         pkgListMan.availablePkgList = avaiablePackageList; // Mediatorのリストを更新\r
160                         pkgListMan.SaveAvailablePackageList();\r
161                         RaiseTaskSetEvent(NaGetTaskSetEventType.COMPLETED_TASKSET, TaskSetNames[currentTaskSetIndex]);\r
162                         currentTaskSetIndex ++;\r
163                 }\r
164                 \r
165                 private void runLocalUpdate()\r
166                 {\r
167                         // インストールトリストの更新\r
168                         RaiseTaskSetEvent(NaGetTaskSetEventType.STARTED_TASKSET, TaskSetNames[currentTaskSetIndex]);\r
169                         pkgListMan.DetectInstalledPkgs();\r
170                         pkgListMan.SaveInstalledPackageList();\r
171                         RaiseTaskSetEvent(NaGetTaskSetEventType.COMPLETED_TASKSET, TaskSetNames[currentTaskSetIndex]);\r
172                         currentTaskSetIndex++;\r
173                 \r
174                         // システムにインストールされているリストの更新\r
175                         RaiseTaskSetEvent(NaGetTaskSetEventType.STARTED_TASKSET, TaskSetNames[currentTaskSetIndex]);\r
176                         pkgListMan.DetectSystemInstalledPkgs();\r
177                         pkgListMan.SaveSystemInstalledPackageList();\r
178                         RaiseTaskSetEvent(NaGetTaskSetEventType.COMPLETED_TASKSET, TaskSetNames[currentTaskSetIndex]);\r
179                         currentTaskSetIndex++;\r
180                 }\r
181                 \r
182                 public override bool Cancel()\r
183                 {\r
184                         return Downloader.Cancel();\r
185                 }\r
186 \r
187                 public override bool Done {\r
188                         get { return done; }\r
189                 }\r
190         }\r
191 }\r