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 ProviderList providerList;\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                                 providerList = new ProviderList(NaGet.Env.ProviderListFile);\r
60                                 \r
61                                 foreach (string url in providerList.Urls) {\r
62                                         taskSetNames.Add(string.Format("リスト取得: {0}", url));\r
63                                 }\r
64                                 taskSetNames.Add(string.Format("リスト更新: {0}", NaGet.Env.PackageListFile));\r
65                         }\r
66                         taskSetNames.Add(string.Format("リスト更新: {0}", NaGet.Env.ArchiveInstalledPackageListFile));\r
67                         taskSetNames.Add(string.Format("リスト更新: {0}", NaGet.Env.SystemInstalledPackageListFile));\r
68                         \r
69                         TaskSetNames = taskSetNames.ToArray();\r
70                 }\r
71 \r
72                 public override void Run()\r
73                 {\r
74                         currentTaskSetIndex ++;\r
75                         RaiseTaskSetEvent(NaGetTaskSetEventType.STARTED, "リスト更新処理開始");\r
76                         \r
77                         try {\r
78                                 // リストのダウンロード\r
79                                 if (downloadPackageLists) {\r
80                                         packageListsDownloaded = false;\r
81                                         try {\r
82                                                 runDownloadPackages();\r
83                                         } catch (NaGetTaskCanceledException) {\r
84                                                 RaiseTaskSetEvent(NaGetTaskSetEventType.WARNING, "リストのダウンロード処理がキャンセルされました");\r
85                                         } catch (System.Net.WebException e) {\r
86                                                 RaiseTaskSetEvent(NaGetTaskSetEventType.WARNING, e.Message);\r
87                                                 if (System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable()) {\r
88                                                         RaiseTaskSetEvent(NaGetTaskSetEventType.WARNING, "ネットワークに接続されていません。");\r
89                                                 } else {\r
90                                                         RaiseTaskSetEvent(NaGetTaskSetEventType.WARNING, "ネットワークに接続できませんでした。ネットワークが切断されているか、ファイアウォールによって遮断された可能性があります。");\r
91                                                 }\r
92                                         } finally {\r
93                                                 currentTaskSetIndex = providerList.Urls.Length + 1;\r
94                                         }\r
95                                         packageListsDownloaded = true;\r
96                                 }\r
97                                 \r
98                                 runLocalUpdate();\r
99                         } finally {\r
100                                 done = true;\r
101                         }\r
102                         \r
103                         RaiseTaskSetEvent(NaGetTaskSetEventType.COMPLETED, "終了", 100);\r
104                 }\r
105                 \r
106                 private void runDownloadPackages()\r
107                 {\r
108                         PackageList<Package> avaiablePackageList = new PackageList<Package>();\r
109                         foreach(string provider in providerList.Urls) {\r
110                                 RaiseTaskSetEvent(NaGetTaskSetEventType.STARTED_TASKSET, TaskSetNames[currentTaskSetIndex]);\r
111                                 \r
112                                 string tmpfileName = Path.GetTempFileName();\r
113                                 try {\r
114                                         Downloader.Download(provider, tmpfileName);\r
115                                         \r
116                                         avaiablePackageList.AddPackages(NaGet.Utils.GetDeserializedObject<PackageList<Package>>(tmpfileName).Packages);\r
117                                 } finally {\r
118                                         if (File.Exists(tmpfileName)) {\r
119                                                 File.Delete(tmpfileName);\r
120                                         }\r
121                                 }\r
122                                 \r
123                                 currentTaskSetIndex ++;\r
124                                 RaiseTaskSetEvent(NaGetTaskSetEventType.COMPLETED_TASKSET, TaskSetNames[currentTaskSetIndex-1]);\r
125                         }\r
126                         \r
127                         RaiseTaskSetEvent(NaGetTaskSetEventType.STARTED_TASKSET, TaskSetNames[currentTaskSetIndex]);\r
128                         pkgListMan.availablePkgList = avaiablePackageList; // Mediatorのリストを更新\r
129                         pkgListMan.SaveAvailablePackageList();\r
130                         currentTaskSetIndex ++;\r
131                         RaiseTaskSetEvent(NaGetTaskSetEventType.COMPLETED_TASKSET, TaskSetNames[currentTaskSetIndex-1]);\r
132                 }\r
133                 \r
134                 private void runLocalUpdate()\r
135                 {\r
136                         // インストールトリストの更新\r
137                         RaiseTaskSetEvent(NaGetTaskSetEventType.STARTED_TASKSET, TaskSetNames[currentTaskSetIndex]);\r
138                         pkgListMan.DetectInstalledPkgs();\r
139                         pkgListMan.SaveInstalledPackageList();\r
140                         currentTaskSetIndex++;\r
141                         RaiseTaskSetEvent(NaGetTaskSetEventType.COMPLETED_TASKSET, TaskSetNames[currentTaskSetIndex-1]);\r
142                 \r
143                         // システムにインストールされているリストの更新\r
144                         RaiseTaskSetEvent(NaGetTaskSetEventType.STARTED_TASKSET, TaskSetNames[currentTaskSetIndex]);\r
145                         pkgListMan.DetectSystemInstalledPkgs();\r
146                         pkgListMan.SaveSystemInstalledPackageList();\r
147                         currentTaskSetIndex++;\r
148                         RaiseTaskSetEvent(NaGetTaskSetEventType.COMPLETED_TASKSET, TaskSetNames[currentTaskSetIndex-1]);\r
149                 }\r
150                 \r
151                 public override bool Cancel()\r
152                 {\r
153                         return Downloader.Cancel();\r
154                 }\r
155 \r
156                 public override bool Done {\r
157                         get { return done; }\r
158                 }\r
159         }\r
160 }\r