Skip to content

Commit 9c69ce4

Browse files
committed
feat(snap): snapcraft 2.26 support
Close #1265
1 parent 481dfa2 commit 9c69ce4

File tree

6 files changed

+30
-14
lines changed

6 files changed

+30
-14
lines changed

packages/electron-builder-util/src/util.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export interface ExecOptions extends BaseExecOptions {
2929
}
3030

3131
export function removePassword(input: string) {
32-
return input.replace(/(-P |pass:|\/p|-pass )([^ ]+)/, function (match, p1, p2) {
32+
return input.replace(/(-P |pass:| \/p|-pass )([^ ]+)/g, function (match, p1, p2) {
3333
return `${p1}${createHash("sha256").update(p2).digest("hex")} (sha256 hash)`
3434
})
3535
}

packages/electron-builder/src/metadata.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,4 +344,16 @@ export interface PlatformSpecificBuildOptions extends TargetSpecificOptions {
344344
export interface Macros {
345345
os: string
346346
arch: string
347+
}
348+
349+
export function getPlatformIconFileName(value: string | null | undefined, isMac: boolean) {
350+
if (value == null) {
351+
return null
352+
}
353+
354+
if (!value.includes(".")) {
355+
return `${value}.${isMac ? "icns" : "ico"}`
356+
}
357+
358+
return value.replace(isMac ? ".ico" : ".icns", isMac ? ".icns" : ".ico")
347359
}

packages/electron-builder/src/packager/mac.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { use, asArray } from "electron-builder-util"
66
import { normalizeExt, PlatformPackager } from "../platformPackager"
77
import { warn } from "electron-builder-util/out/log"
88
import { unlinkIfExists, copyFile } from "electron-builder-util/out/fs"
9+
import { getPlatformIconFileName } from "../metadata"
910

1011
function doRename(basePath: string, oldName: string, newName: string) {
1112
return rename(path.join(basePath, oldName), path.join(basePath, newName))
@@ -109,7 +110,7 @@ export async function createApp(packager: PlatformPackager<any>, appOutDir: stri
109110
if (fileAssociations.length > 0) {
110111
appPlist.CFBundleDocumentTypes = await BluebirdPromise.map(fileAssociations, async fileAssociation => {
111112
const extensions = asArray(fileAssociation.ext).map(normalizeExt)
112-
const customIcon = await packager.getResource(fileAssociation.icon, `${extensions[0]}.icns`)
113+
const customIcon = await packager.getResource(getPlatformIconFileName(fileAssociation.icon, true), `${extensions[0]}.icns`)
113114
let iconFile = appPlist.CFBundleIconFile
114115
if (customIcon != null) {
115116
iconFile = path.basename(customIcon)

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import sanitizeFileName from "sanitize-filename"
1414
import { copyFile } from "electron-builder-util/out/fs"
1515
import { computeDownloadUrl, getPublishConfigs, getPublishConfigsForUpdateInfo } from "../publish/PublishManager"
1616
import { getSignVendorPath } from "../windowsCodeSign"
17+
import { getPlatformIconFileName } from "../metadata"
1718

1819
const ELECTRON_BUILDER_NS_UUID = "50e065bc-3134-11e6-9bab-38c9862bdaf3"
1920

@@ -358,7 +359,7 @@ export default class NsisTarget extends Target {
358359
for (const item of fileAssociations) {
359360
const extensions = asArray(item.ext).map(normalizeExt)
360361
for (const ext of extensions) {
361-
const customIcon = await packager.getResource(item.icon, `${extensions[0]}.ico`)
362+
const customIcon = await packager.getResource(getPlatformIconFileName(item.icon, false), `${extensions[0]}.ico`)
362363
let installedIconPath = "$appExe,0"
363364
if (customIcon != null) {
364365
installedIconPath = `$INSTDIR\\resources\\${path.basename(customIcon)}`

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

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { LinuxTargetHelper } from "./LinuxTargetHelper"
22
import { LinuxPackager } from "../linuxPackager"
33
import { log } from "electron-builder-util/out/log"
44
import { SnapOptions } from "../options/linuxOptions"
5-
import { emptyDir, writeFile, copy } from "fs-extra-p"
5+
import { emptyDir, outputFile, copy } from "fs-extra-p"
66
import * as path from "path"
77
import { safeDump } from "js-yaml"
88
import { spawn } from "electron-builder-util"
@@ -23,10 +23,11 @@ export default class SnapTarget extends Target {
2323
const appInfo = packager.appInfo
2424
const options = this.options
2525

26-
const snapDir = `${appOutDir}-snap`
27-
await emptyDir(snapDir)
26+
const stageDir = `${appOutDir}-snap`
27+
const snapDir = path.join(stageDir, "snap")
28+
await emptyDir(stageDir)
2829

29-
const extraSnapSourceDir = path.join(snapDir, ".extra")
30+
const extraSnapSourceDir = path.join(stageDir, "extra")
3031
const isUseUbuntuPlatform = options.ubuntuAppPlatformContent != null
3132
if (isUseUbuntuPlatform) {
3233
// ubuntu-app-platform requires empty directory
@@ -43,11 +44,11 @@ export default class SnapTarget extends Target {
4344

4445
await this.helper.icons
4546
if (this.helper.maxIconPath != null) {
46-
snap.icon = "setup/gui/icon.png"
47-
await copy(this.helper.maxIconPath, path.join(snapDir, "setup", "gui", "icon.png"))
47+
snap.icon = "snap/gui/icon.png"
48+
await copy(this.helper.maxIconPath, path.join(snapDir, "gui", "icon.png"))
4849
}
4950

50-
await this.helper.computeDesktopEntry(this.options, `${snap.name}`, path.join(snapDir, "setup", "gui", `${snap.name}.desktop`), {
51+
await this.helper.computeDesktopEntry(this.options, `${snap.name}`, path.join(snapDir, "gui", `${snap.name}.desktop`), {
5152
"Icon": "${SNAP}/meta/gui/icon.png"
5253
})
5354

@@ -93,7 +94,7 @@ export default class SnapTarget extends Target {
9394
if (isUseUbuntuPlatform) {
9495
snap.parts.extra = {
9596
plugin: "dump",
96-
source: isUseDocker ? `/out/${path.basename(snapDir)}/${path.basename(extraSnapSourceDir)}` : extraSnapSourceDir
97+
source: isUseDocker ? `/out/${path.basename(stageDir)}/${path.basename(extraSnapSourceDir)}` : extraSnapSourceDir
9798
}
9899
}
99100

@@ -102,7 +103,7 @@ export default class SnapTarget extends Target {
102103
}
103104

104105
const snapcraft = path.join(snapDir, "snapcraft.yaml")
105-
await writeFile(snapcraft, safeDump(snap, {lineWidth: 160}))
106+
await outputFile(snapcraft, safeDump(snap, {lineWidth: 160}))
106107

107108
const snapName = `${snap.name}_${snap.version}_${toLinuxArchString(arch)}.snap`
108109
const resultFile = path.join(this.outDir, snapName)
@@ -114,14 +115,14 @@ export default class SnapTarget extends Target {
114115
// dist dir can be outside of project dir
115116
"-v", `${this.outDir}:/out`,
116117
"electronuserland/electron-builder:latest",
117-
"/bin/bash", "-c", `snapcraft --version && cp -R /out/${path.basename(snapDir)} /s/ && cd /s && snapcraft snap --target-arch ${toLinuxArchString(arch)} -o /out/${snapName}`], {
118+
"/bin/bash", "-c", `snapcraft --version && cp -R /out/${path.basename(stageDir)} /s/ && cd /s && snapcraft snap --target-arch ${toLinuxArchString(arch)} -o /out/${snapName}`], {
118119
cwd: packager.info.projectDir,
119120
stdio: ["ignore", "inherit", "inherit"],
120121
})
121122
}
122123
else {
123124
await spawn("snapcraft", ["snap", "--target-arch", toLinuxArchString(arch), "-o", resultFile], {
124-
cwd: snapDir,
125+
cwd: stageDir,
125126
stdio: ["ignore", "inherit", "pipe"],
126127
})
127128
}

test/src/mac/CodeSignTest.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,5 @@ test.ifMac("create keychain with installers", async () => {
2424
test.ifDevOrLinuxCi("remove password from log", async () => {
2525
expect(removePassword("seq -P foo -B")).toEqual("seq -P 2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae (sha256 hash) -B")
2626
expect(removePassword("pass:foo")).toEqual("pass:2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae (sha256 hash)")
27+
expect(removePassword("/usr/bin/productbuild -P wefwef")).toEqual("/usr/bin/productbuild -P 56ef615b2e26c3b9a10dc2824238fb8b8a154ec7db4907ec6ee357ed7bb350b7 (sha256 hash)")
2728
})

0 commit comments

Comments
 (0)