Skip to content

Commit

Permalink
feat(snap): initialize db and icons for faster startup and remove fon…
Browse files Browse the repository at this point in the history
…ts (#2715)

* snap: update mime db and icon cache on install

In order to improve the startup time for snapped
applications we can generate the mime DB and the
icon caches at snap build time.
In order to do this, we can use snap composition to
override the desktop-gtk{2,3} part, adding an install
script which does this.
The remote part can't do this by default since it
could break legacy applications providing such files
but this won't be the case for electron apps.

* snap: don't ship fonts if desktop plugs are used

When the desktop-interface is used the snap has
access to the system fonts, so there's no point
of adding such files to the final snap.
  • Loading branch information
3v1n0 authored and develar committed Mar 25, 2018
1 parent 24a585b commit 5f0dd86
Show file tree
Hide file tree
Showing 3 changed files with 279 additions and 2 deletions.
30 changes: 28 additions & 2 deletions packages/electron-builder-lib/src/targets/snap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export default class SnapTarget extends Target {

const plugs = normalizePlugConfiguration(options.plugs)
const plugNames = this.replaceDefault(plugs == null ? null : Object.getOwnPropertyNames(plugs), defaultPlugs)
const desktopPart = this.packager.isElectron2 ? "desktop-gtk3" : "desktop-gtk2"

const buildPackages = asArray(options.buildPackages)
this.isUseTemplateApp = this.options.useTemplateApp !== false && arch === Arch.x64 && buildPackages.length === 0
Expand Down Expand Up @@ -75,7 +76,7 @@ export default class SnapTarget extends Target {
app: {
plugin: "nil",
"stage-packages": this.replaceDefault(options.stagePackages, defaultStagePackages),
after: this.replaceDefault(options.after, [this.packager.isElectron2 ? "desktop-gtk3" : "desktop-gtk2"]),
after: this.replaceDefault(options.after, [desktopPart]),
}
},
}
Expand Down Expand Up @@ -105,6 +106,31 @@ export default class SnapTarget extends Target {
if (options.assumes != null) {
snap.assumes = asArray(options.assumes)
}

if (snap.parts.app.after && snap.parts.app.after.indexOf(desktopPart) >= 0) {
const desktopPartOverride: any = {
install: `set -x
export XDG_DATA_DIRS=$SNAPCRAFT_PART_INSTALL/usr/share
update-mime-database $SNAPCRAFT_PART_INSTALL/usr/share/mime
for dir in $SNAPCRAFT_PART_INSTALL/usr/share/icons/*/; do
if [ -f $dir/index.theme ]; then
if which gtk-update-icon-cache-3.0 &> /dev/null; then
gtk-update-icon-cache-3.0 -q $dir
elif which gtk-update-icon-cache &> /dev/null; then
gtk-update-icon-cache -q $dir
fi
fi
done`
}

if (appDescriptor.plugs.indexOf("desktop") >= 0 || appDescriptor.plugs.indexOf("desktop-legacy") >= 0) {
desktopPartOverride.stage = ["-./usr/share/fonts/**"]
}

snap.parts[desktopPart] = desktopPartOverride
}

return snap
}

Expand Down Expand Up @@ -187,4 +213,4 @@ function normalizePlugConfiguration(raw: Array<string | PlugDescriptor> | PlugDe
}
}
return result
}
}
217 changes: 217 additions & 0 deletions test/out/linux/__snapshots__/snapTest.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,24 @@ Object {
"libxtst6",
],
},
"desktop-gtk2": Object {
"install": "set -x
export XDG_DATA_DIRS=$SNAPCRAFT_PART_INSTALL/usr/share
update-mime-database $SNAPCRAFT_PART_INSTALL/usr/share/mime
for dir in $SNAPCRAFT_PART_INSTALL/usr/share/icons/*/; do
if [ -f $dir/index.theme ]; then
if which gtk-update-icon-cache-3.0 &> /dev/null; then
gtk-update-icon-cache-3.0 -q $dir
elif which gtk-update-icon-cache &> /dev/null; then
gtk-update-icon-cache -q $dir
fi
fi
done",
"stage": Array [
"-./usr/share/fonts/**",
],
},
},
"summary": "Sep",
"version": "1.1.0",
Expand All @@ -68,6 +86,69 @@ Object {
}
`;
exports[`custom after, no desktop 1`] = `
Object {
"apps": Object {
"sep": Object {
"adapter": "none",
"command": "command.sh",
"environment": Object {
"LD_LIBRARY_PATH": "$SNAP_LIBRARY_PATH:$SNAP/usr/lib/x86_64-linux-gnu:$SNAP/usr/lib/x86_64-linux-gnu/pulseaudio:$SNAP/usr/lib/x86_64-linux-gnu/mesa-egl:$SNAP/lib:$SNAP/usr/lib:$SNAP/lib/x86_64-linux-gnu:$SNAP/usr/lib/x86_64-linux-gnu:$LD_LIBRARY_PATH:$SNAP/lib:$SNAP/usr/lib:$SNAP/lib/x86_64-linux-gnu:$SNAP/usr/lib/x86_64-linux-gnu",
"PATH": "$SNAP/usr/sbin:$SNAP/usr/bin:$SNAP/sbin:$SNAP/bin:$PATH",
"TMPDIR": "$XDG_RUNTIME_DIR",
},
"plugs": Array [
"desktop",
"desktop-legacy",
"home",
"x11",
"unity7",
"browser-support",
"network",
"gsettings",
"pulseaudio",
"opengl",
],
},
},
"architectures": Array [
"amd64",
],
"confinement": "strict",
"description": "Test Application (test quite “ #378)",
"grade": "stable",
"icon": "snap/gui/icon.png",
"name": "sep",
"parts": Object {
"app": Object {
"after": Array [
"bar",
],
"plugin": "nil",
"stage-packages": Array [
"libasound2",
"libgconf2-4",
"libnotify4",
"libnspr4",
"libnss3",
"libpcre3",
"libpulse0",
"libxss1",
"libxtst6",
],
},
},
"summary": "Sep",
"version": "1.1.0",
}
`;
exports[`custom after, no desktop 2`] = `
Object {
"linux": Array [],
}
`;
exports[`custom env 1`] = `
Object {
"apps": Object {
Expand Down Expand Up @@ -162,6 +243,24 @@ Object {
"libxtst6",
],
},
"desktop-gtk2": Object {
"install": "set -x
export XDG_DATA_DIRS=$SNAPCRAFT_PART_INSTALL/usr/share
update-mime-database $SNAPCRAFT_PART_INSTALL/usr/share/mime
for dir in $SNAPCRAFT_PART_INSTALL/usr/share/icons/*/; do
if [ -f $dir/index.theme ]; then
if which gtk-update-icon-cache-3.0 &> /dev/null; then
gtk-update-icon-cache-3.0 -q $dir
elif which gtk-update-icon-cache &> /dev/null; then
gtk-update-icon-cache -q $dir
fi
fi
done",
"stage": Array [
"-./usr/share/fonts/**",
],
},
},
"summary": "Sep",
"version": "1.1.0",
Expand Down Expand Up @@ -227,6 +326,24 @@ Object {
"custom",
],
},
"desktop-gtk2": Object {
"install": "set -x
export XDG_DATA_DIRS=$SNAPCRAFT_PART_INSTALL/usr/share
update-mime-database $SNAPCRAFT_PART_INSTALL/usr/share/mime
for dir in $SNAPCRAFT_PART_INSTALL/usr/share/icons/*/; do
if [ -f $dir/index.theme ]; then
if which gtk-update-icon-cache-3.0 &> /dev/null; then
gtk-update-icon-cache-3.0 -q $dir
elif which gtk-update-icon-cache &> /dev/null; then
gtk-update-icon-cache -q $dir
fi
fi
done",
"stage": Array [
"-./usr/share/fonts/**",
],
},
},
"summary": "Sep",
"version": "1.1.0",
Expand Down Expand Up @@ -292,6 +409,24 @@ Object {
"libxtst6",
],
},
"desktop-gtk2": Object {
"install": "set -x
export XDG_DATA_DIRS=$SNAPCRAFT_PART_INSTALL/usr/share
update-mime-database $SNAPCRAFT_PART_INSTALL/usr/share/mime
for dir in $SNAPCRAFT_PART_INSTALL/usr/share/icons/*/; do
if [ -f $dir/index.theme ]; then
if which gtk-update-icon-cache-3.0 &> /dev/null; then
gtk-update-icon-cache-3.0 -q $dir
elif which gtk-update-icon-cache &> /dev/null; then
gtk-update-icon-cache -q $dir
fi
fi
done",
"stage": Array [
"-./usr/share/fonts/**",
],
},
},
"summary": "Sep",
"version": "1.1.0",
Expand Down Expand Up @@ -359,6 +494,24 @@ Object {
"foo2",
],
},
"desktop-gtk2": Object {
"install": "set -x
export XDG_DATA_DIRS=$SNAPCRAFT_PART_INSTALL/usr/share
update-mime-database $SNAPCRAFT_PART_INSTALL/usr/share/mime
for dir in $SNAPCRAFT_PART_INSTALL/usr/share/icons/*/; do
if [ -f $dir/index.theme ]; then
if which gtk-update-icon-cache-3.0 &> /dev/null; then
gtk-update-icon-cache-3.0 -q $dir
elif which gtk-update-icon-cache &> /dev/null; then
gtk-update-icon-cache -q $dir
fi
fi
done",
"stage": Array [
"-./usr/share/fonts/**",
],
},
},
"summary": "Sep",
"version": "1.1.0",
Expand All @@ -371,6 +524,40 @@ Object {
}
`;
exports[`no desktop plugs 1`] = `
Object {
"apps": Object {
"sep": Object {
"command": "command.sh",
"environment": Object {
"LD_LIBRARY_PATH": "$SNAP_LIBRARY_PATH:$SNAP/usr/lib/x86_64-linux-gnu:$SNAP/usr/lib/x86_64-linux-gnu/pulseaudio:$SNAP/usr/lib/x86_64-linux-gnu/mesa-egl:$SNAP/lib:$SNAP/usr/lib:$SNAP/lib/x86_64-linux-gnu:$SNAP/usr/lib/x86_64-linux-gnu:$LD_LIBRARY_PATH:$SNAP/lib:$SNAP/usr/lib:$SNAP/lib/x86_64-linux-gnu:$SNAP/usr/lib/x86_64-linux-gnu",
"PATH": "$SNAP/usr/sbin:$SNAP/usr/bin:$SNAP/sbin:$SNAP/bin:$PATH",
"TMPDIR": "$XDG_RUNTIME_DIR",
},
"plugs": Array [
"foo",
"bar",
],
},
},
"architectures": Array [
"amd64",
],
"confinement": "strict",
"description": "Test Application (test quite “ #378)",
"grade": "stable",
"name": "sep",
"summary": "Sep",
"version": "1.1.0",
}
`;
exports[`no desktop plugs 2`] = `
Object {
"linux": Array [],
}
`;
exports[`plugs option 1`] = `
Object {
"apps": Object {
Expand Down Expand Up @@ -414,6 +601,21 @@ Object {
"libxtst6",
],
},
"desktop-gtk2": Object {
"install": "set -x
export XDG_DATA_DIRS=$SNAPCRAFT_PART_INSTALL/usr/share
update-mime-database $SNAPCRAFT_PART_INSTALL/usr/share/mime
for dir in $SNAPCRAFT_PART_INSTALL/usr/share/icons/*/; do
if [ -f $dir/index.theme ]; then
if which gtk-update-icon-cache-3.0 &> /dev/null; then
gtk-update-icon-cache-3.0 -q $dir
elif which gtk-update-icon-cache &> /dev/null; then
gtk-update-icon-cache -q $dir
fi
fi
done",
},
},
"plugs": Object {
"browser-sandbox": Object {
Expand Down Expand Up @@ -475,6 +677,21 @@ Object {
"libxtst6",
],
},
"desktop-gtk2": Object {
"install": "set -x
export XDG_DATA_DIRS=$SNAPCRAFT_PART_INSTALL/usr/share
update-mime-database $SNAPCRAFT_PART_INSTALL/usr/share/mime
for dir in $SNAPCRAFT_PART_INSTALL/usr/share/icons/*/; do
if [ -f $dir/index.theme ]; then
if which gtk-update-icon-cache-3.0 &> /dev/null; then
gtk-update-icon-cache-3.0 -q $dir
elif which gtk-update-icon-cache &> /dev/null; then
gtk-update-icon-cache -q $dir
fi
fi
done",
},
},
"plugs": Object {
"browser-sandbox": Object {
Expand Down
34 changes: 34 additions & 0 deletions test/src/linux/snapTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,37 @@ test.ifDevOrLinuxCi("custom env", app({
return true
},
}))

test.ifDevOrLinuxCi("custom after, no desktop", app({
targets: Platform.LINUX.createTarget("snap"),
config: {
extraMetadata: {
name: "sep",
},
productName: "Sep",
snap: {
after: ["bar"],
}
},
effectiveOptionComputed: async ({ snap }) => {
expect(snap).toMatchSnapshot()
return true
},
}))

test.ifDevOrLinuxCi("no desktop plugs", app({
targets: Platform.LINUX.createTarget("snap"),
config: {
extraMetadata: {
name: "sep",
},
productName: "Sep",
snap: {
plugs: ["foo", "bar"]
}
},
effectiveOptionComputed: async ({ snap }) => {
expect(snap).toMatchSnapshot()
return true
},
}))

0 comments on commit 5f0dd86

Please sign in to comment.