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

Commit e148207

Browse files
committed
Fix .pkg generation so it properly installs to home dir
1 parent 6abdeb1 commit e148207

3 files changed

Lines changed: 61 additions & 28 deletions

File tree

src/Squirrel.CommandLine/OSX/Commands.cs

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ private static void Pack(PackOptions options)
4747
Log.Info("Copying app to release directory");
4848
if (Directory.Exists(appBundlePath)) Utility.DeleteFileOrDirectoryHard(appBundlePath);
4949
Directory.CreateDirectory(appBundlePath);
50-
CopyFiles(new DirectoryInfo(options.packDirectory), new DirectoryInfo(appBundlePath));
50+
Utility.CopyFiles(new DirectoryInfo(options.packDirectory), new DirectoryInfo(appBundlePath));
5151
} else {
5252
Log.Info("Pack directory is not a bundle. Will generate new '.app' bundle from a directory of application files.");
5353

@@ -98,7 +98,7 @@ private static void Pack(PackOptions options)
9898
File.Copy(options.icon, Path.Combine(builder.ResourcesDirectory, Path.GetFileName(options.icon)));
9999

100100
Log.Info("Copying application files into new '.app' bundle");
101-
CopyFiles(new DirectoryInfo(options.packDirectory), new DirectoryInfo(builder.MacosDirectory));
101+
Utility.CopyFiles(new DirectoryInfo(options.packDirectory), new DirectoryInfo(builder.MacosDirectory));
102102

103103
appBundlePath = builder.AppDirectory;
104104
}
@@ -133,7 +133,7 @@ private static void Pack(PackOptions options)
133133
HelperExe.CreateDittoZip(appBundlePath, zipPath);
134134
HelperExe.Notarize(zipPath, options.notaryProfile);
135135
HelperExe.Staple(appBundlePath);
136-
136+
137137
// re-create the zip from the app with the stapled notarization
138138
File.Delete(zipPath);
139139
HelperExe.CreateDittoZip(appBundlePath, zipPath);
@@ -181,20 +181,5 @@ private static void Pack(PackOptions options)
181181

182182
Log.Info("Done.");
183183
}
184-
185-
private static void CopyFiles(DirectoryInfo source, DirectoryInfo target)
186-
{
187-
Directory.CreateDirectory(target.FullName);
188-
189-
foreach (var fileInfo in source.GetFiles()) {
190-
var path = Path.Combine(target.FullName, fileInfo.Name);
191-
fileInfo.CopyTo(path, true);
192-
}
193-
194-
foreach (var sourceSubDir in source.GetDirectories()) {
195-
var targetSubDir = target.CreateSubdirectory(sourceSubDir.Name);
196-
CopyFiles(sourceSubDir, targetSubDir);
197-
}
198-
}
199184
}
200185
}

src/Squirrel.CommandLine/OSX/HelperExe.cs

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.IO;
4+
using System.Linq;
45
using System.Runtime.Versioning;
56
using System.Threading;
67
using Newtonsoft.Json;
@@ -52,22 +53,54 @@ public static void CreateInstallerPkg(string appBundlePath, string pkgOutputPath
5253
{
5354
Log.Info($"Creating installer '.pkg' for app at '{appBundlePath}'");
5455

55-
var args = new List<string> {
56-
"--install-location", "~/Applications",
57-
"--component", appBundlePath,
56+
using var _1 = Utility.GetTempDirectory(out var tmp);
57+
using var _2 = Utility.GetTempDirectory(out var tmpPayload1);
58+
using var _3 = Utility.GetTempDirectory(out var tmpPayload2);
59+
60+
// copy .app to tmp folder
61+
var bundleName = Path.GetFileName(appBundlePath);
62+
var tmpBundlePath = Path.Combine(tmpPayload1, bundleName);
63+
Utility.CopyFiles(new DirectoryInfo(appBundlePath), new DirectoryInfo(tmpBundlePath));
64+
65+
// generate non-relocatable pkg
66+
var pkgPlistPath = Path.Combine(tmp, "tmp.plist");
67+
InvokeAndThrowIfNonZero("pkgbuild", new[] { "--analyze", "--root", tmpPayload1, pkgPlistPath }, null);
68+
InvokeAndThrowIfNonZero("plutil", new[] { "-replace", "BundleIsRelocatable", "-bool", "NO", pkgPlistPath }, null);
69+
70+
var pkg1Path = Path.Combine(tmpPayload2, "1.pkg");
71+
string[] args1 = {
72+
"--root", tmpPayload1,
73+
"--component-plist", pkgPlistPath,
74+
"--install-location", "/Applications",
75+
pkg1Path,
5876
};
77+
78+
InvokeAndThrowIfNonZero("pkgbuild", args1, null);
5979

80+
// create product package that installs to home dir
81+
var distributionPath = Path.Combine(tmp, "distribution.xml");
82+
InvokeAndThrowIfNonZero("productbuild", new[] { "--synthesize", "--package", pkg1Path, distributionPath }, null);
83+
84+
// disable local system installation and build final package
85+
var distXml = File.ReadAllLines(distributionPath).ToList();
86+
distXml.Insert(2, "<domains enable_anywhere=\"false\" enable_currentUserHome=\"true\" enable_localSystem=\"false\" />");
87+
File.WriteAllLines(distributionPath, distXml);
88+
89+
List<string> args2 = new () {
90+
"--distribution", distributionPath,
91+
"--package-path", tmpPayload2,
92+
pkgOutputPath
93+
};
94+
6095
if (!String.IsNullOrEmpty(signIdentity)) {
61-
args.Add("--sign");
62-
args.Add(signIdentity);
96+
args2.Add("--sign");
97+
args2.Add(signIdentity);
6398
} else {
6499
Log.Warn("No Installer signing identity provided. The '.pkg' will not be signed.");
65100
}
66-
67-
args.Add(pkgOutputPath);
68-
69-
InvokeAndThrowIfNonZero("pkgbuild", args, null);
70-
101+
102+
InvokeAndThrowIfNonZero("productbuild", args2, null);
103+
71104
Log.Info("Installer created successfully");
72105
}
73106

src/Squirrel/Internal/Utility.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -781,5 +781,20 @@ public static NuspecManifest ReadManifestFromVersionDir(string appVersionDir)
781781

782782
return null;
783783
}
784+
785+
public static void CopyFiles(DirectoryInfo source, DirectoryInfo target)
786+
{
787+
Directory.CreateDirectory(target.FullName);
788+
789+
foreach (var fileInfo in source.GetFiles()) {
790+
var path = Path.Combine(target.FullName, fileInfo.Name);
791+
fileInfo.CopyTo(path, true);
792+
}
793+
794+
foreach (var sourceSubDir in source.GetDirectories()) {
795+
var targetSubDir = target.CreateSubdirectory(sourceSubDir.Name);
796+
CopyFiles(sourceSubDir, targetSubDir);
797+
}
798+
}
784799
}
785800
}

0 commit comments

Comments
 (0)