Skip to content

Commit

Permalink
fix: reverting migration to electron-rebuild since Cxx flags were bac…
Browse files Browse the repository at this point in the history
…kported to latest versions of electron
  • Loading branch information
mmaietta committed Jul 14, 2023
1 parent 84ed3ff commit bffd6a0
Show file tree
Hide file tree
Showing 5 changed files with 238 additions and 147 deletions.
3 changes: 2 additions & 1 deletion packages/app-builder-lib/src/packager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ export class Packager {
const frameworkInfo = { version: this.framework.version, useCustomDist: true }
const config = this.config
if (config.nodeGypRebuild === true) {
await nodeGypRebuild(frameworkInfo, arch, platform)
await nodeGypRebuild(platform.nodeName, Arch[arch], frameworkInfo)
}

if (config.npmRebuild === false) {
Expand Down Expand Up @@ -526,6 +526,7 @@ export class Packager {
frameworkInfo,
platform: platform.nodeName,
arch: Arch[arch],
productionDeps: this.getNodeDependencyInfo(null),
})
}
}
Expand Down
6 changes: 3 additions & 3 deletions packages/app-builder-lib/src/util/packageMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ export function checkMetadata(metadata: Metadata, devMetadata: any | null, appPa
}

const devDependencies = (metadata as any).devDependencies
if (devDependencies != null && "@electron/rebuild" in devDependencies) {
if (devDependencies != null && ("electron-rebuild" in devDependencies || "@electron/rebuild" in devDependencies)) {
log.info(
'@electron/rebuild is already incorporated into electron-builder, please consider to remove excess dependency from devDependencies\n\nTo ensure your native dependencies are always matched electron version, simply add script `"postinstall": "electron-builder install-app-deps" to your `package.json`'
'@electron/rebuild not required if you use electron-builder, please consider to remove excess dependency from devDependencies\n\nTo ensure your native dependencies are always matched electron version, simply add script `"postinstall": "electron-builder install-app-deps" to your `package.json`'
)
}

Expand Down Expand Up @@ -110,7 +110,7 @@ function checkDependencies(dependencies: { [key: string]: string } | null | unde
errors.push(`At least electron-builder-squirrel-windows 20.32.0 is required by current electron-builder version. Please set electron-builder-squirrel-windows to "^20.32.0"`)
}

const deps = ["electron", "electron-prebuilt", "@electron/rebuild"]
const deps = ["electron", "electron-prebuilt", "electron-rebuild"]
if (process.env.ALLOW_ELECTRON_BUILDER_AS_PRODUCTION_DEPENDENCY !== "true") {
deps.push("electron-builder")
}
Expand Down
73 changes: 42 additions & 31 deletions packages/app-builder-lib/src/util/yarn.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,32 @@
import { Arch, archFromString, asArray, log, spawn } from "builder-util"
import { asArray, log, spawn } from "builder-util"
import { pathExists } from "fs-extra"
import { Lazy } from "lazy-val"
import { homedir } from "os"
import * as path from "path"
import { Configuration } from "../configuration"
import * as electronRebuild from "@electron/rebuild/lib/rebuild"
import * as searchModule from "@electron/rebuild/lib/search-module"
import { EventEmitter } from "events"
import { Platform } from "../core"
import { executeAppBuilderAndWriteJson } from "./appBuilder"
import { NodeModuleDirInfo } from "./packageDependencies"

export async function installOrRebuild(config: Configuration, appDir: string, options: RebuildOptions, forceInstall = false) {
const effectiveOptions = {
buildFromSource: config.buildDependenciesFromSource === true,
additionalArgs: asArray(config.npmArgs),
...options,
}
let isDependenciesInstalled = false

for (const fileOrDir of ["node_modules", ".pnp.js"]) {
if (await pathExists(path.join(appDir, fileOrDir))) {
isDependenciesInstalled = true

break
}
}

if (forceInstall || !isDependenciesInstalled) {
const effectiveOptions: RebuildOptions = {
buildFromSource: config.buildDependenciesFromSource === true,
additionalArgs: asArray(config.npmArgs),
...options,
}
await installDependencies(appDir, effectiveOptions)
} else {
const arch = archFromString(options.arch || process.arch)
const platform = Platform.fromString(options.platform || process.platform)
await rebuild(appDir, config.buildDependenciesFromSource === true, options.frameworkInfo, arch, platform)
await rebuild(appDir, effectiveOptions)
}
}

Expand Down Expand Up @@ -120,8 +119,23 @@ function installDependencies(appDir: string, options: RebuildOptions): Promise<a
})
}

