Skip to content

Commit

Permalink
feat: Different architectures for different platforms
Browse files Browse the repository at this point in the history
Close #1314
  • Loading branch information
develar committed Mar 8, 2017
1 parent 79c94bd commit f2056fa
Show file tree
Hide file tree
Showing 53 changed files with 920 additions and 605 deletions.
1 change: 1 addition & 0 deletions .idea/dictionaries/develar.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# electron-builder [![npm version](https://img.shields.io/npm/v/electron-builder.svg)](https://npmjs.org/package/electron-builder) [![donate](https://img.shields.io/badge/Donate-PayPal-green.svg)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=W6V79R2RGCCHL)
# electron-builder [![npm version](https://img.shields.io/npm/v/electron-builder.svg)](https://npmjs.org/package/electron-builder) [![downloads per month](http://img.shields.io/npm/dm/electron-builder.svg)](https://www.npmjs.org/package/fs-extra) [![donate](https://img.shields.io/badge/Donate-PayPal-green.svg)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=W6V79R2RGCCHL)
A complete solution to package and build a ready for distribution Electron app for macOS, Windows and Linux with “auto update” support out of the box.

* NPM packages management:
Expand Down
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"ajv": "^5.0.3-beta",
"ajv-keywords": "^2.0.1-beta.1",
"archiver": "^1.3.0",
"aws-sdk": "^2.22.0",
"aws-sdk": "^2.24.0",
"bluebird-lst": "^1.0.1",
"chalk": "^1.1.3",
"chromium-pickle-js": "^0.2.0",
Expand All @@ -48,13 +48,14 @@
"minimatch": "^3.0.3",
"node-emoji": "^1.5.1",
"node-forge": "^0.7.0",
"normalize-package-data": "^2.3.5",
"normalize-package-data": "^2.3.6",
"parse-color": "^1.0.0",
"plist": "^2.0.1",
"progress-ex": "^2.0.0",
"sanitize-filename": "^1.6.1",
"semver": "^5.3.0",
"stat-mode": "^0.2.2",
"ts-jsdoc": "^1.0.0",
"tunnel-agent": "^0.6.0",
"update-notifier": "^2.1.0",
"uuid-1345": "^0.99.6",
Expand All @@ -65,7 +66,7 @@
"@types/ini": "^1.3.29",
"@types/jest": "^18.1.1",
"@types/js-yaml": "^3.5.29",
"@types/node-forge": "^0.6.5",
"@types/node-forge": "^0.6.6",
"@types/source-map-support": "^0.2.28",
"@types/xml2js": "^0.0.32",
"babel-plugin-array-includes": "^2.0.3",
Expand All @@ -81,11 +82,11 @@
"electron-download-tf": "4.0.0",
"jest-cli": "^19.0.2",
"jest-environment-node-debug": "^2.0.0",
"jest-junit": "^1.2.0",
"jest-junit": "^1.3.0",
"jsdoc": "^3.4.3",
"path-sort": "^0.1.0",
"source-map-support": "^0.4.11",
"ts-babel": "^1.4.4",
"ts-babel": "^2.0.0",
"tslint": "^4.5.1",
"typescript": "^2.2.1",
"typescript-json-schema": "0.10.0",
Expand Down
25 changes: 24 additions & 1 deletion packages/electron-builder-core/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,26 @@ export enum Arch {
ia32, x64, armv7l
}

export type ArchType = "x64" | "ia32" | "armv7l"

export function getArchSuffix(arch: Arch): string {
return arch === Arch.x64 ? "" : `-${Arch[arch]}`
}

export type TargetConfigType = Array<string | TargetConfig> | string | TargetConfig | null

export interface TargetConfig {
/**
* The target name. e.g. `snap`.
*/
readonly target: string

/**
* The arch or list of archs.
*/
readonly arch?: Array<"x64" | "ia32" | "armv7l"> | string
}

export function toLinuxArchString(arch: Arch) {
return arch === Arch.ia32 ? "i386" : (arch === Arch.x64 ? "amd64" : "armv7l")
}
Expand Down Expand Up @@ -41,6 +57,10 @@ export class Platform {
}

