Skip to content

Commit

Permalink
feat(snap): ability to specify plug options
Browse files Browse the repository at this point in the history
Close #2100
  • Loading branch information
develar committed Feb 2, 2018
1 parent c2fad34 commit e488bfc
Show file tree
Hide file tree
Showing 9 changed files with 235 additions and 50 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.

15 changes: 14 additions & 1 deletion packages/electron-builder-lib/src/options/SnapOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,21 @@ export interface SnapOptions extends CommonLinuxOptions, TargetSpecificOptions {
* Defaults to `["desktop", "desktop-legacy", "home", "x11", "unity7", "browser-support", "network", "gsettings", "pulseaudio", "opengl"]`.
*
* If list contains `default`, it will be replaced to default list, so, `["default", "foo"]` can be used to add custom plug `foo` in addition to defaults.
*
* Additional attributes can be specified using object instead of just name of plug:
* ```
*[
* {
* "browser-sandbox": {
* "interface": "browser-support",
* "allow-sandbox": true
* },
* },
* "another-simple-plug-name"
*]
* ```
*/
readonly plugs?: Array<string> | null
readonly plugs?: Array<string | object> | object | null

/**
* Specifies any [parts](https://snapcraft.io/docs/reference/parts) that should be built before this part.
Expand Down
37 changes: 36 additions & 1 deletion packages/electron-builder-lib/src/targets/snap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ export default class SnapTarget extends Target {
const appInfo = this.packager.appInfo
const options = this.options
const linuxArchName = toAppImageOrSnapArch(arch)

const plugs: { [key: string]: object | null } | null = normalizePlugConfiguration(options.plugs == null ? null : asArray(options.plugs))
const plugNames = this.replaceDefault(plugs == null ? null : Object.getOwnPropertyNames(plugs), defaultPlugs)

const snap: any = {
name: snapName,
version: appInfo.version,
Expand All @@ -74,7 +78,7 @@ export default class SnapTarget extends Target {
].join(":"),
...options.environment,
},
plugs: this.replaceDefault(options.plugs, defaultPlugs),
plugs: plugNames,
}
},
parts: {
Expand All @@ -87,6 +91,20 @@ export default class SnapTarget extends Target {
},
}

if (plugs != null) {
for (const plugName of plugNames) {
const plugOptions = plugs[plugName]
if (plugOptions == null) {
continue
}

if (snap.plugs == null) {
snap.plugs = {}
}
snap.plugs[plugName] = plugOptions
}
}

if (options.assumes != null) {
snap.assumes = asArray(options.assumes)
}
Expand Down Expand Up @@ -234,4 +252,21 @@ export default class SnapTarget extends Target {
stdio: ["ignore", "inherit", "inherit"],
})
}
}

function normalizePlugConfiguration(raw: Array<string | object> | null) {
if (raw == null) {
return null
}

const result: any = {}
for (const item of raw) {
if (typeof item === "string") {
result[item] = null
}
else {
Object.assign(result, item)
}
}
return result
}
4 changes: 4 additions & 0 deletions packages/electron-builder/src/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ export interface CliOptions extends PackagerOptions, PublishOptions {

/** @internal */
export function normalizeOptions(args: CliOptions): BuildOptions {
if ((args as any).extraMetadata != null) {
throw new InvalidConfigurationError("Please specify extraMetadata under config field")
}

if (args.targets != null) {
return args
}
Expand Down
20 changes: 0 additions & 20 deletions test/out/__snapshots__/BuildTest.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -257,26 +257,6 @@ Object {
}
`;
exports[`extraMetadata and config as path 1`] = `
Object {
"linux": Array [],
}
`;
exports[`extraMetadata and config as path 2`] = `
Object {
"author": "Foo Bar <[email protected]>",
"description": "Test Application (test quite \\" #378)",
"field": "bar.js",
"homepage": "http://foo.example.com",
"license": "MIT",
"name": "TestApp",
"private": true,
"productName": "Test App ßW",
"version": "1.1.0",
}
`;
exports[`posix smart unpack 1`] = `
Object {
"linux": Array [],
Expand Down
141 changes: 136 additions & 5 deletions test/out/linux/__snapshots__/snapTest.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ exports[`custom env 1`] = `
Object {
"apps": Object {
"sep": Object {
"command": "command-sep.wrapper",
"adapter": "none",
"command": "bin/desktop-launch $SNAP/sep",
"environment": Object {
"FOO": "bar",
"LD_LIBRARY_PATH": "$SNAP_LIBRARY_PATH:$SNAP/usr/lib/x86_64-linux-gnu:$SNAP/usr/lib/x86_64-linux-gnu/pulseaudio:$SNAP/usr/lib/x86_64-linux-gnu/mesa-egl:$SNAP/lib:$SNAP/usr/lib:$SNAP/lib/x86_64-linux-gnu:$SNAP/usr/lib/x86_64-linux-gnu:$LD_LIBRARY_PATH:$SNAP/lib:$SNAP/usr/lib:$SNAP/lib/x86_64-linux-gnu:$SNAP/usr/lib/x86_64-linux-gnu",
"PATH": "$SNAP/usr/sbin:$SNAP/usr/bin:$SNAP/sbin:$SNAP/bin:$PATH",
"TMPDIR": "$XDG_RUNTIME_DIR",
},
"plugs": Array [
Expand Down Expand Up @@ -42,8 +45,11 @@ exports[`default stagePackages 1`] = `
Object {
"apps": Object {
"sep": Object {
"command": "desktop-launch $SNAP/sep",
"adapter": "none",
"command": "bin/desktop-launch $SNAP/sep",
"environment": Object {
"LD_LIBRARY_PATH": "$SNAP_LIBRARY_PATH:$SNAP/usr/lib/x86_64-linux-gnu:$SNAP/usr/lib/x86_64-linux-gnu/pulseaudio:$SNAP/usr/lib/x86_64-linux-gnu/mesa-egl:$SNAP/lib:$SNAP/usr/lib:$SNAP/lib/x86_64-linux-gnu:$SNAP/usr/lib/x86_64-linux-gnu:$LD_LIBRARY_PATH:$SNAP/lib:$SNAP/usr/lib:$SNAP/lib/x86_64-linux-gnu:$SNAP/usr/lib/x86_64-linux-gnu",
"PATH": "$SNAP/usr/sbin:$SNAP/usr/bin:$SNAP/sbin:$SNAP/bin:$PATH",
"TMPDIR": "$XDG_RUNTIME_DIR",
},
"plugs": Array [
Expand Down Expand Up @@ -99,8 +105,11 @@ exports[`default stagePackages 3`] = `
Object {
"apps": Object {
"sep": Object {
"command": "desktop-launch $SNAP/sep",
"adapter": "none",
"command": "bin/desktop-launch $SNAP/sep",
"environment": Object {
"LD_LIBRARY_PATH": "$SNAP_LIBRARY_PATH:$SNAP/usr/lib/x86_64-linux-gnu:$SNAP/usr/lib/x86_64-linux-gnu/pulseaudio:$SNAP/usr/lib/x86_64-linux-gnu/mesa-egl:$SNAP/lib:$SNAP/usr/lib:$SNAP/lib/x86_64-linux-gnu:$SNAP/usr/lib/x86_64-linux-gnu:$LD_LIBRARY_PATH:$SNAP/lib:$SNAP/usr/lib:$SNAP/lib/x86_64-linux-gnu:$SNAP/usr/lib/x86_64-linux-gnu",
"PATH": "$SNAP/usr/sbin:$SNAP/usr/bin:$SNAP/sbin:$SNAP/bin:$PATH",
"TMPDIR": "$XDG_RUNTIME_DIR",
},
"plugs": Array [
Expand Down Expand Up @@ -158,8 +167,11 @@ exports[`default stagePackages 5`] = `
Object {
"apps": Object {
"sep": Object {
"command": "desktop-launch $SNAP/sep",
"adapter": "none",
"command": "bin/desktop-launch $SNAP/sep",
"environment": Object {
"LD_LIBRARY_PATH": "$SNAP_LIBRARY_PATH:$SNAP/usr/lib/x86_64-linux-gnu:$SNAP/usr/lib/x86_64-linux-gnu/pulseaudio:$SNAP/usr/lib/x86_64-linux-gnu/mesa-egl:$SNAP/lib:$SNAP/usr/lib:$SNAP/lib/x86_64-linux-gnu:$SNAP/usr/lib/x86_64-linux-gnu:$LD_LIBRARY_PATH:$SNAP/lib:$SNAP/usr/lib:$SNAP/lib/x86_64-linux-gnu:$SNAP/usr/lib/x86_64-linux-gnu",
"PATH": "$SNAP/usr/sbin:$SNAP/usr/bin:$SNAP/sbin:$SNAP/bin:$PATH",
"TMPDIR": "$XDG_RUNTIME_DIR",
},
"plugs": Array [
Expand Down Expand Up @@ -217,8 +229,11 @@ exports[`default stagePackages 7`] = `
Object {
"apps": Object {
"sep": Object {
"command": "desktop-launch $SNAP/sep",
"adapter": "none",
"command": "bin/desktop-launch $SNAP/sep",
"environment": Object {
"LD_LIBRARY_PATH": "$SNAP_LIBRARY_PATH:$SNAP/usr/lib/x86_64-linux-gnu:$SNAP/usr/lib/x86_64-linux-gnu/pulseaudio:$SNAP/usr/lib/x86_64-linux-gnu/mesa-egl:$SNAP/lib:$SNAP/usr/lib:$SNAP/lib/x86_64-linux-gnu:$SNAP/usr/lib/x86_64-linux-gnu:$LD_LIBRARY_PATH:$SNAP/lib:$SNAP/usr/lib:$SNAP/lib/x86_64-linux-gnu:$SNAP/usr/lib/x86_64-linux-gnu",
"PATH": "$SNAP/usr/sbin:$SNAP/usr/bin:$SNAP/sbin:$SNAP/bin:$PATH",
"TMPDIR": "$XDG_RUNTIME_DIR",
},
"plugs": Array [
Expand Down Expand Up @@ -274,6 +289,122 @@ Object {
}
`;

exports[`plugs option 1`] = `
Object {
"apps": Object {
"testapp": Object {
"adapter": "none",
"command": "bin/desktop-launch $SNAP/testapp",
"environment": Object {
"LD_LIBRARY_PATH": "$SNAP_LIBRARY_PATH:$SNAP/usr/lib/x86_64-linux-gnu:$SNAP/usr/lib/x86_64-linux-gnu/pulseaudio:$SNAP/usr/lib/x86_64-linux-gnu/mesa-egl:$SNAP/lib:$SNAP/usr/lib:$SNAP/lib/x86_64-linux-gnu:$SNAP/usr/lib/x86_64-linux-gnu:$LD_LIBRARY_PATH:$SNAP/lib:$SNAP/usr/lib:$SNAP/lib/x86_64-linux-gnu:$SNAP/usr/lib/x86_64-linux-gnu",
"PATH": "$SNAP/usr/sbin:$SNAP/usr/bin:$SNAP/sbin:$SNAP/bin:$PATH",
"TMPDIR": "$XDG_RUNTIME_DIR",
},
"plugs": Array [
"browser-sandbox",
"another-simple-plug-name",
],
},
},
"confinement": "strict",
"description": "Test Application (test quite “ #378)",
"grade": "stable",
"icon": "snap/gui/icon.png",
"name": "testapp",
"parts": Object {
"app": Object {
"after": Array [
"desktop-gtk2",
],
"plugin": "dump",
"stage-packages": Array [
"libasound2",
"libgconf2-4",
"libnotify4",
"libnspr4",
"libnss3",
"libpcre3",
"libpulse0",
"libxss1",
"libxtst6",
],
},
},
"plugs": Object {
"browser-sandbox": Object {
"allow-sandbox": true,
"interface": "browser-support",
},
},
"summary": "Test App ßW",
"version": "1.1.0",
}
`;

exports[`plugs option 2`] = `
Object {
"linux": Array [],
}
`;

exports[`plugs option 3`] = `
Object {
"apps": Object {
"testapp": Object {
"adapter": "none",
"command": "bin/desktop-launch $SNAP/testapp",
"environment": Object {
"LD_LIBRARY_PATH": "$SNAP_LIBRARY_PATH:$SNAP/usr/lib/x86_64-linux-gnu:$SNAP/usr/lib/x86_64-linux-gnu/pulseaudio:$SNAP/usr/lib/x86_64-linux-gnu/mesa-egl:$SNAP/lib:$SNAP/usr/lib:$SNAP/lib/x86_64-linux-gnu:$SNAP/usr/lib/x86_64-linux-gnu:$LD_LIBRARY_PATH:$SNAP/lib:$SNAP/usr/lib:$SNAP/lib/x86_64-linux-gnu:$SNAP/usr/lib/x86_64-linux-gnu",
"PATH": "$SNAP/usr/sbin:$SNAP/usr/bin:$SNAP/sbin:$SNAP/bin:$PATH",
"TMPDIR": "$XDG_RUNTIME_DIR",
},
"plugs": Array [
"browser-sandbox",
"another-simple-plug-name",
],
},
},
"confinement": "strict",
"description": "Test Application (test quite “ #378)",
"grade": "stable",
"icon": "snap/gui/icon.png",
"name": "testapp",
"parts": Object {
"app": Object {
"after": Array [
"desktop-gtk2",
],
"plugin": "dump",
"stage-packages": Array [
"libasound2",
"libgconf2-4",
"libnotify4",
"libnspr4",
"libnss3",
"libpcre3",
"libpulse0",
"libxss1",
"libxtst6",
],
},
},
"plugs": Object {
"browser-sandbox": Object {
"allow-sandbox": true,
"interface": "browser-support",
},
},
"summary": "Test App ßW",
"version": "1.1.0",
}
`;

exports[`plugs option 4`] = `
Object {
"linux": Array [],
}
`;

exports[`snap 1`] = `
Object {
"linux": Array [
Expand Down
21 changes: 1 addition & 20 deletions test/src/BuildTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { walk } from "builder-util/out/fs"
import { checkWineVersion } from "builder-util/out/wine"
import { Arch, createTargets, DIR_TARGET, Platform } from "electron-builder"
import { readAsar } from "electron-builder-lib/out/asar/asar"
import { move, outputJson, readFileSync, readJson } from "fs-extra-p"
import { move, outputJson, readFileSync } from "fs-extra-p"
import * as path from "path"
import { app, appTwo, appTwoThrows, assertPack, linuxDirTarget, modifyPackageJson, packageJson } from "./helpers/packTester"
import { ELECTRON_VERSION } from "./helpers/testConfig"
Expand Down Expand Up @@ -70,25 +70,6 @@ test("relative index", appTwo({
}, true)
}))

test("extraMetadata and config as path", app(Object.assign(require("electron-builder/out/builder").normalizeOptions({
extraMetadata: {
field: "bar.js"
},
config: "foo.json",
}), {
targets: linuxDirTarget,
}), {
projectDirCreated: projectDir => {
return outputJson(path.join(projectDir, "foo.json"), {
asar: false
})
},
packed: async context => {
const resourceDir = context.getResources(Platform.LINUX)
expect(await readJson(path.join(resourceDir, "app", "package.json"))).toMatchSnapshot()
}
}))

it.ifDevOrLinuxCi("electron version from electron-prebuilt dependency", app({
targets: linuxDirTarget,
}, {
Expand Down
7 changes: 5 additions & 2 deletions test/src/helpers/runTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ async function runTests() {
}
else if (circleNodeIndex === 2) {
testPatterns.push("snapTest")
testPatterns.push("mainEntryTest")
testPatterns.push("PublishManagerTest", "ArtifactPublisherTest", "httpRequestTest", "RepoSlugTest")
testPatterns.push("PublishManagerTest")
testPatterns.push("macPackagerTest")
testPatterns.push("linuxPackagerTest")
}
Expand All @@ -74,6 +73,10 @@ async function runTests() {
testPatterns.push("webInstallerTest")
testPatterns.push("msiTest")
testPatterns.push("ignoreTest")
testPatterns.push("mainEntryTest")
testPatterns.push("ArtifactPublisherTest")
testPatterns.push("RepoSlugTest")
testPatterns.push("httpRequestTest")
}
console.log(`Test files for node ${circleNodeIndex}: ${testPatterns.join(", ")}`)
}
Expand Down
Loading

0 comments on commit e488bfc

Please sign in to comment.