OSDN Git Service

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