|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +output_root=".." |
| 5 | +matrix_path="internal_dependency_matrix.json" |
| 6 | + |
| 7 | +usage() { |
| 8 | + cat <<'EOF' |
| 9 | +Usage: checkout_internal_dependency_consumers.sh [--output-root PATH] [--matrix PATH] |
| 10 | +
|
| 11 | +Clone QuantStrategyLab consumer repositories referenced by the internal dependency matrix. |
| 12 | +EOF |
| 13 | +} |
| 14 | + |
| 15 | +while [ "$#" -gt 0 ]; do |
| 16 | + case "$1" in |
| 17 | + --output-root) |
| 18 | + output_root="${2:?--output-root requires a path}" |
| 19 | + shift 2 |
| 20 | + ;; |
| 21 | + --matrix) |
| 22 | + matrix_path="${2:?--matrix requires a path}" |
| 23 | + shift 2 |
| 24 | + ;; |
| 25 | + -h|--help) |
| 26 | + usage |
| 27 | + exit 0 |
| 28 | + ;; |
| 29 | + *) |
| 30 | + echo "Unknown argument: $1" >&2 |
| 31 | + usage >&2 |
| 32 | + exit 1 |
| 33 | + ;; |
| 34 | + esac |
| 35 | +done |
| 36 | + |
| 37 | +if ! command -v gh >/dev/null 2>&1; then |
| 38 | + echo "gh CLI is required to checkout internal dependency consumer repos." >&2 |
| 39 | + exit 1 |
| 40 | +fi |
| 41 | + |
| 42 | +if [ -z "${GH_TOKEN:-}" ] && [ -z "${GITHUB_TOKEN:-}" ]; then |
| 43 | + echo "GH_TOKEN or GITHUB_TOKEN is required to checkout internal dependency consumer repos." >&2 |
| 44 | + exit 1 |
| 45 | +fi |
| 46 | + |
| 47 | +export GH_TOKEN="${GH_TOKEN:-${GITHUB_TOKEN}}" |
| 48 | + |
| 49 | +script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 50 | +repo_root="$(cd "${script_dir}/.." && pwd)" |
| 51 | +matrix_file="${matrix_path}" |
| 52 | +if [ ! -f "${matrix_file}" ]; then |
| 53 | + matrix_file="${repo_root}/${matrix_path}" |
| 54 | +fi |
| 55 | +if [ ! -f "${matrix_file}" ]; then |
| 56 | + echo "Matrix file not found: ${matrix_path}" >&2 |
| 57 | + exit 1 |
| 58 | +fi |
| 59 | + |
| 60 | +mkdir -p "${output_root}" |
| 61 | +output_root="$(cd "${output_root}" && pwd)" |
| 62 | + |
| 63 | +mapfile -t consumer_repos < <( |
| 64 | + python3 - "${matrix_file}" <<'PY' |
| 65 | +import json |
| 66 | +import sys |
| 67 | +from pathlib import Path |
| 68 | +
|
| 69 | +payload = json.loads(Path(sys.argv[1]).read_text(encoding="utf-8")) |
| 70 | +repos = sorted( |
| 71 | + { |
| 72 | + item["consumer_repo"] |
| 73 | + for item in payload.get("dependencies", []) |
| 74 | + if isinstance(item, dict) and item.get("consumer_repo") |
| 75 | + } |
| 76 | +) |
| 77 | +for repo in repos: |
| 78 | + print(repo) |
| 79 | +PY |
| 80 | +) |
| 81 | + |
| 82 | +for consumer_repo in "${consumer_repos[@]}"; do |
| 83 | + target_dir="${output_root}/${consumer_repo}" |
| 84 | + if [ -d "${target_dir}/.git" ]; then |
| 85 | + echo "Already checked out ${consumer_repo} at ${target_dir}" |
| 86 | + continue |
| 87 | + fi |
| 88 | + echo "Cloning QuantStrategyLab/${consumer_repo} into ${target_dir}" |
| 89 | + gh repo clone "QuantStrategyLab/${consumer_repo}" "${target_dir}" -- --depth 1 --branch main |
| 90 | +done |
| 91 | + |
| 92 | +echo "Checked out ${#consumer_repos[@]} internal dependency consumer repositories under ${output_root}." |
0 commit comments