OSDN Git Service

na-get-lib,レポジトリのUninstallKeyが不正に空だったときエラーが発生するのを修正
[applistation/AppliStation.git] / archive-inst / Program.cs
1 using System;\r
2 using System.Text;\r
3 using System.IO;\r
4 using Microsoft.Build.BuildEngine;\r
5 using NaGet.Packages;\r
6 using NaGet.Packages.Install;\r
7 \r
8 namespace ArchiveInstall\r
9 {\r
10         class Program\r
11         {\r
12                 public const string InstalledFileListName = ".applistation.installedfiles.xml";\r
13                 \r
14                 public const string InstalledPackageFileName = ".applistation.package.xml";\r
15                 \r
16                 private static void extract(string arcFile, string extractDestDir)\r
17                 {\r
18                         StringBuilder output = new StringBuilder(1024);\r
19                         int res = NaGet.InteropServices.CommonArchiverExtracter.ExtractArchive(arcFile, extractDestDir, output, IntPtr.Zero);\r
20                         if (res != 0) {\r
21                                 Environment.Exit(res);\r
22                         }\r
23                 }\r
24                 \r
25                 private static void install(string fakeTargetDir, string targetDir)\r
26                 {\r
27                         string installSrc = fakeTargetDir;\r
28                         \r
29                         if (Directory.GetFiles(installSrc).Length == 0 &&\r
30                             Directory.GetDirectories(installSrc).Length == 1) {\r
31                                 installSrc = Directory.GetDirectories(installSrc)[0];\r
32                         }\r
33                         \r
34                         // ハッシュ比較\r
35                         if (Directory.Exists(targetDir)) {\r
36                                 InstalledFileList list = null;\r
37                                 try {\r
38                                         list = NaGet.Utils.GetDeserializedObject<InstalledFileList>(Path.Combine(targetDir, InstalledFileListName));\r
39                                 } catch {\r
40                                 }\r
41                                 if (list != null) {\r
42                                         foreach (InstalledFile changedFile in list.Verify(targetDir)) { // 変更されたファイルたちを。。。\r
43                                                 string changedFilePath = Path.Combine(targetDir, changedFile.Path);\r
44                                                 string toBeChangedFilePath = Path.Combine(installSrc, changedFile.Path);\r
45                                                 \r
46                                                 if (File.Exists(toBeChangedFilePath)) {\r
47                                                         File.Move(toBeChangedFilePath, toBeChangedFilePath + ".newfile"); // 新規を退避させて\r
48                                                 }\r
49                                                 File.Copy(changedFilePath, toBeChangedFilePath); // installSrcの方に反映させておく\r
50                                         }\r
51                                 }\r
52                         } else {\r
53                                 Directory.CreateDirectory(targetDir);\r
54                         }\r
55                         \r
56                         // まずはフォルダツリーを作成\r
57                         foreach (string dir in Directory.GetDirectories(installSrc, "*", SearchOption.AllDirectories)) {\r
58                                 string dirPath = NaGet.Utils.GetRelativePath(installSrc, dir);\r
59                                 string targetDirPath = Path.Combine(targetDir, dirPath);\r
60                                 if (! Directory.Exists(targetDirPath)) {\r
61                                         Directory.CreateDirectory(targetDirPath);\r
62                                 }\r
63                         }\r
64                         \r
65                         // ファイルをインストール(高速化のため移動)\r
66                         foreach (string file in Directory.GetFiles(installSrc, "*", SearchOption.AllDirectories)) {\r
67                                 string filePath = NaGet.Utils.GetRelativePath(installSrc, file);\r
68                                 string targetFilePath = Path.Combine(targetDir, filePath);\r
69                                 if (File.Exists(targetFilePath)) {\r
70                                         File.Delete(targetFilePath);\r
71                                 }\r
72                                 File.Move(file, targetFilePath);\r
73                         }\r
74                 }\r
75                 \r
76                 private static void postInstall(string targetDir, Package package)\r
77                 {\r
78                         // SYSTEM32へのコピーの実行\r
79                         if (!string.IsNullOrEmpty(package.System32CopyFiles)) {\r
80                                 if (! Directory.Exists(NaGet.Env.ArchiveSystem32)) {\r
81                                         Directory.CreateDirectory(NaGet.Env.ArchiveSystem32);\r
82                                 }\r
83                                 \r
84                                 string logfile = Path.Combine(NaGet.Env.ArchiveSystem32, ".applistation.install."+package.Name+".log");\r
85                                 if (File.Exists(logfile)) {\r
86                                         File.SetAttributes(logfile, FileAttributes.Normal);\r
87                                 }\r
88                                 using (FileStream fs = new FileStream(logfile, FileMode.Create))\r
89                                 using (StreamWriter sw = new StreamWriter(fs)){\r
90                                         foreach (string pathpat in package.System32CopyFiles.Split(';')) {\r
91                                                 foreach (string path in NaGet.Utils.ExtendWildcardFile(targetDir, pathpat)) {\r
92                                                         if ((File.GetAttributes(path) & FileAttributes.Directory) == 0) { // もしファイルならば\r
93                                                                 string destPath = Path.Combine(NaGet.Env.ArchiveSystem32, Path.GetFileName(path));\r
94                                                                 File.Copy(path, destPath, true);\r
95                                                                 \r
96                                                                 sw.WriteLine(Path.GetFileName(destPath));\r
97                                                         }\r
98                                                 }\r
99                                         }\r
100                                 }\r
101                                 File.SetAttributes(logfile, FileAttributes.Hidden);\r
102                         }\r
103                         \r
104                         // インストールスクリプトの実行\r
105                         if (! string.IsNullOrEmpty(package.InstallScript) ) {\r
106                                 Engine engine = MSBuild.Engine;\r
107                                 Project proj = new Project(engine);\r
108                                 try {\r
109                                         proj.LoadXml(package.InstallScript);\r
110                                 } catch (InvalidProjectFileException e) {\r
111                                         throw new ApplicationException("InstallScript is invalid", e);\r
112                                 }\r
113                                 \r
114                                 engine.BuildProject(proj, "Install");\r
115                         }\r
116                         \r
117                         // 直下のファイルで*.exeがGUIプログラムならプログラムフォルダに登録\r
118                         foreach (string exeFile in Directory.GetFiles(targetDir, "*.exe")) {\r
119                                 if (NaGet.InteropServices.PEFileInfoUtils.GetPEFileType(exeFile) == NaGet.InteropServices.PEFileInfoUtils.PEFileType.WinGUI) {\r
120                                         string progGrpPath = Path.Combine(NaGet.Env.ArchiveProgramGroup, package.Name);\r
121                                         if (! Directory.Exists(progGrpPath)) Directory.CreateDirectory(progGrpPath);\r
122                                         \r
123                                         using (NaGet.InteropServices.ShellLink lnk = new NaGet.InteropServices.ShellLink() ) {\r
124                                                 //string path = NaGet.Utils.GetRelativePath(progGrpPath, exeFile);// lnkファイルに相対パス指定不能\r
125                                                 string path = exeFile;\r
126                                                 \r
127                                                 lnk.Path = path;\r
128                                                 //lnk.SetIconLocation(path, 0);\r
129                                                 \r
130                                                 // .lnk ファイル名\r
131                                                 string lnkFilePath = Path.Combine(progGrpPath, lnk.GetSuitableShellLinkNameFor() + ".lnk");\r
132                                                 if (File.Exists(lnkFilePath)) { // ファイル名がかぶってしまったとき\r
133                                                         lnkFilePath = Path.Combine(progGrpPath, Path.GetFileNameWithoutExtension(exeFile) + ".lnk");\r
134                                                 }\r
135                                                 \r
136                                                 // 保存\r
137                                                 lnk.ToPersistFile().Save(lnkFilePath, true);\r
138                                         }\r
139                                 }\r
140                         }\r
141                 }\r
142                 \r
143                 private static void storeInstalledFileList(string targetDir)\r
144                 {\r
145                         string installedFileListPath = Path.Combine(targetDir, InstalledFileListName);\r
146                         if (File.Exists(installedFileListPath)) {\r
147                                 File.SetAttributes(installedFileListPath, FileAttributes.Normal);\r
148                         }\r
149                         InstalledFileList installedFileList = InstalledFileList.CreateFromFiles(targetDir);\r
150                         NaGet.Utils.PutSerializeObject(installedFileListPath, installedFileList);\r
151                         \r
152                         File.SetAttributes(installedFileListPath, FileAttributes.Hidden | FileAttributes.ReadOnly);\r
153                 }\r
154                 \r
155                 private static void storePackageXml(Package package, string destDir)\r
156                 {\r
157                         if (package == null) {\r
158                                 return;\r
159                         }\r
160                         \r
161                         InstalledPackage pkg = InstalledPackage.PackageConverter(package);\r
162                         UninstallInformation uninfo = pkg.UninstallInfo;\r
163                         uninfo.InstallLocation = destDir;\r
164                         uninfo.UninstallString = string.Format("archive-inst -x \"{0}\"", package.Name);\r
165                         uninfo.EstimatedSize = (int) (NaGet.Utils.GetFileSize(destDir) >> 10);\r
166                         uninfo.InstallDateString = DateTime.Now.ToString("yyyyMMdd");\r
167                         pkg.UninstallInfo = uninfo;\r
168                         \r
169                         string packageXmlFilePath = Path.Combine(destDir, InstalledPackageFileName);\r
170                         if (File.Exists(packageXmlFilePath)) {\r
171                                 File.SetAttributes(packageXmlFilePath, FileAttributes.Normal);\r
172                         }\r
173                         NaGet.Utils.PutSerializeObject(packageXmlFilePath, pkg);\r
174                         File.SetAttributes(packageXmlFilePath, FileAttributes.Hidden | FileAttributes.ReadOnly);\r
175                 }\r
176                 \r
177                 private static void removePackage(InstalledPackage package, string targetDir)\r
178                 {\r
179                         // アンインストールスクリプトの実行\r
180                         if (! string.IsNullOrEmpty(package.InstallScript)) {\r
181                                 Engine engine = MSBuild.Engine;\r
182                                 Project proj = new Project(engine);\r
183                                 try {\r
184                                         proj.LoadXml(package.InstallScript);\r
185                                 } catch (InvalidProjectFileException e) {\r
186                                         throw new ApplicationException("InstallScript is invalid", e);\r
187                                 }\r
188                                 \r
189                                 engine.BuildProject(proj, "Uninstall");\r
190                         }\r
191                         \r
192                         // GUIプログラムでプログラムフォルダに登録しているのを解除\r
193                         string progGrpPath = Path.Combine(NaGet.Env.ArchiveProgramGroup, package.Name);\r
194                         if (Directory.Exists(progGrpPath)) {\r
195                                 NaGet.Utils.SetAttributeRecursive(progGrpPath, FileAttributes.Normal);\r
196                                 Directory.Delete(progGrpPath, true);\r
197                         }\r
198                         \r
199                         // SYSTEM32からの削除の実行\r
200                         if (! string.IsNullOrEmpty(package.System32CopyFiles) ) {\r
201                                 string logfile = Path.Combine(NaGet.Env.ArchiveSystem32, ".applistation.install."+package.Name+".log");\r
202                                 \r
203                                 if (File.Exists(logfile)) {\r
204                                         using (FileStream fs = new FileStream(logfile, FileMode.Open))\r
205                                         using (StreamReader sr = new StreamReader(fs)){\r
206                                                 string fileName = sr.ReadLine().Trim();\r
207                                                 string filePath = Path.Combine(NaGet.Env.ArchiveSystem32, fileName);\r
208                                                 \r
209                                                 if (File.Exists(filePath)) {\r
210                                                         File.SetAttributes(filePath, FileAttributes.Normal);\r
211                                                         File.Delete(filePath);\r
212                                                 }\r
213                                         }\r
214                                         File.SetAttributes(logfile, FileAttributes.Normal);\r
215                                         File.Delete(logfile);\r
216                                 }\r
217                         }\r
218                         \r
219                         NaGet.Utils.SetAttributeRecursive(targetDir, FileAttributes.Normal);\r
220                         Directory.Delete(targetDir, true);\r
221                 }\r
222                 \r
223                 private static void parseMainArguments(string[] args, out string arcFile, out string targetDir, out Package package)\r
224                 {\r
225                         if (args.Length < 1) {\r
226                                 throw new ArgumentException();\r
227                         }\r
228                         \r
229                         switch (args[0].ToLower()) {\r
230                                 case "-t":\r
231                                         if (args.Length != 3) {\r
232                                                 throw new ArgumentException();\r
233                                         }\r
234                                         \r
235                                         arcFile = args[1];\r
236                                         targetDir = args[2];\r
237                                         package = null;\r
238                                         break;\r
239                                 case "-i":\r
240                                         if (args.Length != 3) {\r
241                                                 throw new ArgumentException();\r
242                                         }\r
243                                         \r
244                                         arcFile = args[1];\r
245                                         PackageList<Package> pkgList = NaGet.Utils.GetDeserializedObject<PackageList<Package>>(NaGet.Env.PackageListFile);\r
246                                         package = pkgList.GetPackageForName(args[2]);\r
247                                         targetDir = Path.Combine(NaGet.Env.ArchiveProgramFiles, package.Name);\r
248                                         break;\r
249                                 case "-x":\r
250                                         if (args.Length != 2) {\r
251                                                 throw new ArgumentException();\r
252                                         }\r
253                                         \r
254                                         arcFile = null;\r
255                                         targetDir = Path.Combine(NaGet.Env.ArchiveProgramFiles, args[1]);\r
256                                         package = null;\r
257                                         \r
258                                         string filepath = Path.Combine(targetDir, InstalledPackageFileName);\r
259                                         if (File.Exists(filepath)) {\r
260                                                 package = NaGet.Utils.GetDeserializedObject<InstalledPackage>(filepath);\r
261                                         } else {\r
262                                                 Console.Error.WriteLine("Not found or already removed package : {0}", args[1]);\r
263                                                 Environment.Exit(100);\r
264                                         }\r
265                                         break;\r
266                                 default:\r
267                                         arcFile = null;\r
268                                         targetDir = null;\r
269                                         package = null;\r
270                                         Console.Error.WriteLine("Unreconized command \"{0}\".", args[0]);\r
271                                         Environment.Exit(100);\r
272                                         break;\r
273                         }\r
274                 }\r
275                 \r
276                 [STAThread]\r
277                 public static void Main(string[] args)\r
278                 {\r
279                         // アーカイブSYSTEM32をパスに足す\r
280                         NaGet.Utils.AddDirectoryToPath(NaGet.Env.ArchiveSystem32);\r
281                         \r
282                         string arcFile = null;\r
283                         string targetDir = null;\r
284                         Package package = null;\r
285                         \r
286                         try {\r
287                                 parseMainArguments(args, out arcFile, out targetDir, out package);\r
288                         } catch (ArgumentException) {\r
289                                 string executeFileName = System.AppDomain.CurrentDomain.FriendlyName;\r
290                                 Console.Write("Usage:");\r
291                                 Console.WriteLine("\t{0} -t archive.zip target_dir\tExtraction", executeFileName);\r
292                                 Console.WriteLine("\t{0} -i archive.zip PackageName\tInstall", executeFileName);\r
293                                 Console.WriteLine("\t{0} -x PackageName\t\tUninstall", executeFileName);\r
294                                 Console.WriteLine();\r
295                         }\r
296                                                 \r
297                         if (arcFile != null) { // install or extract    \r
298                                 string tempExtractDir = targetDir + "___temp___"; // HACK \r
299                                 Directory.CreateDirectory(tempExtractDir);\r
300                                 \r
301                                 try {\r
302                                         extract(arcFile, tempExtractDir);\r
303                                         \r
304                                         install(tempExtractDir, targetDir);\r
305                                         \r
306                                         if (package != null) {\r
307                                                 postInstall(targetDir, package);\r
308                                                 \r
309                                                 storeInstalledFileList(targetDir);\r
310                                                 storePackageXml(package, targetDir);\r
311                                         }\r
312                                 } catch (DllNotFoundException) {\r
313                                         Console.Error.WriteLine("E: Does not exist archive dll for {0}", arcFile); // TODO\r
314                                         Environment.Exit(10);\r
315                                 } finally {\r
316                                         Directory.Delete(tempExtractDir, true);\r
317                                 }\r
318                         } else if (package is InstalledPackage) {\r
319                                 removePackage((InstalledPackage) package, targetDir);\r
320                         }\r
321                 }\r
322         }\r
323 }\r