OSDN Git Service

initial build for opensource
[applistation/AppliStation.git] / na-get-lib / NaGet.Packages / PackageListsManager.cs
1 using System;\r
2 using System.IO;\r
3 using System.Collections.Generic;\r
4 using NaGet.Packages;\r
5 using NaGet.Packages.Install;\r
6 \r
7 namespace NaGet.Packages\r
8 {\r
9         public class PackageListsManager\r
10         {\r
11                 internal PackageList<Package> availablePkgList;\r
12                 internal PackageList<InstalledPackage> installedPkgList;\r
13                 internal PackageList<InstalledPackage> systemInstalledPkgList;\r
14                 \r
15                 public PackageList<Package> AvailablePkgList {\r
16                         get { return availablePkgList; }\r
17                 }\r
18                 \r
19                 public PackageList<InstalledPackage> InstalledPkgList {\r
20                         get { return installedPkgList; }\r
21                 }\r
22                 \r
23                 public PackageList<InstalledPackage> SystemInstalledPkgList {\r
24                         get { return systemInstalledPkgList; }\r
25                 }\r
26                 \r
27                 private List<InstallationLog> systemInstalledLogList;\r
28                 \r
29                 public PackageListsManager()\r
30                 {\r
31                         LoadPackageLists();\r
32                 }\r
33                 \r
34                 #region \83t\83@\83C\83\8b\93Ç\82Ý\8f\91\82«\r
35                 \r
36                 /// <summary>\r
37                 /// \83t\83@\83C\83\8b\82ð\93Ç\82Ý\8d\9e\82ñ\82Å\8dX\90V\82·\82é\r
38                 /// </summary>\r
39                 public void LoadPackageLists()\r
40                 {\r
41                         try {\r
42                                 availablePkgList = NaGet.Utils.GetDeserializedObject<PackageList<Package>>(NaGet.Env.PackageListFile);\r
43                         } catch (FileNotFoundException) {\r
44                                 availablePkgList = new PackageList<Package>();\r
45                         }\r
46                         \r
47                         try {\r
48                                 installedPkgList = NaGet.Utils.GetDeserializedObject<PackageList<InstalledPackage>>(NaGet.Env.ArchiveInstalledPackageListFile);\r
49                         } catch (FileNotFoundException) {\r
50                                 installedPkgList = new PackageList<InstalledPackage>();\r
51                         }\r
52                         \r
53                         try {\r
54                                 systemInstalledPkgList = NaGet.Utils.GetDeserializedObject<PackageList<InstalledPackage>>(NaGet.Env.SystemInstalledPackageListFile);\r
55                         } catch (FileNotFoundException) {\r
56                                 systemInstalledPkgList = new PackageList<InstalledPackage>();\r
57                         }\r
58                         \r
59                         \r
60                         systemInstalledLogList = new List<InstallationLog>();\r
61                         try {\r
62                                 systemInstalledLogList.AddRange(\r
63                                         NaGet.Utils.GetDeserializedObject<InstallationLog[]>(NaGet.Env.SystemInstalledPackageLogFile)\r
64                                 );\r
65                         } catch (Exception) {\r
66                                 // do nothing\r
67                         }\r
68                 }\r
69                 \r
70                 public void SaveAvailablePackageList()\r
71                 {\r
72                         NaGet.Utils.PutSerializeObject(NaGet.Env.PackageListFile, availablePkgList);\r
73                 }\r
74                 public void SaveInstalledPackageList()\r
75                 {\r
76                         NaGet.Utils.PutSerializeObject(NaGet.Env.ArchiveInstalledPackageListFile, installedPkgList);\r
77                 }\r
78                 public void SaveSystemInstalledPackageList()\r
79                 {\r
80                         NaGet.Utils.PutSerializeObject(NaGet.Env.SystemInstalledPackageListFile, systemInstalledPkgList);\r
81                 }\r
82                 public void SaveSystemInstalledLogList()\r
83                 {\r
84                         NaGet.Utils.PutSerializeObject(NaGet.Env.SystemInstalledPackageLogFile, systemInstalledLogList);\r
85                 }\r
86                 \r
87                 public void CommitToFile()\r
88                 {\r
89                         SaveAvailablePackageList();\r
90                         SaveInstalledPackageList();\r
91                         SaveSystemInstalledPackageList();\r
92                         \r
93                         SaveSystemInstalledLogList();\r
94                 }\r
95                 \r
96                 #endregion\r
97                 \r
98                 #region \83p\83b\83P\81[\83W\83C\83\93\83X\83g\81[\83\8b\8c\9f\92m\8aÖ\98A\r
99                 \r
100                 public void DetectInstalledPkgs()\r
101                 {\r
102                         PackageList<InstalledPackage> pkgList = new PackageList<InstalledPackage>();\r
103                         if (Directory.Exists(NaGet.Env.ArchiveProgramFiles)) {\r
104                                 foreach (string path in NaGet.Utils.ExtendWildcardFile(NaGet.Env.ArchiveProgramFiles, Path.Combine("*", ".applistation.package.xml"))) {\r
105                                         pkgList.AddPackage(NaGet.Utils.GetDeserializedObject<InstalledPackage>(path));\r
106                                 }\r
107                                 this.installedPkgList = pkgList;\r
108                         }\r
109                 }\r
110                 \r
111                 public void DetectSystemInstalledPkgs()\r
112                 {\r
113                         PackageList<InstalledPackage> installedPkgList = new PackageList<InstalledPackage>();\r
114                         foreach (InstalledPackage pkg in RegistriedUninstallers.DetectInstalledPackages(availablePkgList)) {\r
115                                 InstallationLog log = this.GetLogFor(pkg);\r
116                                 if (log != null) {\r
117                                         InstalledPackage pkg2 = new InstalledPackage();\r
118                                         NaGet.Utils.FieldCopy((Package) log.Package, ref pkg2);\r
119                                         pkg2.UninstallInfo = pkg.UninstallInfo;\r
120                                         \r
121                                         installedPkgList.AddPackage(pkg2);\r
122                                 } else {\r
123                                         installedPkgList.AddPackage(pkg);\r
124                                 }\r
125                         }\r
126                         this.systemInstalledPkgList = installedPkgList;\r
127                 }\r
128                 \r
129                 #endregion\r
130                 \r
131                 #region \83p\83b\83P\81[\83W\95Ö\97\98\83\81\83\\83b\83h\r
132                 \r
133                 public IEnumerable<InstalledPackage> GetAllInstalledPackages()\r
134                 {\r
135                         foreach (InstalledPackage pkg in installedPkgList.Packages) {\r
136                                 yield return pkg;\r
137                         }\r
138                         foreach (InstalledPackage pkg in systemInstalledPkgList.Packages) {\r
139                                 yield return pkg;\r
140                         }\r
141                 }\r
142                 \r
143                 /// <summary>\r
144                 /// \91S\82Ä\82Ì\83p\83b\83P\81[\83W\82ð\83C\83e\83\8c\81[\83^\82Å\95Ô\82·\95Ö\97\98\83\81\83\\83b\83h\81B\r
145                 /// </summary>\r
146                 /// <returns>\83p\83b\83P\81[\83W\82Ì\83C\83e\83\8c\81[\83^</returns>\r
147                 public IEnumerable<Package> GetAllPackages()\r
148                 {\r
149                         foreach (Package pkg in availablePkgList.Packages) {\r
150                                 yield return pkg;\r
151                         }\r
152                         foreach (Package pkg in GetAllInstalledPackages()) {\r
153                                 yield return pkg;\r
154                         }\r
155                 }\r
156                 \r
157                 #endregion\r
158                 \r
159                 #region \83C\83\93\83X\83g\81[\83\8b\83\8d\83O\8aÖ\98A\r
160                 \r
161                 private Predicate<InstallationLog> createPackageNamePredicator(Package pkg)\r
162                 {\r
163                         return delegate(InstallationLog log) {\r
164                                 return log.Package.Name == pkg.Name;\r
165                         };\r
166                 }\r
167                 \r
168                 public InstallationLog GetLogFor(Package pkg)\r
169                 {\r
170                         return systemInstalledLogList.Find(createPackageNamePredicator(pkg));\r
171                 }\r
172                 \r
173                 public void WriteInstallationLog(Installation inst)\r
174                 {\r
175                         // \8fd\95¡\82Í\8dí\8f\9c\r
176                         systemInstalledLogList.RemoveAll(createPackageNamePredicator(inst.InstalledPackage));\r
177                         \r
178                         InstallationLog newLog = new InstallationLog();\r
179                         newLog.Date = DateTime.Now;\r
180                         newLog.Package = inst.InstalledPackage;\r
181                         \r
182                         systemInstalledLogList.Add(newLog);\r
183                 }\r
184                 \r
185                 #endregion\r
186         }\r
187 }\r