Skip to content

Commit

Permalink
feat: --dist by default
Browse files Browse the repository at this point in the history
Closes #413
  • Loading branch information
develar committed May 31, 2016
1 parent fe53388 commit ae3f1bb
Show file tree
Hide file tree
Showing 19 changed files with 412 additions and 439 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,11 @@ See `node_modules/electron-builder/out/electron-builder.d.ts`. [Typings](https:/
"use strict"

const builder = require("electron-builder")
const Platform = builder.Platform

// Promise is returned
builder.build({
platform: [builder.Platform.OSX],
"//": "platform, arch and other properties, see PackagerOptions in the node_modules/electron-builder/out/electron-builder.d.ts",
targets: Platform.OSX.createTarget(),
devMetadata: {
"//": "build and other properties, see https://goo.gl/5jVxoO"
}
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
"devDependencies": {
"ava-tf": "^0.15",
"babel-plugin-array-includes": "^2.0.3",
"babel-plugin-transform-es2015-destructuring": "^6.9.0",
"babel-plugin-transform-es2015-parameters": "^6.9.0",
"babel-plugin-transform-es2015-spread": "^6.8.0",
"decompress-zip": "^0.3.0",
Expand All @@ -103,7 +104,7 @@
"plist": "^1.2.0",
"pre-git": "^3.8.4",
"semantic-release": "^6.3.0",
"should": "^8.4.0",
"should": "^9.0.0",
"ts-babel": "^0.8.6",
"tsconfig-glob": "^0.4.3",
"tslint": "3.10.0-dev.2",
Expand All @@ -114,6 +115,7 @@
"plugins": [
"transform-es2015-parameters",
"transform-es2015-spread",
"transform-es2015-destructuring",
"array-includes"
]
},
Expand Down
149 changes: 57 additions & 92 deletions src/build-cli.ts
Original file line number Diff line number Diff line change
@@ -1,102 +1,67 @@
#! /usr/bin/env node

import { PackagerOptions, commonTargets } from "./platformPackager"
import { normalizePlatforms } from "./packager"
import { build } from "./builder"
import { PublishOptions } from "./gitHubPublisher"
// import { commonTargets } from "./platformPackager"
import { build, CliOptions } from "./builder"
import { printErrorAndExit } from "./promise"
import yargs = require("yargs")
import { underline } from "chalk"
import { Platform } from "./metadata"
import yargs = require("yargs")

//noinspection JSUnusedLocalSymbols
const __awaiter = require("./awaiter")

interface CliOptions extends PackagerOptions, PublishOptions {
osx?: Array<string>
linux?: Array<string>
win?: Array<string>

arch?: string

x64?: boolean
ia32?: boolean
}

const args = <CliOptions>(yargs
.version()
.option("osx", {
alias: "o",
describe: "Build for OS X",
type: "array",
})
.option("linux", {
alias: "l",
describe: "Build for Linux",
type: "array",
})
.option("win", {
alias: ["w", "windows"],
describe: "Build for Windows",
type: "array",
})
.option("x64", {
describe: "Build for x64",
type: "boolean",
})
.option("ia32", {
describe: "Build for ia32",
type: "boolean",
})
.option("target", {
alias: "t",
describe: "Target package types",
choices: commonTargets,
})
.option("publish", {
alias: "p",
describe: `Publish artifacts (to GitHub Releases), see ${underline("https://goo.gl/WMlr4n")}`,
choices: ["onTag", "onTagOrDraft", "always", "never"],
})
.option("platform", {
choices: ["osx", "win", "linux", "darwin", "win32", "all"],
})
.option("arch", {
choices: ["ia32", "x64", "all"],
})
.option("npmRebuild", {
describe: "Runs npm rebuild before starting to package the app.",
default: true,
type: "boolean",
})
.strict()
.help()
.epilog(`Project home: ${underline("https://github.com/electron-userland/electron-builder")}`)
.argv)

const platforms = normalizePlatforms(args.platform)
if (args.osx != null && !platforms.includes(Platform.OSX)) {
platforms.push(Platform.OSX)
}
if (args.linux != null && !platforms.includes(Platform.LINUX)) {
platforms.push(Platform.LINUX)
}
if (args.win != null && !platforms.includes(Platform.WINDOWS)) {
platforms.push(Platform.WINDOWS)
}

const archAsProp = args.arch
const archs = archAsProp === "all" ? ["ia32", "x64"] : (archAsProp == null ? [] : [archAsProp])

if (args.x64 && !archs.includes("x64")) {
archs.push("x64")
}
if (args.ia32 && !archs.includes("ia32")) {
archs.push("ia32")
export function createYargs(): any {
return yargs
.version()
.option("osx", {
alias: "o",
describe: "Build for OS X",
type: "array",
})
.option("linux", {
alias: "l",
describe: "Build for Linux",
type: "array",
})
.option("win", {
alias: ["w", "windows"],
describe: "Build for Windows",
type: "array",
})
.option("x64", {
describe: "Build for x64",
type: "boolean",
})
.option("ia32", {
describe: "Build for ia32",
type: "boolean",
})
// .option("target", {
// alias: "t",
// describe: "Target package types",
// choices: commonTargets,
// })
.option("publish", {
alias: "p",
describe: `Publish artifacts (to GitHub Releases), see ${underline("https://goo.gl/WMlr4n")}`,
choices: ["onTag", "onTagOrDraft", "always", "never"],
})
.option("platform", {
choices: ["osx", "win", "linux", "darwin", "win32", "all"],
})
.option("arch", {
choices: ["ia32", "x64", "all"],
})
.option("npmRebuild", {
describe: "Runs npm rebuild before starting to package the app.",
default: true,
type: "boolean",
})
.strict()
.help()
.epilog(`Project home: ${underline("https://github.com/electron-userland/electron-builder")}`)
}

build(Object.assign({}, args, {
platform: platforms,
arch: archs,
}))
.catch(printErrorAndExit)
if (!module.parent) {
build(<CliOptions>(createYargs().argv))
.catch(printErrorAndExit)
}
159 changes: 151 additions & 8 deletions src/builder.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { Packager, normalizePlatforms } from "./packager"
import { Packager, normalizePlatforms, isEmptyOrSpaces } from "./packager"
import { PackagerOptions } from "./platformPackager"
import { PublishOptions, Publisher, GitHubPublisher } from "./gitHubPublisher"
import { executeFinally } from "./promise"
import { Promise as BluebirdPromise } from "bluebird"
import { InfoRetriever } from "./repositoryInfo"
import { log } from "./util"
import { Platform, Arch, archFromString } from "./metadata"

//noinspection JSUnusedLocalSymbols
const __awaiter = require("./awaiter")
Expand All @@ -27,15 +28,157 @@ export async function createPublisher(packager: Packager, options: BuildOptions,
export interface BuildOptions extends PackagerOptions, PublishOptions {
}

export async function build(originalOptions?: BuildOptions): Promise<void> {
const options: BuildOptions = Object.assign({
cscLink: process.env.CSC_LINK,
cscKeyPassword: process.env.CSC_KEY_PASSWORD,
githubToken: process.env.GH_TOKEN || process.env.GH_TEST_TOKEN,
}, originalOptions)
export interface CliOptions extends PackagerOptions, PublishOptions {
osx?: Array<string>
linux?: Array<string>
win?: Array<string>

options.platform = normalizePlatforms(options.platform)
arch?: string

x64?: boolean
ia32?: boolean

platform?: 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
}

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(): Array<Arch> {
if (args.ia32) {
return [Arch.ia32]
}
else if (args.x64) {
return [Arch.x64]
}
else if (args.ia32 && args.x64) {
return [Arch.x64, Arch.ia32]
}
else {
return [archFromString(process.arch)]
}
}

let archToType = targets.get(platform)
if (archToType == null) {
archToType = new Map<Arch, Array<string>>()
targets.set(platform, archToType)
}

if (types.length === 0) {
for (let arch of commonArch()) {
archToType.set(arch, [])
}
return
}

for (let type of types) {
let arch: string
if (platform === Platform.OSX) {
arch = "x64"
addValue(archToType, Arch.x64, type)
}
else {
const suffixPos = type.lastIndexOf(":")
if (suffixPos > 0) {
addValue(archToType, archFromString(type.substring(suffixPos + 1)), type.substring(0, suffixPos))
}
else {
for (let arch of commonArch()) {
addValue(archToType, arch, type)
}
}
}
}
}

if (args.osx != null) {
processTargets(Platform.OSX, args.osx)
}

if (args.linux != null) {
processTargets(Platform.LINUX, args.linux)
}

if (args.win != null) {
processTargets(Platform.WINDOWS, args.win)
}

if (targets.size === 0) {
targets = createTargets(normalizePlatforms(args.platform), null, args.arch)
}

const result = Object.assign({}, args)
result.targets = targets

if (result.cscLink === undefined && !isEmptyOrSpaces(process.env.CSC_LINK)) {
result.cscLink = process.env.CSC_LINK
}
if (result.cscKeyPassword === undefined && !isEmptyOrSpaces(process.env.CSC_KEY_PASSWORD)) {
result.cscKeyPassword = process.env.CSC_KEY_PASSWORD
}
if (result.githubToken === undefined && !isEmptyOrSpaces(process.env.GH_TOKEN)) {
result.githubToken = process.env.GH_TOKEN
}

delete result.osx
delete result.linux
delete result.win
delete result.platform
delete result.arch

delete (<any>result)["o"]
delete (<any>result)["l"]
delete (<any>result)["w"]
delete (<any>result)["windows"]
delete (<any>result)["$0"]
delete (<any>result)["_"]
delete (<any>result).version
delete (<any>result).help

delete result.ia32
delete result.x64
return result
}

export function createTargets(platforms: Array<Platform>, type?: string | null, arch?: string | null): Map<Platform, Map<Arch, Array<string>>> {
const targets = new Map<Platform, Map<Arch, Array<string>>>()
for (let platform of platforms) {
const archs = platform === Platform.OSX ? [Arch.x64] : (arch === "all" ? [Arch.x64, Arch.ia32] : [archFromString(arch == null ? process.arch : arch)])
let archToType = new Map<Arch, Array<string>>()
targets.set(platform, archToType)

for (let arch of archs) {
archToType.set(arch, type == null ? [] : [type])
}
}
return targets
}

export async function build(rawOptions?: CliOptions): Promise<void> {
const options = normalizeOptions(rawOptions || {})
let isPublishOptionGuessed = false
if (options.publish === undefined) {
if (process.env.npm_lifecycle_event === "release") {
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export { Packager } from "./packager"
export { PackagerOptions, ArtifactCreated, DIR_TARGET } from "./platformPackager"
export { BuildOptions, build, createPublisher } from "./builder"
export { BuildOptions, build, createPublisher, CliOptions, createTargets } from "./builder"
export { PublishOptions, Publisher } from "./gitHubPublisher"
export { AppMetadata, DevMetadata, Platform, getProductName, BuildMetadata, OsXBuildOptions, WinBuildOptions, LinuxBuildOptions } from "./metadata"
export { AppMetadata, DevMetadata, Platform, Arch, archFromString, getProductName, BuildMetadata, OsXBuildOptions, WinBuildOptions, LinuxBuildOptions } from "./metadata"
Loading

0 comments on commit ae3f1bb

Please sign in to comment.