|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +set -euo pipefail |
| 4 | + |
| 5 | +TARGET="${1:-all}" |
| 6 | + |
| 7 | +ROOT_DIR="$(cd "$(dirname "$0")/.." && pwd)" |
| 8 | +STATE_DIR="$ROOT_DIR/shared-native/.sync-state" |
| 9 | +SHARED_ANDROID_DIR="$ROOT_DIR/shared-native/android" |
| 10 | +SHARED_IOS_DIR="$ROOT_DIR/shared-native/ios" |
| 11 | + |
| 12 | +sync_dir_contents() { |
| 13 | + local src_dir="$1" |
| 14 | + local dst_dir="$2" |
| 15 | + |
| 16 | + mkdir -p "$dst_dir" |
| 17 | + |
| 18 | + if command -v rsync >/dev/null 2>&1; then |
| 19 | + rsync -a --delete "$src_dir"/ "$dst_dir"/ |
| 20 | + else |
| 21 | + find "$dst_dir" -mindepth 1 -maxdepth 1 -exec rm -rf {} + |
| 22 | + cp -R "$src_dir"/. "$dst_dir"/ |
| 23 | + fi |
| 24 | +} |
| 25 | + |
| 26 | +hash_manifest_for_dir() { |
| 27 | + local dir="$1" |
| 28 | + local manifest_path="$2" |
| 29 | + |
| 30 | + mkdir -p "$(dirname "$manifest_path")" |
| 31 | + : > "$manifest_path" |
| 32 | + |
| 33 | + if [ ! -d "$dir" ]; then |
| 34 | + return |
| 35 | + fi |
| 36 | + |
| 37 | + while IFS= read -r file; do |
| 38 | + local rel_path="${file#$dir/}" |
| 39 | + local hash |
| 40 | + hash="$(shasum "$file" | awk '{print $1}')" |
| 41 | + printf "%s\t%s\n" "$rel_path" "$hash" >> "$manifest_path" |
| 42 | + done < <(find "$dir" -type f | LC_ALL=C sort) |
| 43 | +} |
| 44 | + |
| 45 | +hash_for_path() { |
| 46 | + local manifest_path="$1" |
| 47 | + local rel_path="$2" |
| 48 | + awk -F '\t' -v path="$rel_path" '$1 == path { print $2; found=1; exit } END { if (!found) print "-" }' "$manifest_path" |
| 49 | +} |
| 50 | + |
| 51 | +sync_platform_with_conflict_detection() { |
| 52 | + local package_name="$1" |
| 53 | + local platform_name="$2" |
| 54 | + local package_dir="$3" |
| 55 | + local shared_dir="$4" |
| 56 | + |
| 57 | + mkdir -p "$STATE_DIR" |
| 58 | + mkdir -p "$package_dir" "$shared_dir" |
| 59 | + |
| 60 | + local baseline_manifest="$STATE_DIR/${package_name}_${platform_name}.manifest" |
| 61 | + local tmp_baseline_manifest |
| 62 | + local tmp_shared_manifest |
| 63 | + local tmp_package_manifest |
| 64 | + local tmp_union_paths |
| 65 | + local tmp_conflicts |
| 66 | + |
| 67 | + tmp_baseline_manifest="$(mktemp)" |
| 68 | + tmp_shared_manifest="$(mktemp)" |
| 69 | + tmp_package_manifest="$(mktemp)" |
| 70 | + tmp_union_paths="$(mktemp)" |
| 71 | + tmp_conflicts="$(mktemp)" |
| 72 | + |
| 73 | + if [ -f "$baseline_manifest" ]; then |
| 74 | + cp "$baseline_manifest" "$tmp_baseline_manifest" |
| 75 | + fi |
| 76 | + |
| 77 | + hash_manifest_for_dir "$shared_dir" "$tmp_shared_manifest" |
| 78 | + hash_manifest_for_dir "$package_dir" "$tmp_package_manifest" |
| 79 | + |
| 80 | + cat "$tmp_baseline_manifest" "$tmp_shared_manifest" "$tmp_package_manifest" \ |
| 81 | + | awk -F '\t' 'NF > 0 { print $1 }' \ |
| 82 | + | LC_ALL=C sort -u > "$tmp_union_paths" |
| 83 | + |
| 84 | + local shared_changed=0 |
| 85 | + local package_changed=0 |
| 86 | + local conflict_count=0 |
| 87 | + |
| 88 | + while IFS= read -r rel_path; do |
| 89 | + [ -z "$rel_path" ] && continue |
| 90 | + |
| 91 | + local baseline_hash |
| 92 | + local shared_hash |
| 93 | + local package_hash |
| 94 | + baseline_hash="$(hash_for_path "$tmp_baseline_manifest" "$rel_path")" |
| 95 | + shared_hash="$(hash_for_path "$tmp_shared_manifest" "$rel_path")" |
| 96 | + package_hash="$(hash_for_path "$tmp_package_manifest" "$rel_path")" |
| 97 | + |
| 98 | + if [ "$shared_hash" != "$baseline_hash" ]; then |
| 99 | + shared_changed=1 |
| 100 | + fi |
| 101 | + if [ "$package_hash" != "$baseline_hash" ]; then |
| 102 | + package_changed=1 |
| 103 | + fi |
| 104 | + |
| 105 | + if [ "$shared_hash" != "$package_hash" ] && [ "$shared_hash" != "$baseline_hash" ] && [ "$package_hash" != "$baseline_hash" ]; then |
| 106 | + conflict_count=$((conflict_count + 1)) |
| 107 | + printf "%s (shared-native=%s, package=%s, baseline=%s)\n" \ |
| 108 | + "$rel_path" "$shared_hash" "$package_hash" "$baseline_hash" >> "$tmp_conflicts" |
| 109 | + fi |
| 110 | + done < "$tmp_union_paths" |
| 111 | + |
| 112 | + if [ "$conflict_count" -gt 0 ]; then |
| 113 | + echo "Conflict detected for $package_name [$platform_name]." |
| 114 | + echo "Both shared-native and package mirror changed the same file(s) differently since last sync:" |
| 115 | + cat "$tmp_conflicts" |
| 116 | + rm -f "$tmp_baseline_manifest" "$tmp_shared_manifest" "$tmp_package_manifest" "$tmp_union_paths" "$tmp_conflicts" |
| 117 | + return 1 |
| 118 | + fi |
| 119 | + |
| 120 | + if [ "$package_changed" -eq 1 ] && [ "$shared_changed" -eq 0 ]; then |
| 121 | + sync_dir_contents "$package_dir" "$shared_dir" |
| 122 | + echo "Applied $platform_name sync direction: package -> shared-native ($package_name)" |
| 123 | + elif [ "$shared_changed" -eq 1 ] && [ "$package_changed" -eq 0 ]; then |
| 124 | + sync_dir_contents "$shared_dir" "$package_dir" |
| 125 | + echo "Applied $platform_name sync direction: shared-native -> package ($package_name)" |
| 126 | + fi |
| 127 | + |
| 128 | + hash_manifest_for_dir "$shared_dir" "$baseline_manifest" |
| 129 | + rm -f "$tmp_baseline_manifest" "$tmp_shared_manifest" "$tmp_package_manifest" "$tmp_union_paths" "$tmp_conflicts" |
| 130 | +} |
| 131 | + |
| 132 | +sync_from_package() { |
| 133 | + local package_name="$1" |
| 134 | + local android_package_dir="$ROOT_DIR/$package_name/android/src/main/java/com/streamchatreactnative/shared" |
| 135 | + local ios_package_dir="$ROOT_DIR/$package_name/ios/shared" |
| 136 | + |
| 137 | + sync_platform_with_conflict_detection \ |
| 138 | + "$package_name" \ |
| 139 | + "android" \ |
| 140 | + "$android_package_dir" \ |
| 141 | + "$SHARED_ANDROID_DIR" |
| 142 | + |
| 143 | + sync_platform_with_conflict_detection \ |
| 144 | + "$package_name" \ |
| 145 | + "ios" \ |
| 146 | + "$ios_package_dir" \ |
| 147 | + "$SHARED_IOS_DIR" |
| 148 | +} |
| 149 | + |
| 150 | +case "$TARGET" in |
| 151 | + native-package) |
| 152 | + sync_from_package "native-package" |
| 153 | + ;; |
| 154 | + expo-package) |
| 155 | + sync_from_package "expo-package" |
| 156 | + ;; |
| 157 | + all) |
| 158 | + # Prefer native-package when both are present. |
| 159 | + if [ -d "$ROOT_DIR/native-package/android/src/main/java/com/streamchatreactnative/shared" ] || [ -d "$ROOT_DIR/native-package/ios/shared" ]; then |
| 160 | + sync_from_package "native-package" |
| 161 | + elif [ -d "$ROOT_DIR/expo-package/android/src/main/java/com/streamchatreactnative/shared" ] || [ -d "$ROOT_DIR/expo-package/ios/shared" ]; then |
| 162 | + sync_from_package "expo-package" |
| 163 | + else |
| 164 | + echo "No package shared native sources found to sync from" |
| 165 | + exit 1 |
| 166 | + fi |
| 167 | + ;; |
| 168 | + *) |
| 169 | + echo "Unknown target: $TARGET" |
| 170 | + echo "Expected one of: native-package, expo-package, all" |
| 171 | + exit 1 |
| 172 | + ;; |
| 173 | +esac |
| 174 | + |
| 175 | +echo "Reconciled package/shared-native directories with conflict checks for target: $TARGET" |
0 commit comments