4 using Microsoft.Build.BuildEngine;
\r
5 using NaGet.Packages;
\r
6 using NaGet.Packages.Install;
\r
8 namespace ArchiveInstall
\r
12 public const string InstalledFileListName = ".applistation.installedfiles.xml";
\r
14 public const string InstalledPackageFileName = ".applistation.package.xml";
\r
16 private static void extract(string arcFile, string extractDestDir)
\r
18 StringBuilder output = new StringBuilder(1024);
\r
19 int res = NaGet.InteropServices.CommonArchiverExtracter.ExtractArchive(arcFile, extractDestDir, output, IntPtr.Zero);
\r
21 Environment.Exit(res);
\r
25 private static void install(string fakeTargetDir, string targetDir)
\r
27 string installSrc = fakeTargetDir;
\r
29 if (Directory.GetFiles(installSrc).Length == 0 &&
\r
30 Directory.GetDirectories(installSrc).Length == 1) {
\r
31 installSrc = Directory.GetDirectories(installSrc)[0];
\r
35 if (Directory.Exists(targetDir)) {
\r
36 InstalledFileList list = null;
\r
38 list = NaGet.Utils.GetDeserializedObject<InstalledFileList>(Path.Combine(targetDir, InstalledFileListName));
\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
48 if (File.Exists(toBeChangedFilePath)) {
\r
49 File.Move(toBeChangedFilePath, toBeChangedFilePath + ".newfile");
\r
52 // 変更済みファイルをinstallSrcの方に反映させる
\r
53 if (File.Exists(changedFilePath)) {
\r
54 if (! Directory.Exists(Path.GetDirectoryName(toBeChangedFilePath))) {
\r
56 Directory.CreateDirectory(Path.GetDirectoryName(toBeChangedFilePath));
\r
58 File.Copy(changedFilePath, toBeChangedFilePath);
\r
63 Directory.CreateDirectory(targetDir);
\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
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
83 File.Move(file, targetFilePath);
\r
87 private static void postInstall(string targetDir, Package package)
\r
90 if (!string.IsNullOrEmpty(package.System32CopyFiles)) {
\r
91 if (! Directory.Exists(NaGet.Env.ArchiveSystem32)) {
\r
92 Directory.CreateDirectory(NaGet.Env.ArchiveSystem32);
\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
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
107 sw.WriteLine(Path.GetFileName(destPath));
\r
112 File.SetAttributes(logfile, FileAttributes.Hidden);
\r
116 if (! string.IsNullOrEmpty(package.InstallScript) ) {
\r
117 Engine engine = MSBuild.Engine;
\r
118 Project proj = new Project(engine);
\r
120 proj.LoadXml(package.InstallScript);
\r
121 } catch (InvalidProjectFileException e) {
\r
122 throw new ApplicationException("InstallScript is invalid", e);
\r
125 engine.BuildProject(proj, "Install");
\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
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
139 //lnk.SetIconLocation(path, 0);
\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
148 lnk.ToPersistFile().Save(lnkFilePath, true);
\r
154 private static void storeInstalledFileList(string targetDir)
\r
156 string installedFileListPath = Path.Combine(targetDir, InstalledFileListName);
\r
157 if (File.Exists(installedFileListPath)) {
\r
158 File.SetAttributes(installedFileListPath, FileAttributes.Normal);
\r
160 InstalledFileList installedFileList = InstalledFileList.CreateFromFiles(targetDir);
\r
161 NaGet.Utils.PutSerializeObject(installedFileListPath, installedFileList);
\r
163 File.SetAttributes(installedFileListPath, FileAttributes.Hidden | FileAttributes.ReadOnly);
\r
166 private static void storePackageXml(Package package, string destDir)
\r
168 if (package == null) {
\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
180 string packageXmlFilePath = Path.Combine(destDir, InstalledPackageFileName);
\r
181 if (File.Exists(packageXmlFilePath)) {
\r
182 File.SetAttributes(packageXmlFilePath, FileAttributes.Normal);
\r
184 NaGet.Utils.PutSerializeObject(packageXmlFilePath, pkg);
\r
185 File.SetAttributes(packageXmlFilePath, FileAttributes.Hidden | FileAttributes.ReadOnly);
\r
188 private static void removePackage(InstalledPackage package, string targetDir)
\r
190 // アンインストールスクリプトの実行
\r
191 if (! string.IsNullOrEmpty(package.InstallScript)) {
\r
192 Engine engine = MSBuild.Engine;
\r
193 Project proj = new Project(engine);
\r
195 proj.LoadXml(package.InstallScript);
\r
196 } catch (InvalidProjectFileException e) {
\r
197 throw new ApplicationException("InstallScript is invalid", e);
\r
200 engine.BuildProject(proj, "Uninstall");
\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
210 // SYSTEM32からの削除の実行
\r
211 if (! string.IsNullOrEmpty(package.System32CopyFiles) ) {
\r
212 string logfile = Path.Combine(NaGet.Env.ArchiveSystem32, ".applistation.install."+package.Name+".log");
\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
220 if (File.Exists(filePath)) {
\r
221 File.SetAttributes(filePath, FileAttributes.Normal);
\r
222 File.Delete(filePath);
\r
225 File.SetAttributes(logfile, FileAttributes.Normal);
\r
226 File.Delete(logfile);
\r
230 NaGet.Utils.SetAttributeRecursive(targetDir, FileAttributes.Normal);
\r
231 Directory.Delete(targetDir, true);
\r
234 private static void parseMainArguments(string[] args, out string arcFile, out string targetDir, out Package package)
\r
236 if (args.Length < 1) {
\r
237 throw new ArgumentException();
\r
240 switch (args[0].ToLower()) {
\r
242 if (args.Length != 3) {
\r
243 throw new ArgumentException();
\r
247 targetDir = args[2];
\r
251 if (args.Length != 3) {
\r
252 throw new ArgumentException();
\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
261 if (args.Length != 2) {
\r
262 throw new ArgumentException();
\r
266 targetDir = Path.Combine(NaGet.Env.ArchiveProgramFiles, args[1]);
\r
269 string filepath = Path.Combine(targetDir, InstalledPackageFileName);
\r
270 if (File.Exists(filepath)) {
\r
271 package = NaGet.Utils.GetDeserializedObject<InstalledPackage>(filepath);
\r
273 Console.Error.WriteLine("Not found or already removed package : {0}", args[1]);
\r
274 Environment.Exit(100);
\r
281 Console.Error.WriteLine("Unreconized command \"{0}\".", args[0]);
\r
282 Environment.Exit(100);
\r
288 public static void Main(string[] args)
\r
290 // アーカイブSYSTEM32をパスに足す
\r
291 NaGet.Utils.AddDirectoryToPath(NaGet.Env.ArchiveSystem32);
\r
293 string arcFile = null;
\r
294 string targetDir = null;
\r
295 Package package = null;
\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
310 if (arcFile != null) {
\r
311 string tempExtractDir = targetDir + "___temp___"; // HACK
\r
312 Directory.CreateDirectory(tempExtractDir);
\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
321 extract(arcFile, tempExtractDir);
\r
324 // STEP2. 展開フォルダでインストールファイルリストの作成
\r
325 storeInstalledFileList(tempExtractDir);
\r
328 install(tempExtractDir, targetDir);
\r
330 if (package != null) {
\r
331 // STEP4. カスタマイズ可能な後処理
\r
332 postInstall(targetDir, package);
\r
334 // STEP5. パッケージ情報をインストール先(targetDir)に置く
\r
335 storePackageXml(package, targetDir);
\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
344 Directory.Delete(tempExtractDir, true);
\r
346 } else if (package is InstalledPackage) {
\r
347 removePackage((InstalledPackage) package, targetDir);
\r