Skip to content

Commit

Permalink
feat: build in distributable format prepackaged directory
Browse files Browse the repository at this point in the history
  • Loading branch information
develar committed Dec 28, 2016
1 parent 94fed39 commit 1c59534
Show file tree
Hide file tree
Showing 12 changed files with 81 additions and 21 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ Use one of the shared run configurations as a template and:

## Run Test using CLI
```sh
TEST_APP_TMP_DIR=/tmp/electron-builder-test NODE_PATH=. ./node_modules/.bin/ava --match="boring" test/out/nsisTest.js
TEST_APP_TMP_DIR=/tmp/electron-builder-test NODE_PATH=. ./node_modules/.bin/jest --env jest-environment-node-debug '/TestFileName\.\w+$'
```

where `TEST_APP_TMP_DIR` is specified to easily inspect and use test build, `boring` is the test name and `test/out/nsisTest.js` is the path to test file.
8 changes: 8 additions & 0 deletions src/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ export interface CliOptions extends PackagerOptions, PublishOptions {
dir?: boolean

platform?: string

project?: string
}

function addValue<K, T>(map: Map<K, Array<T>>, key: K, value: T) {
Expand Down Expand Up @@ -158,6 +160,11 @@ export function normalizeOptions(args: CliOptions): BuildOptions {
delete result.ia32
delete result.x64
delete result.armv7l

if (result.project != null) {
result.projectDir = result.project
}
delete result.project
return result
}

Expand Down Expand Up @@ -231,6 +238,7 @@ export async function build(rawOptions?: CliOptions): Promise<Array<string>> {
}
}

