experiments in a post-browser web
1#!/bin/bash
2# iOS build cache utilities
3# Caches Rust build artifacts to avoid rebuilds across agent workspaces
4
5# Cache location (relative to repo root)
6IOS_CACHE_DIR="tmp/ios-cache"
7
8# Get the repository root directory
9get_repo_root() {
10 git rev-parse --show-toplevel 2>/dev/null || echo "$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
11}
12
13# Compute hash of Rust source files that affect the build
14# Includes Cargo.toml, Cargo.lock, and all .rs files
15get_source_hash() {
16 local tauri_dir="$1"
17
18 if [ -z "$tauri_dir" ]; then
19 echo "Error: tauri_dir required" >&2
20 return 1
21 fi
22
23 # Hash Cargo files, Tauri config, build script, and all Rust source files
24 cat "$tauri_dir/Cargo.toml" \
25 "$tauri_dir/Cargo.lock" \
26 "$tauri_dir/tauri.conf.json" \
27 "$tauri_dir/build.rs" \
28 "$tauri_dir/src"/*.rs 2>/dev/null | shasum -a 256 | cut -d' ' -f1
29}
30
31# Check if cache is valid for a given build type
32# Returns 0 if cache hit, 1 if cache miss
33# Usage: check_cache debug|release tauri_dir
34check_cache() {
35 local build_type="$1"
36 local tauri_dir="$2"
37 local repo_root
38 repo_root="$(get_repo_root)"
39
40 local cache_dir="$repo_root/$IOS_CACHE_DIR/$build_type"
41 local lib_path="$cache_dir/libapp.a"
42 local checksum_path="$cache_dir/checksum.txt"
43
44 # Check if cached files exist
45 if [ ! -f "$lib_path" ] || [ ! -f "$checksum_path" ]; then
46 echo "Cache miss: no cached $build_type build found"
47 return 1
48 fi
49
50 # Compare checksums
51 local current_hash
52 local cached_hash
53 current_hash="$(get_source_hash "$tauri_dir")"
54 cached_hash="$(cat "$checksum_path")"
55
56 if [ "$current_hash" = "$cached_hash" ]; then
57 echo "Cache hit: $build_type build matches (hash: ${current_hash:0:8}...)"
58 return 0
59 else
60 echo "Cache miss: source changed (cached: ${cached_hash:0:8}..., current: ${current_hash:0:8}...)"
61 return 1
62 fi
63}
64
65# Update cache with newly built library
66# Usage: update_cache debug|release tauri_dir lib_path
67update_cache() {
68 local build_type="$1"
69 local tauri_dir="$2"
70 local lib_path="$3"
71 local repo_root
72 repo_root="$(get_repo_root)"
73
74 local cache_dir="$repo_root/$IOS_CACHE_DIR/$build_type"
75
76 # Create cache directory if needed
77 mkdir -p "$cache_dir"
78
79 # Copy library to cache
80 cp "$lib_path" "$cache_dir/libapp.a"
81
82 # Save checksum
83 get_source_hash "$tauri_dir" > "$cache_dir/checksum.txt"
84
85 echo "Cache updated: $build_type build saved to $cache_dir"
86}
87
88# Copy cached library to workspace destination
89# Usage: use_cache debug|release dest_path
90use_cache() {
91 local build_type="$1"
92 local dest_path="$2"
93 local repo_root
94 repo_root="$(get_repo_root)"
95
96 local cache_dir="$repo_root/$IOS_CACHE_DIR/$build_type"
97 local lib_path="$cache_dir/libapp.a"
98
99 # Ensure destination directory exists
100 mkdir -p "$(dirname "$dest_path")"
101
102 # Copy from cache
103 cp "$lib_path" "$dest_path"
104
105 echo "Using cached $build_type build: $dest_path"
106}
107
108# Clear cache for a build type or all caches
109# Usage: clear_cache [debug|release]
110clear_cache() {
111 local build_type="$1"
112 local repo_root
113 repo_root="$(get_repo_root)"
114
115 if [ -z "$build_type" ]; then
116 rm -rf "$repo_root/$IOS_CACHE_DIR"
117 echo "Cleared all iOS build caches"
118 else
119 rm -rf "$repo_root/$IOS_CACHE_DIR/$build_type"
120 echo "Cleared $build_type iOS build cache"
121 fi
122}