| 1 | #!/usr/bin/env bash |
| 2 | # Pack or unpack a Cargo/rustup (and optional cargo-xwin) cache as one tarball. |
| 3 | # BlueGit `cache.paths` copies only files, not directories, so a directory |
| 4 | # such as cargo-home/ would otherwise be skipped (see bluegit runner saveCache). |
| 5 | # Does not cache target/: too large for the runner's job-cache budget. |
| 6 | set -euo pipefail |
| 7 | |
| 8 | usage() { |
| 9 | echo "usage: $0 restore|save linux|windows" >&2 |
| 10 | exit 2 |
| 11 | } |
| 12 | |
| 13 | cmd="${1:-}" |
| 14 | kind="${2:-}" |
| 15 | [[ "$cmd" == restore || "$cmd" == save ]] || usage |
| 16 | [[ "$kind" == linux || "$kind" == windows ]] || usage |
| 17 | |
| 18 | archive=".ci-cache/cargo-${kind}.tar.gz" |
| 19 | |
| 20 | # Locations in the job workspace (set by the CI job / ci-setup.sh). |
| 21 | cargo_home="${CARGO_HOME:-$PWD/cargo-home}" |
| 22 | rustup_home="${RUSTUP_HOME:-$PWD/rustup-home}" |
| 23 | xwin_cache="${XWIN_CACHE_DIR:-$PWD/xwin-cache}" |
| 24 | |
| 25 | relpath() { |
| 26 | local abs="$1" |
| 27 | case "$abs" in |
| 28 | "$PWD"/*) printf '%s\n' "${abs#"$PWD"/}" ;; |
| 29 | /*) printf '%s\n' "$abs" ;; |
| 30 | *) printf '%s\n' "$abs" ;; |
| 31 | esac |
| 32 | } |
| 33 | |
| 34 | if [[ "$cmd" == restore ]]; then |
| 35 | if [[ -f "$archive" ]]; then |
| 36 | tar -xzf "$archive" |
| 37 | echo "CI-cache hersteld ($archive)." |
| 38 | else |
| 39 | echo "Geen CI-cache ($archive)." |
| 40 | fi |
| 41 | exit 0 |
| 42 | fi |
| 43 | |
| 44 | mkdir -p .ci-cache |
| 45 | parts=() |
| 46 | add_if_dir() { |
| 47 | local p="$1" |
| 48 | [[ -d "$p" ]] || return 0 |
| 49 | parts+=("$(relpath "$p")") |
| 50 | } |
| 51 | |
| 52 | # Registry and git checkouts, not registry/src (that is rebuilt from .crate files). |
| 53 | add_if_dir "$cargo_home/bin" |
| 54 | add_if_dir "$cargo_home/registry/index" |
| 55 | add_if_dir "$cargo_home/registry/cache" |
| 56 | add_if_dir "$cargo_home/git/db" |
| 57 | add_if_dir "$rustup_home" |
| 58 | if [[ "$kind" == windows ]]; then |
| 59 | add_if_dir "$xwin_cache" |
| 60 | fi |
| 61 | |
| 62 | if [[ ${#parts[@]} -eq 0 ]]; then |
| 63 | echo "Niets te cachen." |
| 64 | exit 0 |
| 65 | fi |
| 66 | |
| 67 | # gzip -1: kleiner bestand voor de 10 GB-jobcache, weinig CPU. |
| 68 | tar -c --use-compress-program='gzip -1' -f "$archive" "${parts[@]}" |
| 69 | echo "CI-cache opgeslagen ($archive): ${parts[*]}" |
| 70 | ls -lh "$archive" |