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 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
46 if (File.Exists(toBeChangedFilePath)) {
\r
47 File.Move(toBeChangedFilePath, toBeChangedFilePath + ".newfile"); // 新規を退避させて
\r
49 File.Copy(changedFilePath, toBeChangedFilePath); // installSrcの方に反映させておく
\r
53 Directory.CreateDirectory(targetDir);
\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
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
72 File.Move(file, targetFilePath);
\r
76 private static void postInstall(string targetDir, Package package)
\r
79 if (!string.IsNullOrEmpty(package.System32CopyFiles)) {
\r
80 if (! Directory.Exists(NaGet.Env.ArchiveSystem32)) {
\r
81 Directory.CreateDirectory(NaGet.Env.ArchiveSystem32);
\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
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
96 sw.WriteLine(Path.GetFileName(destPath));
\r
101 File.SetAttributes(logfile, FileAttributes.Hidden);
\r
105 if (! string.IsNullOrEmpty(package.InstallScript) ) {
\r
106 Engine engine = MSBuild.Engine;
\r
107 Project proj = new Project(engine);
\r
109 proj.LoadXml(package.InstallScript);
\r
110 } catch (InvalidProjectFileException e) {
\r
111 throw new ApplicationException("InstallScript is invalid", e);
\r
114 engine.BuildProject(proj, "Install");
\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
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
128 //lnk.SetIconLocation(path, 0);
\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
137 lnk.ToPersistFile().Save(lnkFilePath, true);
\r
143 private static void storeInstalledFileList(string targetDir)
\r
145 string installedFileListPath = Path.Combine(targetDir, InstalledFileListName);
\r
146 if (File.Exists(installedFileListPath)) {
\r
147 File.SetAttributes(installedFileListPath, FileAttributes.Normal);
\r
149 InstalledFileList installedFileList = InstalledFileList.CreateFromFiles(targetDir);
\r
150 NaGet.Utils.PutSerializeObject(installedFileListPath, installedFileList);
\r
152 File.SetAttributes(installedFileListPath, FileAttributes.Hidden | FileAttributes.ReadOnly);
\r
155 private static void storePackageXml(Package package, string destDir)
\r
157 if (package == null) {
\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
169 string packageXmlFilePath = Path.Combine(destDir, InstalledPackageFileName);
\r
170 if (File.Exists(packageXmlFilePath)) {
\r
171 File.SetAttributes(packageXmlFilePath, FileAttributes.Normal);
\r
173 NaGet.Utils.PutSerializeObject(packageXmlFilePath, pkg);
\r
174 File.SetAttributes(packageXmlFilePath, FileAttributes.Hidden | FileAttributes.ReadOnly);
\r
177 private static void removePackage(InstalledPackage package, string targetDir)
\r
179 // アンインストールスクリプトの実行
\r
180 if (! string.IsNullOrEmpty(package.InstallScript)) {
\r
181 Engine engine = MSBuild.Engine;
\r
182 Project proj = new Project(engine);
\r
184 proj.LoadXml(package.InstallScript);
\r
185 } catch (InvalidProjectFileException e) {
\r
186 throw new ApplicationException("InstallScript is invalid", e);
\r
189 engine.BuildProject(proj, "Uninstall");
\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
199 // SYSTEM32からの削除の実行
\r
200 if (! string.IsNullOrEmpty(package.System32CopyFiles) ) {
\r
201 string logfile = Path.Combine(NaGet.Env.ArchiveSystem32, ".applistation.install."+package.Name+".log");
\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
209 if (File.Exists(filePath)) {
\r
210 File.SetAttributes(filePath, FileAttributes.Normal);
\r
211 File.Delete(filePath);
\r
214 File.SetAttributes(logfile, FileAttributes.Normal);
\r
215 File.Delete(logfile);
\r
219 NaGet.Utils.SetAttributeRecursive(targetDir, FileAttributes.Normal);
\r
220 Directory.Delete(targetDir, true);
\r
223 private static void parseMainArguments(string[] args, out string arcFile, out string targetDir, out Package package)
\r
225 if (args.Length < 1) {
\r
226 throw new ArgumentException();
\r
229 switch (args[0].ToLower()) {
\r
231 if (args.Length != 3) {
\r
232 throw new ArgumentException();
\r
236 targetDir = args[2];
\r
240 if (args.Length != 3) {
\r
241 throw new ArgumentException();
\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
250 if (args.Length != 2) {
\r
251 throw new ArgumentException();
\r
255 targetDir = Path.Combine(NaGet.Env.ArchiveProgramFiles, args[1]);
\r
258 string filepath = Path.Combine(targetDir, InstalledPackageFileName);
\r
259 if (File.Exists(filepath)) {
\r
260 package = NaGet.Utils.GetDeserializedObject<InstalledPackage>(filepath);
\r
262 Console.Error.WriteLine("Not found or already removed package : {0}", args[1]);
\r
263 Environment.Exit(100);
\r
270 Console.Error.WriteLine("Unreconized command \"{0}\".", args[0]);
\r
271 Environment.Exit(100);
\r
277 public static void Main(string[] args)
\r
279 // アーカイブSYSTEM32をパスに足す
\r
280 NaGet.Utils.AddDirectoryToPath(NaGet.Env.ArchiveSystem32);
\r
282 string arcFile = null;
\r
283 string targetDir = null;
\r
284 Package package = null;
\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
297 if (arcFile != null) { // install or extract
\r
298 string tempExtractDir = targetDir + "___temp___"; // HACK
\r
299 Directory.CreateDirectory(tempExtractDir);
\r
302 extract(arcFile, tempExtractDir);
\r
304 install(tempExtractDir, targetDir);
\r
306 if (package != null) {
\r
307 postInstall(targetDir, package);
\r
309 storeInstalledFileList(targetDir);
\r
310 storePackageXml(package, targetDir);
\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
316 Directory.Delete(tempExtractDir, true);
\r
318 } else if (package is InstalledPackage) {
\r
319 removePackage((InstalledPackage) package, targetDir);
\r