Skip to content

Commit

Permalink
feat: artifactBuildStarted event electron-userland#3493
Browse files Browse the repository at this point in the history
  • Loading branch information
develar committed Nov 27, 2018
1 parent 88d8a6b commit e59f6c3
Show file tree
Hide file tree
Showing 25 changed files with 175 additions and 65 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"dependencies": {
"7zip-bin": "~4.1.0",
"@types/is-ci": "^1.1.0",
"app-builder-bin": "2.5.4",
"app-builder-bin": "2.5.5",
"archiver": "^3.0.0",
"async-exit-hook": "^2.0.1",
"bluebird-lst": "^1.0.6",
Expand All @@ -50,7 +50,7 @@
"js-yaml": "^3.12.0",
"lazy-val": "^1.0.3",
"lodash.isequal": "^4.5.0",
"mime": "^2.3.1",
"mime": "^2.4.0",
"minimatch": "^3.0.4",
"normalize-package-data": "^2.4.0",
"pako": "^1.0.6",
Expand Down
2 changes: 1 addition & 1 deletion packages/app-builder-lib/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"homepage": "https://github.com/electron-userland/electron-builder",
"dependencies": {
"7zip-bin": "~4.1.0",
"app-builder-bin": "2.5.4",
"app-builder-bin": "2.5.5",
"async-exit-hook": "^2.0.1",
"bluebird-lst": "^1.0.6",
"chromium-pickle-js": "^0.2.0",
Expand Down
11 changes: 11 additions & 0 deletions packages/app-builder-lib/src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { SnapOptions } from "./options/SnapOptions"
import { SquirrelWindowsOptions } from "./options/SquirrelWindowsOptions"
import { WindowsConfiguration } from "./options/winOptions"
import { BuildResult } from "./packager"
import { ArtifactBuildStarted, ArtifactCreated } from "./packagerApi"
import { PlatformPackager } from "./platformPackager"
import { NsisOptions, NsisWebOptions, PortableOptions } from "./targets/nsis/nsisOptions"

Expand Down Expand Up @@ -196,10 +197,20 @@ export interface Configuration extends PlatformSpecificBuildOptions {
* 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

/**
* The function (or path to file or module id) to be run on artifact build start.
*/
readonly artifactBuildStarted?: ((context: ArtifactBuildStarted) => Promise<any> | any) | string | null
/**
* The function (or path to file or module id) to be run on artifact build start.
*/
readonly artifactBuildCompleted?: ((context: ArtifactCreated) => Promise<any> | any) | string | null
/**
* The function (or path to file or module id) to be [run after all artifacts are build](#afterAllArtifactBuild).
*/
readonly afterAllArtifactBuild?: ((context: BuildResult) => Promise<Array<string>> | Array<string>) | string | null

/**
* The function (or path to file or module id) to be [run on each node module](#onnodemodulefile) file.
*/
Expand Down
7 changes: 1 addition & 6 deletions packages/app-builder-lib/src/core.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Arch, archFromString, ArchType, log } from "builder-util"
import { Arch, archFromString, ArchType } from "builder-util"
import { Publish } from "builder-util-runtime"

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

// noinspection JSMethodCanBeStatic
protected logBuilding(targetPresentableName: string, artifactPath: string, arch: Arch): void {
log.info({target: targetPresentableName, arch: Arch[arch], file: log.filePath(artifactPath)}, "building")
}

async checkOptions(): Promise<any> {
// ignore
}
Expand Down
2 changes: 1 addition & 1 deletion packages/app-builder-lib/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { resolveFunction } from "./platformPackager"
import { PublishManager } from "./publish/PublishManager"

export { Packager, BuildResult } from "./packager"
export { PackagerOptions, ArtifactCreated } from "./packagerApi"
export { PackagerOptions, ArtifactCreated, ArtifactBuildStarted } from "./packagerApi"
export { TargetConfiguration, Platform, Target, DIR_TARGET, BeforeBuildContext, SourceRepositoryInfo, TargetSpecificOptions, TargetConfigType, DEFAULT_TARGET, CompressionLevel } from "./core"
export { getArchSuffix, Arch, archFromString } from "builder-util"
export { Configuration, AfterPackContext, MetadataDirectories } from "./configuration"
Expand Down
2 changes: 1 addition & 1 deletion packages/app-builder-lib/src/macPackager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ export default class MacPackager extends PlatformPackager<MacConfiguration> {
const artifactName = this.expandArtifactNamePattern(masOptions, "pkg")
const artifactPath = path.join(outDir!, artifactName)
await this.doFlat(appPath, artifactPath, masInstallerIdentity, keychainName)
this.dispatchArtifactCreated(artifactPath, null, Arch.x64, this.computeSafeArtifactName(artifactName, "pkg"))
await this.dispatchArtifactCreated(artifactPath, null, Arch.x64, this.computeSafeArtifactName(artifactName, "pkg"))
}
}

Expand Down
28 changes: 26 additions & 2 deletions packages/app-builder-lib/src/packager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { LibUiFramework } from "./frameworks/LibUiFramework"
import { AfterPackContext, Configuration, Framework, Platform, SourceRepositoryInfo, Target } from "./index"
import MacPackager from "./macPackager"
import { Metadata } from "./options/metadata"
import { ArtifactCreated, PackagerOptions } from "./packagerApi"
import { ArtifactBuildStarted, ArtifactCreated, PackagerOptions } from "./packagerApi"
import { PlatformPackager, resolveFunction } from "./platformPackager"
import { ProtonFramework } from "./ProtonFramework"
import { computeArchToTargetNamesMap, createTargets, NoOpTarget } from "./targets/targetFactory"
Expand Down Expand Up @@ -257,10 +257,34 @@ export class Packager {
return this
}

dispatchArtifactCreated(event: ArtifactCreated) {
async callArtifactBuildStarted(event: ArtifactBuildStarted, logFields?: any): Promise<void> {
log.info(logFields || {
target: event.targetPresentableName,
arch: event.arch == null ? null : Arch[event.arch],
file: log.filePath(event.file),
}, "building")
const handler = resolveFunction(this.config.artifactBuildStarted, "artifactBuildStarted")
if (handler != null) {
await Promise.resolve(handler(event))
}
}

/**
* Only for sub artifacts (update info), for main artifacts use `callArtifactBuildCompleted`.
*/
dispatchArtifactCreated(event: ArtifactCreated): void {
this.eventEmitter.emit("artifactCreated", event)
}

async callArtifactBuildCompleted(event: ArtifactCreated): Promise<void> {
this.dispatchArtifactCreated(event)

const handler = resolveFunction(this.config.artifactBuildCompleted, "artifactBuildCompleted")
if (handler != null) {
await Promise.resolve(handler(event))
}
}

async build(): Promise<BuildResult> {
let configPath: string | null = null
let configFromOptions = this.options.config
Expand Down
8 changes: 8 additions & 0 deletions packages/app-builder-lib/src/packagerApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,12 @@ export interface ArtifactCreated extends UploadTask {
readonly publishConfig?: PublishConfiguration

readonly isWriteUpdateInfo?: boolean
}

export interface ArtifactBuildStarted {
readonly targetPresentableName: string

readonly file: string
// null for NSIS
readonly arch: Arch | null
}
4 changes: 2 additions & 2 deletions packages/app-builder-lib/src/platformPackager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ export abstract class PlatformPackager<DC extends PlatformSpecificBuildOptions>
return this.packagerOptions.prepackaged || path.join(outDir, `${this.platform.buildConfigurationKey}${getArchSuffix(arch)}${this.platform === Platform.MAC ? "" : "-unpacked"}`)
}

dispatchArtifactCreated(file: string, target: Target | null, arch: Arch | null, safeArtifactName?: string | null) {
this.info.dispatchArtifactCreated({
dispatchArtifactCreated(file: string, target: Target | null, arch: Arch | null, safeArtifactName?: string | null): Promise<void> {
return this.info.callArtifactBuildCompleted({
file, safeArtifactName, target, arch,
packager: this,
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export class RemoteBuilder {
const localFile = path.join(outDir, artifact.file)
const artifactCreatedEvent = this.artifactInfoToArtifactCreatedEvent(artifact, localFile, outDir)
// PublishManager uses outDir and options, real (the same as for local build) values must be used
this.packager.info.dispatchArtifactCreated(artifactCreatedEvent)
await this.packager.info.callArtifactBuildCompleted(artifactCreatedEvent)
}
}
}
Expand Down
8 changes: 6 additions & 2 deletions packages/app-builder-lib/src/targets/AppImageTarget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ export default class AppImageTarget extends Target {
// tslint:disable-next-line:no-invalid-template-strings
const artifactName = packager.expandArtifactBeautyNamePattern(options, "AppImage", arch)
const artifactPath = path.join(this.outDir, artifactName)
this.logBuilding("AppImage", artifactPath, arch)
await packager.info.callArtifactBuildStarted({
targetPresentableName: "AppImage",
file: artifactPath,
arch,
})

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

packager.info.dispatchArtifactCreated({
await packager.info.callArtifactBuildCompleted({
file: artifactPath,
safeArtifactName: packager.computeSafeArtifactName(artifactName, "AppImage", arch, false),
target: this,
Expand Down
8 changes: 6 additions & 2 deletions packages/app-builder-lib/src/targets/AppxTarget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ export default class AppXTarget extends Target {
const packager = this.packager
const artifactName = packager.expandArtifactBeautyNamePattern(this.options, "appx", arch)
const artifactPath = path.join(this.outDir, artifactName)
this.logBuilding("AppX", artifactPath, arch)
await packager.info.callArtifactBuildStarted({
targetPresentableName: "AppX",
file: artifactPath,
arch,
})

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

await stageDir.cleanup()

packager.info.dispatchArtifactCreated({
await packager.info.callArtifactBuildCompleted({
file: artifactPath,
packager,
arch,
Expand Down
8 changes: 6 additions & 2 deletions packages/app-builder-lib/src/targets/ArchiveTarget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ export class ArchiveTarget extends Target {
const artifactName = packager.expandArtifactNamePattern(this.options, format, arch, defaultPattern, false)
const artifactPath = path.join(this.outDir, artifactName)

this.logBuilding(`${isMac ? "macOS " : ""}${format}`, artifactPath, arch)
await packager.info.callArtifactBuildStarted({
targetPresentableName: `${isMac ? "macOS " : ""}${format}`,
file: artifactPath,
arch,
})
let updateInfo: any = null
if (format.startsWith("tar.")) {
await tar(packager.compression, format, artifactPath, appOutDir, isMac, packager.info.tempDirManager)
Expand Down Expand Up @@ -62,7 +66,7 @@ export class ArchiveTarget extends Target {
}
}

packager.info.dispatchArtifactCreated({
await packager.info.callArtifactBuildCompleted({
updateInfo,
file: artifactPath,
// tslint:disable-next-line:no-invalid-template-strings
Expand Down
8 changes: 6 additions & 2 deletions packages/app-builder-lib/src/targets/MsiTarget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ export default class MsiTarget extends Target {
const packager = this.packager
const artifactName = packager.expandArtifactBeautyNamePattern(this.options, "msi", arch)
const artifactPath = path.join(this.outDir, artifactName)
this.logBuilding("MSI", artifactPath, arch)
await packager.info.callArtifactBuildStarted({
targetPresentableName: "MSI",
file: artifactPath,
arch,
})

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

await packager.sign(artifactPath)

packager.info.dispatchArtifactCreated({
await packager.info.callArtifactBuildCompleted({
file: artifactPath,
packager,
arch,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export async function createBlockmap(file: string, target: Target, packager: Pla
const blockMapFile = `${file}${BLOCK_MAP_FILE_SUFFIX}`
log.info({blockMapFile: log.filePath(blockMapFile)}, "building block map")
const updateInfo = await executeAppBuilderAsJson<BlockMapDataHolder>(["blockmap", "--input", file, "--output", blockMapFile])
packager.info.dispatchArtifactCreated({
await packager.info.callArtifactBuildCompleted({
file: blockMapFile,
safeArtifactName: safeArtifactName == null ? null : `${safeArtifactName}${BLOCK_MAP_FILE_SUFFIX}`,
target,
Expand Down
20 changes: 12 additions & 8 deletions packages/app-builder-lib/src/targets/fpm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,17 +107,21 @@ export default class FpmTarget extends Target {
isUseArchIfX64 = true
}

const artifactPath = path.join(this.outDir, this.packager.expandArtifactNamePattern(this.options, target, arch, nameFormat, !isUseArchIfX64))
const packager = this.packager
const artifactPath = path.join(this.outDir, packager.expandArtifactNamePattern(this.options, target, arch, nameFormat, !isUseArchIfX64))

this.logBuilding(target, artifactPath, arch)
await packager.info.callArtifactBuildStarted({
targetPresentableName: target,
file: artifactPath,
arch,
})

await unlinkIfExists(artifactPath)
if (this.packager.packagerOptions.prepackaged != null) {
if (packager.packagerOptions.prepackaged != null) {
await ensureDir(this.outDir)
}

const scripts = await this.scriptFiles
const packager = this.packager
const appInfo = packager.appInfo
const options = this.options
const synopsis = options.synopsis
Expand Down Expand Up @@ -162,7 +166,7 @@ export default class FpmTarget extends Target {
}

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

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

if (this.packager.packagerOptions.effectiveOptionComputed != null && await this.packager.packagerOptions.effectiveOptionComputed([args, desktopFilePath])) {
if (packager.packagerOptions.effectiveOptionComputed != null && await packager.packagerOptions.effectiveOptionComputed([args, desktopFilePath])) {
return
}

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

this.packager.dispatchArtifactCreated(artifactPath, this, arch)
await packager.dispatchArtifactCreated(artifactPath, this, arch)
}
}

Expand Down
11 changes: 8 additions & 3 deletions packages/app-builder-lib/src/targets/nsis/NsisTarget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,12 @@ export class NsisTarget extends Target {
logFields.oneClick = oneClick
logFields.perMachine = isPerMachine
}
log.info(logFields, "building")

await packager.info.callArtifactBuildStarted({
targetPresentableName: this.name,
file: installerPath,
arch: null,
}, logFields)

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

if (this.isWebInstaller) {
packager.dispatchArtifactCreated(file, this, arch)
await packager.dispatchArtifactCreated(file, this, arch)
packageFiles[Arch[arch]] = fileInfo
}

Expand Down Expand Up @@ -270,7 +275,7 @@ export class NsisTarget extends Target {
updateInfo.isAdminRightsRequired = true
}

packager.info.dispatchArtifactCreated({
await packager.info.callArtifactBuildCompleted({
file: installerPath,
updateInfo,
target: this,
Expand Down
8 changes: 6 additions & 2 deletions packages/app-builder-lib/src/targets/pkg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ export class PkgTarget extends Target {
const artifactName = packager.expandArtifactNamePattern(options, "pkg")
const artifactPath = path.join(this.outDir, artifactName)

this.logBuilding("pkg", artifactPath, arch)
await packager.info.callArtifactBuildStarted({
targetPresentableName: "pkg",
file: artifactPath,
arch,
})

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

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

packager.dispatchArtifactCreated(artifactPath, this, arch, packager.computeSafeArtifactName(artifactName, "pkg", arch))
await packager.dispatchArtifactCreated(artifactPath, this, arch, packager.computeSafeArtifactName(artifactName, "pkg", arch))
}

private async customizeDistributionConfiguration(distInfoFile: string, appPath: string) {
Expand Down
Loading

0 comments on commit e59f6c3

Please sign in to comment.