Skip to content

Commit

Permalink
feat(snap): Snap for extra after parts and build packages support (#1565
Browse files Browse the repository at this point in the history
)
  • Loading branch information
hychen authored and develar committed May 23, 2017
1 parent eb181b9 commit 22b5ba5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
21 changes: 17 additions & 4 deletions packages/electron-builder/src/options/linuxOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ export interface LinuxBuildOptions extends PlatformSpecificBuildOptions, CommonL

/**
* Target package type: list of `AppImage`, `snap`, `deb`, `rpm`, `freebsd`, `pacman`, `p5p`, `apk`, `7z`, `zip`, `tar.xz`, `tar.lz`, `tar.gz`, `tar.bz2`, `dir`.
*
*
* electron-builder [docker image](https://github.com/electron-userland/electron-builder/wiki/Docker) can be used to build Linux targets on any platform. See [Multi platform build](https://github.com/electron-userland/electron-builder/wiki/Multi-Platform-Build).
*
*
* @see [Please do not put an AppImage into another archive like a .zip or .tar.gz](https://github.com/probonopd/AppImageKit/wiki/Creating-AppImages#common-mistake)
* @default AppImage
*/
Expand Down Expand Up @@ -146,22 +146,35 @@ export interface SnapOptions extends LinuxBuildOptions {
*/
readonly assumes?: Array<string> | null

/**
* The list of debian packages needs to be installed for building this snap.
*/
readonly buildPackages?: Array<string> | null

/**
* The list of Ubuntu packages to use that are needed to support the `app` part creation. Like `depends` for `deb`.
* Defaults to `["libnotify4", "libappindicator1", "libxtst6", "libnss3", "libxss1", "fontconfig-config", "gconf2", "libasound2", "pulseaudio"]`.
*
*
* 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.
*/
readonly stagePackages?: Array<string> | null

/**
* The list of [plugs](https://snapcraft.io/docs/reference/interfaces).
* Defaults to `["home", "x11", "unity7", "browser-support", "network", "gsettings", "pulseaudio", "opengl"]`.
*
*
* 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.
*/
readonly plugs?: Array<string> | null

/**
* Specifies any [parts](https://snapcraft.io/docs/reference/parts) that should be built before this part.
* Defaults to `["desktop-only""]`.
*
* 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.
*/
readonly after?: Array<string> | null

/**
* Specify `ubuntu-app-platform1` 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".
Expand Down
19 changes: 17 additions & 2 deletions packages/electron-builder/src/targets/snap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,14 @@ export default class SnapTarget extends Target {
const isUseDocker = process.platform !== "linux"

const defaultStagePackages = (isUseUbuntuPlatform ? ["libnss3"] : ["libnotify4", "libappindicator1", "libxtst6", "libnss3", "libxss1", "fontconfig-config", "gconf2", "libasound2", "pulseaudio"])
const defaultAfter = isUseUbuntuPlatform ? ["extra", "desktop-ubuntu-app-platform"] : ["desktop-glib-only"]
const after = replaceDefault(options.after, defaultAfter)
snap.parts = {
app: {
plugin: "dump",
"stage-packages": replaceDefault(options.stagePackages, defaultStagePackages),
source: isUseDocker ? `/out/${path.basename(appOutDir)}` : appOutDir,
after: isUseUbuntuPlatform ? ["extra", "desktop-ubuntu-app-platform"] : ["desktop-glib-only"]
after: after
}
}

Expand All @@ -108,19 +110,32 @@ export default class SnapTarget extends Target {
const snapFileName = `${snap.name}_${snap.version}_${toLinuxArchString(arch)}.snap`
const resultFile = path.join(this.outDir, snapFileName)

function mayInstallBuildPackagesCmd() {
if (options.buildPackages && options.buildPackages.length > 0) {
return `apt-get update && apt-get install -y ${options.buildPackages.join(" ")}`
}
else {
return ""
}
}

if (isUseDocker) {
await spawn("docker", ["run", "--rm",
"-v", `${packager.info.projectDir}:/project`,
"-v", `${homedir()}/.electron:/root/.electron`,
// dist dir can be outside of project dir
"-v", `${this.outDir}:/out`,
"electronuserland/electron-builder:latest",
"/bin/bash", "-c", `snapcraft --version && cp -R /out/${path.basename(stageDir)} /s/ && cd /s && snapcraft snap --target-arch ${toLinuxArchString(arch)} -o /out/${snapFileName}`], {
"/bin/bash", "-c", `${mayInstallBuildPackagesCmd()} && snapcraft --version && cp -R /out/${path.basename(stageDir)} /s/ && cd /s && snapcraft snap --target-arch ${toLinuxArchString(arch)} -o /out/${snapFileName}`], {
cwd: packager.info.projectDir,
stdio: ["ignore", "inherit", "inherit"],
})
}
else {
if (options.buildPackages && options.buildPackages.length > 0) {
await spawn("apt-get", ["update"])
await spawn("apt-get", ["install", "-y"].concat(options.buildPackages))
}
await spawn("snapcraft", ["snap", "--target-arch", toLinuxArchString(arch), "-o", resultFile], {
cwd: stageDir,
stdio: ["ignore", "inherit", "pipe"],
Expand Down

0 comments on commit 22b5ba5

Please sign in to comment.