OSDN Git Service

na-get-lib,タスクセットの改修(NaGetTaskSet2)および、そのUnitTestを追加
[applistation/AppliStation.git] / na-get-lib / NaGet.Packages.Install / InstalledPackage.cs
1 using System;\r
2 using System.IO;\r
3 using System.Collections.Generic;\r
4 using System.Xml.Serialization;\r
5 using System.Text.RegularExpressions;\r
6 using NaGet.Packages;\r
7 \r
8 namespace NaGet.Packages.Install\r
9 {       \r
10         public class InstalledPackage : Package\r
11         {\r
12                 /// <summary>\r
13                 /// アンインストール情報\r
14                 /// </summary>\r
15                 public UninstallInformation UninstallInfo;\r
16                 \r
17                 /// <summary>\r
18                 /// インストール先フォルダーを返す。\r
19                 /// <code>UninstallInfo.InstallLocation</code>が空ならば、\r
20                 /// 解決を試みる。\r
21                 /// </summary>\r
22                 /// <returns>インストール先フォルダー。見つからなかったら<code>null</code></returns>\r
23                 public string DiscoverInstalledLocation()\r
24                 {\r
25                         if (string.IsNullOrEmpty(UninstallInfo.InstallLocation)) {\r
26                                 // ○○/名前を探す\r
27                                 string[] suggestions = new string[3];\r
28                                 suggestions[0] = Environment.GetEnvironmentVariable("ProgramFiles");\r
29                                 if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("ProgramFiles(x86)"))) {\r
30                                         suggestions[1] = Environment.GetEnvironmentVariable("ProgramFiles(x86)");\r
31                                 }\r
32                                 suggestions[2] = Path.GetPathRoot(suggestions[0]);\r
33 \r
34                                 foreach (string suggest in suggestions) {\r
35                                         if (!string.IsNullOrEmpty(suggest)) {\r
36                                                 string path = Path.Combine(suggest, this.Name);\r
37                                                 if (Directory.Exists(path)) return path;\r
38                                         }\r
39                                 }\r
40                                 \r
41                                 return null; // when not found\r
42                         } else {\r
43                                 return UninstallInfo.InstallLocation;\r
44                         }\r
45                 }\r
46                 \r
47                 public static InstalledPackage PackageConverter(Package basePkg) \r
48                 {\r
49                         InstalledPackage pkg = new InstalledPackage();\r
50                         \r
51                         NaGet.Utils.FieldCopy(basePkg, ref pkg);\r
52                         pkg.UninstallInfo = UninstallInformation.NewInstance(basePkg);\r
53                         \r
54                         return pkg;\r
55                 }\r
56                 \r
57                 public static InstalledPackage PackageConverter(Package basePkg, UninstallInformation info)\r
58                 {\r
59                         InstalledPackage instPkg = new InstalledPackage();\r
60                         \r
61                         NaGet.Utils.FieldCopy(basePkg, ref instPkg);\r
62                         instPkg.UninstallInfo = info;\r
63                         \r
64                         Match match = Regex.Match(info.DisplayName, basePkg.UninstallerKey);\r
65                         if (! match.Success) {\r
66                                 throw new ArgumentException(string.Format("{0}({1}) does not matched for {2}.", basePkg.Name, basePkg.Version, info.DisplayName));\r
67                         } else if (match.Groups[1].Success) { // DisplayNameの方のバージョン表記を優先\r
68                                 instPkg.Version = match.Groups[1].Value;\r
69                         } else if (! string.IsNullOrEmpty(info.DisplayVersion) ) {\r
70                                 instPkg.Version = info.DisplayVersion;\r
71                         } else {\r
72                                 instPkg.Version = string.Empty;\r
73                         }\r
74                         \r
75                         return instPkg;\r
76                 }\r
77         }\r
78 }\r