Snapcraft GuideSnapcraft指南 (Linux)
This guide provides information on how to package your Electron application for any Snapcraft environment, including the Ubuntu Software Center.本指南提供了有关如何在任何Snapcraft环境(包括Ubuntu软件中心)中打包Electron应用程序的信息。
Background and Requirements背景和要求
Together with the broader Linux community, Canonical aims to fix many of the common software installation problems with the snapcraft project. Canonical与更广泛的Linux社区共同致力于修复snapcraft项目中的许多常见软件安装问题。Snaps are containerized software packages that include required dependencies, auto-update, and work on all major Linux distributions without system modification.快照是容器化的软件包,其中包括所需的依赖项、自动更新,并且可以在所有主要Linux发行版上运行,而无需修改系统。
There are three ways to create a 创建.snap
file:.snap
文件有三种方法:
1) Using electron-forge or electron-builder, both tools that come with 使用electron-forge或electron-builder,这两种工具都带有现成的弹簧支撑。snap
support out of the box. This is the easiest option. 这是最简单的选择。2) Using 使用electron-installer-snap
, which takes electron-packager
's output. electron-installer-snap
,获取electron-packager
的输出。3) Using an already created 使用已创建的.deb
package..deb
包。
In some cases, you will need to have the 在某些情况下,您需要安装snapcraft
tool installed. snapcraft
工具。Instructions to install 此处提供了为您的特定发行版安装snapcraft
for your particular distribution are available here.snapcraft
的说明。
Using 使用electron-installer-snap
The module works like electron-winstaller and similar modules in that its scope is limited to building snap packages. 该模块的工作原理类似于electron-winstaller和类似模块,因为其范围仅限于构建快照包。You can install it with:您可以使用以下软件安装:
npm install --save-dev electron-installer-snap
Step 1: Package Your Electron Application打包Electron应用程序
Package the application using electron-packager (or a similar tool). 使用Electron打包器(或类似工具)打包应用程序。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.node_modules
,因为实际上不需要的任何模块都会增加应用程序的大小。
The output should look roughly like this:输出大致如下:
.
└── 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
Step 2: Running 运行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.PATH
中有snapcraft
的终端运行electron-installer-snap
,只需要一个参数--src
,它是在第一步中创建的打包electron应用程序的位置。
npx electron-installer-snap --src=out/myappname-linux-x64
If you have an existing build pipeline, you can use 如果您有一个现有的构建管道,您可以通过编程方式使用electron-installer-snap
programmatically. electron-installer-snap
。For more information, see the Snapcraft API docs.有关更多信息,请参阅Snapcraft API文档。
const snap = require('electron-installer-snap')
snap(options)
.then(snapPath => console.log(`Created snap at ${snapPath}!`))
Using snapcraft
with electron-packager
使用带有electron-packager
的snapcraft
snapcraft
with electron-packager
Step 1: Create Sample Snapcraft Project创建Snapcraft项目示例
Create your project directory and add the following to 创建项目目录并将以下内容添加到snap/snapcraft.yaml
: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: core18
confinement: strict
grade: stable
apps:
electron-packager-hello-world:
command: electron-quick-start/electron-quick-start --no-sandbox
extensions: [gnome-3-34]
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
If you want to apply this example to an existing project:如果要将此示例应用于现有项目:
Replace使用source: https://github.com/electron/electron-quick-start.git
withsource: .
.source: .
替换source: https://github.com/electron/electron-quick-start.git
。Replace all instances of用项目名称替换所有electron-quick-start
with your project's name.electron-quick-start
实例。
Step 2: Build the snap构建快照
$ snapcraft
<output snipped>
Snapped electron-packager-hello-world_0.1_amd64.snap
Step 3: Install the snap安装快照
sudo snap install electron-packager-hello-world_0.1_amd64.snap --dangerous
Step 4: Run the snap运行快照
electron-packager-hello-world
Using an Existing Debian Package使用现有Debian软件包
Snapcraft is capable of taking an existing Snapcraft能够获取现有的.deb
file and turning it into a .snap
file. .deb
文件并将其转换为.snap
文件。The creation of a snap is configured using a 快照的创建是使用snapcraft.yaml
file that describes the sources, dependencies, description, and other core building blocks.snapcraft.yaml
文件配置的,该文件描述了源、依赖项、描述和其他核心构建块。
Step 1: Create a Debian Package创建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.
Step 2: Create a snapcraft.yaml
For more information on the available configuration options, see the documentation on the snapcraft syntax. 有关可用配置选项的更多信息,请参阅snapcraft语法的文档。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
. snapcraft.yaml
指示系统启动一个名为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:strict
限制来构建snap
,可以使用desktop-launch
(桌面启动)命令:
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