Skip to content

Commit

Permalink
feat: migrate electronDist to be an electron-builder Hook (#8525)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmaietta authored Oct 6, 2024
1 parent e45fecf commit 13f55a3
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 27 deletions.
5 changes: 5 additions & 0 deletions .changeset/tiny-cobras-walk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"app-builder-lib": minor
---

feat: migrate `electronDist` to be an electron-builder `Hook`
11 changes: 7 additions & 4 deletions packages/app-builder-lib/scheme.json
Original file line number Diff line number Diff line change
Expand Up @@ -6709,7 +6709,7 @@
]
}
],
"description": "Appx manifest created on disk - not packed into .appx package yet."
"description": "The function (or path to file or module id) to be run after Appx manifest created on disk - not packed into .appx package yet."
},
"artifactBuildCompleted": {
"anyOf": [
Expand Down Expand Up @@ -6934,10 +6934,13 @@
"typeof": "function"
},
{
"type": "string"
"type": [
"null",
"string"
]
}
],
"description": "Returns the path to custom Electron build (e.g. `~/electron/out/R`). Zip files must follow the pattern `electron-v${version}-${platformName}-${arch}.zip`, otherwise it will be assumed to be an unpacked Electron app directory"
"description": "The function (or path to file or module id) to be run when staging the electron artifact environment.\nReturns the path to custom Electron build (e.g. `~/electron/out/R`) or folder of electron zips. Zip files must follow the pattern `electron-v${version}-${platformName}-${arch}.zip`, otherwise it will be assumed to be an unpacked Electron app directory"
},
"electronDownload": {
"$ref": "#/definitions/ElectronDownloadOptions",
Expand Down Expand Up @@ -7221,7 +7224,7 @@
]
}
],
"description": "MSI project created on disk - not packed into .msi package yet."
"description": "The function (or path to file or module id) to be run after MSI project created on disk - not packed into .msi package yet."
},
"msiWrapped": {
"anyOf": [
Expand Down
16 changes: 7 additions & 9 deletions packages/app-builder-lib/src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,6 @@ export interface Configuration extends CommonConfiguration, PlatformSpecificBuil
*/
readonly electronCompile?: boolean

/**
* Returns the path to custom Electron build (e.g. `~/electron/out/R`). Zip files must follow the pattern `electron-v${version}-${platformName}-${arch}.zip`, otherwise it will be assumed to be an unpacked Electron app directory
*/
readonly electronDist?: string | CustomElectronDistributable

/**
* The [electron-download](/~https://github.com/electron-userland/electron-download#usage) options.
*/
Expand Down Expand Up @@ -246,8 +241,6 @@ export interface Configuration extends CommonConfiguration, PlatformSpecificBuil
readonly disableSanityCheckAsar?: boolean
}

export type CustomElectronDistributable = (options: PrepareApplicationStageDirectoryOptions) => string

export type Hook<T, V> = (contextOrPath: T) => Promise<V> | V

export interface PackContext {
Expand Down Expand Up @@ -344,11 +337,11 @@ Configuration in the same way as `afterPack` (see above).
*/
readonly afterAllArtifactBuild?: Hook<BuildResult, Array<string>> | string | null
/**
* MSI project created on disk - not packed into .msi package yet.
* The function (or path to file or module id) to be run after MSI project created on disk - not packed into .msi package yet.
*/
readonly msiProjectCreated?: Hook<string, any> | string | null
/**
* Appx manifest created on disk - not packed into .appx package yet.
* The function (or path to file or module id) to be run after Appx manifest created on disk - not packed into .appx package yet.
*/
readonly appxManifestCreated?: Hook<string, any> | string | null
/**
Expand All @@ -361,6 +354,11 @@ Configuration in the same way as `afterPack` (see above).
* If provided and `node_modules` are missing, it will not invoke production dependencies check.
*/
readonly beforeBuild?: Hook<BeforeBuildContext, boolean | void> | string | null
/**
* The function (or path to file or module id) to be run when staging the electron artifact environment.
* Returns the path to custom Electron build (e.g. `~/electron/out/R`) or folder of electron zips. Zip files must follow the pattern `electron-v${version}-${platformName}-${arch}.zip`, otherwise it will be assumed to be an unpacked Electron app directory
*/
readonly electronDist?: Hook<PrepareApplicationStageDirectoryOptions, string> | string | null
}

export interface MetadataDirectories {
Expand Down
12 changes: 10 additions & 2 deletions packages/app-builder-lib/src/electron/ElectronFramework.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { addWinAsarIntegrity } from "./electronWin"
import { computeElectronVersion, getElectronVersionFromInstalled } from "./electronVersion"
import * as fs from "fs/promises"
import injectFFMPEG from "./injectFFMPEG"
import { resolveFunction } from "../util/resolve"

export type ElectronPlatformName = "darwin" | "linux" | "win32" | "mas"

Expand Down Expand Up @@ -184,15 +185,22 @@ export async function createElectronFrameworkSupport(configuration: Configuratio
async function unpack(prepareOptions: PrepareApplicationStageDirectoryOptions, options: ElectronDownloadOptions, distMacOsAppName: string) {
const { packager, appOutDir, platformName } = prepareOptions

const electronDist = packager.config.electronDist
let dist: string | undefined | null = typeof electronDist === "function" ? electronDist(prepareOptions) : electronDist
let customElectronDist = packager.config.electronDist
try {
customElectronDist = await resolveFunction(packager.appInfo.type, customElectronDist, "electronDist")
} catch (_e: any) {
// ignored. We already log in `resolveFunction` if it fails, and we ignore here because electronDist could just be a folder path (backward compatibility)
}
let dist: string | undefined | null = await Promise.resolve(typeof customElectronDist === "function" ? customElectronDist(prepareOptions) : customElectronDist)
if (dist != null) {
const zipFile = `electron-v${options.version}-${platformName}-${options.arch}.zip`
const resolvedDist = path.isAbsolute(dist) ? dist : path.resolve(packager.projectDir, dist)
if ((await statOrNull(path.join(resolvedDist, zipFile))) != null) {
log.info({ resolvedDist, zipFile }, "resolved electronDist")
options.cache = resolvedDist
dist = null
} else {
log.warn({ electronDist: log.filePath(resolvedDist), expectedFile: zipFile }, "custom electronDist provided but no assets found")
}
}

Expand Down
13 changes: 1 addition & 12 deletions packages/app-builder-lib/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,7 @@ export {
CompressionLevel,
} from "./core"
export { getArchSuffix, Arch, archFromString } from "builder-util"
export {
CommonConfiguration,
Configuration,
AfterPackContext,
MetadataDirectories,
BeforePackContext,
AfterExtractContext,
CustomElectronDistributable,
Hooks,
Hook,
PackContext,
} from "./configuration"
export { CommonConfiguration, Configuration, AfterPackContext, MetadataDirectories, BeforePackContext, AfterExtractContext, Hooks, Hook, PackContext } from "./configuration"
export { ElectronBrandingOptions, ElectronDownloadOptions, ElectronPlatformName } from "./electron/ElectronFramework"
export { PlatformSpecificBuildOptions, AsarOptions, FileSet, Protocol, ReleaseInfo, FilesBuildOptions } from "./options/PlatformSpecificBuildOptions"
export { FileAssociation } from "./options/FileAssociation"
Expand Down
6 changes: 6 additions & 0 deletions test/snapshots/ExtraBuildResourcesTest.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ Object {
}
`;

exports[`electronDist as callback function for path to local folder with electron builds zipped 1`] = `
Object {
"linux": Array [],
}
`;

exports[`electronDist as path to local folder with electron builds zipped 1`] = `
Object {
"linux": Array [],
Expand Down
12 changes: 12 additions & 0 deletions test/src/ExtraBuildResourcesTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,18 @@ test.ifNotWindows(
})
)

test.ifNotWindows(
"electronDist as callback function for path to local folder with electron builds zipped ",
app({
targets: linuxDirTarget,
config: {
electronDist: (_context) => {
return Promise.resolve(getElectronCacheDir())
}
},
})
)

const overridePublishChannel: any = {
channel: "beta",
}
Expand Down

0 comments on commit 13f55a3

Please sign in to comment.