createTarget(type?: string | Array<string> | null, ...archs: Array<Arch>): Map<Platform, Map<Arch, Array<string>>> {
if (type == null && (archs == null || archs.length === 0)) {
return new Map([[this, new Map()]])
}

const archToType = new Map()
if (this === Platform.MAC) {
archs = [Arch.x64]
Expand Down Expand Up @@ -97,4 +117,7 @@ export interface TargetSpecificOptions {
readonly artifactName?: string | null

readonly forceCodeSigning?: boolean
}
}

export const DEFAULT_TARGET = "default"
export const DIR_TARGET = "dir"
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Arch, getArchSuffix, Target } from "electron-builder-core"
import { getBinFromBintray } from "electron-builder-util/out/binDownload"
import { log, warn } from "electron-builder-util/out/log"
import { SquirrelWindowsOptions } from "electron-builder/out/options/winOptions"
import { WinPackager } from "electron-builder/out/winPackager"
import * as path from "path"
import { warn, log } from "electron-builder-util/out/log"
import { getBinFromBintray } from "electron-builder-util/out/binDownload"
import { buildInstaller, convertVersion, SquirrelOptions } from "./squirrelPack"
import { SquirrelWindowsOptions } from "electron-builder/out/options/winOptions"
import { Target, Arch, getArchSuffix } from "electron-builder-core"

const SW_VERSION = "1.5.2.0"
//noinspection SpellCheckingInspection
Expand Down Expand Up @@ -75,7 +75,7 @@ export default class SquirrelWindowsTarget extends Target {
iconUrl: iconUrl,
extraMetadataSpecs: projectUrl == null ? null : `\n <projectUrl>${projectUrl}</projectUrl>`,
copyright: appInfo.copyright,
packageCompressionLevel: packager.config.compression === "store" ? 0 : 9,
packageCompressionLevel: parseInt(process.env.ELECTRON_BUILDER_COMPRESSION_LEVEL) || (packager.config.compression === "store" ? 0 : 9),
vendorPath: await getBinFromBintray("Squirrel.Windows", SW_VERSION, SW_SHA2)
}, this.options)

Expand Down
29 changes: 28 additions & 1 deletion packages/electron-builder-util/src/util.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import "source-map-support/register"
import BluebirdPromise from "bluebird-lst"
import { red, yellow } from "chalk"
import { ChildProcess, execFile, spawn as _spawn, SpawnOptions } from "child_process"
import { createHash } from "crypto"
import _debug from "debug"
import { homedir, tmpdir } from "os"
import * as path from "path"
import "source-map-support/register"
import { statOrNull } from "./fs"
import { log, warn } from "./log"

Expand Down Expand Up @@ -252,4 +252,31 @@ export class Lazy<T> {
constructor(creator: () => Promise<T>) {
this.creator = creator
}
}

export function addValue<K, T>(map: Map<K, Array<T>>, key: K, value: T) {
const list = map.get(key)
if (list == null) {
map.set(key, [value])
}
else if (!list.includes(value)) {
list.push(value)
}
}

export function replaceDefault(inList: Array<string> | null | undefined, defaultList: Array<string>): Array<string> {
if (inList == null) {
return defaultList
}

const index = inList.indexOf("default")
if (index >= 0) {
let list = inList.slice(0, index)
list.push(...defaultList)
if (index != (inList.length - 1)) {
list.push(...inList.slice(index + 1))
}
inList = list
}
return inList
}
2 changes: 1 addition & 1 deletion packages/electron-builder/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
"isbinaryfile": "^3.0.2",
"js-yaml": "^3.8.2",
"minimatch": "^3.0.3",
"normalize-package-data": "^2.3.5",
"normalize-package-data": "^2.3.6",
"parse-color": "^1.0.0",
"plist": "^2.0.1",
"sanitize-filename": "^1.6.1",
Expand Down
61 changes: 21 additions & 40 deletions packages/electron-builder/src/builder.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import BluebirdPromise from "bluebird-lst"
import { Arch, archFromString, Platform } from "electron-builder-core"
import { Arch, archFromString, DIR_TARGET, Platform } from "electron-builder-core"
import { CancellationToken } from "electron-builder-http/out/CancellationToken"
import { isEmptyOrSpaces } from "electron-builder-util"
import { addValue, isEmptyOrSpaces } from "electron-builder-util"
import { warn } from "electron-builder-util/out/log"
import { executeFinally } from "electron-builder-util/out/promise"
import { PublishOptions } from "electron-publish"
import { normalizePlatforms, Packager } from "./packager"
import { PackagerOptions } from "./packagerApi"
import { PublishManager } from "./publish/PublishManager"
import { DIR_TARGET } from "./targets/targetFactory"

export interface BuildOptions extends PackagerOptions, PublishOptions {
}
Expand All @@ -31,16 +30,6 @@ export interface CliOptions extends PackagerOptions, PublishOptions {
project?: string
}

