Skip to content

Commit e488bfc

Browse files
committed
feat(snap): ability to specify plug options
Close #2100
1 parent c2fad34 commit e488bfc

File tree

9 files changed

+235
-50
lines changed

9 files changed

+235
-50
lines changed

.idea/dictionaries/develar.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/electron-builder-lib/src/options/SnapOptions.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,21 @@ export interface SnapOptions extends CommonLinuxOptions, TargetSpecificOptions {
5454
* Defaults to `["desktop", "desktop-legacy", "home", "x11", "unity7", "browser-support", "network", "gsettings", "pulseaudio", "opengl"]`.
5555
*
5656
* 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.
57+
*
58+
* Additional attributes can be specified using object instead of just name of plug:
59+
* ```
60+
*[
61+
* {
62+
* "browser-sandbox": {
63+
* "interface": "browser-support",
64+
* "allow-sandbox": true
65+
* },
66+
* },
67+
* "another-simple-plug-name"
68+
*]
69+
* ```
5770
*/
58-
readonly plugs?: Array<string> | null
71+
readonly plugs?: Array<string | object> | object | null
5972

6073
/**
6174
* Specifies any [parts](https://snapcraft.io/docs/reference/parts) that should be built before this part.

packages/electron-builder-lib/src/targets/snap.ts

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ export default class SnapTarget extends Target {
5050
const appInfo = this.packager.appInfo
5151
const options = this.options
5252
const linuxArchName = toAppImageOrSnapArch(arch)
53+
54+
const plugs: { [key: string]: object | null } | null = normalizePlugConfiguration(options.plugs == null ? null : asArray(options.plugs))
55+
const plugNames = this.replaceDefault(plugs == null ? null : Object.getOwnPropertyNames(plugs), defaultPlugs)
56+
5357
const snap: any = {
5458
name: snapName,
5559
version: appInfo.version,
@@ -74,7 +78,7 @@ export default class SnapTarget extends Target {
7478
].join(":"),
7579
...options.environment,
7680
},
77-
plugs: this.replaceDefault(options.plugs, defaultPlugs),
81+
plugs: plugNames,
7882
}
7983
},
8084
parts: {
@@ -87,6 +91,20 @@ export default class SnapTarget extends Target {
8791
},
8892
}
8993

94+
if (plugs != null) {
95+
for (const plugName of plugNames) {
96+
const plugOptions = plugs[plugName]
97+
if (plugOptions == null) {
98+
continue
99+
}
100+
101+
if (snap.plugs == null) {
102+
snap.plugs = {}
103+
}
104+
snap.plugs[plugName] = plugOptions
105+
}
106+
}
107+
90108
if (options.assumes != null) {
91109
snap.assumes = asArray(options.assumes)
92110
}
@@ -234,4 +252,21 @@ export default class SnapTarget extends Target {
234252
stdio: ["ignore", "inherit", "inherit"],
235253
})
236254
}
255+
}
256+
257+
function normalizePlugConfiguration(raw: Array<string | object> | null) {
258+
if (raw == null) {
259+
return null
260+
}
261+
262+
const result: any = {}
263+
for (const item of raw) {
264+
if (typeof item === "string") {
265+
result[item] = null
266+
}
267+
else {
268+
Object.assign(result, item)
269+
}
270+
}
271+
return result
237272
}

packages/electron-builder/src/builder.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ export interface CliOptions extends PackagerOptions, PublishOptions {
2121

2222
/** @internal */
2323
export function normalizeOptions(args: CliOptions): BuildOptions {
24+
if ((args as any).extraMetadata != null) {
25+
throw new InvalidConfigurationError("Please specify extraMetadata under config field")
26+
}
27+
2428
if (args.targets != null) {
2529
return args
2630
}

test/out/__snapshots__/BuildTest.js.snap

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -257,26 +257,6 @@ Object {
257257
}
258258
`;
259259
260-
exports[`extraMetadata and config as path 1`] = `
261-
Object {
262-
"linux": Array [],
263-
}
264-
`;
265-
266-
exports[`extraMetadata and config as path 2`] = `
267-
Object {
268-
"author": "Foo Bar <foo@example.com>",
269-
"description": "Test Application (test quite \\" #378)",
270-
"field": "bar.js",
271-
"homepage": "http://foo.example.com",
272-
"license": "MIT",
273-
"name": "TestApp",
274-
"private": true,
275-
"productName": "Test App ßW",
276-
"version": "1.1.0",
277-
}
278-
`;
279-
280260
exports[`posix smart unpack 1`] = `
281261
Object {
282262
"linux": Array [],

test/out/linux/__snapshots__/snapTest.js.snap

Lines changed: 136 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@ exports[`custom env 1`] = `
44
Object {
55
"apps": Object {
66
"sep": Object {
7-
"command": "command-sep.wrapper",
7+
"adapter": "none",
8+
"command": "bin/desktop-launch $SNAP/sep",
89
"environment": Object {
910
"FOO": "bar",
11+
"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",
12+
"PATH": "$SNAP/usr/sbin:$SNAP/usr/bin:$SNAP/sbin:$SNAP/bin:$PATH",
1013
"TMPDIR": "$XDG_RUNTIME_DIR",
1114
},
1215
"plugs": Array [
@@ -42,8 +45,11 @@ exports[`default stagePackages 1`] = `
4245
Object {
4346
"apps": Object {
4447
"sep": Object {
45-
"command": "desktop-launch $SNAP/sep",
48+
"adapter": "none",
49+
"command": "bin/desktop-launch $SNAP/sep",
4650
"environment": Object {
51+
"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",
52+
"PATH": "$SNAP/usr/sbin:$SNAP/usr/bin:$SNAP/sbin:$SNAP/bin:$PATH",
4753
"TMPDIR": "$XDG_RUNTIME_DIR",
4854
},
4955
"plugs": Array [
@@ -99,8 +105,11 @@ exports[`default stagePackages 3`] = `
99105
Object {
100106
"apps": Object {
101107
"sep": Object {
102-
"command": "desktop-launch $SNAP/sep",
108+
"adapter": "none",
109+
"command": "bin/desktop-launch $SNAP/sep",
103110
"environment": Object {
111+
"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",
112+
"PATH": "$SNAP/usr/sbin:$SNAP/usr/bin:$SNAP/sbin:$SNAP/bin:$PATH",
104113
"TMPDIR": "$XDG_RUNTIME_DIR",
105114
},
106115
"plugs": Array [
@@ -158,8 +167,11 @@ exports[`default stagePackages 5`] = `
158167
Object {
159168
"apps": Object {
160169
"sep": Object {
161-
"command": "desktop-launch $SNAP/sep",
170+
"adapter": "none",
171+
"command": "bin/desktop-launch $SNAP/sep",
162172
"environment": Object {
173+
"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",
174+
"PATH": "$SNAP/usr/sbin:$SNAP/usr/bin:$SNAP/sbin:$SNAP/bin:$PATH",
163175
"TMPDIR": "$XDG_RUNTIME_DIR",
164176
},
165177
"plugs": Array [
@@ -217,8 +229,11 @@ exports[`default stagePackages 7`] = `
217229
Object {
218230
"apps": Object {
219231
"sep": Object {
220-
"command": "desktop-launch $SNAP/sep",
232+
"adapter": "none",
233+
"command": "bin/desktop-launch $SNAP/sep",
221234
"environment": Object {
235+
"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",
236+
"PATH": "$SNAP/usr/sbin:$SNAP/usr/bin:$SNAP/sbin:$SNAP/bin:$PATH",
222237
"TMPDIR": "$XDG_RUNTIME_DIR",
223238
},
224239
"plugs": Array [
@@ -274,6 +289,122 @@ Object {
274289
}
275290
`;
276291

292+
exports[`plugs option 1`] = `
293+
Object {
294+
"apps": Object {
295+
"testapp": Object {
296+
"adapter": "none",
297+
"command": "bin/desktop-launch $SNAP/testapp",
298+
"environment": Object {
299+
"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",
300+
"PATH": "$SNAP/usr/sbin:$SNAP/usr/bin:$SNAP/sbin:$SNAP/bin:$PATH",
301+
"TMPDIR": "$XDG_RUNTIME_DIR",
302+
},
303+
"plugs": Array [
304+
"browser-sandbox",
305+
"another-simple-plug-name",
306+
],
307+
},
308+
},
309+
"confinement": "strict",
310+
"description": "Test Application (test quite “ #378)",
311+
"grade": "stable",
312+
"icon": "snap/gui/icon.png",
313+
"name": "testapp",
314+
"parts": Object {
315+
"app": Object {
316+
"after": Array [
317+
"desktop-gtk2",
318+
],
319+
"plugin": "dump",
320+
"stage-packages": Array [
321+
"libasound2",
322+
"libgconf2-4",
323+
"libnotify4",
324+
"libnspr4",
325+
"libnss3",
326+
"libpcre3",
327+
"libpulse0",
328+
"libxss1",
329+
"libxtst6",
330+
],
331+
},
332+
},
333+
"plugs": Object {
334+
"browser-sandbox": Object {
335+
"allow-sandbox": true,
336+
"interface": "browser-support",
337+
},
338+
},
339+
"summary": "Test App ßW",
340+
"version": "1.1.0",
341+
}
342+
`;
343+
344+
exports[`plugs option 2`] = `
345+
Object {
346+
"linux": Array [],
347+
}
348+
`;
349+
350+
exports[`plugs option 3`] = `
351+
Object {
352+
"apps": Object {
353+
"testapp": Object {
354+
"adapter": "none",
355+
"command": "bin/desktop-launch $SNAP/testapp",
356+
"environment": Object {
357+
"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",
358+
"PATH": "$SNAP/usr/sbin:$SNAP/usr/bin:$SNAP/sbin:$SNAP/bin:$PATH",
359+
"TMPDIR": "$XDG_RUNTIME_DIR",
360+
},
361+
"plugs": Array [
362+
"browser-sandbox",
363+
"another-simple-plug-name",
364+
],
365+
},
366+
},
367+
"confinement": "strict",
368+
"description": "Test Application (test quite “ #378)",
369+
"grade": "stable",
370+
"icon": "snap/gui/icon.png",
371+
"name": "testapp",
372+
"parts": Object {
373+
"app": Object {
374+
"after": Array [
375+
"desktop-gtk2",
376+
],
377+
"plugin": "dump",
378+
"stage-packages": Array [
379+
"libasound2",
380+
"libgconf2-4",
381+
"libnotify4",
382+
"libnspr4",
383+
"libnss3",
384+
"libpcre3",
385+
"libpulse0",
386+
"libxss1",
387+
"libxtst6",
388+
],
389+
},
390+
},
391+
"plugs": Object {
392+
"browser-sandbox": Object {
393+
"allow-sandbox": true,
394+
"interface": "browser-support",
395+
},
396+
},
397+
"summary": "Test App ßW",
398+
"version": "1.1.0",
399+
}
400+
`;
401+
402+
exports[`plugs option 4`] = `
403+
Object {
404+
"linux": Array [],
405+
}
406+
`;
407+
277408
exports[`snap 1`] = `
278409
Object {
279410
"linux": Array [

test/src/BuildTest.ts

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { walk } from "builder-util/out/fs"
33
import { checkWineVersion } from "builder-util/out/wine"
44
import { Arch, createTargets, DIR_TARGET, Platform } from "electron-builder"
55
import { readAsar } from "electron-builder-lib/out/asar/asar"
6-
import { move, outputJson, readFileSync, readJson } from "fs-extra-p"
6+
import { move, outputJson, readFileSync } from "fs-extra-p"
77
import * as path from "path"
88
import { app, appTwo, appTwoThrows, assertPack, linuxDirTarget, modifyPackageJson, packageJson } from "./helpers/packTester"
99
import { ELECTRON_VERSION } from "./helpers/testConfig"
@@ -70,25 +70,6 @@ test("relative index", appTwo({
7070
}, true)
7171
}))
7272

73-
test("extraMetadata and config as path", app(Object.assign(require("electron-builder/out/builder").normalizeOptions({
74-
extraMetadata: {
75-
field: "bar.js"
76-
},
77-
config: "foo.json",
78-
}), {
79-
targets: linuxDirTarget,
80-
}), {
81-
projectDirCreated: projectDir => {
82-
return outputJson(path.join(projectDir, "foo.json"), {
83-
asar: false
84-
})
85-
},
86-
packed: async context => {
87-
const resourceDir = context.getResources(Platform.LINUX)
88-
expect(await readJson(path.join(resourceDir, "app", "package.json"))).toMatchSnapshot()
89-
}
90-
}))
91-
9273
it.ifDevOrLinuxCi("electron version from electron-prebuilt dependency", app({
9374
targets: linuxDirTarget,
9475
}, {

test/src/helpers/runTests.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,7 @@ async function runTests() {
5959
}
6060
else if (circleNodeIndex === 2) {
6161
testPatterns.push("snapTest")
62-
testPatterns.push("mainEntryTest")
63-
testPatterns.push("PublishManagerTest", "ArtifactPublisherTest", "httpRequestTest", "RepoSlugTest")
62+
testPatterns.push("PublishManagerTest")
6463
testPatterns.push("macPackagerTest")
6564
testPatterns.push("linuxPackagerTest")
6665
}
@@ -74,6 +73,10 @@ async function runTests() {
7473
testPatterns.push("webInstallerTest")
7574
testPatterns.push("msiTest")
7675
testPatterns.push("ignoreTest")
76+
testPatterns.push("mainEntryTest")
77+
testPatterns.push("ArtifactPublisherTest")
78+
testPatterns.push("RepoSlugTest")
79+
testPatterns.push("httpRequestTest")
7780
}
7881
console.log(`Test files for node ${circleNodeIndex}: ${testPatterns.join(", ")}`)
7982
}

0 commit comments

Comments
 (0)