|
| 1 | +#!/bin/bash |
| 2 | +set -euo pipefail |
| 3 | +set -x |
| 4 | + |
| 5 | +script_dir=$(cd "$(dirname "$0")" && pwd) |
| 6 | +snap_dir="$script_dir/snap" |
| 7 | +env_file="${SNAP_ENV_FILE:-}" |
| 8 | + |
| 9 | +docker_image="${SNAPCRAFT_DOCKER_IMAGE:-ghcr.io/canonical/snapcraft:8_core24}" |
| 10 | +container_workspace="${SNAPCRAFT_CONTAINER_WORKSPACE:-/project}" |
| 11 | +output_dir="${SNAP_OUTPUT_DIR:-$script_dir}" |
| 12 | +platform="${SNAPCRAFT_DOCKER_PLATFORM:-}" |
| 13 | +apt_update="${SNAPCRAFT_APT_UPDATE:-1}" |
| 14 | + |
| 15 | +load_env_file() { |
| 16 | + local candidate="$1" |
| 17 | + |
| 18 | + if [[ -z "$candidate" || ! -f "$candidate" ]]; then |
| 19 | + return 0 |
| 20 | + fi |
| 21 | + |
| 22 | + set -a |
| 23 | + # shellcheck disable=SC1090 |
| 24 | + . "$candidate" |
| 25 | + set +a |
| 26 | +} |
| 27 | + |
| 28 | +strip_trailing_cr() { |
| 29 | + printf '%s' "${1%$'\r'}" |
| 30 | +} |
| 31 | + |
| 32 | +if [[ -n "$env_file" ]]; then |
| 33 | + load_env_file "$env_file" |
| 34 | +else |
| 35 | + load_env_file "$script_dir/.env" |
| 36 | + load_env_file "$snap_dir/.env" |
| 37 | +fi |
| 38 | + |
| 39 | +docker_image=$(strip_trailing_cr "${SNAPCRAFT_DOCKER_IMAGE:-$docker_image}") |
| 40 | +container_workspace=$(strip_trailing_cr "${SNAPCRAFT_CONTAINER_WORKSPACE:-$container_workspace}") |
| 41 | +output_dir=$(strip_trailing_cr "${SNAP_OUTPUT_DIR:-$output_dir}") |
| 42 | +platform=$(strip_trailing_cr "${SNAPCRAFT_DOCKER_PLATFORM:-$platform}") |
| 43 | +apt_update=$(strip_trailing_cr "${SNAPCRAFT_APT_UPDATE:-$apt_update}") |
| 44 | + |
| 45 | +if [[ "$output_dir" != /* ]]; then |
| 46 | + output_dir="$script_dir/$output_dir" |
| 47 | +fi |
| 48 | + |
| 49 | +if ! command -v docker >/dev/null 2>&1; then |
| 50 | + echo "Docker is required to build the snap without a local snapcraft installation." >&2 |
| 51 | + exit 1 |
| 52 | +fi |
| 53 | + |
| 54 | +if ! command -v rsync >/dev/null 2>&1; then |
| 55 | + echo "rsync is required to prepare a clean Docker build context." >&2 |
| 56 | + exit 1 |
| 57 | +fi |
| 58 | + |
| 59 | +if [[ ! -f "$snap_dir/snapcraft.yaml" ]]; then |
| 60 | + echo "snap/snapcraft.yaml was not found." >&2 |
| 61 | + exit 1 |
| 62 | +fi |
| 63 | + |
| 64 | +mkdir -p "$output_dir" |
| 65 | + |
| 66 | +build_context=$(mktemp -d) |
| 67 | +cleanup_host() { |
| 68 | + if [[ -d "$build_context" ]]; then |
| 69 | + chmod -R u+rwX "$build_context" 2>/dev/null || true |
| 70 | + rm -rf "$build_context" || true |
| 71 | + fi |
| 72 | +} |
| 73 | +trap cleanup_host EXIT |
| 74 | + |
| 75 | +rsync -a --no-owner --no-group --delete \ |
| 76 | + --exclude '.git' \ |
| 77 | + --exclude '.dart_tool' \ |
| 78 | + --exclude '.flatpak-builder' \ |
| 79 | + --exclude '.pub-cache' \ |
| 80 | + --exclude '.snapcraft' \ |
| 81 | + --exclude 'build' \ |
| 82 | + --exclude 'parts' \ |
| 83 | + --exclude 'prime' \ |
| 84 | + --exclude 'stage' \ |
| 85 | + --exclude '*.snap' \ |
| 86 | + "$script_dir/" \ |
| 87 | + "$build_context/" |
| 88 | + |
| 89 | +docker_args=( |
| 90 | + run |
| 91 | + --rm |
| 92 | + --entrypoint /bin/sh |
| 93 | + --volume "$build_context:$container_workspace" |
| 94 | + --workdir "$container_workspace" |
| 95 | + --env "HOST_UID=$(id -u)" |
| 96 | + --env "HOST_GID=$(id -g)" |
| 97 | + --env "SNAPCRAFT_APT_UPDATE=$apt_update" |
| 98 | +) |
| 99 | + |
| 100 | +if [[ -n "$platform" ]]; then |
| 101 | + docker_args+=(--platform "$platform") |
| 102 | +fi |
| 103 | + |
| 104 | +docker_args+=( |
| 105 | + "$docker_image" |
| 106 | + -c |
| 107 | + 'set -eu |
| 108 | +cleanup() { |
| 109 | + chown -R "$HOST_UID:$HOST_GID" . |
| 110 | +} |
| 111 | +trap cleanup EXIT |
| 112 | +if [ ! -e /usr/share/snapcraft/extensions/desktop ]; then |
| 113 | + extensions_desktop_dir=$(find /usr/lib -path "*/site-packages/extensions/desktop" -type d | head -n 1) |
| 114 | + if [ -n "$extensions_desktop_dir" ]; then |
| 115 | + mkdir -p /usr/share/snapcraft/extensions |
| 116 | + ln -s "$extensions_desktop_dir" /usr/share/snapcraft/extensions/desktop |
| 117 | + fi |
| 118 | +fi |
| 119 | +if [ "${SNAPCRAFT_APT_UPDATE:-1}" = "1" ]; then |
| 120 | + apt-get update |
| 121 | +fi |
| 122 | +snapcraft pack --destructive-mode "$@"' |
| 123 | + sh |
| 124 | +) |
| 125 | + |
| 126 | +docker "${docker_args[@]}" "$@" |
| 127 | + |
| 128 | +mapfile -t snap_files < <(find "$build_context" -maxdepth 1 -type f -name "*.snap" -print) |
| 129 | +if [[ "${#snap_files[@]}" -eq 0 ]]; then |
| 130 | + echo "No snap artifact was produced." >&2 |
| 131 | + exit 1 |
| 132 | +fi |
| 133 | + |
| 134 | +for snap_file in "${snap_files[@]}"; do |
| 135 | + mv "$snap_file" "$output_dir/" |
| 136 | +done |
| 137 | + |
| 138 | +printf 'Snap artifact(s):\n' |
| 139 | +for snap_file in "${snap_files[@]}"; do |
| 140 | + printf ' %s\n' "$output_dir/$(basename "$snap_file")" |
| 141 | +done |
0 commit comments