Skip to content

Commit

Permalink
refactor: merge electron-packager
Browse files Browse the repository at this point in the history
  • Loading branch information
develar committed Jul 13, 2016
1 parent 1bd32a7 commit 0022b86
Show file tree
Hide file tree
Showing 27 changed files with 338 additions and 169 deletions.
19 changes: 10 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
"compile": "npm run compile-production && npm run compile-test",
"compile-production": "ts-babel",
"compile-test": "ts-babel test",
"lint": "tslint src/*.ts test/src/*.ts",
"lint": "tslint src/**/*.ts test/src/**/*.ts",
"pretest": "npm run compile && npm run lint",
"test": "node ./test/out/helpers/runTests.js",
"semantic-release": "semantic-release pre && npm publish && semantic-release post",
"//": "Update wiki if docs changed. Update only if functionalily are generally available (latest release, not next)",
"update-wiki": "git subtree split -b wiki --prefix docs/ && git push -f wiki wiki:master",
"whitespace": "whitespace src/*.ts",
"whitespace": "whitespace src/**/*.ts",
"docker-images": "docker/build.sh"
},
"repository": {
Expand Down Expand Up @@ -65,19 +65,22 @@
"chalk": "^1.1.3",
"cli-cursor": "^1.0.2",
"debug": "^2.2.0",
"electron-download": "^2.1.2",
"electron-osx-sign": "^0.4.0-beta4",
"electron-packager-tf": "~7.5.3",
"electron-winstaller-fixed": "~2.11.7",
"electron-winstaller-fixed": "~3.0.0",
"extract-zip": "^1.5.0",
"fs-extra-p": "^1.0.5",
"hosted-git-info": "^2.1.5",
"image-size": "^0.5.0",
"lodash.template": "^4.2.5",
"mime": "^1.3.4",
"minimatch": "^3.0.2",
"path-sort": "^0.1.0",
"plist": "^1.2.0",
"pretty-ms": "^2.1.0",
"progress": "^1.1.8",
"progress-stream": "^1.2.0",
"rcedit": "^0.5.1",
"read-package-json": "^2.0.4",
"sanitize-filename": "^1.6.0",
"semver": "^5.2.0",
Expand All @@ -100,6 +103,7 @@
]
},
"devDependencies": {
"@develar/semantic-release": "^6.3.1",
"@types/debug": "0.0.27-alpha",
"@types/mime": "0.0.27-alpha",
"@types/progress": "^1.1.26-alpha",
Expand All @@ -108,19 +112,16 @@
"ava-tf": "^0.15.3",
"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-parameters": "^6.11.3",
"babel-plugin-transform-es2015-spread": "^6.8.0",
"decompress-zip": "^0.3.0",
"diff": "^2.2.3",
"electron-download": "^2.1.2",
"json8": "^0.9.0",
"plist": "^1.2.0",
"pre-git": "^3.10.0",
"@develar/semantic-release": "^6.3.1",
"should": "^9.0.2",
"ts-babel": "^1.0.3",
"tslint": "3.13.0",
"typescript": "2.0.0-dev.20160620-1.0",
"typescript": "^2.1.0-dev.20160712",
"whitespace": "^2.0.0"
},
"babel": {
Expand Down
5 changes: 2 additions & 3 deletions src/appInfo.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { ElectronPackagerOptions } from "electron-packager-tf"
import { DevMetadata, AppMetadata } from "./metadata"
import { warn } from "./util/log"
import { smarten } from "./platformPackager"
Expand Down Expand Up @@ -30,7 +29,7 @@ export class AppInfo {
constructor(public metadata: AppMetadata, private devMetadata: DevMetadata, buildVersion?: string | null) {
this.version = metadata.version!

this.buildNumber = this.devMetadata.build["build-version"] || process.env.TRAVIS_BUILD_NUMBER || process.env.APPVEYOR_BUILD_NUMBER || process.env.CIRCLE_BUILD_NUM || process.env.BUILD_NUMBER
this.buildNumber = (<any>this.devMetadata.build)["build-version"] || process.env.TRAVIS_BUILD_NUMBER || process.env.APPVEYOR_BUILD_NUMBER || process.env.CIRCLE_BUILD_NUM || process.env.BUILD_NUMBER

if (isEmptyOrSpaces(buildVersion)) {
buildVersion = this.version
Expand Down Expand Up @@ -75,7 +74,7 @@ export class AppInfo {
}

get copyright(): string {
const copyright = (<ElectronPackagerOptions>this.devMetadata.build)["app-copyright"]
const copyright = (<any>this.devMetadata.build)["app-copyright"]
if (copyright != null) {
return copyright
}
Expand Down
2 changes: 1 addition & 1 deletion src/linuxPackager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export class LinuxPackager extends PlatformPackager<LinuxBuildOptions> {

async pack(outDir: string, arch: Arch, targets: Array<Target>, postAsyncTasks: Array<Promise<any>>): Promise<any> {
const appOutDir = this.computeAppOutDir(outDir, arch)
await this.doPack(await this.computePackOptions(outDir, appOutDir, arch), outDir, appOutDir, arch, this.platformSpecificBuildOptions)
await this.doPack(await this.computePackOptions(), outDir, appOutDir, this.platform.nodeName, arch, this.platformSpecificBuildOptions)

postAsyncTasks.push(this.packageInDistributableFormat(outDir, appOutDir, arch, targets))
}
Expand Down
10 changes: 3 additions & 7 deletions src/macPackager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,14 @@ export default class MacPackager extends PlatformPackager<MacOptions> {
}

async pack(outDir: string, arch: Arch, targets: Array<Target>, postAsyncTasks: Array<Promise<any>>): Promise<any> {
const packOptions = await this.computePackOptions(outDir, this.computeAppOutDir(outDir, arch), arch)
const packOptions = await this.computePackOptions()
let nonMasPromise: Promise<any> | null = null

const hasMas = targets.length !== 0 && targets.some(it => it.name === "mas")

if (!hasMas || targets.length > 1) {
const appOutDir = this.computeAppOutDir(outDir, arch)
nonMasPromise = this.doPack(packOptions, outDir, appOutDir, arch, this.platformSpecificBuildOptions)
nonMasPromise = this.doPack(packOptions, outDir, appOutDir, this.platform.nodeName, arch, this.platformSpecificBuildOptions)
.then(() => this.sign(appOutDir, null))
.then(() => {
this.packageInDistributableFormat(appOutDir, targets, postAsyncTasks)
Expand All @@ -88,11 +88,7 @@ export default class MacPackager extends PlatformPackager<MacOptions> {
const appOutDir = path.join(outDir, "mas")
const masBuildOptions = deepAssign({}, this.platformSpecificBuildOptions, (<any>this.devMetadata.build)["mas"])
//noinspection JSUnusedGlobalSymbols
await this.doPack(Object.assign({}, packOptions, {
platform: "mas",
"osx-sign": false,
generateFinalBasename: function () { return "mas" }
}), outDir, appOutDir, arch, masBuildOptions)
await this.doPack(packOptions, outDir, appOutDir, "mas", arch, masBuildOptions)
await this.sign(appOutDir, masBuildOptions)
}

Expand Down
8 changes: 5 additions & 3 deletions src/metadata.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ElectronPackagerOptions } from "electron-packager-tf"
import { AsarOptions } from "asar"
import { ElectronPackagerOptions } from "./packager/dirPackager"

export interface Metadata {
readonly repository?: string | RepositoryInfo | null
Expand Down Expand Up @@ -185,8 +185,6 @@ export interface BuildMetadata {
*/
readonly compression?: CompressionLevel | null

readonly "build-version"?: string | null

/*
*programmatic API only* The function to be run after pack (but before pack into distributable format and sign). Promise must be returned.
*/
Expand Down Expand Up @@ -535,6 +533,10 @@ export enum Arch {
ia32, x64
}

export function archToString(arch: Arch): string {
return arch === Arch.ia32 ? "ia32" : "x64"
}

export function archFromString(name: string): Arch {
if (name === "x64") {
return Arch.x64
Expand Down
2 changes: 1 addition & 1 deletion src/packager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ export function normalizePlatforms(rawPlatforms: Array<string | Platform> | stri
}

function checkConflictingOptions(options: any) {
for (let name of ["all", "out", "tmpdir", "version", "platform", "dir", "arch", "name"]) {
for (let name of ["all", "out", "tmpdir", "version", "platform", "dir", "arch", "name", "extra-resource"]) {
if (name in options) {
throw new Error(`Option ${name} is ignored, do not specify it.`)
}
Expand Down
39 changes: 39 additions & 0 deletions src/packager/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import * as path from "path"
import { ElectronPackagerOptions } from "./dirPackager"

export function userIgnoreFilter(opts: ElectronPackagerOptions, appDir: string) {
let ignore = opts.ignore || []
let ignoreFunc: any

if (typeof (ignore) === "function") {
ignoreFunc = function (file: string) { return !ignore(file) }
}
else {
if (!Array.isArray(ignore)) {
ignore = [ignore]
}

ignoreFunc = function (file: string) {
for (let i = 0; i < ignore.length; i++) {
if (file.match(ignore[i])) {
return false
}
}

return true
}
}

return function filter(file: string) {
let name = file.split(path.resolve(appDir))[1]
if (path.sep === "\\") {
// convert slashes so unix-format ignores work
name = name.replace(/\\/g, "/")
}
return ignoreFunc(name)
}
}

export function initializeApp(opts: any, buildDir: string, appRelativePath: string) {
return opts.initializeApp(opts, buildDir, appRelativePath)
}
64 changes: 64 additions & 0 deletions src/packager/dirPackager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { Promise as BluebirdPromise } from "bluebird"
import { emptyDir } from "fs-extra-p"
import { warn } from "../util/log"
import { AppInfo } from "../appInfo"

const downloadElectron: (options: any) => Promise<any> = BluebirdPromise.promisify(require("electron-download"))
const extract: any = BluebirdPromise.promisify(require("extract-zip"))

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

export interface ElectronPackagerOptions {
"extend-info"?: string
"app-category-type"?: string
appBundleId: string

protocols?: any

appInfo: AppInfo

icon?: string;

"app-bundle-id"?: string | null;

"helper-bundle-id"?: string | null;

ignore?: any

initializeApp?: (opts: ElectronPackagerOptions, buildDir: string, appRelativePath: string) => Promise<any>
}

const supportedPlatforms: any = {
// Maps to module ID for each platform (lazy-required if used)
darwin: "./mac",
linux: "./linux",
mas: "./mac", // map to darwin
win32: "./win32"
}

function createDownloadOpts(opts: any, platform: string, arch: string, electronVersion: string) {
const downloadOpts = Object.assign({
cache: opts.cache,
strictSSL: opts["strict-ssl"]
}, opts.download)

subOptionWarning(downloadOpts, "download", "platform", platform)
subOptionWarning(downloadOpts, "download", "arch", arch)
subOptionWarning(downloadOpts, "download", "version", electronVersion)
return downloadOpts
}

function subOptionWarning (properties: any, optionName: any, parameter: any, value: any) {
if (properties.hasOwnProperty(parameter)) {
warn(`${optionName}.${parameter} will be inferred from the main options`)
}
properties[parameter] = value
}

export async function pack(opts: ElectronPackagerOptions, out: string, platform: string, arch: string, electronVersion: string) {
const zipPath = await downloadElectron(createDownloadOpts(opts, platform, arch, electronVersion))
await emptyDir(out)
await extract(zipPath, {dir: out})
await require(supportedPlatforms[platform]).createApp(opts, out)
}
12 changes: 12 additions & 0 deletions src/packager/linux.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { rename } from "fs-extra-p"
import * as path from "path"
import { initializeApp } from "./common"
import { ElectronPackagerOptions } from "./dirPackager"
import { Promise as BluebirdPromise } from "bluebird"

export function createApp(opts: ElectronPackagerOptions, buildDir: string) {
return BluebirdPromise.all([
initializeApp(opts, buildDir, path.join("resources", "app")),
rename(path.join(buildDir, "electron"), path.join(buildDir, opts.appInfo.productFilename))
])
}
Loading

0 comments on commit 0022b86

Please sign in to comment.