Skip to content

Commit

Permalink
perf(nsis): run tasks in parallel
Browse files Browse the repository at this point in the history
  • Loading branch information
develar committed Apr 10, 2017
1 parent ada9cac commit 6f56905
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 27 deletions.
10 changes: 10 additions & 0 deletions docs/Developer API.md
Original file line number Diff line number Diff line change
Expand Up @@ -4909,6 +4909,7 @@ File permission is fixed — allow execute for all if owner can, allow read for
* [electron-builder-util/out/promise](#module_electron-builder-util/out/promise)
* [.NestedError](#NestedError) ⇐ <code>Error</code>
* [`.all(promises)`](#module_electron-builder-util/out/promise.all) ⇒ <code>module:bluebird-lst.Bluebird&lt;any&gt;</code>
* [`.asyncAll(tasks)`](#module_electron-builder-util/out/promise.asyncAll) ⇒ <code>module:bluebird-lst.Bluebird&lt;Array&lt;any&gt;&gt;</code>
* [`.executeFinally(promise, task)`](#module_electron-builder-util/out/promise.executeFinally) ⇒ <code>Promise&lt;module:electron-builder-util/out/promise.T&gt;</code>
* [`.printErrorAndExit(error)`](#module_electron-builder-util/out/promise.printErrorAndExit)
* [`.throwError(errors)`](#module_electron-builder-util/out/promise.throwError)
Expand All @@ -4927,6 +4928,15 @@ File permission is fixed — allow execute for all if owner can, allow read for
| --- | --- |
| promises | <code>Array&lt;Promise&lt;any&gt;&gt;</code> |

<a name="module_electron-builder-util/out/promise.asyncAll"></a>

### `electron-builder-util/out/promise.asyncAll(tasks)` ⇒ <code>module:bluebird-lst.Bluebird&lt;Array&lt;any&gt;&gt;</code>
**Kind**: method of <code>[electron-builder-util/out/promise](#module_electron-builder-util/out/promise)</code>

| Param | Type |
| --- | --- |
| tasks | <code>Array&lt;module:electron-builder-util/out/promise.__type&gt;</code> |

<a name="module_electron-builder-util/out/promise.executeFinally"></a>

### `electron-builder-util/out/promise.executeFinally(promise, task)` ⇒ <code>Promise&lt;module:electron-builder-util/out/promise.T&gt;</code>
Expand Down
4 changes: 4 additions & 0 deletions packages/electron-builder-util/src/promise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,8 @@ export function throwError(errors: Array<Error>) {
else if (errors.length > 1) {
throw new NestedError(errors, "Cannot cleanup: ")
}
}

export function asyncAll(tasks: Array<() => Promise<any>>) {
return BluebirdPromise.map(tasks, it => it())
}
7 changes: 1 addition & 6 deletions packages/electron-builder-util/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,7 @@ export function handleProcess(event: string, childProcess: ChildProcess, command

if (code !== 0) {
function formatOut(text: string, title: string) {
if (text.length === 0) {
return ""
}
else {
return `\n${title}:\n${text}`
}
return text.length === 0 ? "" : `\n${title}:\n${text}`
}

reject(new Error(`${command} exited with code ${code}${formatOut(out, "Output")}${formatOut(errorOut, "Error output")}`))
Expand Down
56 changes: 35 additions & 21 deletions packages/electron-builder/src/targets/nsis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { asArray, debug, doSpawn, exec, getPlatformIconFileName, handleProcess,
import { getBinFromBintray } from "electron-builder-util/out/binDownload"
import { copyFile } from "electron-builder-util/out/fs"
import { log, subTask, warn } from "electron-builder-util/out/log"
import { asyncAll } from "electron-builder-util/out/promise"
import { outputFile, readFile, unlink } from "fs-extra-p"
import { safeLoad } from "js-yaml"
import * as path from "path"
Expand Down Expand Up @@ -416,35 +417,48 @@ export class NsisTarget extends Target {
private async computeFinalScript(originalScript: string, isInstaller: boolean) {
const packager = this.packager
let scriptHeader = `!addincludedir "${path.win32.join(__dirname, "..", "..", "templates", "nsis", "include")}"\n`

const pluginArch = this.isUnicodeEnabled ? "x86-unicode" : "x86-ansi"
scriptHeader += `!addplugindir /${pluginArch} "${path.join(await nsisResourcePathPromise, "plugins", pluginArch)}"\n`
scriptHeader += `!addplugindir /${pluginArch} "${path.join(packager.buildResourcesDir, pluginArch)}"\n`

// http://stackoverflow.com/questions/997456/nsis-license-file-based-on-language-selection
const licensePage = await this.computeLicensePage()
if (licensePage != null) {
scriptHeader += createMacro("licensePage", licensePage)
}

const addCustomMessageFileInclude = async (input: string) => {
scriptHeader += "!include " + await this.writeCustomLangFile(computeCustomMessageTranslations(safeLoad(await readFile(path.join(this.nsisTemplatesDir, input), "utf-8"))).join("\n")) + "\n"
}
return "!include " + await this.writeCustomLangFile(computeCustomMessageTranslations(safeLoad(await readFile(path.join(this.nsisTemplatesDir, input), "utf-8"))).join("\n")) + "\n"
}

const tasks: Array<() => Promise<any>> = [
async () => {
const pluginArch = this.isUnicodeEnabled ? "x86-unicode" : "x86-ansi"
let result = `!addplugindir /${pluginArch} "${path.join(await nsisResourcePathPromise, "plugins", pluginArch)}"\n`
result += `!addplugindir /${pluginArch} "${path.join(packager.buildResourcesDir, pluginArch)}"\n`
return result
},
async () => {
// http://stackoverflow.com/questions/997456/nsis-license-file-based-on-language-selection
const licensePage = await this.computeLicensePage()
return licensePage == null ? "" : createMacro("licensePage", licensePage)
},
() => addCustomMessageFileInclude("messages.yml"),
]

await addCustomMessageFileInclude("messages.yml")
if (!this.isPortable) {
if (this.options.oneClick === false) {
tasks.push(() => addCustomMessageFileInclude("boringMessages.yml"))
}

if (this.isPortable) {
return scriptHeader + originalScript
tasks.push(async () => {
let result = ""
const customInclude = await packager.getResource(this.options.include, "installer.nsh")
if (customInclude != null) {
result += `!addincludedir "${packager.buildResourcesDir}"\n`
result += `!include "${customInclude}"\n\n`
}
return result
})
}

if (this.options.oneClick === false) {
await addCustomMessageFileInclude("boringMessages.yml")
for (const s of await asyncAll(tasks)) {
scriptHeader += s
}

const customInclude = await packager.getResource(this.options.include, "installer.nsh")
if (customInclude != null) {
scriptHeader += `!addincludedir "${packager.buildResourcesDir}"\n`
scriptHeader += `!include "${customInclude}"\n\n`
if (this.isPortable) {
return scriptHeader + originalScript
}

const fileAssociations = packager.fileAssociations
Expand Down
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1078,6 +1078,10 @@ ecc-jsbn@~0.1.1:
dependencies:
jsbn "~0.1.0"

electron-is-dev@^0.1.2:
version "0.1.2"
resolved "https://registry.yarnpkg.com/electron-is-dev/-/electron-is-dev-0.1.2.tgz#8a1043e32b3a1da1c3f553dce28ce764246167e3"

[email protected]:
version "0.4.4"
resolved "https://registry.yarnpkg.com/electron-osx-sign/-/electron-osx-sign-0.4.4.tgz#afdf38450ccaebe6dabeca71fb0fad6294a8c57c"
Expand Down

0 comments on commit 6f56905

Please sign in to comment.