export async function nodeGypRebuild(frameworkInfo: DesktopFrameworkInfo, arch: Arch, platform: Platform) {
return rebuild(process.cwd(), false, frameworkInfo, arch, platform)
export async function nodeGypRebuild(platform: NodeJS.Platform, arch: string, frameworkInfo: DesktopFrameworkInfo) {
log.info({ platform, arch }, "executing node-gyp rebuild")
// this script must be used only for electron
const nodeGyp = `node-gyp${process.platform === "win32" ? ".cmd" : ""}`
const args = ["rebuild"]
// headers of old Electron versions do not have a valid config.gypi file
// and --force-process-config must be passed to node-gyp >= 8.4.0 to
// correctly build modules for them.
// see also https://github.com/nodejs/node-gyp/pull/2497
const [major, minor] = frameworkInfo.version
.split(".")
.slice(0, 2)
.map(n => parseInt(n, 10))
if (major <= 13 || (major == 14 && minor <= 1) || (major == 15 && minor <= 2)) {
args.push("--force-process-config")
}
await spawn(nodeGyp, args, { env: getGypEnv(frameworkInfo, platform, arch, true) })
}

function getPackageToolPath() {
Expand All @@ -139,6 +153,7 @@ function isRunningYarn(execPath: string | null | undefined) {

export interface RebuildOptions {
frameworkInfo: DesktopFrameworkInfo
productionDeps?: Lazy<Array<NodeModuleDirInfo>>

platform?: NodeJS.Platform
arch?: string
Expand All @@ -149,21 +164,17 @@ export interface RebuildOptions {
}

/** @internal */
export async function rebuild(appDir: string, buildFromSource: boolean, frameworkInfo: DesktopFrameworkInfo, arch: Arch, platform: Platform) {
log.info({ arch: Arch[arch], platform: platform.name, version: frameworkInfo.version, appDir }, "executing @electron/rebuild")
const rootPath = await searchModule.getProjectRootPath(appDir)
const rebuilderOptions: electronRebuild.RebuilderOptions = {
buildPath: appDir,
electronVersion: frameworkInfo.version,
arch: Arch[arch],
projectRootPath: rootPath,
disablePreGypCopy: true,
lifecycle: new EventEmitter(),
}
if (buildFromSource) {
rebuilderOptions.prebuildTagPrefix = "totally-not-a-real-prefix-to-force-rebuild"
export async function rebuild(appDir: string, options: RebuildOptions) {
const configuration: any = {
dependencies: await options.productionDeps!.value,
nodeExecPath: process.execPath,
platform: options.platform || process.platform,
arch: options.arch || process.arch,
additionalArgs: options.additionalArgs,
execPath: process.env.npm_execpath || process.env.NPM_CLI_JS,
buildFromSource: options.buildFromSource === true,
}
const rebuilder = new electronRebuild.Rebuilder(rebuilderOptions)
rebuilder.platform = platform.nodeName
return rebuilder.rebuild()

const env = getGypEnv(options.frameworkInfo, configuration.platform, configuration.arch, options.buildFromSource === true)
await executeAppBuilderAndWriteJson(["rebuild-node-modules"], configuration, { env, cwd: appDir })
}
2 changes: 1 addition & 1 deletion packages/electron-builder/src/cli/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,5 @@ async function checkIsOutdated() {
async function rebuildAppNativeCode(args: any) {
const projectDir = process.cwd()
// this script must be used only for electron
return nodeGypRebuild({ version: await getElectronVersion(projectDir), useCustomDist: true }, args.arch, args.platform)
return nodeGypRebuild(args.platform, args.arch, { version: await getElectronVersion(projectDir), useCustomDist: true })
}
Loading

0 comments on commit bffd6a0

Please sign in to comment.