|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +set -euo pipefail |
| 4 | + |
| 5 | +# Copies the repo-root .env (and other root-level machine-local config, if |
| 6 | +# present) from the main checkout into the current git worktree, so that a |
| 7 | +# subsequent `dev up` can regenerate all nested sample app configuration from it. |
| 8 | +# |
| 9 | +# Runs automatically as a `dev up` step. With --check it reports (without |
| 10 | +# copying) whether the current worktree is missing any root env files: exit 0 |
| 11 | +# when nothing is needed (main checkout, or already seeded) and non-zero when a |
| 12 | +# copy is required, so it can serve as a `dev up` met? probe. |
| 13 | +# |
| 14 | +# This command prints file paths and status only; it never prints configured |
| 15 | +# values. |
| 16 | + |
| 17 | +mode="copy" |
| 18 | +if [[ "${1:-}" == "--check" ]]; then |
| 19 | + mode="check" |
| 20 | +elif [[ $# -gt 0 ]]; then |
| 21 | + echo "Usage: scripts/copy_worktree_env [--check]" >&2 |
| 22 | + exit 1 |
| 23 | +fi |
| 24 | + |
| 25 | +CURRENT_ROOT="$(git rev-parse --show-toplevel)" |
| 26 | +COMMON_GIT_DIR="$(git rev-parse --path-format=absolute --git-common-dir)" |
| 27 | +MAIN_ROOT="$(dirname "${COMMON_GIT_DIR}")" |
| 28 | + |
| 29 | +if [[ "${CURRENT_ROOT}" == "${MAIN_ROOT}" ]]; then |
| 30 | + # In the main checkout there is never anything to copy. |
| 31 | + [[ "${mode}" == "check" ]] && exit 0 |
| 32 | + echo "Already in the main checkout (${CURRENT_ROOT}); nothing to copy." |
| 33 | + exit 0 |
| 34 | +fi |
| 35 | + |
| 36 | +# Root-level, gitignored source-of-truth files. Nested sample config |
| 37 | +# (Android/Swift/RN) is intentionally omitted: `dev up` regenerates it from the |
| 38 | +# root .env via scripts/setup_storefront_env. |
| 39 | +ROOT_FILES=( |
| 40 | + ".env" |
| 41 | + ".dev.env" |
| 42 | + "local.properties" |
| 43 | +) |
| 44 | + |
| 45 | +copied_any="false" |
| 46 | + |
| 47 | +for rel in "${ROOT_FILES[@]}"; do |
| 48 | + src="${MAIN_ROOT}/${rel}" |
| 49 | + dest="${CURRENT_ROOT}/${rel}" |
| 50 | + |
| 51 | + if [[ ! -e "${src}" ]]; then |
| 52 | + continue |
| 53 | + fi |
| 54 | + |
| 55 | + if [[ -e "${dest}" ]]; then |
| 56 | + [[ "${mode}" == "check" ]] && continue |
| 57 | + echo "skip (exists): ${rel}" |
| 58 | + continue |
| 59 | + fi |
| 60 | + |
| 61 | + # A source file exists in the main checkout but is missing here. |
| 62 | + if [[ "${mode}" == "check" ]]; then |
| 63 | + echo "missing: ${rel}" |
| 64 | + exit 1 |
| 65 | + fi |
| 66 | + |
| 67 | + cp "${src}" "${dest}" |
| 68 | + echo "copied: ${rel}" |
| 69 | + copied_any="true" |
| 70 | +done |
| 71 | + |
| 72 | +# Check mode reached here with nothing missing: the worktree is already seeded. |
| 73 | +[[ "${mode}" == "check" ]] && exit 0 |
| 74 | + |
| 75 | +if [[ "${copied_any}" != "true" ]]; then |
| 76 | + echo "No new root env files to copy." |
| 77 | +fi |
| 78 | + |
| 79 | +echo "Next: run \`dev up\` in this worktree to regenerate sample app config." |
0 commit comments