Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow updating mac version running inside Rosetta to native ARM #5901

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 13 additions & 2 deletions packages/electron-updater/src/MacUpdater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { AppUpdater, DownloadUpdateOptions } from "./AppUpdater"
import { ResolvedUpdateFileInfo } from "./main"
import { findFile } from "./providers/Provider"
import AutoUpdater = Electron.AutoUpdater
import { execShellCommand } from "./util"

export class MacUpdater extends AppUpdater {
private readonly nativeUpdater: AutoUpdater = require("electron").autoUpdater
Expand All @@ -26,13 +27,23 @@ export class MacUpdater extends AppUpdater {
})
}

protected doDownloadUpdate(downloadUpdateOptions: DownloadUpdateOptions): Promise<Array<string>> {
protected async doDownloadUpdate(downloadUpdateOptions: DownloadUpdateOptions): Promise<Array<string>> {
let files = downloadUpdateOptions.updateInfoAndProvider.provider.resolveFiles(downloadUpdateOptions.updateInfoAndProvider.info)

// Detect if we are running inside Rosetta emulation
const sysctlRosettaInfoKey = "sysctl.proc_translated"
let isRosetta: boolean
try {
const { stdout, stderr } = await execShellCommand(`sysctl ${sysctlRosettaInfoKey}`)
isRosetta = !stderr && stdout?.toString()?.includes(`${sysctlRosettaInfoKey}: 1`)
} catch (e) {
this._logger.info(`sysctl shell command to check for macOS Rosetta environment failed: ${e}`)
}

// Allow arm64 macs to install universal or rosetta2(x64) - https://github.com/electron-userland/electron-builder/pull/5524
const isArm64 = (file: ResolvedUpdateFileInfo) => file.url.pathname.includes("arm64")
if (files.some(isArm64)) {
files = files.filter(file => (process.arch === "arm64") === isArm64(file))
files = files.filter(file => (process.arch === "arm64" || isRosetta) === isArm64(file))
}

const zipFileInfo = findFile(files, "zip", ["pkg", "dmg"])
Expand Down
13 changes: 13 additions & 0 deletions packages/electron-updater/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { URL } from "url"
// @ts-ignore
import * as escapeRegExp from "lodash.escaperegexp"
import { exec, ExecException } from "child_process"

/** @internal */
export function newBaseUrl(url: string): URL {
Expand Down Expand Up @@ -35,3 +36,15 @@ export function blockmapFiles(baseUrl: URL, oldVersion: string, newVersion: stri
const oldBlockMapUrl = newUrlFromBase(`${baseUrl.pathname.replace(new RegExp(escapeRegExp(newVersion), "g"), oldVersion)}.blockmap`, baseUrl)
return [oldBlockMapUrl, newBlockMapUrl]
}

export function execShellCommand(cmd: string): Promise<{ error: ExecException | null; stdout: string | Buffer; stderr: string | Buffer }> {
return new Promise((resolve, reject) => {
try {
exec(cmd, (error, stdout, stderr) => {
resolve({ error, stdout, stderr })
bartoszhernas marked this conversation as resolved.
Show resolved Hide resolved
})
} catch (e) {
reject(e)
}
})
}