Files
Logic/add_submodules.sh
2026-04-27 04:31:32 +03:30

98 lines
2.0 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
BASE_URL="ssh://git@git.crop-logic.ir:2222/sajad-dev"
TARGET_BRANCH="${TARGET_BRANCH:-develop}"
FORCE_SUBMODULE_ADD=false
MODULES=(
"Ai"
"SensorHub"
"Backend"
"Accsess"
"Schemas"
)
usage() {
cat <<'EOF'
Usage: ./add_submodules.sh [--force|-f]
Options:
-f, --force Pass --force to `git submodule add`
EOF
}
while [[ "$#" -gt 0 ]]; do
case "$1" in
-f|--force)
FORCE_SUBMODULE_ADD=true
;;
-h|--help)
usage
exit 0
;;
*)
echo "Unknown argument: $1" >&2
usage >&2
exit 1
;;
esac
shift
done
ensure_gitmodules_file() {
if [[ ! -f ".gitmodules" ]]; then
touch .gitmodules
echo "Created empty .gitmodules"
fi
}
checkout_target_branch() {
local repo_dir="$1"
git -C "$repo_dir" fetch origin "$TARGET_BRANCH"
if git -C "$repo_dir" show-ref --verify --quiet "refs/heads/${TARGET_BRANCH}"; then
git -C "$repo_dir" checkout "$TARGET_BRANCH"
else
git -C "$repo_dir" checkout -b "$TARGET_BRANCH" "origin/${TARGET_BRANCH}"
fi
}
ensure_gitmodules_file
for module in "${MODULES[@]}"; do
repo_url="${BASE_URL}/${module}.git"
add_args=(-b "$TARGET_BRANCH")
if [[ "$FORCE_SUBMODULE_ADD" == true ]]; then
add_args+=(--force)
fi
if git config --file .gitmodules --get-regexp "submodule\\..*\\.path" 2>/dev/null | awk '{print $2}' | grep -Fxq "$module"; then
echo "Skipping ${module}: submodule already exists."
else
echo "Adding ${module} from ${repo_url} on branch ${TARGET_BRANCH}"
git submodule add "${add_args[@]}" "$repo_url"
fi
checkout_target_branch "$module"
echo "Checked out ${module} on ${TARGET_BRANCH}"
env_example_path="${module}/.env.example"
env_path="${module}/.env"
if [[ -f "$env_example_path" ]]; then
if [[ -f "$env_path" ]]; then
echo "Skipping ${module}: .env already exists."
else
cp "$env_example_path" "$env_path"
echo "Created ${env_path} from .env.example"
fi
else
echo "Skipping ${module}: .env.example not found."
fi
done
echo "Done."