function addValue<K, T>(map: Map<K, Array<T>>, key: K, value: T) {
const list = map.get(key)
if (list == null) {
map.set(key, [value])
}
else {
list.push(value)
}
}

export function normalizeOptions(args: CliOptions): BuildOptions {
if (args.targets != null) {
return args
Expand All @@ -49,14 +38,11 @@ export function normalizeOptions(args: CliOptions): BuildOptions {
let targets = new Map<Platform, Map<Arch, Array<string>>>()

function processTargets(platform: Platform, types: Array<string>) {
if (args.platform != null) {
throw new Error(`--platform cannot be used if --${platform.buildConfigurationKey} is passed`)
}
if (args.arch != null) {
throw new Error(`--arch cannot be used if --${platform.buildConfigurationKey} is passed`)
}
function commonArch(currentIfNotSpecified: boolean): Array<Arch> {
if (platform === Platform.MAC) {
return args.x64 || currentIfNotSpecified ? [Arch.x64] : []
}

function commonArch(): Array<Arch> {
const result = Array<Arch>()
if (args.x64) {
result.push(Arch.x64)
Expand All @@ -68,7 +54,14 @@ export function normalizeOptions(args: CliOptions): BuildOptions {
result.push(Arch.ia32)
}

return result.length === 0 ? [archFromString(process.arch)] : result
return result.length === 0 && currentIfNotSpecified ? [archFromString(process.arch)] : result
}

if (args.platform != null) {
throw new Error(`--platform cannot be used if --${platform.buildConfigurationKey} is passed`)
}
if (args.arch != null) {
throw new Error(`--arch cannot be used if --${platform.buildConfigurationKey} is passed`)
}

let archToType = targets.get(platform)
Expand All @@ -79,32 +72,20 @@ export function normalizeOptions(args: CliOptions): BuildOptions {

if (types.length === 0) {
const defaultTargetValue = args.dir ? [DIR_TARGET] : []
if (platform === Platform.MAC) {
archToType.set(Arch.x64, defaultTargetValue)
}
else {
for (const arch of commonArch()) {
archToType.set(arch, defaultTargetValue)
}
for (const arch of commonArch(args.dir === true)) {
archToType.set(arch, defaultTargetValue)
}
return
}

for (const type of types) {
let arch: string
if (platform === Platform.MAC) {
arch = "x64"
addValue(archToType, Arch.x64, type)
const suffixPos = type.lastIndexOf(":")
if (suffixPos > 0) {
addValue(archToType, archFromString(type.substring(suffixPos + 1)), type.substring(0, suffixPos))
}
else {
const suffixPos = type.lastIndexOf(":")
if (suffixPos > 0) {
addValue(archToType, archFromString(type.substring(suffixPos + 1)), type.substring(0, suffixPos))
}
else {
for (const arch of commonArch()) {
addValue(archToType, arch, type)
}
for (const arch of commonArch(true)) {
addValue(archToType, arch, type)
}
}
}
Expand Down
5 changes: 2 additions & 3 deletions packages/electron-builder/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
export { Packager } from "./packager"
export { PackagerOptions, ArtifactCreated, BuildInfo, SourceRepositoryInfo } from "./packagerApi"
export { getArchSuffix, Platform, Arch, archFromString, Target } from "electron-builder-core"
export { getArchSuffix, Platform, Arch, archFromString, Target, DIR_TARGET } from "electron-builder-core"
export { BuildOptions, build, CliOptions, createTargets } from "./builder"
export { Metadata, Config, CompressionLevel, FilePattern } from "./metadata"
export { MacOptions, DmgOptions, MasBuildOptions, MacOsTargetName, PkgOptions } from "./options/macOptions"
export { WinBuildOptions, NsisOptions, SquirrelWindowsOptions, AppXOptions } from "./options/winOptions"
export { LinuxBuildOptions, DebOptions } from "./options/linuxOptions"
export { DIR_TARGET } from "./targets/targetFactory"
export { LinuxBuildOptions, DebOptions } from "./options/linuxOptions"
4 changes: 2 additions & 2 deletions packages/electron-builder/src/linuxPackager.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Platform, Target } from "electron-builder-core"
import { DIR_TARGET, Platform, Target } from "electron-builder-core"
import { rename } from "fs-extra-p"
import * as path from "path"
import sanitizeFileName from "sanitize-filename"
Expand All @@ -9,7 +9,7 @@ import AppImageTarget from "./targets/appImage"
import FpmTarget from "./targets/fpm"
import { LinuxTargetHelper } from "./targets/LinuxTargetHelper"
import SnapTarget from "./targets/snap"
import { createCommonTarget, DIR_TARGET } from "./targets/targetFactory"
import { createCommonTarget } from "./targets/targetFactory"

export class LinuxPackager extends PlatformPackager<LinuxBuildOptions> {
readonly executableName: string
Expand Down
4 changes: 2 additions & 2 deletions packages/electron-builder/src/macPackager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import BluebirdPromise from "bluebird-lst"
import { Arch, Platform, Target } from "electron-builder-core"
import { Arch, DIR_TARGET, Platform, Target } from "electron-builder-core"
import { exec } from "electron-builder-util"
import { deepAssign } from "electron-builder-util/out/deepAssign"
import { log, task, warn } from "electron-builder-util/out/log"
Expand All @@ -13,7 +13,7 @@ import { BuildInfo } from "./packagerApi"
import { PlatformPackager } from "./platformPackager"
import { DmgTarget } from "./targets/dmg"
import { PkgTarget, prepareProductBuildArgs } from "./targets/pkg"
import { createCommonTarget, DIR_TARGET, NoOpTarget } from "./targets/targetFactory"
import { createCommonTarget, NoOpTarget } from "./targets/targetFactory"

export default class MacPackager extends PlatformPackager<MacOptions> {
readonly codeSigningInfo: Promise<CodeSigningInfo>
Expand Down
15 changes: 11 additions & 4 deletions packages/electron-builder/src/metadata.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Arch, Platform, Target, TargetSpecificOptions } from "electron-builder-core"
import { Arch, Platform, Target, TargetConfig, TargetSpecificOptions } from "electron-builder-core"
import { Publish } from "electron-builder-http/out/publishOptions"
import { DebOptions, LinuxBuildOptions, SnapOptions } from "./options/linuxOptions"
import { AppImageOptions, DebOptions, LinuxBuildOptions, SnapOptions } from "./options/linuxOptions"
import { DmgOptions, MacOptions, MasBuildOptions, PkgOptions } from "./options/macOptions"
import { AppXOptions, NsisOptions, SquirrelWindowsOptions, WinBuildOptions } from "./options/winOptions"
import { PlatformPackager } from "./platformPackager"
Expand Down Expand Up @@ -143,13 +143,20 @@ export interface Config extends PlatformSpecificBuildOptions, TargetSpecificOpti

readonly win?: WinBuildOptions | null
readonly nsis?: NsisOptions | null
readonly portable?: NsisOptions | null
readonly pkg?: PkgOptions | null
readonly squirrelWindows?: SquirrelWindowsOptions | null
readonly appx?: AppXOptions | null
readonly squirrelWindows?: SquirrelWindowsOptions | null

readonly linux?: LinuxBuildOptions | null
readonly deb?: DebOptions | null
readonly snap?: SnapOptions | null
readonly appimage?: AppImageOptions | null
readonly pacman?: LinuxBuildOptions | null
readonly rpm?: LinuxBuildOptions | null
readonly freebsd?: LinuxBuildOptions | null
readonly p5p?: LinuxBuildOptions | null
readonly apk?: LinuxBuildOptions | null

/**
The compression level, one of `store`, `normal`, `maximum` (default: `normal`). If you want to rapidly test build, `store` can reduce build time significantly.
Expand Down Expand Up @@ -336,7 +343,7 @@ export interface PlatformSpecificBuildOptions extends TargetSpecificOptions {

readonly asar?: AsarOptions | boolean | null

readonly target?: Array<string> | string | null
readonly target?: Array<string | TargetConfig> | string | TargetConfig | null

readonly icon?: string | null

Expand Down
3 changes: 2 additions & 1 deletion packages/electron-builder/src/options/linuxOptions.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { TargetConfigType } from "electron-builder-core"
import { PlatformSpecificBuildOptions } from "../metadata"

/**
Expand Down Expand Up @@ -26,7 +27,7 @@ export interface LinuxBuildOptions extends PlatformSpecificBuildOptions {
electron-builder [docker image](https://github.com/electron-userland/electron-builder/wiki/Docker) can be used to build Linux targets on any platform. See [Multi platform build](https://github.com/electron-userland/electron-builder/wiki/Multi-Platform-Build).
*/
readonly target?: Array<string> | null
readonly target?: TargetConfigType

/**
The maintainer. Defaults to [author](#AppMetadata-author).
Expand Down
Loading

0 comments on commit f2056fa

Please sign in to comment.