electron-launch 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. #!/usr/bin/env bash
  2. # On Fedora $SNAP is under /var and there is some magic to map it to /snap.
  3. # We need to handle that case and reset $SNAP
  4. SNAP=$(echo "$SNAP" | sed -e "s|/var/lib/snapd||g")
  5. #
  6. # Exports are based on https://github.com/snapcore/snapcraft/blob/master/extensions/desktop/common/desktop-exports
  7. #
  8. # ensure_dir_exists calls `mkdir -p` if the given path is not a directory.
  9. # This speeds up execution time by avoiding unnecessary calls to mkdir.
  10. #
  11. # Usage: ensure_dir_exists <path> [<mkdir-options>]...
  12. #
  13. function ensure_dir_exists() {
  14. [ -d "$1" ] || mkdir -p "$@"
  15. }
  16. declare -A PIDS
  17. function async_exec() {
  18. "$@" &
  19. PIDS[$!]=$*
  20. }
  21. function wait_for_async_execs() {
  22. for pid in "${!PIDS[@]}"
  23. do
  24. wait "$pid" && continue || echo "ERROR: ${PIDS[$pid]} exited abnormally with status $?"
  25. done
  26. }
  27. function prepend_dir() {
  28. local -n var="$1"
  29. local dir="$2"
  30. # We can't check if the dir exists when the dir contains variables
  31. if [[ "$dir" == *"\$"* || -d "$dir" ]]; then
  32. export "${!var}=${dir}${var:+:$var}"
  33. fi
  34. }
  35. function append_dir() {
  36. local -n var="$1"
  37. local dir="$2"
  38. # We can't check if the dir exists when the dir contains variables
  39. if [[ "$dir" == *"\$"* || -d "$dir" ]]; then
  40. export "${!var}=${var:+$var:}${dir}"
  41. fi
  42. }
  43. function copy_env_variable() {
  44. local -n var="$1"
  45. if [[ "+$var" ]]; then
  46. export "${!var}_VSCODE_SNAP_ORIG=${var}"
  47. else
  48. export "${!var}_VSCODE_SNAP_ORIG=''"
  49. fi
  50. }
  51. # shellcheck source=/dev/null
  52. source "$SNAP_USER_DATA/.last_revision" 2>/dev/null || true
  53. if [ "$SNAP_DESKTOP_LAST_REVISION" = "$SNAP_VERSION" ]; then
  54. needs_update=false
  55. else
  56. needs_update=true
  57. fi
  58. # Set $REALHOME to the users real home directory
  59. REALHOME=$(getent passwd $UID | cut -d ':' -f 6)
  60. # Set config folder to local path
  61. ensure_dir_exists "$SNAP_USER_DATA/.config"
  62. chmod 700 "$SNAP_USER_DATA/.config"
  63. if [ "$SNAP_ARCH" == "amd64" ]; then
  64. ARCH="x86_64-linux-gnu"
  65. elif [ "$SNAP_ARCH" == "armhf" ]; then
  66. ARCH="arm-linux-gnueabihf"
  67. elif [ "$SNAP_ARCH" == "arm64" ]; then
  68. ARCH="aarch64-linux-gnu"
  69. else
  70. ARCH="$SNAP_ARCH-linux-gnu"
  71. fi
  72. export SNAP_LAUNCHER_ARCH_TRIPLET="$ARCH"
  73. function is_subpath() {
  74. dir="$(realpath "$1")"
  75. parent="$(realpath "$2")"
  76. [ "${dir##"${parent}"/}" != "${dir}" ] && return 0 || return 1
  77. }
  78. function can_open_file() {
  79. [ -f "$1" ] && [ -r "$1" ]
  80. }
  81. # Preserve system variables that get modified below
  82. copy_env_variable XDG_CONFIG_DIRS
  83. copy_env_variable XDG_DATA_DIRS
  84. copy_env_variable LOCPATH
  85. copy_env_variable GIO_MODULE_DIR
  86. copy_env_variable GSETTINGS_SCHEMA_DIR
  87. copy_env_variable GDK_PIXBUF_MODULE_FILE
  88. copy_env_variable GDK_PIXBUF_MODULEDIR
  89. copy_env_variable GDK_BACKEND
  90. copy_env_variable GTK_PATH
  91. copy_env_variable GTK_EXE_PREFIX
  92. copy_env_variable GTK_IM_MODULE_FILE
  93. # XDG Config
  94. prepend_dir XDG_CONFIG_DIRS "$SNAP/etc/xdg"
  95. # Define snaps' own data dir
  96. prepend_dir XDG_DATA_DIRS "$SNAP/usr/share"
  97. prepend_dir XDG_DATA_DIRS "$SNAP/share"
  98. prepend_dir XDG_DATA_DIRS "$SNAP/data-dir"
  99. prepend_dir XDG_DATA_DIRS "$SNAP_USER_DATA"
  100. # Set XDG_DATA_HOME to local path
  101. ensure_dir_exists "$SNAP_USER_DATA/.local/share"
  102. # Workaround for GLib < 2.53.2 not searching for schemas in $XDG_DATA_HOME:
  103. # https://bugzilla.gnome.org/show_bug.cgi?id=741335
  104. prepend_dir XDG_DATA_DIRS "$SNAP_USER_DATA/.local/share"
  105. # Set cache folder to local path
  106. if [[ -d "$SNAP_USER_DATA/.cache" && ! -e "$SNAP_USER_COMMON/.cache" ]]; then
  107. # the .cache directory used to be stored under $SNAP_USER_DATA, migrate it
  108. mv "$SNAP_USER_DATA/.cache" "$SNAP_USER_COMMON/"
  109. fi
  110. ensure_dir_exists "$SNAP_USER_COMMON/.cache"
  111. # Create $XDG_RUNTIME_DIR if not exists (to be removed when LP: #1656340 is fixed)
  112. # shellcheck disable=SC2174
  113. ensure_dir_exists "$XDG_RUNTIME_DIR" -m 700
  114. # Ensure the app finds locale definitions (requires locales-all to be installed)
  115. append_dir LOCPATH "$SNAP/usr/lib/locale"
  116. # If detect wayland server socket, then set environment so applications prefer
  117. # wayland, and setup compat symlink (until we use user mounts. Remember,
  118. # XDG_RUNTIME_DIR is /run/user/<uid>/snap.$SNAP so look in the parent directory
  119. # for the socket. For details:
  120. # https://forum.snapcraft.io/t/wayland-dconf-and-xdg-runtime-dir/186/10
  121. # Applications that don't support wayland natively may define DISABLE_WAYLAND
  122. # (to any non-empty value) to skip that logic entirely.
  123. wayland_available=false
  124. if [[ -n "$XDG_RUNTIME_DIR" && -z "$DISABLE_WAYLAND" ]]; then
  125. wdisplay="wayland-0"
  126. if [ -n "$WAYLAND_DISPLAY" ]; then
  127. wdisplay="$WAYLAND_DISPLAY"
  128. fi
  129. wayland_sockpath="$XDG_RUNTIME_DIR/../$wdisplay"
  130. wayland_snappath="$XDG_RUNTIME_DIR/$wdisplay"
  131. if [ -S "$wayland_sockpath" ]; then
  132. # if running under wayland, use it
  133. #export WAYLAND_DEBUG=1
  134. # shellcheck disable=SC2034
  135. wayland_available=true
  136. # create the compat symlink for now
  137. if [ ! -e "$wayland_snappath" ]; then
  138. ln -s "$wayland_sockpath" "$wayland_snappath"
  139. fi
  140. fi
  141. fi
  142. # Keep an array of data dirs, for looping through them
  143. IFS=':' read -r -a data_dirs_array <<< "$XDG_DATA_DIRS"
  144. # Build mime.cache
  145. # needed for gtk and qt icon
  146. if [ "$needs_update" = true ]; then
  147. rm -rf "$SNAP_USER_DATA/.local/share/mime"
  148. if [ ! -f "$SNAP/usr/share/mime/mime.cache" ]; then
  149. if command -v update-mime-database >/dev/null; then
  150. cp --preserve=timestamps -dR "$SNAP/usr/share/mime" "$SNAP_USER_DATA/.local/share"
  151. async_exec update-mime-database "$SNAP_USER_DATA/.local/share/mime"
  152. fi
  153. fi
  154. fi
  155. # Gio modules and cache (including gsettings module)
  156. export GIO_MODULE_DIR="$SNAP_USER_COMMON/.cache/gio-modules"
  157. function compile_giomodules {
  158. if [ -f "$1/glib-2.0/gio-querymodules" ]; then
  159. rm -rf "$GIO_MODULE_DIR"
  160. ensure_dir_exists "$GIO_MODULE_DIR"
  161. ln -s "$SNAP"/usr/lib/"$ARCH"/gio/modules/*.so "$GIO_MODULE_DIR"
  162. "$1/glib-2.0/gio-querymodules" "$GIO_MODULE_DIR"
  163. fi
  164. }
  165. if [ "$needs_update" = true ]; then
  166. async_exec compile_giomodules "/snap/core20/current/usr/lib/$ARCH"
  167. fi
  168. # Setup compiled gsettings schema
  169. export GSETTINGS_SCHEMA_DIR="$SNAP_USER_DATA/.local/share/glib-2.0/schemas"
  170. function compile_schemas {
  171. if [ -f "$1" ]; then
  172. rm -rf "$GSETTINGS_SCHEMA_DIR"
  173. ensure_dir_exists "$GSETTINGS_SCHEMA_DIR"
  174. for ((i = 0; i < ${#data_dirs_array[@]}; i++)); do
  175. schema_dir="${data_dirs_array[$i]}/glib-2.0/schemas"
  176. if [ -f "$schema_dir/gschemas.compiled" ]; then
  177. # This directory already has compiled schemas
  178. continue
  179. fi
  180. if [ -n "$(ls -A "$schema_dir"/*.xml 2>/dev/null)" ]; then
  181. ln -s "$schema_dir"/*.xml "$GSETTINGS_SCHEMA_DIR"
  182. fi
  183. if [ -n "$(ls -A "$schema_dir"/*.override 2>/dev/null)" ]; then
  184. ln -s "$schema_dir"/*.override "$GSETTINGS_SCHEMA_DIR"
  185. fi
  186. done
  187. # Only compile schemas if we copied anything
  188. if [ -n "$(ls -A "$GSETTINGS_SCHEMA_DIR"/*.xml "$GSETTINGS_SCHEMA_DIR"/*.override 2>/dev/null)" ]; then
  189. "$1" "$GSETTINGS_SCHEMA_DIR"
  190. fi
  191. fi
  192. }
  193. if [ "$needs_update" = true ]; then
  194. async_exec compile_schemas "/snap/core20/current/usr/lib/$ARCH/glib-2.0/glib-compile-schemas"
  195. fi
  196. # Gdk-pixbuf loaders
  197. export GDK_PIXBUF_MODULE_FILE="$SNAP_USER_COMMON/.cache/gdk-pixbuf-loaders.cache"
  198. export GDK_PIXBUF_MODULEDIR="$SNAP/usr/lib/$ARCH/gdk-pixbuf-2.0/2.10.0/loaders"
  199. if [ "$needs_update" = true ] || [ ! -f "$GDK_PIXBUF_MODULE_FILE" ]; then
  200. rm -f "$GDK_PIXBUF_MODULE_FILE"
  201. if [ -f "$SNAP/usr/lib/$ARCH/gdk-pixbuf-2.0/gdk-pixbuf-query-loaders" ]; then
  202. async_exec "$SNAP/usr/lib/$ARCH/gdk-pixbuf-2.0/gdk-pixbuf-query-loaders" > "$GDK_PIXBUF_MODULE_FILE"
  203. fi
  204. fi
  205. # shellcheck disable=SC2154
  206. if [ "$wayland_available" = true ]; then
  207. export GDK_BACKEND="wayland"
  208. fi
  209. append_dir GTK_PATH "$SNAP/usr/lib/$ARCH/gtk-3.0"
  210. append_dir GTK_PATH "$SNAP/usr/lib/gtk-3.0"
  211. # We don't have gtk libraries in this path but
  212. # enforcing this environment variable will disallow
  213. # gtk binaries like `gtk-query-immodules` to not search
  214. # in system default library paths.
  215. # Based on https://gitlab.gnome.org/GNOME/gtk/-/blob/main/gtk/gtkmodules.c#L104-136
  216. export GTK_EXE_PREFIX="$SNAP/usr"
  217. # ibus and fcitx integration
  218. GTK_IM_MODULE_DIR="$SNAP_USER_COMMON/.cache/immodules"
  219. export GTK_IM_MODULE_FILE="$GTK_IM_MODULE_DIR/immodules.cache"
  220. # shellcheck disable=SC2154
  221. if [ "$needs_update" = true ]; then
  222. rm -rf "$GTK_IM_MODULE_DIR"
  223. ensure_dir_exists "$GTK_IM_MODULE_DIR"
  224. ln -s "$SNAP"/usr/lib/"$ARCH"/gtk-3.0/3.0.0/immodules/*.so "$GTK_IM_MODULE_DIR"
  225. async_exec "$SNAP/usr/lib/$ARCH/libgtk-3-0/gtk-query-immodules-3.0" > "$GTK_IM_MODULE_FILE"
  226. fi
  227. # shellcheck disable=SC2154
  228. [ "$needs_update" = true ] && echo "SNAP_DESKTOP_LAST_REVISION=$SNAP_VERSION" > "$SNAP_USER_DATA/.last_revision"
  229. wait_for_async_execs
  230. exec "$@"