Skip to content

Commit e59f6c3

Browse files
committed
feat: artifactBuildStarted event #3493
1 parent 88d8a6b commit e59f6c3

File tree

25 files changed

+175
-65
lines changed

25 files changed

+175
-65
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"dependencies": {
3333
"7zip-bin": "~4.1.0",
3434
"@types/is-ci": "^1.1.0",
35-
"app-builder-bin": "2.5.4",
35+
"app-builder-bin": "2.5.5",
3636
"archiver": "^3.0.0",
3737
"async-exit-hook": "^2.0.1",
3838
"bluebird-lst": "^1.0.6",
@@ -50,7 +50,7 @@
5050
"js-yaml": "^3.12.0",
5151
"lazy-val": "^1.0.3",
5252
"lodash.isequal": "^4.5.0",
53-
"mime": "^2.3.1",
53+
"mime": "^2.4.0",
5454
"minimatch": "^3.0.4",
5555
"normalize-package-data": "^2.4.0",
5656
"pako": "^1.0.6",

packages/app-builder-lib/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
"homepage": "https://github.com/electron-userland/electron-builder",
4343
"dependencies": {
4444
"7zip-bin": "~4.1.0",
45-
"app-builder-bin": "2.5.4",
45+
"app-builder-bin": "2.5.5",
4646
"async-exit-hook": "^2.0.1",
4747
"bluebird-lst": "^1.0.6",
4848
"chromium-pickle-js": "^0.2.0",

packages/app-builder-lib/src/configuration.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { SnapOptions } from "./options/SnapOptions"
1111
import { SquirrelWindowsOptions } from "./options/SquirrelWindowsOptions"
1212
import { WindowsConfiguration } from "./options/winOptions"
1313
import { BuildResult } from "./packager"
14+
import { ArtifactBuildStarted, ArtifactCreated } from "./packagerApi"
1415
import { PlatformPackager } from "./platformPackager"
1516
import { NsisOptions, NsisWebOptions, PortableOptions } from "./targets/nsis/nsisOptions"
1617

@@ -196,10 +197,20 @@ export interface Configuration extends PlatformSpecificBuildOptions {
196197
* The function (or path to file or module id) to be [run after pack and sign](#aftersign) (but before pack into distributable format).
197198
*/
198199
readonly afterSign?: ((context: AfterPackContext) => Promise<any> | any) | string | null
200+
201+
/**
202+
* The function (or path to file or module id) to be run on artifact build start.
203+
*/
204+
readonly artifactBuildStarted?: ((context: ArtifactBuildStarted) => Promise<any> | any) | string | null
205+
/**
206+
* The function (or path to file or module id) to be run on artifact build start.
207+
*/
208+
readonly artifactBuildCompleted?: ((context: ArtifactCreated) => Promise<any> | any) | string | null
199209
/**
200210
* The function (or path to file or module id) to be [run after all artifacts are build](#afterAllArtifactBuild).
201211
*/
202212
readonly afterAllArtifactBuild?: ((context: BuildResult) => Promise<Array<string>> | Array<string>) | string | null
213+
203214
/**
204215
* The function (or path to file or module id) to be [run on each node module](#onnodemodulefile) file.
205216
*/

packages/app-builder-lib/src/core.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Arch, archFromString, ArchType, log } from "builder-util"
1+
import { Arch, archFromString, ArchType } from "builder-util"
22
import { Publish } from "builder-util-runtime"
33

44
export type TargetConfigType = Array<string | TargetConfiguration> | string | TargetConfiguration | null
@@ -75,11 +75,6 @@ export abstract class Target {
7575
protected constructor(readonly name: string, readonly isAsyncSupported: boolean = true) {
7676
}
7777

78-
// noinspection JSMethodCanBeStatic
79-
protected logBuilding(targetPresentableName: string, artifactPath: string, arch: Arch): void {
80-
log.info({target: targetPresentableName, arch: Arch[arch], file: log.filePath(artifactPath)}, "building")
81-
}
82-
8378
async checkOptions(): Promise<any> {
8479
// ignore
8580
}

packages/app-builder-lib/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { resolveFunction } from "./platformPackager"
88
import { PublishManager } from "./publish/PublishManager"
99

1010
export { Packager, BuildResult } from "./packager"
11-
export { PackagerOptions, ArtifactCreated } from "./packagerApi"
11+
export { PackagerOptions, ArtifactCreated, ArtifactBuildStarted } from "./packagerApi"
1212
export { TargetConfiguration, Platform, Target, DIR_TARGET, BeforeBuildContext, SourceRepositoryInfo, TargetSpecificOptions, TargetConfigType, DEFAULT_TARGET, CompressionLevel } from "./core"
1313
export { getArchSuffix, Arch, archFromString } from "builder-util"
1414
export { Configuration, AfterPackContext, MetadataDirectories } from "./configuration"

packages/app-builder-lib/src/macPackager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ export default class MacPackager extends PlatformPackager<MacConfiguration> {
196196
const artifactName = this.expandArtifactNamePattern(masOptions, "pkg")
197197
const artifactPath = path.join(outDir!, artifactName)
198198
await this.doFlat(appPath, artifactPath, masInstallerIdentity, keychainName)
199-
this.dispatchArtifactCreated(artifactPath, null, Arch.x64, this.computeSafeArtifactName(artifactName, "pkg"))
199+
await this.dispatchArtifactCreated(artifactPath, null, Arch.x64, this.computeSafeArtifactName(artifactName, "pkg"))
200200
}
201201
}
202202

packages/app-builder-lib/src/packager.ts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { LibUiFramework } from "./frameworks/LibUiFramework"
1515
import { AfterPackContext, Configuration, Framework, Platform, SourceRepositoryInfo, Target } from "./index"
1616
import MacPackager from "./macPackager"
1717
import { Metadata } from "./options/metadata"
18-
import { ArtifactCreated, PackagerOptions } from "./packagerApi"
18+
import { ArtifactBuildStarted, ArtifactCreated, PackagerOptions } from "./packagerApi"
1919
import { PlatformPackager, resolveFunction } from "./platformPackager"
2020
import { ProtonFramework } from "./ProtonFramework"
2121
import { computeArchToTargetNamesMap, createTargets, NoOpTarget } from "./targets/targetFactory"
@@ -257,10 +257,34 @@ export class Packager {
257257
return this
258258
}
259259

260-
dispatchArtifactCreated(event: ArtifactCreated) {
260+
async callArtifactBuildStarted(event: ArtifactBuildStarted, logFields?: any): Promise<void> {
261+
log.info(logFields || {
262+
target: event.targetPresentableName,
263+
arch: event.arch == null ? null : Arch[event.arch],
264+
file: log.filePath(event.file),
265+
}, "building")
266+
const handler = resolveFunction(this.config.artifactBuildStarted, "artifactBuildStarted")
267+
if (handler != null) {
268+
await Promise.resolve(handler(event))
269+
}
270+
}
271+
272+
/**
273+
* Only for sub artifacts (update info), for main artifacts use `callArtifactBuildCompleted`.
274+
*/
275+
dispatchArtifactCreated(event: ArtifactCreated): void {
261276
this.eventEmitter.emit("artifactCreated", event)
262277
}
263278

279+
async callArtifactBuildCompleted(event: ArtifactCreated): Promise<void> {
280+
this.dispatchArtifactCreated(event)
281+
282+
const handler = resolveFunction(this.config.artifactBuildCompleted, "artifactBuildCompleted")
283+
if (handler != null) {
284+
await Promise.resolve(handler(event))
285+
}
286+
}
287+
264288
async build(): Promise<BuildResult> {
265289
let configPath: string | null = null
266290
let configFromOptions = this.options.config

packages/app-builder-lib/src/packagerApi.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,12 @@ export interface ArtifactCreated extends UploadTask {
3535
readonly publishConfig?: PublishConfiguration
3636

3737
readonly isWriteUpdateInfo?: boolean
38+
}
39+
40+
export interface ArtifactBuildStarted {
41+
readonly targetPresentableName: string
42+
43+
readonly file: string
44+
// null for NSIS
45+
readonly arch: Arch | null
3846
}

packages/app-builder-lib/src/platformPackager.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ export abstract class PlatformPackager<DC extends PlatformSpecificBuildOptions>
101101
return this.packagerOptions.prepackaged || path.join(outDir, `${this.platform.buildConfigurationKey}${getArchSuffix(arch)}${this.platform === Platform.MAC ? "" : "-unpacked"}`)
102102
}
103103

104-
dispatchArtifactCreated(file: string, target: Target | null, arch: Arch | null, safeArtifactName?: string | null) {
105-
this.info.dispatchArtifactCreated({
104+
dispatchArtifactCreated(file: string, target: Target | null, arch: Arch | null, safeArtifactName?: string | null): Promise<void> {
105+
return this.info.callArtifactBuildCompleted({
106106
file, safeArtifactName, target, arch,
107107
packager: this,
108108
})

packages/app-builder-lib/src/remoteBuilder/RemoteBuilder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ export class RemoteBuilder {
9494
const localFile = path.join(outDir, artifact.file)
9595
const artifactCreatedEvent = this.artifactInfoToArtifactCreatedEvent(artifact, localFile, outDir)
9696
// PublishManager uses outDir and options, real (the same as for local build) values must be used
97-
this.packager.info.dispatchArtifactCreated(artifactCreatedEvent)
97+
await this.packager.info.callArtifactBuildCompleted(artifactCreatedEvent)
9898
}
9999
}
100100
}

packages/app-builder-lib/src/targets/AppImageTarget.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,11 @@ export default class AppImageTarget extends Target {
3333
// tslint:disable-next-line:no-invalid-template-strings
3434
const artifactName = packager.expandArtifactBeautyNamePattern(options, "AppImage", arch)
3535
const artifactPath = path.join(this.outDir, artifactName)
36-
this.logBuilding("AppImage", artifactPath, arch)
36+
await packager.info.callArtifactBuildStarted({
37+
targetPresentableName: "AppImage",
38+
file: artifactPath,
39+
arch,
40+
})
3741

3842
const c = await Promise.all([
3943
this.desktopEntry.value,
@@ -76,7 +80,7 @@ export default class AppImageTarget extends Target {
7680
args.push("--compression", "xz")
7781
}
7882

79-
packager.info.dispatchArtifactCreated({
83+
await packager.info.callArtifactBuildCompleted({
8084
file: artifactPath,
8185
safeArtifactName: packager.computeSafeArtifactName(artifactName, "AppImage", arch, false),
8286
target: this,

packages/app-builder-lib/src/targets/AppxTarget.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,11 @@ export default class AppXTarget extends Target {
3838
const packager = this.packager
3939
const artifactName = packager.expandArtifactBeautyNamePattern(this.options, "appx", arch)
4040
const artifactPath = path.join(this.outDir, artifactName)
41-
this.logBuilding("AppX", artifactPath, arch)
41+
await packager.info.callArtifactBuildStarted({
42+
targetPresentableName: "AppX",
43+
file: artifactPath,
44+
arch,
45+
})
4246

4347
const vendorPath = await getSignVendorPath()
4448
const vm = await packager.vm.value
@@ -110,7 +114,7 @@ export default class AppXTarget extends Target {
110114

111115
await stageDir.cleanup()
112116

113-
packager.info.dispatchArtifactCreated({
117+
await packager.info.callArtifactBuildCompleted({
114118
file: artifactPath,
115119
packager,
116120
arch,

packages/app-builder-lib/src/targets/ArchiveTarget.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ export class ArchiveTarget extends Target {
3131
const artifactName = packager.expandArtifactNamePattern(this.options, format, arch, defaultPattern, false)
3232
const artifactPath = path.join(this.outDir, artifactName)
3333

34-
this.logBuilding(`${isMac ? "macOS " : ""}${format}`, artifactPath, arch)
34+
await packager.info.callArtifactBuildStarted({
35+
targetPresentableName: `${isMac ? "macOS " : ""}${format}`,
36+
file: artifactPath,
37+
arch,
38+
})
3539
let updateInfo: any = null
3640
if (format.startsWith("tar.")) {
3741
await tar(packager.compression, format, artifactPath, appOutDir, isMac, packager.info.tempDirManager)
@@ -62,7 +66,7 @@ export class ArchiveTarget extends Target {
6266
}
6367
}
6468

65-
packager.info.dispatchArtifactCreated({
69+
await packager.info.callArtifactBuildCompleted({
6670
updateInfo,
6771
file: artifactPath,
6872
// tslint:disable-next-line:no-invalid-template-strings

packages/app-builder-lib/src/targets/MsiTarget.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,11 @@ export default class MsiTarget extends Target {
4444
const packager = this.packager
4545
const artifactName = packager.expandArtifactBeautyNamePattern(this.options, "msi", arch)
4646
const artifactPath = path.join(this.outDir, artifactName)
47-
this.logBuilding("MSI", artifactPath, arch)
47+
await packager.info.callArtifactBuildStarted({
48+
targetPresentableName: "MSI",
49+
file: artifactPath,
50+
arch,
51+
})
4852

4953
const stageDir = await createStageDir(this, packager, arch)
5054
const vm = this.vm
@@ -88,7 +92,7 @@ export default class MsiTarget extends Target {
8892

8993
await packager.sign(artifactPath)
9094

91-
packager.info.dispatchArtifactCreated({
95+
await packager.info.callArtifactBuildCompleted({
9296
file: artifactPath,
9397
packager,
9498
arch,

packages/app-builder-lib/src/targets/differentialUpdateInfoBuilder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export async function createBlockmap(file: string, target: Target, packager: Pla
7171
const blockMapFile = `${file}${BLOCK_MAP_FILE_SUFFIX}`
7272
log.info({blockMapFile: log.filePath(blockMapFile)}, "building block map")
7373
const updateInfo = await executeAppBuilderAsJson<BlockMapDataHolder>(["blockmap", "--input", file, "--output", blockMapFile])
74-
packager.info.dispatchArtifactCreated({
74+
await packager.info.callArtifactBuildCompleted({
7575
file: blockMapFile,
7676
safeArtifactName: safeArtifactName == null ? null : `${safeArtifactName}${BLOCK_MAP_FILE_SUFFIX}`,
7777
target,

packages/app-builder-lib/src/targets/fpm.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -107,17 +107,21 @@ export default class FpmTarget extends Target {
107107
isUseArchIfX64 = true
108108
}
109109

110-
const artifactPath = path.join(this.outDir, this.packager.expandArtifactNamePattern(this.options, target, arch, nameFormat, !isUseArchIfX64))
110+
const packager = this.packager
111+
const artifactPath = path.join(this.outDir, packager.expandArtifactNamePattern(this.options, target, arch, nameFormat, !isUseArchIfX64))
111112

112-
this.logBuilding(target, artifactPath, arch)
113+
await packager.info.callArtifactBuildStarted({
114+
targetPresentableName: target,
115+
file: artifactPath,
116+
arch,
117+
})
113118

114119
await unlinkIfExists(artifactPath)
115-
if (this.packager.packagerOptions.prepackaged != null) {
120+
if (packager.packagerOptions.prepackaged != null) {
116121
await ensureDir(this.outDir)
117122
}
118123

119124
const scripts = await this.scriptFiles
120-
const packager = this.packager
121125
const appInfo = packager.appInfo
122126
const options = this.options
123127
const synopsis = options.synopsis
@@ -162,7 +166,7 @@ export default class FpmTarget extends Target {
162166
}
163167

164168
// noinspection JSDeprecatedSymbols
165-
let depends = options.depends || this.packager.platformSpecificBuildOptions.depends
169+
let depends = options.depends || packager.platformSpecificBuildOptions.depends
166170
if (depends == null) {
167171
if (target === "deb") {
168172
depends = ["gconf2", "gconf-service", "libnotify4", "libappindicator1", "libxtst6", "libnss3", "libxss1"]
@@ -206,9 +210,9 @@ export default class FpmTarget extends Target {
206210
}
207211

208212
const desktopFilePath = await this.helper.writeDesktopEntry(this.options)
209-
args.push(`${desktopFilePath}=/usr/share/applications/${this.packager.executableName}.desktop`)
213+
args.push(`${desktopFilePath}=/usr/share/applications/${packager.executableName}.desktop`)
210214

211-
if (this.packager.packagerOptions.effectiveOptionComputed != null && await this.packager.packagerOptions.effectiveOptionComputed([args, desktopFilePath])) {
215+
if (packager.packagerOptions.effectiveOptionComputed != null && await packager.packagerOptions.effectiveOptionComputed([args, desktopFilePath])) {
212216
return
213217
}
214218

@@ -231,7 +235,7 @@ export default class FpmTarget extends Target {
231235
}
232236
await exec(await fpmPath.value, args, {env})
233237

234-
this.packager.dispatchArtifactCreated(artifactPath, this, arch)
238+
await packager.dispatchArtifactCreated(artifactPath, this, arch)
235239
}
236240
}
237241

packages/app-builder-lib/src/targets/nsis/NsisTarget.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,12 @@ export class NsisTarget extends Target {
140140
logFields.oneClick = oneClick
141141
logFields.perMachine = isPerMachine
142142
}
143-
log.info(logFields, "building")
143+
144+
await packager.info.callArtifactBuildStarted({
145+
targetPresentableName: this.name,
146+
file: installerPath,
147+
arch: null,
148+
}, logFields)
144149

145150
const guid = options.guid || UUID.v5(appInfo.id, ELECTRON_BUILDER_NS_UUID)
146151
const uninstallAppKey = guid.replace(/\\/g, " - ")
@@ -202,7 +207,7 @@ export class NsisTarget extends Target {
202207
defines[`${defineKey}_HASH`] = Buffer.from(fileInfo.sha512, "base64").toString("hex").toUpperCase()
203208

204209
if (this.isWebInstaller) {
205-
packager.dispatchArtifactCreated(file, this, arch)
210+
await packager.dispatchArtifactCreated(file, this, arch)
206211
packageFiles[Arch[arch]] = fileInfo
207212
}
208213

@@ -270,7 +275,7 @@ export class NsisTarget extends Target {
270275
updateInfo.isAdminRightsRequired = true
271276
}
272277

273-
packager.info.dispatchArtifactCreated({
278+
await packager.info.callArtifactBuildCompleted({
274279
file: installerPath,
275280
updateInfo,
276281
target: this,

packages/app-builder-lib/src/targets/pkg.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,11 @@ export class PkgTarget extends Target {
3737
const artifactName = packager.expandArtifactNamePattern(options, "pkg")
3838
const artifactPath = path.join(this.outDir, artifactName)
3939

40-
this.logBuilding("pkg", artifactPath, arch)
40+
await packager.info.callArtifactBuildStarted({
41+
targetPresentableName: "pkg",
42+
file: artifactPath,
43+
arch,
44+
})
4145

4246
const keychainName = (await packager.codeSigningInfo.value).keychainName
4347

@@ -66,7 +70,7 @@ export class PkgTarget extends Target {
6670
})
6771
await Promise.all([unlink(innerPackageFile), unlink(distInfoFile)])
6872

69-
packager.dispatchArtifactCreated(artifactPath, this, arch, packager.computeSafeArtifactName(artifactName, "pkg", arch))
73+
await packager.dispatchArtifactCreated(artifactPath, this, arch, packager.computeSafeArtifactName(artifactName, "pkg", arch))
7074
}
7175

7276
private async customizeDistributionConfiguration(distInfoFile: string, appPath: string) {

0 commit comments

Comments
 (0)