Skip to content

Commit 3aed0b5

Browse files
committed
feat(snap): custom plugs
Close #1226
1 parent 7cae06a commit 3aed0b5

File tree

7 files changed

+278
-24
lines changed

7 files changed

+278
-24
lines changed

docs/Options.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ See [NSIS target notes](https://github.com/electron-userland/electron-builder/wi
194194
### `pkg` macOS Product Archive Options
195195
| Name | Description
196196
| --- | ---
197-
| scripts | <a name="PkgOptions-scripts"></a><p>The scripts directory, relative to <code>build</code> (build resources directory). Defaults to <code>build/pkg-scripts</code>. [Scripting in installer packages](http://macinstallers.blogspot.de/2012/07/scripting-in-installer-packages.html) The scripts can be in any language so long as the files are marked executable and have the appropriate shebang indicating the path to the interpreter.</p> <p>Scripts are required to be executable (<code>chmod +x file</code>). It is not checked and corrected by electron-builder.</p>
197+
| scripts | <a name="PkgOptions-scripts"></a><p>The scripts directory, relative to <code>build</code> (build resources directory). Defaults to <code>build/pkg-scripts</code>. See [Scripting in installer packages](http://macinstallers.blogspot.de/2012/07/scripting-in-installer-packages.html). The scripts can be in any language so long as the files are marked executable and have the appropriate shebang indicating the path to the interpreter.</p> <p>Scripts are required to be executable (<code>chmod +x file</code>).</p>
198198
| installLocation | <a name="PkgOptions-installLocation"></a>The install location. Defaults to `/Applications`.
199199

200200
<a name="Protocol"></a>
@@ -246,7 +246,8 @@ To use Squirrel.Windows please install `electron-builder-squirrel-windows` depen
246246
| summary | <a name="SnapOptions-summary"></a>The 78 character long summary. Defaults to [productName](#AppMetadata-productName).
247247
| grade | <a name="SnapOptions-grade"></a><p>The quality grade of the snap. It can be either <code>devel</code> (i.e. a development version of the snap, so not to be published to the “stable” or “candidate” channels) or “stable” (i.e. a stable release or release candidate, which can be released to all channels). Defaults to <code>stable</code>.</p>
248248
| assumes | <a name="SnapOptions-assumes"></a>The list of features that must be supported by the core in order for this snap to install.
249-
| stagePackages | <a name="SnapOptions-stagePackages"></a><p>The list of Ubuntu packages to use that are needed to support the <code>app</code> part creation. Like <code>depends</code> for <code>deb</code>. Defaults to <code>[&quot;libnotify4&quot;, &quot;libappindicator1&quot;, &quot;libxtst6&quot;, &quot;libnss3&quot;, &quot;libxss1&quot;, &quot;fontconfig-config&quot;, &quot;gconf2&quot;, &quot;libasound2&quot;]</code>.</p>
249+
| stagePackages | <a name="SnapOptions-stagePackages"></a><p>The list of Ubuntu packages to use that are needed to support the <code>app</code> part creation. Like <code>depends</code> for <code>deb</code>. Defaults to <code>[&quot;libnotify4&quot;, &quot;libappindicator1&quot;, &quot;libxtst6&quot;, &quot;libnss3&quot;, &quot;libxss1&quot;, &quot;fontconfig-config&quot;, &quot;gconf2&quot;, &quot;libasound2&quot;, &quot;pulseaudio&quot;]</code>.</p> <p>If list contains <code>default</code>, it will be replaced to default list, so, <code>[&quot;default&quot;, &quot;foo&quot;]</code> can be used to add custom package <code>foo</code> in addition to defaults.</p>
250+
| plugs | <a name="SnapOptions-plugs"></a><p>The list of [plugs](https://snapcraft.io/docs/reference/interfaces). Defaults to <code>[&quot;home&quot;, &quot;x11&quot;, &quot;unity7&quot;, &quot;browser-support&quot;, &quot;network&quot;, &quot;gsettings&quot;, &quot;pulseaudio&quot;, &quot;opengl&quot;]</code>.</p> <p>If list contains <code>default</code>, it will be replaced to default list, so, <code>[&quot;default&quot;, &quot;foo&quot;]</code> can be used to add custom plug <code>foo</code> in addition to defaults.</p>
250251
| ubuntuAppPlatformContent | <a name="SnapOptions-ubuntuAppPlatformContent"></a><p>Specify <code>ubuntu-app-platform1</code> to use [ubuntu-app-platform](https://insights.ubuntu.com/2016/11/17/how-to-create-snap-packages-on-qt-applications/). Snap size will be greatly reduced, but it is not recommended for now because “the snaps must be connected before running uitk-gallery for the first time”.</p>
251252

252253
<a name="AppImageOptions"></a>

packages/electron-builder/src/options/linuxOptions.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,35 +93,45 @@ export interface SnapOptions extends LinuxBuildOptions {
9393
/*
9494
The type of confinement supported by the snap. Can be either `devmode` (i.e. this snap doesn’t support running under confinement) or `strict` (i.e. full confinement supported via interfaces). Defaults to `strict`.
9595
*/
96-
confinement?: "devmode" | "strict" | null
96+
readonly confinement?: "devmode" | "strict" | null
9797

9898
/*
9999
The 78 character long summary. Defaults to [productName](#AppMetadata-productName).
100100
*/
101-
summary?: string | null
101+
readonly summary?: string | null
102102

103103
/*
104104
The quality grade of the snap. It can be either `devel` (i.e. a development version of the snap, so not to be published to the “stable” or “candidate” channels) or “stable” (i.e. a stable release or release candidate, which can be released to all channels).
105105
Defaults to `stable`.
106106
*/
107-
grade?: "devel" | "stable" | null
107+
readonly grade?: "devel" | "stable" | null
108108

109109
/*
110110
The list of features that must be supported by the core in order for this snap to install.
111111
*/
112-
assumes?: Array<string> | null
112+
readonly assumes?: Array<string> | null
113113

114114
/*
115115
The list of Ubuntu packages to use that are needed to support the `app` part creation. Like `depends` for `deb`.
116-
Defaults to `["libnotify4", "libappindicator1", "libxtst6", "libnss3", "libxss1", "fontconfig-config", "gconf2", "libasound2"]`.
116+
Defaults to `["libnotify4", "libappindicator1", "libxtst6", "libnss3", "libxss1", "fontconfig-config", "gconf2", "libasound2", "pulseaudio"]`.
117+
118+
If list contains `default`, it will be replaced to default list, so, `["default", "foo"]` can be used to add custom package `foo` in addition to defaults.
119+
*/
120+
readonly stagePackages?: Array<string> | null
121+
122+
/*
123+
The list of [plugs](https://snapcraft.io/docs/reference/interfaces).
124+
Defaults to `["home", "x11", "unity7", "browser-support", "network", "gsettings", "pulseaudio", "opengl"]`.
125+
126+
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.
117127
*/
118-
stagePackages?: Array<string> | null
128+
readonly plugs?: Array<string> | null
119129

120130
/*
121131
Specify `ubuntu-app-platform1` to use [ubuntu-app-platform](https://insights.ubuntu.com/2016/11/17/how-to-create-snap-packages-on-qt-applications/).
122132
Snap size will be greatly reduced, but it is not recommended for now because "the snaps must be connected before running uitk-gallery for the first time".
123133
*/
124-
ubuntuAppPlatformContent?: string | null
134+
readonly ubuntuAppPlatformContent?: string | null
125135
}
126136

127137
/*
@@ -131,5 +141,5 @@ export interface AppImageOptions extends LinuxBuildOptions {
131141
/*
132142
Whether to include required system libraries (`gconf2`, `libappindicator1`). Defaults to `false`.
133143
*/
134-
includeRequiredLib?: boolean | null
144+
readonly includeRequiredLib?: boolean | null
135145
}

packages/electron-builder/src/options/macOptions.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,11 @@ export interface MacOptions extends PlatformSpecificBuildOptions {
6666
*/
6767
export interface PkgOptions {
6868
/*
69-
The scripts directory, relative to `build` (build resources directory). Defaults to `build/pkg-scripts`. [Scripting in installer packages](http://macinstallers.blogspot.de/2012/07/scripting-in-installer-packages.html)
69+
The scripts directory, relative to `build` (build resources directory). Defaults to `build/pkg-scripts`.
70+
See [Scripting in installer packages](http://macinstallers.blogspot.de/2012/07/scripting-in-installer-packages.html).
7071
The scripts can be in any language so long as the files are marked executable and have the appropriate shebang indicating the path to the interpreter.
7172
72-
Scripts are required to be executable (`chmod +x file`). It is not checked and corrected by electron-builder.
73+
Scripts are required to be executable (`chmod +x file`).
7374
*/
7475
readonly scripts?: string | null
7576

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

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,7 @@ export default class SnapTarget extends Target {
6161
snap.apps = {
6262
[snap.name]: {
6363
command: `desktop-launch $SNAP/${packager.executableName}`,
64-
plugs: [
65-
"home", "x11", "unity7", "browser-support", "network", "gsettings", "pulseaudio", "opengl",
66-
]
64+
plugs: [replaceDefault(options.plugs, ["home", "x11", "unity7", "browser-support", "network", "gsettings", "pulseaudio", "opengl"])]
6765
}
6866
}
6967

@@ -81,10 +79,12 @@ export default class SnapTarget extends Target {
8179

8280
// libxss1, libasound2, gconf2 - was "error while loading shared libraries: libXss.so.1" on Xubuntu 16.04
8381
const isUseDocker = process.platform !== "linux"
82+
83+
const defaultStagePackages = (isUseUbuntuPlatform ? ["libnss3"] : ["libnotify4", "libappindicator1", "libxtst6", "libnss3", "libxss1", "fontconfig-config", "gconf2", "libasound2", "pulseaudio"])
8484
snap.parts = {
8585
app: {
8686
plugin: "dump",
87-
"stage-packages": options.stagePackages || (isUseUbuntuPlatform ? ["libnss3"] : ["libnotify4", "libappindicator1", "libxtst6", "libnss3", "libxss1", "fontconfig-config", "gconf2", "libasound2"]),
87+
"stage-packages": replaceDefault(options.stagePackages, defaultStagePackages),
8888
source: isUseDocker ? `/out/${path.basename(appOutDir)}` : appOutDir,
8989
after: isUseUbuntuPlatform ? ["extra", "desktop-ubuntu-app-platform"] : ["desktop-glib-only"]
9090
}
@@ -97,6 +97,10 @@ export default class SnapTarget extends Target {
9797
}
9898
}
9999

100+
if (packager.packagerOptions.effectiveOptionComputed != null && await packager.packagerOptions.effectiveOptionComputed(snap)) {
101+
return
102+
}
103+
100104
const snapcraft = path.join(snapDir, "snapcraft.yaml")
101105
await writeFile(snapcraft, safeDump(snap, {lineWidth: 160}))
102106

@@ -123,4 +127,21 @@ export default class SnapTarget extends Target {
123127
}
124128
packager.dispatchArtifactCreated(resultFile, this)
125129
}
130+
}
131+
132+
function replaceDefault(inList: Array<string> | n, defaultList: Array<string>): Array<string> {
133+
if (inList == null) {
134+
return defaultList
135+
}
136+
137+
const index = inList.indexOf("default")
138+
if (index >= 0) {
139+
let list = inList.slice(0, index)
140+
list.push(...defaultList)
141+
if (index != (inList.length - 1)) {
142+
list.push(...inList.slice(index + 1))
143+
}
144+
inList = list
145+
}
146+
return inList
126147
}

packages/electron-publisher-s3/src/s3Publisher.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Publisher, PublishContext } from "electron-builder-publisher"
22
import { S3Options } from "electron-builder-http/out/publishOptions"
33
import { S3 } from "aws-sdk"
4-
import { createReadStream, stat } from "fs-extra-p"
4+
import { stat } from "fs-extra-p"
55
import mime from "mime"
66
import BluebirdPromise from "bluebird-lst-c"
77
import { debug, isEmptyOrSpaces } from "electron-builder-util"
@@ -30,13 +30,9 @@ export default class S3Publisher extends Publisher {
3030
const fileName = basename(file)
3131
const fileStat = await stat(file)
3232
return this.context.cancellationToken.trackPromise(new BluebirdPromise((resolve, reject, onCancel) => {
33-
//noinspection JSUnusedLocalSymbols
34-
const fileStream = createReadStream(file)
35-
fileStream.on("error", reject)
36-
3733
const upload = this.s3.upload({
3834
Bucket: this.info.bucket!,
39-
Key: fileName,
35+
Key: (this.info.path == null ? "" : `${this.info.path}/`) + fileName,
4036
ACL: this.info.acl || "public-read",
4137
Body: this.createReadStreamAndProgressBar(file, fileStat, this.createProgressBar(fileName, fileStat), reject),
4238
ContentLength: fileStat.size,
Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
exports[`test default stagePackages 1`] = `
2+
Object {
3+
"apps": Object {
4+
"sep": Object {
5+
"command": "desktop-launch $SNAP/sep",
6+
"plugs": Array [
7+
Array [
8+
"home",
9+
"x11",
10+
"unity7",
11+
"browser-support",
12+
"network",
13+
"gsettings",
14+
"pulseaudio",
15+
"opengl",
16+
],
17+
],
18+
},
19+
},
20+
"confinement": "strict",
21+
"description": "Test Application (test quite “ #378)",
22+
"grade": "stable",
23+
"icon": "setup/gui/icon.png",
24+
"name": "sep",
25+
"parts": Object {
26+
"app": Object {
27+
"after": Array [
28+
"desktop-glib-only",
29+
],
30+
"plugin": "dump",
31+
"source": "/out/linux-unpacked",
32+
"stage-packages": Array [
33+
"libnotify4",
34+
"libappindicator1",
35+
"libxtst6",
36+
"libnss3",
37+
"libxss1",
38+
"fontconfig-config",
39+
"gconf2",
40+
"libasound2",
41+
"pulseaudio",
42+
],
43+
},
44+
},
45+
"summary": "Sep",
46+
"version": "1.1.0",
47+
}
48+
`;
49+
50+
exports[`test default stagePackages 2`] = `
51+
Object {
52+
"apps": Object {
53+
"sep": Object {
54+
"command": "desktop-launch $SNAP/sep",
55+
"plugs": Array [
56+
Array [
57+
"home",
58+
"x11",
59+
"unity7",
60+
"browser-support",
61+
"network",
62+
"gsettings",
63+
"pulseaudio",
64+
"opengl",
65+
"custom",
66+
],
67+
],
68+
},
69+
},
70+
"confinement": "strict",
71+
"description": "Test Application (test quite “ #378)",
72+
"grade": "stable",
73+
"icon": "setup/gui/icon.png",
74+
"name": "sep",
75+
"parts": Object {
76+
"app": Object {
77+
"after": Array [
78+
"desktop-glib-only",
79+
],
80+
"plugin": "dump",
81+
"source": "/out/linux-unpacked",
82+
"stage-packages": Array [
83+
"libnotify4",
84+
"libappindicator1",
85+
"libxtst6",
86+
"libnss3",
87+
"libxss1",
88+
"fontconfig-config",
89+
"gconf2",
90+
"libasound2",
91+
"pulseaudio",
92+
"custom",
93+
],
94+
},
95+
},
96+
"summary": "Sep",
97+
"version": "1.1.0",
98+
}
99+
`;
100+
101+
exports[`test default stagePackages 3`] = `
102+
Object {
103+
"apps": Object {
104+
"sep": Object {
105+
"command": "desktop-launch $SNAP/sep",
106+
"plugs": Array [
107+
Array [
108+
"custom",
109+
"home",
110+
"x11",
111+
"unity7",
112+
"browser-support",
113+
"network",
114+
"gsettings",
115+
"pulseaudio",
116+
"opengl",
117+
],
118+
],
119+
},
120+
},
121+
"confinement": "strict",
122+
"description": "Test Application (test quite “ #378)",
123+
"grade": "stable",
124+
"icon": "setup/gui/icon.png",
125+
"name": "sep",
126+
"parts": Object {
127+
"app": Object {
128+
"after": Array [
129+
"desktop-glib-only",
130+
],
131+
"plugin": "dump",
132+
"source": "/out/linux-unpacked",
133+
"stage-packages": Array [
134+
"custom",
135+
"libnotify4",
136+
"libappindicator1",
137+
"libxtst6",
138+
"libnss3",
139+
"libxss1",
140+
"fontconfig-config",
141+
"gconf2",
142+
"libasound2",
143+
"pulseaudio",
144+
],
145+
},
146+
},
147+
"summary": "Sep",
148+
"version": "1.1.0",
149+
}
150+
`;
151+
152+
exports[`test default stagePackages 4`] = `
153+
Object {
154+
"apps": Object {
155+
"sep": Object {
156+
"command": "desktop-launch $SNAP/sep",
157+
"plugs": Array [
158+
Array [
159+
"foo1",
160+
"home",
161+
"x11",
162+
"unity7",
163+
"browser-support",
164+
"network",
165+
"gsettings",
166+
"pulseaudio",
167+
"opengl",
168+
"foo2",
169+
],
170+
],
171+
},
172+
},
173+
"confinement": "strict",
174+
"description": "Test Application (test quite “ #378)",
175+
"grade": "stable",
176+
"icon": "setup/gui/icon.png",
177+
"name": "sep",
178+
"parts": Object {
179+
"app": Object {
180+
"after": Array [
181+
"desktop-glib-only",
182+
],
183+
"plugin": "dump",
184+
"source": "/out/linux-unpacked",
185+
"stage-packages": Array [
186+
"foo1",
187+
"libnotify4",
188+
"libappindicator1",
189+
"libxtst6",
190+
"libnss3",
191+
"libxss1",
192+
"fontconfig-config",
193+
"gconf2",
194+
"libasound2",
195+
"pulseaudio",
196+
"foo2",
197+
],
198+
},
199+
},
200+
"summary": "Sep",
201+
"version": "1.1.0",
202+
}
203+
`;

0 commit comments

Comments
 (0)