OSDN Git Service

na-get-lib,チケット #36130 に関連して、更新パッケージから非対応パッケージを除外するようにした
[applistation/AppliStation.git] / na-get-lib / NaGet / Env.cs
1 using System;\r
2 using System.IO;\r
3 using System.Net;\r
4 \r
5 namespace NaGet\r
6 {\r
7         /// <summary>\r
8         /// 環境変数などを取り扱うクラス\r
9         /// </summary>\r
10         public sealed class Env\r
11         {               \r
12                 /// <summary>\r
13                 /// 呼び出し禁止\r
14                 /// </summary>\r
15                 private Env()\r
16                 {\r
17                 }\r
18                 \r
19                 #region pref.xml関連\r
20                 \r
21                 /// <summary>\r
22                 /// 設定を格納\r
23                 /// </summary>\r
24                 private static NaGetLibPref pref;\r
25                 \r
26                 /// <summary>\r
27                 /// 設定(のクローンオブジェクト)を返す。\r
28                 /// </summary>\r
29                 public static NaGetLibPref Pref {\r
30                         get {\r
31                                 NaGetLibPref prefCopied = new NaGetLibPref();\r
32                                 \r
33                                 if (pref == null) {\r
34                                         LoadPref();\r
35                                 }\r
36                                 NaGet.Utils.FieldCopy(pref, ref prefCopied);\r
37                                 \r
38                                 return prefCopied;\r
39                         }\r
40                 }\r
41                 \r
42                 /// <summary>\r
43                 /// 設定ファイルのある場所を取得する\r
44                 /// </summary>\r
45                 public static string PrefPath {\r
46                         get {\r
47                                 return Path.Combine(Environment.CurrentDirectory, "pref.xml");\r
48                         }\r
49                 }\r
50                 \r
51                 /// <summary>\r
52                 /// 設定ファイルを読み込む。または再読み込みする。\r
53                 /// </summary>\r
54                 public static void LoadPref()\r
55                 {\r
56                         string path = PrefPath;\r
57                         try {\r
58                                 if (! File.Exists(path)) throw new FileNotFoundException(string.Empty, path);\r
59                                 pref = NaGet.Utils.GetDeserializedObject<NaGetLibPref>(path);\r
60                         } catch(Exception) {\r
61                                 pref = new NaGetLibPref();\r
62                         }\r
63                 }\r
64                 \r
65                 \r
66                 #endregion\r
67 \r
68                 #region ファイルパス\r
69                 \r
70                 /// <summary>\r
71                 /// ファイルリスト提供レポジトリリストのファイル\r
72                 /// </summary>\r
73                 public static readonly string RepositoriesListFile = "repos.list.xml";\r
74 \r
75                 /// <summary>\r
76                 /// ファイルリストから読み込まれたパッケージのリストファイル\r
77                 /// </summary>\r
78                 public static readonly string PackageListFile = "packages.list.xml";\r
79 \r
80                 /// <summary>\r
81                 /// 本ソフトウェアを介してインストールされたパッケージのリストファイル\r
82                 /// </summary>\r
83                 public static readonly string ArchiveInstalledPackageListFile = "packages.envinstalled.xml";\r
84 \r
85                 /// <summary>\r
86                 /// システムから検出されたパッケージのリストファイル\r
87                 /// </summary>\r
88                 public static readonly string SystemInstalledPackageListFile = "packages.sysinstalled.xml";\r
89                 \r
90                 /// <summary>\r
91                 /// インストールログファイル\r
92                 /// </summary>\r
93                 public static readonly string SystemInstalledPackageLogFile = "packages.sysinstalled.log.xml";\r
94                 \r
95                 /// <summary>\r
96                 /// アプリケーションデータを保存するフォルダーのパス\r
97                 /// </summary>\r
98                 public static string AppDataFolderPath\r
99                 {\r
100                         get {\r
101                                 if (pref == null) LoadPref();\r
102                                 \r
103                                 if (! string.IsNullOrEmpty(pref.AppDataFolder)) {\r
104                                         return pref.AppDataFolder;\r
105                                 }\r
106                                 \r
107                                 return Environment.CurrentDirectory;\r
108                         }\r
109                 }\r
110 \r
111                 /// <summary>\r
112                 /// インストーラーの一時置き場の親ディレクトリ\r
113                 /// </summary>\r
114                 public static string ArchiveFolderPath\r
115                 {\r
116                         get {\r
117                                 if (pref == null) LoadPref();\r
118 \r
119                                 if (! string.IsNullOrEmpty(pref.CacheFolder)) {\r
120                                         return pref.CacheFolder;\r
121                                 }\r
122                                 \r
123                                 return Path.Combine(AppDataFolderPath, "Cache");\r
124                         \r
125                         }\r
126                 }\r
127                 \r
128                 /// <summary>\r
129                 /// アーカイバ方式のパッケージのインストール先フォルダー\r
130                 /// </summary>\r
131                 public static string ArchiveProgramFiles {\r
132                         get {\r
133                                 return Path.Combine(AppDataFolderPath, "progs");\r
134                         }\r
135                 }\r
136                 \r
137                 /// <summary>\r
138                 /// アーカイバ方式のパッケージのプログラムグループフォルダー\r
139                 /// </summary>\r
140                 public static string ArchiveProgramGroup {\r
141                         get {\r
142                                 return Path.Combine(AppDataFolderPath, "programs");\r
143                         }\r
144                 }\r
145 \r
146                 /// <summary>\r
147                 /// アーカイバ方式のパッケージのSystem32のフォルダー\r
148                 /// </summary>\r
149                 public static string ArchiveSystem32 {\r
150                         get {\r
151                                 return Path.Combine(ArchiveProgramFiles, ".system32");\r
152                         }\r
153                 }\r
154                 \r
155                 #endregion\r
156                 \r
157                 #region ネットワーク\r
158                 \r
159                 /// <summary>\r
160                 /// ウェブアクセスのためのプロキシを取得する。\r
161                 /// </summary>\r
162                 /// <returns>プロキシ</returns>\r
163                 public static IWebProxy WebProxy\r
164                 {\r
165                         get {\r
166                                 if (pref == null) LoadPref();\r
167                                 \r
168                                 if (string.IsNullOrEmpty(pref.ProxyAddress)) {\r
169                                         // 設定されていないときはシステムのデフォルトを使う\r
170                                         IWebProxy proxy = WebRequest.GetSystemWebProxy();\r
171                                         proxy.Credentials = CredentialCache.DefaultNetworkCredentials;\r
172                                         return proxy;\r
173                                 } else if (pref.ProxyAddress == "-") {\r
174                                         // "-"のとき直接接続\r
175                                         return null;\r
176                                 } else {\r
177                                         // host:portが設定されているならば、それをもとに設定\r
178                                         WebProxy proxy = new WebProxy(pref.ProxyAddress);\r
179                                         proxy.Credentials = CredentialCache.DefaultCredentials;\r
180                                         \r
181                                         // 認証プロキシの場合、認証情報を付加する\r
182                                         try {\r
183                                                 Uri uri = new Uri(pref.ProxyAddress);\r
184                                                 if (! string.IsNullOrEmpty(uri.UserInfo)) {\r
185                                                         int pos = uri.UserInfo.IndexOf(':');\r
186                                                         if (pos >= 0) {\r
187                                                                 proxy.Credentials = new NetworkCredential(\r
188                                                                         uri.UserInfo.Substring(0, pos),\r
189                                                                         uri.UserInfo.Substring(pos+1)\r
190                                                                 );\r
191                                                         } else {\r
192                                                                 proxy.Credentials = CredentialCache.DefaultCredentials;\r
193                                                         }\r
194                                                 }\r
195                                         } catch (Exception) {\r
196                                         }\r
197                                         \r
198                                         return proxy;\r
199                                 }\r
200                         }\r
201                 }\r
202                 \r
203                 /// <summary>\r
204                 /// User-Agent文字列\r
205                 /// </summary>\r
206                 public static string UserAgentString {\r
207                         get {\r
208                                 System.Reflection.Assembly asm = System.Reflection.Assembly.GetExecutingAssembly();\r
209                                 string name = asm.GetName().Name;\r
210                                 Version version = asm.GetName().Version;\r
211                                 \r
212                                 return string.Format("{0}/{1}.{2}.{3}", name, version.Major, version.Minor, version.Build);\r
213                         }\r
214                 }\r
215                 \r
216                 #endregion\r
217                 \r
218                 #region インストールプロセス\r
219                 \r
220                 /// <summary>\r
221                 /// インストーラーをウイルススキャンするか?\r
222                 /// </summary>\r
223                 public static bool EnableScanInstallerFile {\r
224                         get {\r
225                                 if (pref == null) LoadPref();\r
226                                 return pref.EnableScanInstallerFile;\r
227                         }\r
228                         set {\r
229                                 if (pref == null) LoadPref();\r
230                                 pref.EnableScanInstallerFile = value;\r
231                         }\r
232                 }\r
233 \r
234                 /// <summary>\r
235                 /// インストールプロセスをバックグラウンドで行うか?\r
236                 /// </summary>\r
237                 public static bool InstallProcessOnBackground {\r
238                         get {\r
239                                 if (pref == null) LoadPref();\r
240                                 return pref.InstallOnBackground;\r
241                         }\r
242                         set {\r
243                                 if (pref == null) LoadPref();\r
244                                 pref.EnableScanInstallerFile = value;\r
245                         }\r
246                 }\r
247                 \r
248                 /// <summary>\r
249                 /// アップデートから除外するパッケージの名前\r
250                 /// </summary>\r
251                 public static string[] ExcludeUpdatePackageNames {\r
252                         get {\r
253                                 if (pref == null) LoadPref();\r
254                                 return pref.ExcludeUpdatePackageNames;\r
255                         }\r
256                         set {\r
257                                 if (pref == null) LoadPref();\r
258                                 pref.ExcludeUpdatePackageNames = value;\r
259                         }\r
260                 }\r
261                 \r
262                 #endregion\r
263                 \r
264         }\r
265 }\r