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 (! Directory.Exists(Path.GetDirectoryName(toBeChangedFilePath))) {
\r
55 Directory.CreateDirectory(Path.GetDirectoryName(toBeChangedFilePath));
\r
57 File.Copy(changedFilePath, toBeChangedFilePath);
\r
61 Directory.CreateDirectory(targetDir);
\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
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
80 File.Move(file, targetFilePath);
\r
84 private static void postInstall(string targetDir, Package package)
\r
87 if (!string.IsNullOrEmpty(package.System32CopyFiles)) {
\r
88 if (! Directory.Exists(NaGet.Env.ArchiveSystem32)) {
\r
89 Directory.CreateDirectory(NaGet.Env.ArchiveSystem32);
\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
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
104 sw.WriteLine(Path.GetFileName(destPath));
\r
109 File.SetAttributes(logfile, FileAttributes.Hidden);
\r
113 if (! string.IsNullOrEmpty(package.InstallScript) ) {
\r
114 Engine engine = MSBuild.Engine;
\r
115 Project proj = new Project(engine);
\r
117 proj.LoadXml(package.InstallScript);
\r
118 } catch (InvalidProjectFileException e) {
\r
119 throw new ApplicationException("InstallScript is invalid", e);
\r
122 engine.BuildProject(proj, "Install");
\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
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
136 //lnk.SetIconLocation(path, 0);
\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
145 lnk.ToPersistFile().Save(lnkFilePath, true);
\r
151 private static void storeInstalledFileList(string targetDir)
\r
153 string installedFileListPath = Path.Combine(targetDir, InstalledFileListName);
\r
154 if (File.Exists(installedFileListPath)) {
\r
155 File.SetAttributes(installedFileListPath, FileAttributes.Normal);
\r
157 InstalledFileList installedFileList = InstalledFileList.CreateFromFiles(targetDir);
\r
158 NaGet.Utils.PutSerializeObject(installedFileListPath, installedFileList);
\r
160 File.SetAttributes(installedFileListPath, FileAttributes.Hidden | FileAttributes.ReadOnly);
\r
163 private static void storePackageXml(Package package, string destDir)
\r
165 if (package == null) {
\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
177 string packageXmlFilePath = Path.Combine(destDir, InstalledPackageFileName);
\r
178 if (File.Exists(packageXmlFilePath)) {
\r
179 File.SetAttributes(packageXmlFilePath, FileAttributes.Normal);
\r
181 NaGet.Utils.PutSerializeObject(packageXmlFilePath, pkg);
\r
182 File.SetAttributes(packageXmlFilePath, FileAttributes.Hidden | FileAttributes.ReadOnly);
\r
185 private static void removePackage(InstalledPackage package, string targetDir)
\r
187 // アンインストールスクリプトの実行
\r
188 if (! string.IsNullOrEmpty(package.InstallScript)) {
\r
189 Engine engine = MSBuild.Engine;
\r
190 Project proj = new Project(engine);
\r
192 proj.LoadXml(package.InstallScript);
\r
193 } catch (InvalidProjectFileException e) {
\r
194 throw new ApplicationException("InstallScript is invalid", e);
\r
197 engine.BuildProject(proj, "Uninstall");
\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
207 // SYSTEM32からの削除の実行
\r
208 if (! string.IsNullOrEmpty(package.System32CopyFiles) ) {
\r
209 string logfile = Path.Combine(NaGet.Env.ArchiveSystem32, ".applistation.install."+package.Name+".log");
\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
217 if (File.Exists(filePath)) {
\r
218 File.SetAttributes(filePath, FileAttributes.Normal);
\r
219 File.Delete(filePath);
\r
222 File.SetAttributes(logfile, FileAttributes.Normal);
\r
223 File.Delete(logfile);
\r
227 NaGet.Utils.SetAttributeRecursive(targetDir, FileAttributes.Normal);
\r
228 Directory.Delete(targetDir, true);
\r
231 private static void parseMainArguments(string[] args, out string arcFile, out string targetDir, out Package package)
\r
233 if (args.Length < 1) {
\r
234 throw new ArgumentException();
\r
237 switch (args[0].ToLower()) {
\r
239 if (args.Length != 3) {
\r
240 throw new ArgumentException();
\r
244 targetDir = args[2];
\r
248 if (args.Length != 3) {
\r
249 throw new ArgumentException();
\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
258 if (args.Length != 2) {
\r
259 throw new ArgumentException();
\r
263 targetDir = Path.Combine(NaGet.Env.ArchiveProgramFiles, args[1]);
\r
266 string filepath = Path.Combine(targetDir, InstalledPackageFileName);
\r
267 if (File.Exists(filepath)) {
\r
268 package = NaGet.Utils.GetDeserializedObject<InstalledPackage>(filepath);
\r
270 Console.Error.WriteLine("Not found or already removed package : {0}", args[1]);
\r
271 Environment.Exit(100);
\r
278 Console.Error.WriteLine("Unreconized command \"{0}\".", args[0]);
\r
279 Environment.Exit(100);
\r
285 public static void Main(string[] args)
\r
287 // アーカイブSYSTEM32をパスに足す
\r
288 NaGet.Utils.AddDirectoryToPath(NaGet.Env.ArchiveSystem32);
\r
290 string arcFile = null;
\r
291 string targetDir = null;
\r
292 Package package = null;
\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
307 if (arcFile != null) {
\r
308 string tempExtractDir = targetDir + "___temp___"; // HACK
\r
309 Directory.CreateDirectory(tempExtractDir);
\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
318 extract(arcFile, tempExtractDir);
\r
322 install(tempExtractDir, targetDir);
\r
324 if (package != null) {
\r
325 // STEP3. カスタマイズ可能な後処理
\r
326 postInstall(targetDir, package);
\r
328 // STEP4. パッケージ情報をインストール先(targetDir)に置く
\r
329 storeInstalledFileList(targetDir);
\r
330 storePackageXml(package, targetDir);
\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
336 Directory.Delete(tempExtractDir, true);
\r
338 } else if (package is InstalledPackage) {
\r
339 removePackage((InstalledPackage) package, targetDir);
\r