From a75897e344d250c26cac8819da71174b6b1d7b86 Mon Sep 17 00:00:00 2001 From: Caelan Sayler Date: Tue, 5 Jul 2022 23:27:59 +0100 Subject: [PATCH] Add post-install script to run the app --- src/Squirrel.CommandLine/OSX/Commands.cs | 2 +- src/Squirrel.CommandLine/OSX/HelperExe.cs | 19 ++++++++++++++----- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/Squirrel.CommandLine/OSX/Commands.cs b/src/Squirrel.CommandLine/OSX/Commands.cs index cf701d5dc..946a3daf8 100644 --- a/src/Squirrel.CommandLine/OSX/Commands.cs +++ b/src/Squirrel.CommandLine/OSX/Commands.cs @@ -38,7 +38,7 @@ private static void Pack(PackOptions options) if (options.mainExe != null) Log.Warn("--exeName is ignored if the pack directory is a '.app' bundle."); - + if (options.appleId != null) Log.Warn("--appleId is ignored if the pack directory is a '.app' bundle."); diff --git a/src/Squirrel.CommandLine/OSX/HelperExe.cs b/src/Squirrel.CommandLine/OSX/HelperExe.cs index 2f70f0697..4ef948dbd 100644 --- a/src/Squirrel.CommandLine/OSX/HelperExe.cs +++ b/src/Squirrel.CommandLine/OSX/HelperExe.cs @@ -5,7 +5,6 @@ using System.Runtime.Versioning; using System.Threading; using Newtonsoft.Json; -using Squirrel.SimpleSplat; namespace Squirrel.CommandLine.OSX { @@ -43,7 +42,7 @@ public static void CodeSign(string identity, string entitlements, string filePat Log.Info($"Beginning codesign for package..."); Console.WriteLine(InvokeAndThrowIfNonZero("codesign", args, null)); - + Log.Info("codesign completed successfully"); } @@ -61,20 +60,23 @@ public static void SpctlAssess(string filePath) [SupportedOSPlatform("osx")] public static void CreateInstallerPkg(string appBundlePath, string pkgOutputPath, string signIdentity) { - Log.Info($"Creating installer '.pkg' for app at '{appBundlePath}'"); + // https://matthew-brett.github.io/docosx/flat_packages.html + Log.Info($"Creating installer '.pkg' for app at '{appBundlePath}'"); + if (File.Exists(pkgOutputPath)) File.Delete(pkgOutputPath); using var _1 = Utility.GetTempDirectory(out var tmp); using var _2 = Utility.GetTempDirectory(out var tmpPayload1); using var _3 = Utility.GetTempDirectory(out var tmpPayload2); + using var _4 = Utility.GetTempDirectory(out var tmpScripts); // copy .app to tmp folder var bundleName = Path.GetFileName(appBundlePath); var tmpBundlePath = Path.Combine(tmpPayload1, bundleName); Utility.CopyFiles(new DirectoryInfo(appBundlePath), new DirectoryInfo(tmpBundlePath)); - // generate non-relocatable pkg + // generate non-relocatable component pkg. this will be included into a product archive var pkgPlistPath = Path.Combine(tmp, "tmp.plist"); InvokeAndThrowIfNonZero("pkgbuild", new[] { "--analyze", "--root", tmpPayload1, pkgPlistPath }, null); InvokeAndThrowIfNonZero("plutil", new[] { "-replace", "BundleIsRelocatable", "-bool", "NO", pkgPlistPath }, null); @@ -88,6 +90,12 @@ public static void CreateInstallerPkg(string appBundlePath, string pkgOutputPath }; InvokeAndThrowIfNonZero("pkgbuild", args1, null); + + // create postinstall scripts to open app after install + // https://stackoverflow.com/questions/35619036/open-app-after-installation-from-pkg-file-in-mac + var postinstall = Path.Combine(tmpScripts, "postinstall"); + File.WriteAllText(postinstall, $"#!/bin/sh\nopen \"$2/{bundleName}/\"\nexit0"); + PlatformUtil.ChmodFileAsExecutable(postinstall); // create product package that installs to home dir var distributionPath = Path.Combine(tmp, "distribution.xml"); @@ -101,6 +109,7 @@ public static void CreateInstallerPkg(string appBundlePath, string pkgOutputPath List args2 = new() { "--distribution", distributionPath, "--package-path", tmpPayload2, + "--scripts", tmpScripts, pkgOutputPath }; @@ -157,7 +166,7 @@ public static void Notarize(string filePath, string keychainProfileName) Log.Info("Notarization completed successfully"); } - + [SupportedOSPlatform("osx")] public static void Staple(string filePath) {