Skip to content

Commit 10ccea8

Browse files
committed
feat(snap): add option to not use template app
1 parent 59336dd commit 10ccea8

File tree

7 files changed

+41
-50
lines changed

7 files changed

+41
-50
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,9 @@ export interface SnapOptions extends CommonLinuxOptions, TargetSpecificOptions {
6464
* If list contains `default`, it will be replaced to default list, so, `["default", "foo"]` can be used to add custom parts `foo` in addition to defaults.
6565
*/
6666
readonly after?: Array<string> | null
67+
68+
/**
69+
* Whether to use template snap. Defaults to `true` if `stagePackages` not specified.
70+
*/
71+
readonly useTemplateApp?: boolean
6772
}

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

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const defaultPlugs = ["desktop", "desktop-legacy", "home", "x11", "unity7", "bro
3232
export default class SnapTarget extends Target {
3333
readonly options: SnapOptions = {...this.packager.platformSpecificBuildOptions, ...(this.packager.config as any)[this.name]}
3434

35-
private isUsePrepackedSnap = true
35+
private isUseTemplateApp = false
3636

3737
constructor(name: string, private readonly packager: LinuxPackager, private readonly helper: LinuxTargetHelper, readonly outDir: string) {
3838
super(name)
@@ -41,20 +41,14 @@ export default class SnapTarget extends Target {
4141
private replaceDefault(inList: Array<string> | null | undefined, defaultList: Array<string>) {
4242
const result = _replaceDefault(inList, defaultList)
4343
if (result !== defaultList) {
44-
this.isUsePrepackedSnap = false
44+
this.isUseTemplateApp = false
4545
}
4646
return result
4747
}
4848

49-
async build(appOutDir: string, arch: Arch): Promise<any> {
50-
const packager = this.packager
51-
const appInfo = packager.appInfo
49+
private createDescriptor(snapName: string, appOutDir: string, isUseDocker: boolean): any {
50+
const appInfo = this.packager.appInfo
5251
const options = this.options
53-
const snapName = packager.executableName.toLowerCase()
54-
const buildPackages = asArray(options.buildPackages)
55-
const isUseDocker = process.platform !== "linux" || isEnvTrue(process.env.SNAP_USE_DOCKER)
56-
this.isUsePrepackedSnap = arch === Arch.x64 && buildPackages.length === 0
57-
5852
const snap: any = {
5953
name: snapName,
6054
version: appInfo.version,
@@ -64,7 +58,7 @@ export default class SnapTarget extends Target {
6458
grade: options.grade || "stable",
6559
apps: {
6660
[snapName]: {
67-
command: `desktop-launch $SNAP/${packager.executableName}`,
61+
command: `desktop-launch $SNAP/${this.packager.executableName}`,
6862
environment: {
6963
TMPDIR: "$XDG_RUNTIME_DIR",
7064
...options.environment,
@@ -82,28 +76,39 @@ export default class SnapTarget extends Target {
8276
},
8377
}
8478

85-
const wrapperFileName = `command-${packager.executableName}.wrapper`
86-
if (this.isUsePrepackedSnap) {
87-
delete snap.parts
88-
snap.apps[snapName].command = wrapperFileName
79+
if (options.assumes != null) {
80+
snap.assumes = asArray(options.assumes)
8981
}
82+
return snap
83+
}
9084

91-
const snapFileName = `${snap.name}_${snap.version}_${toLinuxArchString(arch)}.snap`
85+
async build(appOutDir: string, arch: Arch): Promise<any> {
86+
const packager = this.packager
87+
const options = this.options
88+
const snapName = packager.executableName.toLowerCase()
89+
const buildPackages = asArray(options.buildPackages)
90+
const isUseDocker = process.platform !== "linux" || isEnvTrue(process.env.SNAP_USE_DOCKER)
91+
this.isUseTemplateApp = this.options.useTemplateApp !== false && arch === Arch.x64 && buildPackages.length === 0
92+
93+
const snapFileName = `${snapName}_${packager.appInfo.version}_${toLinuxArchString(arch)}.snap`
9294
const artifactPath = path.join(this.outDir, snapFileName)
9395
this.logBuilding("snap", artifactPath, arch)
9496

95-
if (options.assumes != null) {
96-
snap.assumes = asArray(options.assumes)
97+
const snap: any = this.createDescriptor(snapName, appOutDir, isUseDocker)
98+
const wrapperFileName = `command-${packager.executableName}.wrapper`
99+
if (this.isUseTemplateApp) {
100+
delete snap.parts
101+
snap.apps[snapName].command = wrapperFileName
97102
}
98103

99104
const stageDir = await createStageDir(this, packager, arch)
100105
// snapcraft.yaml inside a snap directory
101106
const snapDir = path.join(stageDir.dir, "snap")
102-
const snapMetaDir = this.isUsePrepackedSnap ? path.join(stageDir.dir, "meta") : snapDir
107+
const snapMetaDir = this.isUseTemplateApp ? path.join(stageDir.dir, "meta") : snapDir
103108

104109
await this.helper.icons
105110
if (this.helper.maxIconPath != null) {
106-
if (!this.isUsePrepackedSnap) {
111+
if (!this.isUseTemplateApp) {
107112
snap.icon = "snap/gui/icon.png"
108113
}
109114
await copyFile(this.helper.maxIconPath, path.join(snapMetaDir, "gui", "icon.png"))
@@ -126,9 +131,9 @@ export default class SnapTarget extends Target {
126131
return
127132
}
128133

129-
const snapcraftFile = path.join(snapMetaDir, this.isUsePrepackedSnap ? "snap.yaml" : "snapcraft.yaml")
134+
const snapcraftFile = path.join(snapMetaDir, this.isUseTemplateApp ? "snap.yaml" : "snapcraft.yaml")
130135
await outputFile(snapcraftFile, serializeToYaml(snap))
131-
if (this.isUsePrepackedSnap) {
136+
if (this.isUseTemplateApp) {
132137
// noinspection SpellCheckingInspection
133138
await writeFile(path.join(stageDir.dir, wrapperFileName), `#!/bin/sh
134139
export PATH="$SNAP/usr/sbin:$SNAP/usr/bin:$SNAP/sbin:$SNAP/bin:$PATH"
@@ -146,7 +151,7 @@ exec "desktop-launch" "$SNAP/${packager.executableName}" "$@"
146151
}
147152

148153
if (isUseDocker) {
149-
if (this.isUsePrepackedSnap) {
154+
if (this.isUseTemplateApp) {
150155
await this.buildUsingDockerAndPrepackedSnap(snapFileName, stageDir)
151156
}
152157
else {
@@ -178,7 +183,7 @@ exec "desktop-launch" "$SNAP/${packager.executableName}" "$@"
178183
}
179184

180185
let primeDir: string
181-
if (this.isUsePrepackedSnap) {
186+
if (this.isUseTemplateApp) {
182187
primeDir = stageDir
183188
}
184189
else {

test/out/__snapshots__/extraMetadataTest.js.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Object {
1717

1818
exports[`cli 2`] = `
1919
Object {
20+
"cExtraMetadataDevLoginUrl": true,
2021
"config": Object {
2122
"extraMetadata": Object {
2223
"dev": Object {
@@ -25,7 +26,6 @@ Object {
2526
},
2627
},
2728
"draft": undefined,
28-
"emDevLoginUrl": true,
2929
"prepackaged": undefined,
3030
"prerelease": undefined,
3131
"projectDir": undefined,

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

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ exports[`custom env 1`] = `
44
Object {
55
"apps": Object {
66
"sep": Object {
7-
"command": "desktop-launch $SNAP/sep",
7+
"command": "command-sep.wrapper",
88
"environment": Object {
99
"FOO": "bar",
1010
"TMPDIR": "$XDG_RUNTIME_DIR",
@@ -26,27 +26,7 @@ Object {
2626
"confinement": "strict",
2727
"description": "Test Application (test quite “ #378)",
2828
"grade": "stable",
29-
"icon": "snap/gui/icon.png",
3029
"name": "sep",
31-
"parts": Object {
32-
"app": Object {
33-
"after": Array [
34-
"desktop-gtk2",
35-
],
36-
"plugin": "dump",
37-
"stage-packages": Array [
38-
"libasound2",
39-
"libgconf2-4",
40-
"libnotify4",
41-
"libnspr4",
42-
"libnss3",
43-
"libpcre3",
44-
"libpulse0",
45-
"libxss1",
46-
"libxtst6",
47-
],
48-
},
49-
},
5030
"summary": "Sep",
5131
"version": "1.1.0",
5232
}

test/src/extraMetadataTest.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,6 @@ test("cli", async () => {
7171
return result
7272
}
7373

74-
expect(parseExtraMetadata("--em.foo=bar")).toMatchSnapshot()
75-
expect(parseExtraMetadata("--em.dev.login-url")).toMatchSnapshot()
74+
expect(parseExtraMetadata("--c.extraMetadata.foo=bar")).toMatchSnapshot()
75+
expect(parseExtraMetadata("--c.extraMetadata.dev.login-url")).toMatchSnapshot()
7676
})

test/src/helpers/runTests.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ async function runTests() {
6363
testPatterns.push("PublishManagerTest", "ArtifactPublisherTest", "httpRequestTest", "RepoSlugTest")
6464
testPatterns.push("macPackagerTest")
6565
testPatterns.push("linuxPackagerTest")
66-
testPatterns.push("ignoreTest")
6766
}
6867
else {
6968
testPatterns.push("portableTest")
@@ -74,6 +73,7 @@ async function runTests() {
7473
testPatterns.push("globTest")
7574
testPatterns.push("webInstallerTest")
7675
testPatterns.push("msiTest")
76+
testPatterns.push("ignoreTest")
7777
}
7878
console.log(`Test files for node ${circleNodeIndex}: ${testPatterns.join(", ")}`)
7979
}

test/src/linux/snapTest.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ test.ifAll.ifDevOrLinuxCi("default stagePackages", async () => {
4646
stagePackages: p,
4747
plugs: p,
4848
confinement: "classic",
49+
// otherwise "parts" will be removed
50+
useTemplateApp: false,
4951
}
5052
},
5153
effectiveOptionComputed: async ({snap}) => {
@@ -71,7 +73,6 @@ test.ifAll.ifDevOrLinuxCi("custom env", app({
7173
}
7274
},
7375
effectiveOptionComputed: async ({snap}) => {
74-
delete snap.parts.app.source
7576
expect(snap).toMatchSnapshot()
7677
return true
7778
},

0 commit comments

Comments
 (0)