fix(setup): replace managed Hopper on reinstall
This commit is contained in:
@@ -55,6 +55,7 @@ export interface MacHopperInstallHost {
|
|||||||
source: string,
|
source: string,
|
||||||
destination: string,
|
destination: string,
|
||||||
stage: string,
|
stage: string,
|
||||||
|
replaceExisting: boolean,
|
||||||
): Promise<boolean>;
|
): Promise<boolean>;
|
||||||
launcherReady(path: string): Promise<boolean>;
|
launcherReady(path: string): Promise<boolean>;
|
||||||
openApplication(path: string): Promise<void>;
|
openApplication(path: string): Promise<void>;
|
||||||
@@ -64,6 +65,7 @@ export interface MacHopperInstallHost {
|
|||||||
|
|
||||||
/** Download, verify, and install Hopper into the current user's Applications directory. */
|
/** Download, verify, and install Hopper into the current user's Applications directory. */
|
||||||
export const installMacHopper = async (
|
export const installMacHopper = async (
|
||||||
|
options: { readonly replaceExisting?: boolean } = {},
|
||||||
host: MacHopperInstallHost = systemMacHopperInstallHost(),
|
host: MacHopperInstallHost = systemMacHopperInstallHost(),
|
||||||
): Promise<MacHopperInstallResult> => {
|
): Promise<MacHopperInstallResult> => {
|
||||||
let temporary: string | undefined;
|
let temporary: string | undefined;
|
||||||
@@ -83,7 +85,8 @@ export const installMacHopper = async (
|
|||||||
return { status: "failed", reason: "integrity" };
|
return { status: "failed", reason: "integrity" };
|
||||||
|
|
||||||
const destination = host.destination();
|
const destination = host.destination();
|
||||||
if (await host.destinationExists(destination))
|
const destinationExists = await host.destinationExists(destination);
|
||||||
|
if (destinationExists && options.replaceExisting !== true)
|
||||||
return { status: "failed", reason: "destination_exists" };
|
return { status: "failed", reason: "destination_exists" };
|
||||||
temporary = await host.createTemporaryDirectory();
|
temporary = await host.createTemporaryDirectory();
|
||||||
const packagePath = join(temporary, "hopper.dmg");
|
const packagePath = join(temporary, "hopper.dmg");
|
||||||
@@ -97,7 +100,9 @@ export const installMacHopper = async (
|
|||||||
if (source === undefined || !(await host.validBundle(source)))
|
if (source === undefined || !(await host.validBundle(source)))
|
||||||
return { status: "failed", reason: "bundle" };
|
return { status: "failed", reason: "bundle" };
|
||||||
const stage = `${destination}.rea-stage`;
|
const stage = `${destination}.rea-stage`;
|
||||||
if (!(await host.installBundle(source, destination, stage)))
|
if (
|
||||||
|
!(await host.installBundle(source, destination, stage, destinationExists))
|
||||||
|
)
|
||||||
return { status: "failed", reason: "copy" };
|
return { status: "failed", reason: "copy" };
|
||||||
const launcherPath = join(destination, "Contents/MacOS/hopper");
|
const launcherPath = join(destination, "Contents/MacOS/hopper");
|
||||||
if (!(await host.launcherReady(launcherPath)))
|
if (!(await host.launcherReady(launcherPath)))
|
||||||
@@ -181,15 +186,27 @@ const systemMacHopperInstallHost = (): MacHopperInstallHost => ({
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
async installBundle(source, destination, stage) {
|
async installBundle(source, destination, stage, replaceExisting) {
|
||||||
|
const previous = `${stage}-previous`;
|
||||||
try {
|
try {
|
||||||
await mkdir(join(homedir(), "Applications"), { recursive: true });
|
await mkdir(join(homedir(), "Applications"), { recursive: true });
|
||||||
await rm(stage, { recursive: true, force: true });
|
await rm(stage, { recursive: true, force: true });
|
||||||
|
await rm(previous, { recursive: true, force: true });
|
||||||
await execFileAsync("ditto", [source, stage]);
|
await execFileAsync("ditto", [source, stage]);
|
||||||
|
await access(join(stage, "Contents/MacOS/hopper"));
|
||||||
|
if (replaceExisting) await rename(destination, previous);
|
||||||
await rename(stage, destination);
|
await rename(stage, destination);
|
||||||
|
if (replaceExisting) await rm(previous, { recursive: true, force: true });
|
||||||
return true;
|
return true;
|
||||||
} catch {
|
} catch {
|
||||||
await rm(stage, { recursive: true, force: true });
|
await rm(stage, { recursive: true, force: true });
|
||||||
|
if (replaceExisting) {
|
||||||
|
try {
|
||||||
|
await rename(previous, destination);
|
||||||
|
} catch {
|
||||||
|
// The original destination was not moved, or rollback is impossible.
|
||||||
|
}
|
||||||
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -69,7 +69,7 @@ export interface SetupHost {
|
|||||||
macosVersion(): Promise<string | undefined>;
|
macosVersion(): Promise<string | undefined>;
|
||||||
linuxDistribution(): Promise<LinuxDistribution | undefined>;
|
linuxDistribution(): Promise<LinuxDistribution | undefined>;
|
||||||
hopperPath(): Promise<string | undefined>;
|
hopperPath(): Promise<string | undefined>;
|
||||||
installHopper(): Promise<SetupHopperInstallResult>;
|
installHopper(replaceExisting: boolean): Promise<SetupHopperInstallResult>;
|
||||||
detectedClients(): Promise<readonly SetupClient[]>;
|
detectedClients(): Promise<readonly SetupClient[]>;
|
||||||
configureClient(
|
configureClient(
|
||||||
client: SetupClient,
|
client: SetupClient,
|
||||||
@@ -170,7 +170,7 @@ export const runSetup = async (
|
|||||||
};
|
};
|
||||||
|
|
||||||
if (installHopper && (interactiveApproval || options.installHopper)) {
|
if (installHopper && (interactiveApproval || options.installHopper)) {
|
||||||
const installed = await host.installHopper();
|
const installed = await host.installHopper(options.installHopper);
|
||||||
if (installed.status === "failed")
|
if (installed.status === "failed")
|
||||||
return {
|
return {
|
||||||
status: "needs_human",
|
status: "needs_human",
|
||||||
@@ -320,11 +320,11 @@ const systemSetupHost = (): SetupHost => {
|
|||||||
macosVersion: () => doctorHost.macosVersion(),
|
macosVersion: () => doctorHost.macosVersion(),
|
||||||
linuxDistribution: readLinuxDistribution,
|
linuxDistribution: readLinuxDistribution,
|
||||||
hopperPath: async () => (await runDoctor(undefined, doctorHost)).hopperPath,
|
hopperPath: async () => (await runDoctor(undefined, doctorHost)).hopperPath,
|
||||||
installHopper: async () => {
|
installHopper: async (replaceExisting) => {
|
||||||
const result =
|
const result =
|
||||||
process.platform === "linux"
|
process.platform === "linux"
|
||||||
? await installLinuxHopper()
|
? await installLinuxHopper()
|
||||||
: await installMacHopper();
|
: await installMacHopper({ replaceExisting });
|
||||||
if (result.status === "installed") return result;
|
if (result.status === "installed") return result;
|
||||||
return setupInstallFailure(result.reason);
|
return setupInstallFailure(result.reason);
|
||||||
},
|
},
|
||||||
|
|||||||
+24
-5
@@ -19,6 +19,7 @@ class FakeMacHopperHost implements MacHopperInstallHost {
|
|||||||
unmounts = 0;
|
unmounts = 0;
|
||||||
cleanups = 0;
|
cleanups = 0;
|
||||||
opens = 0;
|
opens = 0;
|
||||||
|
replaces = false;
|
||||||
|
|
||||||
download = (url: string) =>
|
download = (url: string) =>
|
||||||
Promise.resolve(
|
Promise.resolve(
|
||||||
@@ -51,7 +52,15 @@ class FakeMacHopperHost implements MacHopperInstallHost {
|
|||||||
appBundle = () => Promise.resolve("/mounted/Hopper Disassembler.app");
|
appBundle = () => Promise.resolve("/mounted/Hopper Disassembler.app");
|
||||||
validBundle = () => Promise.resolve(this.valid);
|
validBundle = () => Promise.resolve(this.valid);
|
||||||
destinationExists = () => Promise.resolve(this.existing);
|
destinationExists = () => Promise.resolve(this.existing);
|
||||||
installBundle = () => Promise.resolve(this.copySucceeds);
|
installBundle = (
|
||||||
|
_source: string,
|
||||||
|
_destination: string,
|
||||||
|
_stage: string,
|
||||||
|
replaceExisting: boolean,
|
||||||
|
) => {
|
||||||
|
this.replaces = replaceExisting;
|
||||||
|
return Promise.resolve(this.copySucceeds);
|
||||||
|
};
|
||||||
launcherReady = () => Promise.resolve(this.launcherSucceeds);
|
launcherReady = () => Promise.resolve(this.launcherSucceeds);
|
||||||
openApplication = () => {
|
openApplication = () => {
|
||||||
this.opens += 1;
|
this.opens += 1;
|
||||||
@@ -67,7 +76,7 @@ class FakeMacHopperHost implements MacHopperInstallHost {
|
|||||||
describe("macOS Hopper installation", () => {
|
describe("macOS Hopper installation", () => {
|
||||||
it("installs, verifies, opens, and cleans the official package", async () => {
|
it("installs, verifies, opens, and cleans the official package", async () => {
|
||||||
const host = new FakeMacHopperHost();
|
const host = new FakeMacHopperHost();
|
||||||
await expect(installMacHopper(host)).resolves.toEqual({
|
await expect(installMacHopper({}, host)).resolves.toEqual({
|
||||||
status: "installed",
|
status: "installed",
|
||||||
launcherPath:
|
launcherPath:
|
||||||
"/home/user/Applications/Hopper Disassembler.app/Contents/MacOS/hopper",
|
"/home/user/Applications/Hopper Disassembler.app/Contents/MacOS/hopper",
|
||||||
@@ -80,7 +89,7 @@ describe("macOS Hopper installation", () => {
|
|||||||
it("rejects an integrity mismatch before mounting", async () => {
|
it("rejects an integrity mismatch before mounting", async () => {
|
||||||
const host = new FakeMacHopperHost();
|
const host = new FakeMacHopperHost();
|
||||||
host.badHash = true;
|
host.badHash = true;
|
||||||
await expect(installMacHopper(host)).resolves.toEqual({
|
await expect(installMacHopper({}, host)).resolves.toEqual({
|
||||||
status: "failed",
|
status: "failed",
|
||||||
reason: "integrity",
|
reason: "integrity",
|
||||||
});
|
});
|
||||||
@@ -91,16 +100,26 @@ describe("macOS Hopper installation", () => {
|
|||||||
it("refuses to overwrite an existing application", async () => {
|
it("refuses to overwrite an existing application", async () => {
|
||||||
const host = new FakeMacHopperHost();
|
const host = new FakeMacHopperHost();
|
||||||
host.existing = true;
|
host.existing = true;
|
||||||
await expect(installMacHopper(host)).resolves.toEqual({
|
await expect(installMacHopper({}, host)).resolves.toEqual({
|
||||||
status: "failed",
|
status: "failed",
|
||||||
reason: "destination_exists",
|
reason: "destination_exists",
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("replaces the managed application when explicitly requested", async () => {
|
||||||
|
const host = new FakeMacHopperHost();
|
||||||
|
host.existing = true;
|
||||||
|
|
||||||
|
await expect(
|
||||||
|
installMacHopper({ replaceExisting: true }, host),
|
||||||
|
).resolves.toMatchObject({ status: "installed" });
|
||||||
|
expect(host.replaces).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
it("cleans and detaches after bundle validation fails", async () => {
|
it("cleans and detaches after bundle validation fails", async () => {
|
||||||
const host = new FakeMacHopperHost();
|
const host = new FakeMacHopperHost();
|
||||||
host.valid = false;
|
host.valid = false;
|
||||||
await expect(installMacHopper(host)).resolves.toEqual({
|
await expect(installMacHopper({}, host)).resolves.toEqual({
|
||||||
status: "failed",
|
status: "failed",
|
||||||
reason: "bundle",
|
reason: "bundle",
|
||||||
});
|
});
|
||||||
|
|||||||
+6
-1
@@ -22,6 +22,7 @@ class FakeSetupHost implements SetupHost {
|
|||||||
clients: readonly SetupClient[] = [];
|
clients: readonly SetupClient[] = [];
|
||||||
clientResults = new Map<string, ClientConfigurationResult>();
|
clientResults = new Map<string, ClientConfigurationResult>();
|
||||||
hopperInstalls = 0;
|
hopperInstalls = 0;
|
||||||
|
hopperReplaceRequests: boolean[] = [];
|
||||||
configurations = 0;
|
configurations = 0;
|
||||||
skillInstalls = 0;
|
skillInstalls = 0;
|
||||||
checkedHopperPaths: Array<string | undefined> = [];
|
checkedHopperPaths: Array<string | undefined> = [];
|
||||||
@@ -38,8 +39,11 @@ class FakeSetupHost implements SetupHost {
|
|||||||
linuxDistribution = (): Promise<LinuxDistribution | undefined> =>
|
linuxDistribution = (): Promise<LinuxDistribution | undefined> =>
|
||||||
Promise.resolve(this.distribution);
|
Promise.resolve(this.distribution);
|
||||||
hopperPath = (): Promise<string | undefined> => Promise.resolve(this.hopper);
|
hopperPath = (): Promise<string | undefined> => Promise.resolve(this.hopper);
|
||||||
installHopper = (): Promise<SetupHopperInstallResult> => {
|
installHopper = (
|
||||||
|
replaceExisting: boolean,
|
||||||
|
): Promise<SetupHopperInstallResult> => {
|
||||||
this.hopperInstalls += 1;
|
this.hopperInstalls += 1;
|
||||||
|
this.hopperReplaceRequests.push(replaceExisting);
|
||||||
this.linuxDemoRuntimeMissing = false;
|
this.linuxDemoRuntimeMissing = false;
|
||||||
if (this.hopperInstallSucceeds) {
|
if (this.hopperInstallSucceeds) {
|
||||||
this.hopper = "/manual/Hopper";
|
this.hopper = "/manual/Hopper";
|
||||||
@@ -248,6 +252,7 @@ describe("setup workflow", () => {
|
|||||||
]);
|
]);
|
||||||
expect(result.appliedActions).toEqual(["installed_hopper"]);
|
expect(result.appliedActions).toEqual(["installed_hopper"]);
|
||||||
expect(host.hopperInstalls).toBe(1);
|
expect(host.hopperInstalls).toBe(1);
|
||||||
|
expect(host.hopperReplaceRequests).toEqual([true]);
|
||||||
expect(host.configurations).toBe(0);
|
expect(host.configurations).toBe(0);
|
||||||
expect(host.checkedHopperPaths).toEqual(["/manual/Hopper"]);
|
expect(host.checkedHopperPaths).toEqual(["/manual/Hopper"]);
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user