Skip to content

Commit

Permalink
add beforePack hook
Browse files Browse the repository at this point in the history
  • Loading branch information
gaodeng committed Aug 23, 2021
1 parent 7dd33bc commit e672999
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 23 deletions.
14 changes: 14 additions & 0 deletions packages/app-builder-lib/scheme.json
Expand Up @@ -5820,6 +5820,20 @@
],
"description": "The function (or path to file or module id) to be [run after all artifacts are build](#afterAllArtifactBuild)."
},
"beforePack": {
"anyOf": [
{
"typeof": "function"
},
{
"type": [
"null",
"string"
]
}
],
"description": "The function (or path to file or module id) to be [run before pack](#beforepack)"
},
"afterPack": {
"anyOf": [
{
Expand Down
4 changes: 2 additions & 2 deletions packages/app-builder-lib/src/Framework.ts
@@ -1,6 +1,6 @@
import { FileTransformer } from "builder-util/out/fs"
import { AsarIntegrity } from "./asar/integrity"
import { Platform, PlatformPackager, ElectronPlatformName, AfterPackContext } from "./index"
import { Platform, PlatformPackager, ElectronPlatformName, PackContext } from "./index"

export interface Framework {
readonly name: string
Expand All @@ -23,7 +23,7 @@ export interface Framework {

beforeCopyExtraFiles?(options: BeforeCopyExtraFilesOptions): Promise<any>

afterPack?(context: AfterPackContext): Promise<any>
afterPack?(context: PackContext): Promise<any>

createTransformer?(): FileTransformer | null
}
Expand Down
8 changes: 5 additions & 3 deletions packages/app-builder-lib/src/configuration.ts
Expand Up @@ -195,14 +195,16 @@ export interface Configuration extends PlatformSpecificBuildOptions {
*/
readonly framework?: string | null

readonly beforePack?: ((context: PackContext) => Promise<any> | any) | string | null

/**
* The function (or path to file or module id) to be [run after pack](#afterpack) (but before pack into distributable format and sign).
*/
readonly afterPack?: ((context: AfterPackContext) => Promise<any> | any) | string | null
readonly afterPack?: ((context: PackContext) => Promise<any> | any) | string | null
/**
* The function (or path to file or module id) to be [run after pack and sign](#aftersign) (but before pack into distributable format).
*/
readonly afterSign?: ((context: AfterPackContext) => Promise<any> | any) | string | null
readonly afterSign?: ((context: PackContext) => Promise<any> | any) | string | null

/**
* The function (or path to file or module id) to be run on artifact build start.
Expand Down Expand Up @@ -262,7 +264,7 @@ export interface Configuration extends PlatformSpecificBuildOptions {
readonly removePackageKeywords?: boolean
}

export interface AfterPackContext {
export interface PackContext {
readonly outDir: string
readonly appOutDir: string
readonly packager: PlatformPackager<any>
Expand Down
4 changes: 2 additions & 2 deletions packages/app-builder-lib/src/frameworks/LibUiFramework.ts
Expand Up @@ -2,7 +2,7 @@ import { emptyDir } from "fs-extra"
import { mkdir, chmod, rename, writeFile } from "fs/promises"
import * as path from "path"
import { executeAppBuilder } from "builder-util"
import { AfterPackContext } from "../configuration"
import { PackContext } from "../configuration"
import { Platform } from "../core"
import { Framework, PrepareApplicationStageDirectoryOptions } from "../Framework"
import { LinuxPackager } from "../linuxPackager"
Expand Down Expand Up @@ -92,7 +92,7 @@ export class LibUiFramework implements Framework {
)
}

async afterPack(context: AfterPackContext) {
async afterPack(context: PackContext) {
const packager = context.packager
if (!this.isUseLaunchUiForPlatform(packager.platform)) {
return
Expand Down
2 changes: 1 addition & 1 deletion packages/app-builder-lib/src/index.ts
Expand Up @@ -22,7 +22,7 @@ export {
CompressionLevel,
} from "./core"
export { getArchSuffix, Arch, archFromString } from "builder-util"
export { Configuration, AfterPackContext, MetadataDirectories } from "./configuration"
export { Configuration, PackContext, MetadataDirectories } from "./configuration"
export { ElectronBrandingOptions, ElectronDownloadOptions, ElectronPlatformName } from "./electron/ElectronFramework"
export { PlatformSpecificBuildOptions, AsarOptions, FileSet, Protocol, ReleaseInfo } from "./options/PlatformSpecificBuildOptions"
export { FileAssociation } from "./options/FileAssociation"
Expand Down
4 changes: 2 additions & 2 deletions packages/app-builder-lib/src/macPackager.ts
Expand Up @@ -9,7 +9,7 @@ import { orIfFileNotExist } from "builder-util/out/promise"
import { AppInfo } from "./appInfo"
import { CertType, CodeSigningInfo, createKeychain, findIdentity, Identity, isSignAllowed, removeKeychain, reportError } from "./codeSign/macCodeSign"
import { DIR_TARGET, Platform, Target } from "./core"
import { AfterPackContext, ElectronPlatformName } from "./index"
import { PackContext, ElectronPlatformName } from "./index"
import { MacConfiguration, MasConfiguration } from "./options/macOptions"
import { Packager } from "./packager"
import { chooseNotNull, PlatformPackager } from "./platformPackager"
Expand Down Expand Up @@ -426,7 +426,7 @@ export default class MacPackager extends PlatformPackager<MacConfiguration> {
}
}

protected async signApp(packContext: AfterPackContext, isAsar: boolean): Promise<any> {
protected async signApp(packContext: PackContext, isAsar: boolean): Promise<any> {
const appFileName = `${this.appInfo.productFilename}.app`

await BluebirdPromise.map(readdir(packContext.appOutDir), (file: string): any => {
Expand Down
8 changes: 4 additions & 4 deletions packages/app-builder-lib/src/packager.ts
Expand Up @@ -9,7 +9,7 @@ import * as path from "path"
import { getArtifactArchName } from "builder-util/out/arch"
import { AppInfo } from "./appInfo"
import { readAsarJson } from "./asar/asar"
import { AfterPackContext, Configuration } from "./configuration"
import { PackContext, Configuration } from "./configuration"
import { Platform, SourceRepositoryInfo, Target } from "./core"
import { createElectronFrameworkSupport } from "./electron/ElectronFramework"
import { Framework } from "./Framework"
Expand Down Expand Up @@ -107,7 +107,7 @@ export class Packager {

private _repositoryInfo = new Lazy<SourceRepositoryInfo | null>(() => getRepositoryInfo(this.projectDir, this.metadata, this.devMetadata))

private readonly afterPackHandlers: Array<(context: AfterPackContext) => Promise<any> | null> = []
private readonly afterPackHandlers: Array<(context: PackContext) => Promise<any> | null> = []

readonly options: PackagerOptions

Expand Down Expand Up @@ -239,7 +239,7 @@ export class Packager {
}
}

addAfterPackHandler(handler: (context: AfterPackContext) => Promise<any> | null) {
addAfterPackHandler(handler: (context: PackContext) => Promise<any> | null) {
this.afterPackHandlers.push(handler)
}

Expand Down Expand Up @@ -522,7 +522,7 @@ export class Packager {
}
}

async afterPack(context: AfterPackContext): Promise<any> {
async afterPack(context: PackContext): Promise<any> {
const afterPack = resolveFunction(this.config.afterPack, "afterPack")
const handlers = this.afterPackHandlers.slice()
if (afterPack != null) {
Expand Down
22 changes: 17 additions & 5 deletions packages/app-builder-lib/src/platformPackager.ts
Expand Up @@ -15,7 +15,7 @@ import { copyFiles, FileMatcher, getFileMatchers, GetFileMatchersOptions, getMai
import { createTransformer, isElectronCompileUsed } from "./fileTransformer"
import { Framework, isElectronBased } from "./Framework"
import {
AfterPackContext,
PackContext,
AsarOptions,
CompressionLevel,
Configuration,
Expand Down Expand Up @@ -201,6 +201,18 @@ export abstract class PlatformPackager<DC extends PlatformSpecificBuildOptions>
return
}

const beforePack = resolveFunction(this.config.beforePack, "beforePack")
if (beforePack != null) {
await beforePack({
appOutDir,
outDir,
arch,
targets,
packager: this,
electronPlatformName: platformName,
})
}

await this.info.installAppDependencies(this.platform, arch)

if (this.info.cancellationToken.cancelled) {
Expand Down Expand Up @@ -243,7 +255,7 @@ export abstract class PlatformPackager<DC extends PlatformSpecificBuildOptions>
const extraFileMatchers = this.getExtraFileMatchers(false, appOutDir, getFileMatchersOptions)
computeParsedPatterns(extraFileMatchers)

const packContext: AfterPackContext = {
const packContext: PackContext = {
appOutDir,
outDir,
arch,
Expand Down Expand Up @@ -320,7 +332,7 @@ export abstract class PlatformPackager<DC extends PlatformSpecificBuildOptions>
}

// eslint-disable-next-line
protected createTransformerForExtraFiles(packContext: AfterPackContext): FileTransformer | null {
protected createTransformerForExtraFiles(packContext: PackContext): FileTransformer | null {
return null
}

Expand All @@ -329,7 +341,7 @@ export abstract class PlatformPackager<DC extends PlatformSpecificBuildOptions>
asarOptions: AsarOptions | null,
resourcePath: string,
defaultDestination: string,
packContext: AfterPackContext,
packContext: PackContext,
platformSpecificBuildOptions: DC,
excludePatterns: Array<Minimatch>,
macroExpander: (it: string) => string
Expand Down Expand Up @@ -408,7 +420,7 @@ export abstract class PlatformPackager<DC extends PlatformSpecificBuildOptions>
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
protected signApp(packContext: AfterPackContext, isAsar: boolean): Promise<any> {
protected signApp(packContext: PackContext, isAsar: boolean): Promise<any> {
return Promise.resolve()
}

Expand Down
6 changes: 3 additions & 3 deletions packages/app-builder-lib/src/winPackager.ts
Expand Up @@ -18,7 +18,7 @@ import {
sign,
WindowsSignOptions,
} from "./codeSign/windowsCodeSign"
import { AfterPackContext } from "./configuration"
import { PackContext } from "./configuration"
import { DIR_TARGET, Platform, Target } from "./core"
import { RequestedExecutionLevel, WindowsConfiguration } from "./options/winOptions"
import { Packager } from "./packager"
Expand Down Expand Up @@ -351,7 +351,7 @@ export class WinPackager extends PlatformPackager<WindowsConfiguration> {
return this.platformSpecificBuildOptions.signDlls === true
}

protected createTransformerForExtraFiles(packContext: AfterPackContext): FileTransformer | null {
protected createTransformerForExtraFiles(packContext: PackContext): FileTransformer | null {
if (this.platformSpecificBuildOptions.signAndEditExecutable === false) {
return null
}
Expand All @@ -367,7 +367,7 @@ export class WinPackager extends PlatformPackager<WindowsConfiguration> {
}
}

protected async signApp(packContext: AfterPackContext, isAsar: boolean): Promise<any> {
protected async signApp(packContext: PackContext, isAsar: boolean): Promise<any> {
const exeFileName = `${this.appInfo.productFilename}.exe`
if (this.platformSpecificBuildOptions.signAndEditExecutable === false) {
return
Expand Down
2 changes: 1 addition & 1 deletion packages/electron-builder/src/index.ts
Expand Up @@ -27,7 +27,7 @@ export {
LinuxTargetSpecificOptions,
AppImageOptions,
Configuration,
AfterPackContext,
PackContext,
MetadataDirectories,
Protocol,
ReleaseInfo,
Expand Down

0 comments on commit e672999

Please sign in to comment.