Snapcraft 指南 (Linux)
本指南提供了有关如何包装你的 Electron 应用程序的任何 Snapcraft 环境, 包括 Ubuntu 软件中心的信息。
背景和要求
与更广泛的 Linux 社区一起, 规范旨在解决 snapcraft
项目中的许多常见的软件安装问题。 Snaps 是容器化的软件包, 包括所需的依赖项、自动更新和对所有主要 Linux 发行版的工作, 而无需进行系统修改。
创建 .snap
文件有三种方法:
- 使用 Electron Forge或
electron-builder
,这两个工具都提供snap
开箱即用支持 这是最简单的选择。 2) Usingelectron-installer-snap
, which takes@electron/packager
's output. 3) 使用已经创建的.deb
包。
在某些情况下,您需要安装 snapcraft
工具。 安装特定发行版的 snapcraft
的指南在这里能看到。
使用 electron-installer-snap
The module works like electron-winstaller
and similar modules in that its scope is limited to building snap packages. 你可以这样安装:
npm install --save-dev electron-installer-snap
步骤 1: 打包你的 Electron 应用程序
Package the application using @electron/packager (or a similar tool). Make sure to remove node_modules
that you don't need in your final application, since any module you don't actually need will increase your application's size.
结构输出应该看起来大致像这样:
.
└── dist
└── app-linux-x64
├── LICENSE
├── LICENSES.chromium.html
├── content_shell.pak
├── app
├── icudtl.dat
├── libgcrypt.so.11
├── libnode.so
├── locales
├── resources
├── v8_context_snapshot.bin
└── version
步骤 2: 运行 electron-installer-snap
From a terminal that has snapcraft
in its PATH
, run electron-installer-snap
with the only required parameter --src
, which is the location of your packaged Electron application created in the first step.
npx electron-installer-snap --src=out/myappname-linux-x64
If you have an existing build pipeline, you can use electron-installer-snap
programmatically. For more information, see the Snapcraft API docs.
const snap = require('electron-installer-snap')
snap(options)
.then(snapPath => console.log(`Created snap at ${snapPath}!`))
Using snapcraft
with @electron/packager
第 1 步:创建示例 Snapcraft 项目
创建你的项目目录并将以下内容添加到 snap/snapcraft.yaml
:
name: electron-packager-hello-world
version: '0.1'
summary: Hello World Electron app
description: |
Simple Hello World Electron app as an example
base: core22
confinement: strict
grade: stable
apps:
electron-packager-hello-world:
command: electron-quick-start/electron-quick-start --no-sandbox
extensions: [gnome]
plugs:
- browser-support
- network
- network-bind
environment:
# Correct the TMPDIR path for Chromium Framework/Electron to ensure
# libappindicator has readable resources.
TMPDIR: $XDG_RUNTIME_DIR
parts:
electron-quick-start:
plugin: nil
source: https://github.com/electron/electron-quick-start.git
override-build: |
npm install electron @electron/packager
npx electron-packager . --overwrite --platform=linux --output=release-build --prune=true
cp -rv ./electron-quick-start-linux-* $SNAPCRAFT_PART_INSTALL/electron-quick-start
build-snaps:
- node/14/stable
build-packages:
- unzip
stage-packages:
- libnss3
- libnspr4
如果要将此示例应用于现有项目:
- 替代
source: https://github.com/electron/electron-quick-start.git
为source: .
. - 替代所有的
electron-quick-start
为你的项目名称。
第 2 步:构建 snap
$ snapcraft
<output snipped>
Snapped electron-packager-hello-world_0.1_amd64.snap
第 3 步:安装 snap
sudo snap install electron-packager-hello-world_0.1_amd64.snap --dangerous
第 4 步:运行 snap
electron-packager-hello-world
使用一个现有的 Debian 包
Snapcraft is capable of taking an existing .deb
file and turning it into a .snap
file. The creation of a snap is configured using a snapcraft.yaml
file that describes the sources, dependencies, description, and other core building blocks.
步骤 1: 创建一个 Debian 包
If you do not already have a .deb
package, using electron-installer-snap
might be an easier path to create snap packages. However, multiple solutions for creating Debian packages exist, including Electron Forge, electron-builder
or electron-installer-debian
.
步骤 2: 创建一个 snapcraft.yaml
For more information on the available configuration options, see the documentation on the snapcraft syntax. Let's look at an example:
name: myApp
version: '2.0.0'
summary: A little description for the app.
description: |
You know what? This app is amazing! It does all the things
for you. Some say it keeps you young, maybe even happy.
grade: stable
confinement: classic
parts:
slack:
plugin: dump
source: my-deb.deb
source-type: deb
after:
- desktop-gtk3
stage-packages:
- libasound2
- libnotify4
- libnspr4
- libnss3
- libpcre3
- libpulse0
- libxss1
- libxtst6
electron-launch:
plugin: dump
source: files/
prepare: |
chmod +x bin/electron-launch
apps:
myApp:
command: bin/electron-launch $SNAP/usr/lib/myApp/myApp
desktop: usr/share/applications/myApp.desktop
# Correct the TMPDIR path for Chromium Framework/Electron to ensure
# libappindicator has readable resources.
environment:
TMPDIR: $XDG_RUNTIME_DIR
As you can see, the snapcraft.yaml
instructs the system to launch a file called electron-launch
. In this example, it passes information on to the app's binary:
#!/bin/sh
exec "$@" --executed-from="$(pwd)" --pid=$$ > /dev/null 2>&1 &
Alternatively, if you're building your snap
with strict
confinement, you can use the desktop-launch
command:
apps:
myApp:
# Correct the TMPDIR path for Chromium Framework/Electron to ensure
# libappindicator has readable resources.
command: env TMPDIR=$XDG_RUNTIME_DIR PATH=/usr/local/bin:${PATH} ${SNAP}/bin/desktop-launch $SNAP/myApp/desktop
desktop: usr/share/applications/desktop.desktop
Optional: Enabling desktop capture
Capturing the desktop requires PipeWire library in some Linux configurations that use the Wayland protocol. To bundle PipeWire with your application, ensure that the base snap is set to core22
or newer. Next, create a part called pipewire
and add it to the after
section of your application:
pipewire:
plugin: nil
build-packages: [libpipewire-0.3-dev]
stage-packages: [pipewire]
prime:
- usr/lib/*/pipewire-*
- usr/lib/*/spa-*
- usr/lib/*/libpipewire*.so*
- usr/share/pipewire
Finally, configure your application's environment for PipeWire:
environment:
SPA_PLUGIN_DIR: $SNAP/usr/lib/$CRAFT_ARCH_TRIPLET/spa-0.2
PIPEWIRE_CONFIG_NAME: $SNAP/usr/share/pipewire/pipewire.conf
PIPEWIRE_MODULE_DIR: $SNAP/usr/lib/$CRAFT_ARCH_TRIPLET/pipewire-0.3