//noinspection JSMismatchedCollectionQueryUpdate
const artifactPaths: Array<string> = []
packager.artifactCreated(event => {
if (event.file != null) {
Expand Down
11 changes: 10 additions & 1 deletion src/cli/cliOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,16 @@ export function createYargs(): any {
.option("extraMetadata", {
alias: ["em"],
group: buildGroup,
describe: "Inject properties to application package.json (asar only)",
describe: "Inject properties to package.json (asar only)",
})
.option("prepackaged", {
alias: ["pd"],
group: buildGroup,
describe: "The path to prepackaged app (to pack into distributable format). Also requires `project`",
})
.option("project", {
group: buildGroup,
describe: "The path to project directory (required only for `prepackaged`)",
})
.strict()
.group(["help", "version"], "Other:")
Expand Down
6 changes: 5 additions & 1 deletion src/packager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export class Packager implements BuildInfo {

const platformToTarget: Map<Platform, Map<String, Target>> = new Map()
// custom packager - don't check wine
let checkWine = this.options.platformPackagerFactory == null
let checkWine = this.options.prepackaged == null && this.options.platformPackagerFactory == null
for (const [platform, archToType] of this.options.targets!) {
if (platform === Platform.MAC && process.platform === Platform.WINDOWS.nodeName) {
throw new Error("Build for macOS is supported only on macOS, please see https://github.com/electron-userland/electron-builder/wiki/Multi-Platform-Build")
Expand Down Expand Up @@ -241,6 +241,10 @@ export class Packager implements BuildInfo {
}

private async installAppDependencies(platform: Platform, arch: Arch): Promise<any> {
if (this.options.prepackaged != null) {
return
}

const options = this.config
if (options.nodeGypRebuild === true) {
log(`Executing node-gyp rebuild for arch ${Arch[arch]}`)
Expand Down
8 changes: 7 additions & 1 deletion src/platformPackager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ export interface PackagerOptions {
readonly effectiveOptionComputed?: (options: any) => Promise<boolean>

readonly extraMetadata?: any

readonly prepackaged?: string
}

export interface BuildInfo {
Expand Down Expand Up @@ -161,7 +163,7 @@ export abstract class PlatformPackager<DC extends PlatformSpecificBuildOptions>
}

async pack(outDir: string, arch: Arch, targets: Array<Target>, postAsyncTasks: Array<Promise<any>>): Promise<any> {
const appOutDir = this.computeAppOutDir(outDir, arch)
const appOutDir = this.options.prepackaged || this.computeAppOutDir(outDir, arch)
await this.doPack(outDir, appOutDir, this.platform.nodeName, arch, this.platformSpecificBuildOptions)
this.packageInDistributableFormat(appOutDir, arch, targets, postAsyncTasks)
}
Expand All @@ -177,6 +179,10 @@ export abstract class PlatformPackager<DC extends PlatformSpecificBuildOptions>
}

protected async doPack(outDir: string, appOutDir: string, platformName: string, arch: Arch, platformSpecificBuildOptions: DC) {
if (this.info.options.prepackaged != null) {
return
}

const asarOptions = this.computeAsarOptions(platformSpecificBuildOptions)
const fileMatchOptions: FileMatchOptions = {
arch: Arch[arch],
Expand Down
17 changes: 14 additions & 3 deletions src/targets/archive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { CompressionLevel } from "../metadata"
import * as path from "path"
import { unlink } from "fs-extra-p"
import { path7za } from "7zip-bin"
import { exists } from "../util/fs"

class CompressionDescriptor {
constructor(public flag: string, public env: string, public minLevel: string, public maxLevel: string = "-9") {
Expand Down Expand Up @@ -72,9 +73,19 @@ export async function archive(compression: CompressionLevel | n, format: string,

args.push(outFile, withoutDir ? "." : path.basename(dirToArchive))

await spawn(path7za, args, {
cwd: withoutDir ? dirToArchive : path.dirname(dirToArchive),
})
try {
await spawn(path7za, args, {
cwd: withoutDir ? dirToArchive : path.dirname(dirToArchive),
})
}
catch (e) {
if (e.code === "ENOENT" && !(await exists(dirToArchive))) {
throw new Error(`Cannot create archive: "${dirToArchive}" doesn't exist`)
}
else {
throw e
}
}

return outFile
}
9 changes: 7 additions & 2 deletions src/targets/fpm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ import { smarten } from "../platformPackager"
import { use, exec } from "../util/util"
import * as path from "path"
import { getBin } from "../util/binDownload"
import { readFile, outputFile } from "fs-extra-p"
import { readFile, outputFile, ensureDir } from "fs-extra-p"
import BluebirdPromise from "bluebird-lst-c"
import { LinuxTargetHelper, installPrefix } from "./LinuxTargetHelper"
import * as errorMessages from "../errorMessages"
import { TmpDir } from "../util/tmp"
import { LinuxPackager } from "../linuxPackager"
import { log } from "../util/log"
import { Target } from "./targetFactory"
import { unlinkIfExists } from "../util/fs"

const template = require("lodash.template")

Expand Down Expand Up @@ -71,6 +72,10 @@ export default class FpmTarget extends Target {
log(`Building ${target}`)

const destination = path.join(this.outDir, this.packager.generateName(target, arch, true /* on Linux we use safe name — without space */))
await unlinkIfExists(destination)
if (this.packager.info.options.prepackaged != null) {
await ensureDir(this.outDir)
}

const scripts = await this.scriptFiles
const packager = this.packager
Expand Down Expand Up @@ -157,7 +162,7 @@ export default class FpmTarget extends Target {
args.push(mapping.join("=/usr/share/icons/hicolor/"))
}

args.push(`${await this.desktopEntry}=/usr/share/applications/${appInfo.productFilename}.desktop`)
args.push(`${await this.desktopEntry}=/usr/share/applications/${this.packager.executableName}.desktop`)

await exec(await fpmPath, args)

Expand Down
8 changes: 3 additions & 5 deletions src/targets/snap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { safeDump } from "js-yaml"
import { spawn } from "../util/util"
import { homedir } from "os"
import { Target } from "./targetFactory"
import BluebirdPromise from "bluebird-lst-c"

export default class SnapTarget extends Target {
private readonly options: SnapOptions = Object.assign({}, this.packager.platformSpecificBuildOptions, (<any>this.packager.config)[this.name])
Expand All @@ -33,7 +32,7 @@ export default class SnapTarget extends Target {
const isUseUbuntuPlatform = options.ubuntuAppPlatformContent != null
if (isUseUbuntuPlatform) {
// ubuntu-app-platform requires empty directory
await BluebirdPromise.all([this.helper.icons, emptyDir(path.join(extraSnapSourceDir, "ubuntu-app-platform"))])
await emptyDir(path.join(extraSnapSourceDir, "ubuntu-app-platform"))
}

const snap: any = {}
Expand Down Expand Up @@ -95,7 +94,7 @@ export default class SnapTarget extends Target {
if (isUseUbuntuPlatform) {
snap.parts.extra = {
plugin: "dump",
source: extraSnapSourceDir
source: isUseDocker ? `/out/${path.basename(snapDir)}/${path.basename(extraSnapSourceDir)}` : extraSnapSourceDir
}
}

Expand All @@ -108,14 +107,13 @@ export default class SnapTarget extends Target {
if (isUseDocker) {
await spawn("docker", ["run", "--rm",
"-v", `${packager.info.projectDir}:/project`,
"-v", `/tmp/apt-cache:/var/cache/apt/archives`,
"-v", `${homedir()}/.electron:/root/.electron`,
// dist dir can be outside of project dir
"-v", `${this.outDir}:/out`,
"electronuserland/electron-builder:latest",
"/bin/bash", "-c", `snapcraft --version && cp -R /out/${path.basename(snapDir)} /s/ && cd /s && snapcraft snap --target-arch ${toLinuxArchString(arch)} -o /out/${snapName}`], {
cwd: packager.info.projectDir,
stdio: ["ignore", "inherit", "pipe"],
stdio: ["ignore", "inherit", "inherit"],
})
}
else {
Expand Down
10 changes: 10 additions & 0 deletions test/out/__snapshots__/BuildTest.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ exports[`test cli 1`] = `
Object {
"draft": undefined,
"extraMetadata": undefined,
"prepackaged": undefined,
"prerelease": undefined,
"publish": undefined,
"targets": Map {
Expand All @@ -20,6 +21,7 @@ exports[`test cli 2`] = `
Object {
"draft": undefined,
"extraMetadata": undefined,
"prepackaged": undefined,
"prerelease": undefined,
"publish": undefined,
"targets": Map {
Expand All @@ -40,6 +42,7 @@ exports[`test cli 3`] = `
Object {
"draft": undefined,
"extraMetadata": undefined,
"prepackaged": undefined,
"prerelease": undefined,
"publish": undefined,
"targets": Map {
Expand All @@ -60,6 +63,7 @@ exports[`test cli 4`] = `
Object {
"draft": undefined,
"extraMetadata": undefined,
"prepackaged": undefined,
"prerelease": undefined,
"publish": undefined,
"targets": Map {
Expand All @@ -78,6 +82,7 @@ exports[`test cli 5`] = `
Object {
"draft": undefined,
"extraMetadata": undefined,
"prepackaged": undefined,
"prerelease": undefined,
"publish": undefined,
"targets": Map {
Expand All @@ -96,6 +101,7 @@ exports[`test cli 6`] = `
Object {
"draft": undefined,
"extraMetadata": undefined,
"prepackaged": undefined,
"prerelease": undefined,
"publish": undefined,
"targets": Map {
Expand Down Expand Up @@ -128,6 +134,7 @@ exports[`test cli 7`] = `
Object {
"draft": undefined,
"extraMetadata": undefined,
"prepackaged": undefined,
"prerelease": undefined,
"publish": undefined,
"targets": Map {
Expand All @@ -148,6 +155,7 @@ exports[`test cli 8`] = `
Object {
"draft": undefined,
"extraMetadata": undefined,
"prepackaged": undefined,
"prerelease": undefined,
"publish": undefined,
"targets": Map {
Expand All @@ -168,6 +176,7 @@ exports[`test cli 9`] = `
Object {
"draft": undefined,
"extraMetadata": undefined,
"prepackaged": undefined,
"prerelease": undefined,
"publish": undefined,
"targets": Map {
Expand All @@ -188,6 +197,7 @@ exports[`test cli 10`] = `
Object {
"draft": undefined,
"extraMetadata": undefined,
"prepackaged": undefined,
"prerelease": undefined,
"publish": undefined,
"targets": Map {
Expand Down
18 changes: 15 additions & 3 deletions test/src/BuildTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@ import { move, outputJson } from "fs-extra-p"
import BluebirdPromise from "bluebird-lst-c"
import * as path from "path"
import { assertThat } from "./helpers/fileAssert"
import { BuildOptions, Platform, PackagerOptions, DIR_TARGET } from "out"
import { normalizeOptions } from "out/builder"
import { BuildOptions, Platform, PackagerOptions, DIR_TARGET, Arch } from "out"
import { normalizeOptions, build } from "out/builder"
import { createYargs } from "out/cli/cliOptions"
import { extractFile } from "asar-electron-builder"
import { ELECTRON_VERSION } from "./helpers/config"
import isCi from "is-ci"
import { checkWineVersion } from "out/packager"
import { Arch } from "out/metadata"

test("cli", async () => {
const yargs = createYargs()
Expand Down Expand Up @@ -220,3 +219,16 @@ function currentPlatform(): PackagerOptions {
targets: Platform.fromString(process.platform).createTarget(DIR_TARGET),
}
}

test.ifDevOrLinuxCi("prepackaged", app({
targets: Platform.LINUX.createTarget(DIR_TARGET),
}, {
packed: async (context) => {
await build(normalizeOptions({
prepackaged: path.join(context.outDir, "linux-unpacked"),
project: context.projectDir,
linux: ["deb"]
}))
await assertThat(path.join(context.projectDir, "dist", "TestApp_1.1.0_amd64.deb")).isFile()
}
}))
2 changes: 1 addition & 1 deletion test/src/helpers/expectedContents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const expectedLinuxContents = ["/",
"/opt/TestApp/resources/app.asar",
"/opt/TestApp/resources/electron.asar",
"/usr/share/applications/",
"/usr/share/applications/TestApp.desktop",
"/usr/share/applications/testapp.desktop",
"/usr/share/doc/",
"/usr/share/icons/",
"/usr/share/doc/testapp/",
Expand Down
3 changes: 0 additions & 3 deletions test/src/helpers/packTester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,6 @@ async function checkLinuxResult(outDir: string, packager: Packager, checkOptions
if (it === "/opt/TestApp/TestApp") {
return `/opt/${productFilename}/TestApp`
}
else if (it === "/usr/share/applications/TestApp.desktop") {
return `/usr/share/applications/${productFilename}.desktop`
}
else {
return it.replace(new RegExp("/opt/TestApp/", "g"), `/opt/${productFilename}/`)
}
Expand Down

0 comments on commit 1c59534

Please sign in to comment.