Skip to content

Commit 22746bd

Browse files
committed
feat: artifact file name pattern for pkg and dmg
Close #966
1 parent f74a16c commit 22746bd

File tree

11 files changed

+54
-21
lines changed

11 files changed

+54
-21
lines changed

docs/Options.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ Most of the options accept `null` — for example, to explicitly set that DMG ic
7070
| publish | <a name="Config-publish"></a>See [publish](https://github.com/electron-userland/electron-builder/wiki/Publishing-Artifacts#PublishConfiguration).
7171
| forceCodeSigning | <a name="Config-forceCodeSigning"></a>Whether to fail if application will be not signed (to prevent unsigned app if code signing configuration is not correct).
7272
| electronVersion | <a name="Config-electronVersion"></a>The version of electron you are packaging for. Defaults to version of `electron`, `electron-prebuilt` or `electron-prebuilt-compile` dependency.
73+
| artifactName | <a name="Config-artifactName"></a><p>The [artifact file name pattern](https://github.com/electron-userland/electron-builder/wiki/Options#artifact-file-name-pattern). Defaults to <code>${productName}-${version}.${ext}</code> (some target can have another defaults, see corresponding options).</p> <p>Currently supported only for <code>pkg</code>, <code>dmg</code> and <code>nsis</code>.</p>
7374

7475
<a name="AppXOptions"></a>
7576
### `appx`
@@ -188,7 +189,7 @@ See [NSIS target notes](https://github.com/electron-userland/electron-builder/wi
188189
| language | <a name="NsisOptions-language"></a>* [LCID Dec](https://msdn.microsoft.com/en-au/goglobal/bb964664.aspx), defaults to `1033`(`English - United States`).
189190
| warningsAsErrors | <a name="NsisOptions-warningsAsErrors"></a>Defaults to `true`. If `warningsAsErrors` is `true` (default): NSIS will treat warnings as errors. If `warningsAsErrors` is `false`: NSIS will allow warnings.
190191
| menuCategory | <a name="NsisOptions-menuCategory"></a>Whether to create submenu for start menu shortcut and program files directory. Defaults to `false`. If `true`, company name will be used. Or string value.
191-
| artifactName | <a name="NsisOptions-artifactName"></a><p>The artifact file name pattern. Defaults to <code>${productName} Setup ${version}.${ext}</code>. <code>${name}</code>, <code>${productName}</code>, <code>${version}</code>, <code>${ext}</code>, <code>${arch}</code>, <code>${os}</code> (expanded to <code>mac</code>, <code>linux</code> or <code>win</code> according to current platform) macro are supported.</p> <p>If no <code>arch</code>, macro will be removed from your pattern with leading space, <code>-</code> or <code>_</code> (so, you don’t need to worry and can reuse pattern).</p>
192+
| artifactName | <a name="NsisOptions-artifactName"></a>The [artifact file name pattern](https://github.com/electron-userland/electron-builder/wiki/Options#artifact-file-name-pattern). Defaults to `${productName} Setup ${version}.${ext}`.
192193

193194
<a name="PkgOptions"></a>
194195
### `pkg` macOS Product Archive Options
@@ -340,5 +341,17 @@ If `to` is given as a relative path, it is relative to the app's content directo
340341

341342
You can use `${os}` and `${arch}` in the `from` and `to` fields as well.
342343

344+
## Artifact File Name Pattern
345+
346+
Supported macros:
347+
* `${name}`
348+
* `${productName}`[Sanitized](https://www.npmjs.com/package/sanitize-filename) product name.
349+
* `${version}`
350+
* `${ext}`
351+
* `${arch}` — If no `arch`, macro will be removed from your pattern with leading space, `-` and `_` (so, you don't need to worry and can reuse pattern).
352+
* `${os}` — expanded to `mac`, `linux` or `win` according to target platform.
353+
354+
355+
343356
## Build Version Management
344357
`CFBundleVersion` (MacOS) and `FileVersion` (Windows) will be set automatically to `version`.`build_number` on CI server (Travis, AppVeyor and CircleCI supported).

docs/Publishing Artifacts.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ Amazon S3 — `https` must be used, so, if you use direct Amazon S3 endpoints, f
101101
### `publish` Generic (any https server)
102102
| Name | Description
103103
| --- | ---
104-
| **url** | <a name="GenericServerOptions-url"></a>The base url. e.g. `https://s3.amazonaws.com/bucket_name`. You can use `${os}` (expanded to `mac`, `linux` or `win` according to current platform) and `${arch}` macros.
104+
| **url** | <a name="GenericServerOptions-url"></a>The base url. e.g. `https://s3.amazonaws.com/bucket_name`. You can use `${os}` (expanded to `mac`, `linux` or `win` according to target platform) and `${arch}` macros.
105105
| channel | <a name="GenericServerOptions-channel"></a>The channel. Defaults to `latest`.
106106

107107
<a name="GithubOptions"></a>

packages/electron-builder-core/src/core.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,13 @@ export abstract class Target {
8686
finishBuild(): Promise<any> {
8787
return Promise.resolve()
8888
}
89+
}
90+
91+
export interface TargetSpecificOptions {
92+
/*
93+
The [artifact file name pattern](https://github.com/electron-userland/electron-builder/wiki/Options#artifact-file-name-pattern).
94+
*/
95+
readonly artifactName?: string | null
96+
97+
readonly forceCodeSigning?: boolean
8998
}

packages/electron-builder-http/src/publishOptions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export interface PublishConfiguration {
3434
*/
3535
export interface GenericServerOptions extends PublishConfiguration {
3636
/*
37-
The base url. e.g. `https://s3.amazonaws.com/bucket_name`. You can use `${os}` (expanded to `mac`, `linux` or `win` according to current platform) and `${arch}` macros.
37+
The base url. e.g. `https://s3.amazonaws.com/bucket_name`. You can use `${os}` (expanded to `mac`, `linux` or `win` according to target platform) and `${arch}` macros.
3838
*/
3939
url: string
4040

packages/electron-builder/src/metadata.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Arch, Platform } from "electron-builder-core"
1+
import { Arch, Platform, TargetSpecificOptions } from "electron-builder-core"
22
import { Publish } from "electron-builder-http/out/publishOptions"
33
import { DebOptions, LinuxBuildOptions, SnapOptions } from "./options/linuxOptions"
44
import { DmgOptions, MacOptions, MasBuildOptions, PkgOptions } from "./options/macOptions"
@@ -83,7 +83,7 @@ export interface FilePattern {
8383
/*
8484
## Configuration Options
8585
*/
86-
export interface Config extends PlatformSpecificBuildOptions {
86+
export interface Config extends PlatformSpecificBuildOptions, TargetSpecificOptions {
8787
/*
8888
The application id. Used as
8989
[CFBundleIdentifier](https://developer.apple.com/library/ios/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html#//apple_ref/doc/uid/20001431-102070) for MacOS and as
@@ -217,6 +217,13 @@ export interface Config extends PlatformSpecificBuildOptions {
217217
The version of electron you are packaging for. Defaults to version of `electron`, `electron-prebuilt` or `electron-prebuilt-compile` dependency.
218218
*/
219219
readonly electronVersion?: string | null
220+
221+
/*
222+
The [artifact file name pattern](https://github.com/electron-userland/electron-builder/wiki/Options#artifact-file-name-pattern). Defaults to `${productName}-${version}.${ext}` (some target can have another defaults, see corresponding options).
223+
224+
Currently supported only for `pkg`, `dmg` and `nsis`.
225+
*/
226+
readonly artifactName?: string | null
220227
}
221228

222229
export interface AfterPackContext {
@@ -316,7 +323,7 @@ export interface MetadataDirectories {
316323
readonly app?: string | null
317324
}
318325

319-
export interface PlatformSpecificBuildOptions {
326+
export interface PlatformSpecificBuildOptions extends TargetSpecificOptions {
320327
readonly files?: Array<string> | string | null
321328
readonly extraFiles?: Array<FilePattern> | FilePattern | Array<string> | string | null
322329
readonly extraResources?: Array<FilePattern> | FilePattern | Array<string> | string | null
@@ -332,8 +339,6 @@ export interface PlatformSpecificBuildOptions {
332339
readonly fileAssociations?: Array<FileAssociation> | FileAssociation
333340

334341
readonly publish?: Publish
335-
336-
readonly forceCodeSigning?: boolean
337342
}
338343

339344
export interface Macros {

packages/electron-builder/src/options/macOptions.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { PlatformSpecificBuildOptions } from "../metadata"
2+
import { TargetSpecificOptions } from "electron-builder-core"
23

34
export type MacOsTargetName = "default" | "dmg" | "mas" | "pkg" | "7z" | "zip" | "tar.xz" | "tar.lz" | "tar.gz" | "tar.bz2" | "dir"
45

@@ -64,7 +65,7 @@ export interface MacOptions extends PlatformSpecificBuildOptions {
6465
/*
6566
### `pkg` macOS Product Archive Options
6667
*/
67-
export interface PkgOptions {
68+
export interface PkgOptions extends TargetSpecificOptions {
6869
/*
6970
The scripts directory, relative to `build` (build resources directory). Defaults to `build/pkg-scripts`.
7071
See [Scripting in installer packages](http://macinstallers.blogspot.de/2012/07/scripting-in-installer-packages.html).
@@ -88,7 +89,7 @@ export interface PkgOptions {
8889
/*
8990
### `dmg` macOS DMG Options
9091
*/
91-
export interface DmgOptions {
92+
export interface DmgOptions extends TargetSpecificOptions {
9293
/*
9394
The path to background image (default: `build/background.tiff` or `build/background.png` if exists). The resolution of this file determines the resolution of the installer window.
9495
If background is not specified, use `window.size`. Default locations expected background size to be 540x380.

packages/electron-builder/src/options/winOptions.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,7 @@ export interface NsisOptions {
147147
readonly useZip?: boolean
148148

149149
/*
150-
The artifact file name pattern. Defaults to `${productName} Setup ${version}.${ext}`. `${name}`, `${productName}`, `${version}`, `${ext}`, `${arch}`, `${os}` (expanded to `mac`, `linux` or `win` according to current platform) macro are supported.
151-
152-
If no `arch`, macro will be removed from your pattern with leading space, `-` or `_` (so, you don't need to worry and can reuse pattern).
150+
The [artifact file name pattern](https://github.com/electron-userland/electron-builder/wiki/Options#artifact-file-name-pattern). Defaults to `${productName} Setup ${version}.${ext}`.
153151
*/
154152
readonly artifactName?: string | null
155153
}

packages/electron-builder/src/platformPackager.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import BluebirdPromise from "bluebird-lst-c"
2-
import { Arch, getArchSuffix, Platform, Target } from "electron-builder-core"
2+
import { Arch, getArchSuffix, Platform, Target, TargetSpecificOptions } from "electron-builder-core"
33
import { asArray, debug, isEmptyOrSpaces, use } from "electron-builder-util"
44
import { deepAssign } from "electron-builder-util/out/deepAssign"
55
import { copyDir, statOrNull, unlinkIfExists } from "electron-builder-util/out/fs"
@@ -394,17 +394,24 @@ export abstract class PlatformPackager<DC extends PlatformSpecificBuildOptions>
394394
await this.checkFileInPackage(resourcesDir, "package.json", "Application", isAsar)
395395
}
396396

397-
expandArtifactNamePattern(pattern: string, ext: string, arch: Arch | null): string {
398-
let p = pattern
397+
expandArtifactNamePattern(targetSpecificOptions: TargetSpecificOptions | n, ext: string, arch?: Arch | null, defaultPattern?: string): string {
398+
let pattern = targetSpecificOptions == null ? null : targetSpecificOptions.artifactName
399+
if (pattern == null) {
400+
pattern = this.platformSpecificBuildOptions.artifactName
401+
}
402+
if (pattern == null) {
403+
pattern = defaultPattern || "${productName}-${version}.${ext}"
404+
}
405+
399406
if (arch == null) {
400-
p = p
407+
pattern = pattern
401408
.replace("-${arch}", "")
402409
.replace(" ${arch}", "")
403410
.replace("_${arch}", "")
404411
}
405412

406413
const appInfo = this.appInfo
407-
return p.replace(/\$\{([a-zA-Z]+)\}/g, (match, p1): string => {
414+
return pattern.replace(/\$\{([a-zA-Z]+)\}/g, (match, p1): string => {
408415
switch (p1) {
409416
case "name":
410417
return appInfo.name

packages/electron-builder/src/targets/dmg.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ export class DmgTarget extends Target {
160160
}
161161
})
162162

163-
const artifactPath = path.join(appOutDir, `${appInfo.productFilename}-${appInfo.version}.dmg`)
163+
const artifactPath = path.join(appOutDir, packager.expandArtifactNamePattern(packager.config.dmg, "dmg"))
164164
//noinspection SpellCheckingInspection
165165
await spawn("hdiutil", addVerboseIfNeed(["convert", tempDmg, "-format", packager.config.compression === "store" ? "UDRO" : "UDBZ", "-imagekey", "zlib-level=9", "-o", artifactPath]))
166166
await exec("hdiutil", addVerboseIfNeed(["internet-enable", "-no"]).concat(artifactPath))

packages/electron-builder/src/targets/nsis.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ export default class NsisTarget extends Target {
8080
const appInfo = packager.appInfo
8181
const version = appInfo.version
8282
const options = this.options
83-
const installerFilename = packager.expandArtifactNamePattern(options.artifactName || "${productName} Setup ${version}.${ext}", "exe", null)
83+
const installerFilename = packager.expandArtifactNamePattern(options, "exe", null, "${productName} Setup ${version}.${ext}")
8484
const iconPath = await packager.getResource(options.installerIcon, "installerIcon.ico") || await packager.getIconPath()
8585
const oneClick = options.oneClick !== false
8686

0 commit comments

Comments
 (0)