OSDN Git Service

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