Skip to content
This repository has been archived by the owner on Jul 5, 2024. It is now read-only.

Commit

Permalink
Add post-install script to run the app
Browse files Browse the repository at this point in the history
  • Loading branch information
caesay committed Jul 5, 2022
1 parent 4ff62a1 commit a75897e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/Squirrel.CommandLine/OSX/Commands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.");

Expand Down
19 changes: 14 additions & 5 deletions src/Squirrel.CommandLine/OSX/HelperExe.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Runtime.Versioning;
using System.Threading;
using Newtonsoft.Json;
using Squirrel.SimpleSplat;

namespace Squirrel.CommandLine.OSX
{
Expand Down Expand Up @@ -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");
}

Expand All @@ -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);
Expand All @@ -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");
Expand All @@ -101,6 +109,7 @@ public static void CreateInstallerPkg(string appBundlePath, string pkgOutputPath
List<string> args2 = new() {
"--distribution", distributionPath,
"--package-path", tmpPayload2,
"--scripts", tmpScripts,
pkgOutputPath
};

Expand Down Expand Up @@ -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)
{
Expand Down

0 comments on commit a75897e

Please sign in to comment.