-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathsnap.ts
320 lines (275 loc) · 10.5 KB
/
snap.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
import { Arch, deepAssign, executeAppBuilder, InvalidConfigurationError, log, replaceDefault as _replaceDefault, serializeToYaml, toLinuxArchString } from "builder-util"
import { asArray } from "builder-util-runtime"
import { outputFile, readFile } from "fs-extra"
import { load } from "js-yaml"
import * as path from "path"
import * as semver from "semver"
import { Target } from "../core"
import { LinuxPackager } from "../linuxPackager"
import { PlugDescriptor, SnapOptions } from "../options/SnapOptions"
import { getTemplatePath } from "../util/pathManager"
import { LinuxTargetHelper } from "./LinuxTargetHelper"
import { createStageDirPath } from "./targetUtil"
const defaultPlugs = ["desktop", "desktop-legacy", "home", "x11", "wayland", "unity7", "browser-support", "network", "gsettings", "audio-playback", "pulseaudio", "opengl"]
export default class SnapTarget extends Target {
readonly options: SnapOptions = { ...this.packager.platformSpecificBuildOptions, ...(this.packager.config as any)[this.name] }
public isUseTemplateApp = false
constructor(name: string, private readonly packager: LinuxPackager, private readonly helper: LinuxTargetHelper, readonly outDir: string) {
super(name)
}
private replaceDefault(inList: Array<string> | null | undefined, defaultList: Array<string>) {
const result = _replaceDefault(inList, defaultList)
if (result !== defaultList) {
this.isUseTemplateApp = false
}
return result
}
private async createDescriptor(arch: Arch): Promise<any> {
if (!this.isElectronVersionGreaterOrEqualThan("4.0.0")) {
if (!this.isElectronVersionGreaterOrEqualThan("2.0.0-beta.1")) {
throw new InvalidConfigurationError("Electron 2 and higher is required to build Snap")
}
log.warn("Electron 4 and higher is highly recommended for Snap")
}
const appInfo = this.packager.appInfo
const snapName = this.packager.executableName.toLowerCase()
const options = this.options
const plugs = normalizePlugConfiguration(this.options.plugs)
const plugNames = this.replaceDefault(plugs == null ? null : Object.getOwnPropertyNames(plugs), defaultPlugs)
const slots = normalizePlugConfiguration(this.options.slots)
const buildPackages = asArray(options.buildPackages)
const defaultStagePackages = getDefaultStagePackages()
const stagePackages = this.replaceDefault(options.stagePackages, defaultStagePackages)
this.isUseTemplateApp =
this.options.useTemplateApp !== false &&
(arch === Arch.x64 || arch === Arch.armv7l) &&
buildPackages.length === 0 &&
isArrayEqualRegardlessOfSort(stagePackages, defaultStagePackages)
const appDescriptor: any = {
command: "command.sh",
plugs: plugNames,
adapter: "none",
}
const snap: any = load(await readFile(path.join(getTemplatePath("snap"), "snapcraft.yaml"), "utf-8"))
if (this.isUseTemplateApp) {
delete appDescriptor.adapter
}
if (options.base != null) {
snap.base = options.base
// from core22 onwards adapter is legacy
if (Number(snap.base.split("core")[1]) >= 22) {
delete appDescriptor.adapter
}
}
if (options.grade != null) {
snap.grade = options.grade
}
if (options.confinement != null) {
snap.confinement = options.confinement
}
if (options.appPartStage != null) {
snap.parts.app.stage = options.appPartStage
}
if (options.layout != null) {
snap.layout = options.layout
}
if (slots != null) {
appDescriptor.slots = Object.getOwnPropertyNames(slots)
for (const slotName of appDescriptor.slots) {
const slotOptions = slots[slotName]
if (slotOptions == null) {
continue
}
if (!snap.slots) {
snap.slots = {}
}
snap.slots[slotName] = slotOptions
}
}
deepAssign(snap, {
name: snapName,
version: appInfo.version,
title: options.title || appInfo.productName,
summary: options.summary || appInfo.productName,
compression: options.compression,
description: this.helper.getDescription(options),
architectures: [toLinuxArchString(arch, "snap")],
apps: {
[snapName]: appDescriptor,
},
parts: {
app: {
"stage-packages": stagePackages,
},
},
})
if (options.autoStart) {
appDescriptor.autostart = `${snap.name}.desktop`
}
if (options.confinement === "classic") {
delete appDescriptor.plugs
delete snap.plugs
} else {
const archTriplet = archNameToTriplet(arch)
appDescriptor.environment = {
DISABLE_WAYLAND: options.allowNativeWayland ? "" : "1",
PATH: "$SNAP/usr/sbin:$SNAP/usr/bin:$SNAP/sbin:$SNAP/bin:$PATH",
SNAP_DESKTOP_RUNTIME: "$SNAP/gnome-platform",
LD_LIBRARY_PATH: [
"$SNAP_LIBRARY_PATH",
"$SNAP/lib:$SNAP/usr/lib:$SNAP/lib/" + archTriplet + ":$SNAP/usr/lib/" + archTriplet,
"$LD_LIBRARY_PATH:$SNAP/lib:$SNAP/usr/lib",
"$SNAP/lib/" + archTriplet + ":$SNAP/usr/lib/" + archTriplet,
].join(":"),
...options.environment,
}
if (plugs != null) {
for (const plugName of plugNames) {
const plugOptions = plugs[plugName]
if (plugOptions == null) {
continue
}
snap.plugs[plugName] = plugOptions
}
}
}
if (buildPackages.length > 0) {
snap.parts.app["build-packages"] = buildPackages
}
if (options.after != null) {
snap.parts.app.after = options.after
}
if (options.assumes != null) {
snap.assumes = asArray(options.assumes)
}
return snap
}
async build(appOutDir: string, arch: Arch): Promise<any> {
const packager = this.packager
const options = this.options
// tslint:disable-next-line:no-invalid-template-strings
const artifactName = packager.expandArtifactNamePattern(this.options, "snap", arch, "${name}_${version}_${arch}.${ext}", false)
const artifactPath = path.join(this.outDir, artifactName)
await packager.info.callArtifactBuildStarted({
targetPresentableName: "snap",
file: artifactPath,
arch,
})
const snap = await this.createDescriptor(arch)
const stageDir = await createStageDirPath(this, packager, arch)
const snapArch = toLinuxArchString(arch, "snap")
const args = ["snap", "--app", appOutDir, "--stage", stageDir, "--arch", snapArch, "--output", artifactPath, "--executable", this.packager.executableName]
await this.helper.icons
if (this.helper.maxIconPath != null) {
if (!this.isUseTemplateApp) {
snap.icon = "snap/gui/icon.png"
}
args.push("--icon", this.helper.maxIconPath)
}
// snapcraft.yaml inside a snap directory
const snapMetaDir = path.join(stageDir, this.isUseTemplateApp ? "meta" : "snap")
const desktopFile = path.join(snapMetaDir, "gui", `${snap.name}.desktop`)
await this.helper.writeDesktopEntry(this.options, packager.executableName + " %U", desktopFile, {
// tslint:disable:no-invalid-template-strings
Icon: "${SNAP}/meta/gui/icon.png",
})
const extraAppArgs: Array<string> = options.executableArgs ?? []
if (this.isElectronVersionGreaterOrEqualThan("5.0.0") && !isBrowserSandboxAllowed(snap)) {
const noSandboxArg = "--no-sandbox"
if (!extraAppArgs.includes(noSandboxArg)) {
extraAppArgs.push(noSandboxArg)
}
if (this.isUseTemplateApp) {
args.push("--exclude", "chrome-sandbox")
}
}
if (extraAppArgs.length > 0) {
args.push("--extraAppArgs=" + extraAppArgs.join(" "))
}
if (snap.compression != null) {
args.push("--compression", snap.compression)
}
if (this.isUseTemplateApp) {
// remove fields that are valid in snapcraft.yaml, but not snap.yaml
const fieldsToStrip = ["compression", "contact", "donation", "issues", "parts", "source-code", "website"]
for (const field of fieldsToStrip) {
delete snap[field]
}
}
if (packager.packagerOptions.effectiveOptionComputed != null && (await packager.packagerOptions.effectiveOptionComputed({ snap, desktopFile, args }))) {
return
}
await outputFile(path.join(snapMetaDir, this.isUseTemplateApp ? "snap.yaml" : "snapcraft.yaml"), serializeToYaml(snap))
const hooksDir = await packager.getResource(options.hooks, "snap-hooks")
if (hooksDir != null) {
args.push("--hooks", hooksDir)
}
if (this.isUseTemplateApp) {
args.push("--template-url", `electron4:${snapArch}`)
}
await executeAppBuilder(args)
await packager.info.callArtifactBuildCompleted({
file: artifactPath,
safeArtifactName: packager.computeSafeArtifactName(artifactName, "snap", arch, false),
target: this,
arch,
packager,
publishConfig: options.publish == null ? { provider: "snapStore" } : null,
})
}
private isElectronVersionGreaterOrEqualThan(version: string) {
return semver.gte(this.packager.config.electronVersion || "7.0.0", version)
}
}
function archNameToTriplet(arch: Arch): string {
switch (arch) {
case Arch.x64:
return "x86_64-linux-gnu"
case Arch.ia32:
return "i386-linux-gnu"
case Arch.armv7l:
// noinspection SpellCheckingInspection
return "arm-linux-gnueabihf"
case Arch.arm64:
return "aarch64-linux-gnu"
default:
throw new Error(`Unsupported arch ${arch}`)
}
}
function isArrayEqualRegardlessOfSort(a: Array<string>, b: Array<string>) {
a = a.slice()
b = b.slice()
a.sort()
b.sort()
return a.length === b.length && a.every((value, index) => value === b[index])
}
function normalizePlugConfiguration(raw: Array<string | PlugDescriptor> | PlugDescriptor | null | undefined): { [key: string]: { [name: string]: any } | null } | null {
if (raw == null) {
return null
}
const result: any = {}
for (const item of Array.isArray(raw) ? raw : [raw]) {
if (typeof item === "string") {
result[item] = null
} else {
Object.assign(result, item)
}
}
return result
}
function isBrowserSandboxAllowed(snap: any): boolean {
if (snap.plugs != null) {
for (const plugName of Object.keys(snap.plugs)) {
const plug = snap.plugs[plugName]
if (plug.interface === "browser-support" && plug["allow-sandbox"] === true) {
return true
}
}
}
return false
}
function getDefaultStagePackages() {
// libxss1 - was "error while loading shared libraries: libXss.so.1" on Xubuntu 16.04
// noinspection SpellCheckingInspection
return ["libnspr4", "libnss3", "libxss1", "libappindicator3-1", "libsecret-1-0"]
}