OSDN Git Service

na-get-lib,provider.list.txtからrepos.list.xmlへ移行
[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                                 }\r
29                                 return downloader;\r
30                         }\r
31                 }\r
32                 \r
33                 private Downloader downloader;\r
34                 \r
35                 private int currentTaskSetIndex = -1;\r
36                 \r
37                 private bool done = false;\r
38                 \r
39                 public override int CurrentTaskSetIndex {\r
40                         get { return currentTaskSetIndex; }\r
41                 }\r
42                 \r
43                 public override bool Cancelable {\r
44                         get { return ! packageListsDownloaded; }\r
45                 }\r
46                 \r
47                 public NaGetUpdate(PackageListsManager pkgListMan)\r
48                         : this(pkgListMan, true)\r
49                 {\r
50                 }\r
51                 \r
52                 public NaGetUpdate(PackageListsManager pkgMan, bool downloadPackageListsFlag)\r
53                 {\r
54                         pkgListMan = pkgMan;\r
55                         downloadPackageLists = downloadPackageListsFlag;\r
56                         \r
57                         System.Collections.Generic.List<string> taskSetNames = new System.Collections.Generic.List<string>();\r
58                         if (downloadPackageLists) {\r
59                                 if ((! File.Exists(NaGet.Env.RepositoriesListFile)) && File.Exists(NaGet.Env.ProviderListFile)) {\r
60                                         // repos.list.xmlがなくてprovider.list.txtがあるとき、マイグレートする。\r
61                                         repoList = RepositoriesList.MigrateFromProviderListTxt(NaGet.Env.ProviderListFile);\r
62                                         NaGet.Utils.PutSerializeObject(NaGet.Env.RepositoriesListFile, repoList);\r
63                                 } else {\r
64                                         // repos.list.xmlがあるとき、そこからよみとる。\r
65                                         repoList = NaGet.Utils.GetDeserializedObject<RepositoriesList>(NaGet.Env.RepositoriesListFile);\r
66                                 }\r
67                                 \r
68                                 foreach (RepositoryReference repo in repoList.Repositories) {\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                                                 runDownloadPackages();\r
90                                         } catch (NaGetTaskCanceledException) {\r
91                                                 RaiseTaskSetEvent(NaGetTaskSetEventType.WARNING, "リストのダウンロード処理がキャンセルされました");\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                                         } finally {\r
100                                                 currentTaskSetIndex = repoList.Repositories.Length + 1;\r
101                                         }\r
102                                         packageListsDownloaded = true;\r
103                                 }\r
104                                 \r
105                                 runLocalUpdate();\r
106                         } finally {\r
107                                 done = true;\r
108                         }\r
109                         \r
110                         RaiseTaskSetEvent(NaGetTaskSetEventType.COMPLETED, "終了", 100);\r
111                 }\r
112                 \r
113                 private void runDownloadPackages()\r
114                 {\r
115                         PackageList<Package> avaiablePackageList = new PackageList<Package>();\r
116                         foreach(RepositoryReference repo in repoList.Repositories) {\r
117                                 RaiseTaskSetEvent(NaGetTaskSetEventType.STARTED_TASKSET, TaskSetNames[currentTaskSetIndex]);\r
118                                 \r
119                                 string tmpfileName = Path.GetTempFileName();\r
120                                 try {\r
121                                         Downloader.Download(repo.Url.Href, tmpfileName);\r
122                                         \r
123                                         PackageList<Package> pkgList = NaGet.Utils.GetDeserializedObject<PackageList<Package>>(tmpfileName);\r
124                                         \r
125                                         // RepositoryReferenceの名前を読み込む // TODO RepositoryReferenceの名前を読み込む処理はここでいいのか?\r
126                                         repo.Name = (string.IsNullOrEmpty(pkgList.Name))? repo.Name : pkgList.Name;\r
127                                         \r
128                                         avaiablePackageList.AddPackages(pkgList.Packages);\r
129                                 } finally {\r
130                                         if (File.Exists(tmpfileName)) {\r
131                                                 File.Delete(tmpfileName);\r
132                                         }\r
133                                 }\r
134                                 \r
135                                 currentTaskSetIndex ++;\r
136                                 RaiseTaskSetEvent(NaGetTaskSetEventType.COMPLETED_TASKSET, TaskSetNames[currentTaskSetIndex-1]);\r
137                         }\r
138                         \r
139                         // TODO 暫定的にかならず常にrepositoryリストに書き込む。\r
140                         if ( true ) {\r
141                                 NaGet.Utils.PutSerializeObject(NaGet.Env.RepositoriesListFile, repoList);\r
142                         }\r
143                         \r
144                         RaiseTaskSetEvent(NaGetTaskSetEventType.STARTED_TASKSET, TaskSetNames[currentTaskSetIndex]);\r
145                         pkgListMan.availablePkgList = avaiablePackageList; // Mediatorのリストを更新\r
146                         pkgListMan.SaveAvailablePackageList();\r
147                         currentTaskSetIndex ++;\r
148                         RaiseTaskSetEvent(NaGetTaskSetEventType.COMPLETED_TASKSET, TaskSetNames[currentTaskSetIndex-1]);\r
149                 }\r
150                 \r
151                 private void runLocalUpdate()\r
152                 {\r
153                         // インストールトリストの更新\r
154                         RaiseTaskSetEvent(NaGetTaskSetEventType.STARTED_TASKSET, TaskSetNames[currentTaskSetIndex]);\r
155                         pkgListMan.DetectInstalledPkgs();\r
156                         pkgListMan.SaveInstalledPackageList();\r
157                         currentTaskSetIndex++;\r
158                         RaiseTaskSetEvent(NaGetTaskSetEventType.COMPLETED_TASKSET, TaskSetNames[currentTaskSetIndex-1]);\r
159                 \r
160                         // システムにインストールされているリストの更新\r
161                         RaiseTaskSetEvent(NaGetTaskSetEventType.STARTED_TASKSET, TaskSetNames[currentTaskSetIndex]);\r
162                         pkgListMan.DetectSystemInstalledPkgs();\r
163                         pkgListMan.SaveSystemInstalledPackageList();\r
164                         currentTaskSetIndex++;\r
165                         RaiseTaskSetEvent(NaGetTaskSetEventType.COMPLETED_TASKSET, TaskSetNames[currentTaskSetIndex-1]);\r
166                 }\r
167                 \r
168                 public override bool Cancel()\r
169                 {\r
170                         return Downloader.Cancel();\r
171                 }\r
172 \r
173                 public override bool Done {\r
174                         get { return done; }\r
175                 }\r
176         }\r
177 }\r