OSDN Git Service

archive-inst,インストール時に設定ファイルが上書きされてしまうことがある不具合とI/Oエラー時の異常終了を対処
[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                                         // 変更されたファイル(設定ファイル)をキープする処理\r
43                                         foreach (InstalledFile changedFile in list.Verify(targetDir)) {\r
44                                                 string changedFilePath = Path.Combine(targetDir, changedFile.Path);\r
45                                                 string toBeChangedFilePath = Path.Combine(installSrc, changedFile.Path);\r
46                                                 \r
47                                                 // 新規のファイルは退避させる\r
48                                                 if (File.Exists(toBeChangedFilePath)) {\r
49                                                         File.Move(toBeChangedFilePath, toBeChangedFilePath + ".newfile");\r
50                                                 }\r
51                                                 \r
52                                                 // 変更済みファイルをinstallSrcの方に反映させる\r
53                                                 if (File.Exists(changedFilePath)) {\r
54                                                         if (! Directory.Exists(Path.GetDirectoryName(toBeChangedFilePath))) {\r
55                                                                 // フォルダーがない場合は作る\r
56                                                                 Directory.CreateDirectory(Path.GetDirectoryName(toBeChangedFilePath));\r
57                                                         }\r
58                                                         File.Copy(changedFilePath, toBeChangedFilePath);\r
59                                                 }\r
60                                         }\r
61                                 }\r
62                         } else {\r
63                                 Directory.CreateDirectory(targetDir);\r
64                         }\r
65                         \r
66                         // まずはフォルダーツリーを作成\r
67                         foreach (string dir in Directory.GetDirectories(installSrc, "*", SearchOption.AllDirectories)) {\r
68                                 string dirPath = NaGet.Utils.GetRelativePath(installSrc, dir);\r
69                                 string targetDirPath = Path.Combine(targetDir, dirPath);\r
70                                 if (! Directory.Exists(targetDirPath)) {\r
71                                         Directory.CreateDirectory(targetDirPath);\r
72                                 }\r
73                         }\r
74                         \r
75                         // ファイルをインストール(高速化のため移動)\r
76                         foreach (string file in Directory.GetFiles(installSrc, "*", SearchOption.AllDirectories)) {\r
77                                 string filePath = NaGet.Utils.GetRelativePath(installSrc, file);\r
78                                 string targetFilePath = Path.Combine(targetDir, filePath);\r
79                                 if (File.Exists(targetFilePath)) {\r
80                                         File.SetAttributes(targetFilePath, FileAttributes.Normal);\r
81                                         File.Delete(targetFilePath);\r
82                                 }\r
83                                 File.Move(file, targetFilePath);\r
84                         }\r
85                 }\r
86                 \r
87                 private static void postInstall(string targetDir, Package package)\r
88                 {\r
89                         // SYSTEM32へのコピーの実行\r
90                         if (!string.IsNullOrEmpty(package.System32CopyFiles)) {\r
91                                 if (! Directory.Exists(NaGet.Env.ArchiveSystem32)) {\r
92                                         Directory.CreateDirectory(NaGet.Env.ArchiveSystem32);\r
93                                 }\r
94                                 \r
95                                 string logfile = Path.Combine(NaGet.Env.ArchiveSystem32, ".applistation.install."+package.Name+".log");\r
96                                 if (File.Exists(logfile)) {\r
97                                         File.SetAttributes(logfile, FileAttributes.Normal);\r
98                                 }\r
99                                 using (FileStream fs = new FileStream(logfile, FileMode.Create))\r
100                                 using (StreamWriter sw = new StreamWriter(fs)){\r
101                                         foreach (string pathpat in package.System32CopyFiles.Split(';')) {\r
102                                                 foreach (string path in NaGet.Utils.ExtendWildcardFile(targetDir, pathpat)) {\r
103                                                         if ((File.GetAttributes(path) & FileAttributes.Directory) == 0) { // もしファイルならば\r
104                                                                 string destPath = Path.Combine(NaGet.Env.ArchiveSystem32, Path.GetFileName(path));\r
105                                                                 File.Copy(path, destPath, true);\r
106                                                                 \r
107                                                                 sw.WriteLine(Path.GetFileName(destPath));\r
108                                                         }\r
109                                                 }\r
110                                         }\r
111                                 }\r
112                                 File.SetAttributes(logfile, FileAttributes.Hidden);\r
113                         }\r
114                         \r
115                         // インストールスクリプトの実行\r
116                         if (! string.IsNullOrEmpty(package.InstallScript) ) {\r
117                                 Engine engine = MSBuild.Engine;\r
118                                 Project proj = new Project(engine);\r
119                                 try {\r
120                                         proj.LoadXml(package.InstallScript);\r
121                                 } catch (InvalidProjectFileException e) {\r
122                                         throw new ApplicationException("InstallScript is invalid", e);\r
123                                 }\r
124                                 \r
125                                 engine.BuildProject(proj, "Install");\r
126                         }\r
127                         \r
128                         // 直下のファイルで*.exeがGUIプログラムならプログラムフォルダーに登録\r
129                         foreach (string exeFile in Directory.GetFiles(targetDir, "*.exe")) {\r
130                                 if (NaGet.InteropServices.PEFileInfoUtils.GetPEFileType(exeFile) == NaGet.InteropServices.PEFileType.WinGUI) {\r
131                                         string progGrpPath = Path.Combine(NaGet.Env.ArchiveProgramGroup, package.Name);\r
132                                         if (! Directory.Exists(progGrpPath)) Directory.CreateDirectory(progGrpPath);\r
133                                         \r
134                                         using (NaGet.InteropServices.ShellLink lnk = new NaGet.InteropServices.ShellLink() ) {\r
135                                                 //string path = NaGet.Utils.GetRelativePath(progGrpPath, exeFile);// lnkファイルに相対パス指定不能\r
136                                                 string path = exeFile;\r
137                                                 \r
138                                                 lnk.Path = path;\r
139                                                 //lnk.SetIconLocation(path, 0);\r
140                                                 \r
141                                                 // .lnk ファイル名\r
142                                                 string lnkFilePath = Path.Combine(progGrpPath, lnk.GetSuitableShellLinkNameFor() + ".lnk");\r
143                                                 if (File.Exists(lnkFilePath)) { // ファイル名がかぶってしまったとき\r
144                                                         lnkFilePath = Path.Combine(progGrpPath, Path.GetFileNameWithoutExtension(exeFile) + ".lnk");\r
145                                                 }\r
146                                                 \r
147                                                 // 保存\r
148                                                 lnk.ToPersistFile().Save(lnkFilePath, true);\r
149                                         }\r
150                                 }\r
151                         }\r
152                 }\r
153                 \r
154                 private static void storeInstalledFileList(string targetDir)\r
155                 {\r
156                         string installedFileListPath = Path.Combine(targetDir, InstalledFileListName);\r
157                         if (File.Exists(installedFileListPath)) {\r
158                                 File.SetAttributes(installedFileListPath, FileAttributes.Normal);\r
159                         }\r
160                         InstalledFileList installedFileList = InstalledFileList.CreateFromFiles(targetDir);\r
161                         NaGet.Utils.PutSerializeObject(installedFileListPath, installedFileList);\r
162                         \r
163                         File.SetAttributes(installedFileListPath, FileAttributes.Hidden | FileAttributes.ReadOnly);\r
164                 }\r
165                 \r
166                 private static void storePackageXml(Package package, string destDir)\r
167                 {\r
168                         if (package == null) {\r
169                                 return;\r
170                         }\r
171                         \r
172                         InstalledPackage pkg = InstalledPackage.PackageConverter(package);\r
173                         UninstallInformation uninfo = pkg.UninstallInfo;\r
174                         uninfo.InstallLocation = destDir;\r
175                         uninfo.UninstallString = string.Format("archive-inst -x \"{0}\"", package.Name);\r
176                         uninfo.EstimatedSize = (int) (NaGet.Utils.GetFileSize(destDir) >> 10);\r
177                         uninfo.InstallDateString = DateTime.Now.ToString("yyyyMMdd");\r
178                         pkg.UninstallInfo = uninfo;\r
179                         \r
180                         string packageXmlFilePath = Path.Combine(destDir, InstalledPackageFileName);\r
181                         if (File.Exists(packageXmlFilePath)) {\r
182                                 File.SetAttributes(packageXmlFilePath, FileAttributes.Normal);\r
183                         }\r
184                         NaGet.Utils.PutSerializeObject(packageXmlFilePath, pkg);\r
185                         File.SetAttributes(packageXmlFilePath, FileAttributes.Hidden | FileAttributes.ReadOnly);\r
186                 }\r
187                 \r
188                 private static void removePackage(InstalledPackage package, string targetDir)\r
189                 {\r
190                         // アンインストールスクリプトの実行\r
191                         if (! string.IsNullOrEmpty(package.InstallScript)) {\r
192                                 Engine engine = MSBuild.Engine;\r
193                                 Project proj = new Project(engine);\r
194                                 try {\r
195                                         proj.LoadXml(package.InstallScript);\r
196                                 } catch (InvalidProjectFileException e) {\r
197                                         throw new ApplicationException("InstallScript is invalid", e);\r
198                                 }\r
199                                 \r
200                                 engine.BuildProject(proj, "Uninstall");\r
201                         }\r
202                         \r
203                         // GUIプログラムでプログラムフォルダーに登録しているのを解除\r
204                         string progGrpPath = Path.Combine(NaGet.Env.ArchiveProgramGroup, package.Name);\r
205                         if (Directory.Exists(progGrpPath)) {\r
206                                 NaGet.Utils.SetAttributeRecursive(progGrpPath, FileAttributes.Normal);\r
207                                 Directory.Delete(progGrpPath, true);\r
208                         }\r
209                         \r
210                         // SYSTEM32からの削除の実行\r
211                         if (! string.IsNullOrEmpty(package.System32CopyFiles) ) {\r
212                                 string logfile = Path.Combine(NaGet.Env.ArchiveSystem32, ".applistation.install."+package.Name+".log");\r
213                                 \r
214                                 if (File.Exists(logfile)) {\r
215                                         using (FileStream fs = new FileStream(logfile, FileMode.Open))\r
216                                         using (StreamReader sr = new StreamReader(fs)){\r
217                                                 string fileName = sr.ReadLine().Trim();\r
218                                                 string filePath = Path.Combine(NaGet.Env.ArchiveSystem32, fileName);\r
219                                                 \r
220                                                 if (File.Exists(filePath)) {\r
221                                                         File.SetAttributes(filePath, FileAttributes.Normal);\r
222                                                         File.Delete(filePath);\r
223                                                 }\r
224                                         }\r
225                                         File.SetAttributes(logfile, FileAttributes.Normal);\r
226                                         File.Delete(logfile);\r
227                                 }\r
228                         }\r
229                         \r
230                         NaGet.Utils.SetAttributeRecursive(targetDir, FileAttributes.Normal);\r
231                         Directory.Delete(targetDir, true);\r
232                 }\r
233                 \r
234                 private static void parseMainArguments(string[] args, out string arcFile, out string targetDir, out Package package)\r
235                 {\r
236                         if (args.Length < 1) {\r
237                                 throw new ArgumentException();\r
238                         }\r
239                         \r
240                         switch (args[0].ToLower()) {\r
241                                 case "-t":\r
242                                         if (args.Length != 3) {\r
243                                                 throw new ArgumentException();\r
244                                         }\r
245                                         \r
246                                         arcFile = args[1];\r
247                                         targetDir = args[2];\r
248                                         package = null;\r
249                                         break;\r
250                                 case "-i":\r
251                                         if (args.Length != 3) {\r
252                                                 throw new ArgumentException();\r
253                                         }\r
254                                         \r
255                                         arcFile = args[1];\r
256                                         PackageList<Package> pkgList = NaGet.Utils.GetDeserializedObject<PackageList<Package>>(NaGet.Env.PackageListFile);\r
257                                         package = pkgList.GetPackageForName(args[2]);\r
258                                         targetDir = Path.Combine(NaGet.Env.ArchiveProgramFiles, package.Name);\r
259                                         break;\r
260                                 case "-x":\r
261                                         if (args.Length != 2) {\r
262                                                 throw new ArgumentException();\r
263                                         }\r
264                                         \r
265                                         arcFile = null;\r
266                                         targetDir = Path.Combine(NaGet.Env.ArchiveProgramFiles, args[1]);\r
267                                         package = null;\r
268                                         \r
269                                         string filepath = Path.Combine(targetDir, InstalledPackageFileName);\r
270                                         if (File.Exists(filepath)) {\r
271                                                 package = NaGet.Utils.GetDeserializedObject<InstalledPackage>(filepath);\r
272                                         } else {\r
273                                                 Console.Error.WriteLine("Not found or already removed package : {0}", args[1]);\r
274                                                 Environment.Exit(100);\r
275                                         }\r
276                                         break;\r
277                                 default:\r
278                                         arcFile = null;\r
279                                         targetDir = null;\r
280                                         package = null;\r
281                                         Console.Error.WriteLine("Unreconized command \"{0}\".", args[0]);\r
282                                         Environment.Exit(100);\r
283                                         break;\r
284                         }\r
285                 }\r
286                 \r
287                 [STAThread]\r
288                 public static void Main(string[] args)\r
289                 {\r
290                         // アーカイブSYSTEM32をパスに足す\r
291                         NaGet.Utils.AddDirectoryToPath(NaGet.Env.ArchiveSystem32);\r
292                         \r
293                         string arcFile = null;\r
294                         string targetDir = null;\r
295                         Package package = null;\r
296                         \r
297                         // 引数パースおよびヘルプ表示\r
298                         try {\r
299                                 parseMainArguments(args, out arcFile, out targetDir, out package);\r
300                         } catch (ArgumentException) {\r
301                                 string executeFileName = System.AppDomain.CurrentDomain.FriendlyName;\r
302                                 Console.Write("Usage:");\r
303                                 Console.WriteLine("\t{0} -t archive.zip target_dir\tExtraction", executeFileName);\r
304                                 Console.WriteLine("\t{0} -i archive.zip PackageName\tInstall", executeFileName);\r
305                                 Console.WriteLine("\t{0} -x PackageName\t\tUninstall", executeFileName);\r
306                                 Console.WriteLine();\r
307                         }\r
308 \r
309                         // インストールおよび展開処理\r
310                         if (arcFile != null) {\r
311                                 string tempExtractDir = targetDir + "___temp___"; // HACK\r
312                                 Directory.CreateDirectory(tempExtractDir);\r
313                                 \r
314                                 try {\r
315                                         // STEP1. 書庫の展開\r
316                                         if (package != null && package.Type == InstallerType.ITSELF) {\r
317                                                 // 書庫でない場合展開せずにそのままコピーする\r
318                                                 string destFile = Path.Combine(tempExtractDir, Path.GetFileName(arcFile));\r
319                                                 File.Copy(arcFile, destFile);\r
320                                         } else {\r
321                                                 extract(arcFile, tempExtractDir);\r
322                                         }\r
323                                         \r
324                                         // STEP2. 展開フォルダでインストールファイルリストの作成\r
325                                         storeInstalledFileList(tempExtractDir);\r
326                                         \r
327                                         // STEP3. インストール\r
328                                         install(tempExtractDir, targetDir);\r
329                                         \r
330                                         if (package != null) {\r
331                                                 // STEP4. カスタマイズ可能な後処理\r
332                                                 postInstall(targetDir, package);\r
333                                                 \r
334                                                 // STEP5. パッケージ情報をインストール先(targetDir)に置く\r
335                                                 storePackageXml(package, targetDir);\r
336                                         }\r
337                                 } catch (DllNotFoundException) {\r
338                                         Console.Error.WriteLine("E: Does not exist archive dll for {0}", arcFile); // TODO\r
339                                         Environment.Exit(10);\r
340                                 } catch (IOException e) {\r
341                                         Console.Error.WriteLine("E: File I/O Error : {0}", e.Message);\r
342                                         Environment.Exit(1);\r
343                                 } finally {\r
344                                         Directory.Delete(tempExtractDir, true);\r
345                                 }\r
346                         } else if (package is InstalledPackage) {\r
347                                 removePackage((InstalledPackage) package, targetDir);\r
348                         }\r
349                 }\r
350         }\r
351 }\r