Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement snap packaging, snaps are universal Linux packages(#704) #706

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,12 @@ build-*
src/simplescreenrecorder

private-*

# Snap packaging specific rules
/snap/.snapcraft/
/parts/
/stage/
/prime/

/*.snap
/*_source.tar.bz2
14 changes: 14 additions & 0 deletions snap/local/launchers/simplescreenrecorder-launch
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env bash
# This is the maintainence launcher for the snap, make necessary runtime environment changes to make the snap work here. You may also insert security confinement/deprecation/obsoletion notice of the snap here.

set \
-o errexit \
-o errtrace \
-o nounset \
-o pipefail

# gtk-common-themes support
export QT_QPA_PLATFORMTHEME=gtk3

# Finally run the next part of the command chain
exec "${@}"
220 changes: 220 additions & 0 deletions snap/local/patching/patch-desktop-entries.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
#!/usr/bin/env bash
# Utility script to patch desktop entries in snap, should only be run in the `override-prime` scriptlet
# 林博仁 © 2018

## Makes debuggers' life easier - Unofficial Bash Strict Mode
## BASHDOC: Shell Builtin Commands - Modifying Shell Behavior - The Set Builtin
set \
-o errexit \
-o errtrace \
-o nounset \
-o pipefail

## Runtime Dependencies Checking
declare\
runtime_dependency_checking_result=still-pass\
required_software

for required_command in \
basename \
dirname \
find \
realpath \
sed \
xargs; do
if ! command -v "${required_command}" &>/dev/null; then
runtime_dependency_checking_result=fail

case "${required_command}" in
basename \
|dirname \
|realpath)
required_software='GNU Coreutils'
;;
find \
|xargs)
required_software='GNU Findutils'
;;
sed)
required_software='GNU Sed'
;;
*)
required_software="${required_command}"
;;
esac

printf -- \
'Error: This program requires "%s" to be installed and its executables in the executable searching paths.\n' \
"${required_software}" \
1>&2
unset required_software
fi
done; unset required_command required_software

if [ "${runtime_dependency_checking_result}" = fail ]; then
printf -- \
'Error: Runtime dependency checking fail, the progrom cannot continue.\n' \
1>&2
exit 1
fi; unset runtime_dependency_checking_result

## Non-overridable Primitive Variables
## BASHDOC: Shell Variables » Bash Variables
## BASHDOC: Basic Shell Features » Shell Parameters » Special Parameters
if [ -v 'BASH_SOURCE[0]' ]; then
RUNTIME_EXECUTABLE_PATH="$(realpath --strip "${BASH_SOURCE[0]}")"
RUNTIME_EXECUTABLE_FILENAME="$(basename "${RUNTIME_EXECUTABLE_PATH}")"
RUNTIME_EXECUTABLE_NAME="${RUNTIME_EXECUTABLE_FILENAME%.*}"
RUNTIME_EXECUTABLE_DIRECTORY="$(dirname "${RUNTIME_EXECUTABLE_PATH}")"
RUNTIME_COMMANDLINE_BASECOMMAND="${0}"
# We intentionally leaves these variables for script developers
# shellcheck disable=SC2034
declare -r \
RUNTIME_EXECUTABLE_PATH \
RUNTIME_EXECUTABLE_FILENAME \
RUNTIME_EXECUTABLE_NAME \
RUNTIME_EXECUTABLE_DIRECTORY \
RUNTIME_COMMANDLINE_BASECOMMAND
fi
declare -ar RUNTIME_COMMANDLINE_ARGUMENTS=("${@}")

## init function: entrypoint of main program
## This function is called near the end of the file,
## with the script's command-line parameters as arguments
init(){
if ! process_commandline_arguments; then
printf -- \
'Error: Invalid command-line parameters.\n' \
1>&2

printf '\n' # separate error message and help message
print_help
exit 1
fi

if ! test -v SNAPCRAFT_PRIME \
|| test "${PWD}" != "${SNAPCRAFT_PRIME}"; then
printf -- \
"%s: Error: This script should be run by \`snapcraft\` in the \`override-prime\` scriptlet.\\n" \
"${RUNTIME_EXECUTABLE_NAME}"
exit 1
fi

if test -d share/applications; then
find \
share/applications \
-name '*.desktop' \
-print0 \
| xargs \
--no-run-if-empty \
--null \
--verbose \
sed \
--file "${RUNTIME_EXECUTABLE_DIRECTORY}"/patch-desktop-entries.sed \
--in-place
fi

exit 0
}; declare -fr init

print_help(){
# Backticks in help message is Markdown's <code> markup
# shellcheck disable=SC2016
{
printf '# Help Information for %s #\n' \
"${RUNTIME_COMMANDLINE_BASECOMMAND}"
printf '## SYNOPSIS ##\n'
printf '* `"%s" _command-line_options_`\n\n' \
"${RUNTIME_COMMANDLINE_BASECOMMAND}"

printf '## COMMAND-LINE OPTIONS ##\n'
printf '### `-d` / `--debug` ###\n'
printf 'Enable script debugging\n\n'

printf '### `-h` / `--help` ###\n'
printf 'Print this message\n\n'
}
return 0
}; declare -fr print_help;

process_commandline_arguments() {
if [ "${#RUNTIME_COMMANDLINE_ARGUMENTS[@]}" -eq 0 ]; then
return 0
fi

# Modifyable parameters for parsing by consuming
local -a parameters=("${RUNTIME_COMMANDLINE_ARGUMENTS[@]}")

# Normally we won't want debug traces to appear during parameter parsing, so we add this flag and defer its activation till returning(Y: Do debug)
local enable_debug=N

while true; do
if [ "${#parameters[@]}" -eq 0 ]; then
break
else
case "${parameters[0]}" in
--debug \
|-d)
enable_debug=Y
;;
--help \
|-h)
print_help;
exit 0
;;
*)
printf -- \
'%s: Error: Unknown command-line argument "%s"\n' \
"${FUNCNAME[0]}" \
"${parameters[0]}" \
>&2
return 1
;;
esac
# shift array by 1 = unset 1st then repack
unset 'parameters[0]'
if [ "${#parameters[@]}" -ne 0 ]; then
parameters=("${parameters[@]}")
fi
fi
done

if [ "${enable_debug}" = Y ]; then
trap 'trap_return "${FUNCNAME[0]}"' RETURN
set -o xtrace
fi
return 0
}; declare -fr process_commandline_arguments

## Traps: Functions that are triggered when certain condition occurred
## Shell Builtin Commands » Bourne Shell Builtins » trap
trap_errexit(){
printf \
'An error occurred and the script is prematurely aborted\n' \
1>&2
return 0
}; declare -fr trap_errexit; trap trap_errexit ERR

trap_exit(){
return 0
}; declare -fr trap_exit; trap trap_exit EXIT

trap_return(){
local returning_function="${1}"

printf \
'DEBUG: %s: returning from %s\n' \
"${FUNCNAME[0]}" \
"${returning_function}" \
1>&2
}; declare -fr trap_return

trap_interrupt(){
printf '\n' # Separate previous output
printf \
'Recieved SIGINT, script is interrupted.' \
1>&2
return 1
}; declare -fr trap_interrupt; trap trap_interrupt INT

init "${@}"
28 changes: 28 additions & 0 deletions snap/local/patching/patch-desktop-entries.sed
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# This sed script patches the desktop entries of the snapped
# application.
#
# Documentation:
#
# * GNU Sed Manual
# https://www.gnu.org/software/sed/manual
# * `sed` script overview - `sed` scripts
# * `sed` commands summary - `sed` scripts
# * The `s` Command - `sed` scripts
# * Overview of basic regular expression syntax - Regular
# Expressions: selecting text
# * Back-references and Subexpressions - Regular Expressions:
# selecting text

## Append '(Snap)' to the application name to make it
## distinguishable with the other same application using different
## software distribution technologies
##
## FIXME: The appended string is not localizable, the proper way to
## implement this is to probably use a new X-Snappy-Name keys
## with localestring format to let the translators fill in
## additional localized string and use these values to replace
## the Name keys here.
s/^\(Name\(\[.\+\]\)\?=.*\)$/\1 (Snap)/g

## Fix-up application icon lookup
s|^Icon=.*|Icon=\${SNAP}/meta/gui/icon.svg|
74 changes: 74 additions & 0 deletions snap/local/scriptlets/main-adopt-info
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#!/usr/bin/env bash
# Extract and set the snap's version from the main part
# 林博仁(Buo-ren, Lin) <Buo.Ren.Lin@gmail.com> © 2019

set \
-o errexit \
-o errtrace \
-o nounset \
-o pipefail

init(){
local \
flag_dry_run=false \
upstream_version \
packaging_revision \
some_place_under_the_project_repo \
snap_version_string

for commandline_argument in "${@}"; do
case "${commandline_argument}" in
# Enable execution tracing
--debug)
set -o xtrace
;;
# Don't run snapcraftctl for testing purpose
--dry-run)
flag_dry_run=true
;;
--usage)
print_usage
exit 0
;;
*)
printf -- \
'main-adopt-info: Error: Invalid command-line argument.\n' \
>&2
print_usage
exit 1
;;
esac
done

if ! git describe &>/dev/null; then
upstream_version=unknown
else
upstream_version="$(
git \
describe \
--always \
--dirty=-d \
--tags \
| sed s/^v//
)"
fi

snap_version_string="${upstream_version}"

printf -- \
'main-adopt-info: Setting snap version to "%s".\n' \
"${snap_version_string}"
if [ "${flag_dry_run}" = false ]; then
snapcraftctl set-version \
"${snap_version_string}"
fi

exit 0
}

print_usage(){
printf -- 'Usage: main-adopt-info [options] _snap_name_\n'
printf -- 'options: [--debug|--dry-run|--help]\n'
}

init "${@}"
Loading