3c111887d1
setup-git-ssh.sh работает без declare -A; Set-GitRemotes не дублирует .git и не падает на пустом legacy; shell-скрипты executable.
143 lines
3.9 KiB
Bash
Executable File
143 lines
3.9 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# Инициализация Cursor в текущем проекте; на новом хосте сначала настраивает git/SSH.
|
|
|
|
get_cursor_rules_dest() {
|
|
printf '%s' "$HOME/CursorRules"
|
|
}
|
|
|
|
get_cursor_bootstrap_root() {
|
|
local rules_dest script_dir
|
|
rules_dest="$(get_cursor_rules_dest)"
|
|
script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
|
|
if [[ -f "$script_dir/scripts/invoke-machine-bootstrap.sh" ]]; then
|
|
printf '%s' "$script_dir"
|
|
return 0
|
|
fi
|
|
if [[ -f "$rules_dest/local/git-ssh-hosts.conf" ]]; then
|
|
printf '%s' "$rules_dest"
|
|
return 0
|
|
fi
|
|
if [[ -f "$script_dir/local/git-ssh-hosts.conf" ]]; then
|
|
printf '%s' "$script_dir"
|
|
return 0
|
|
fi
|
|
return 1
|
|
}
|
|
|
|
initialize_cursor_rules_environment() {
|
|
local update=0
|
|
[[ "${1:-}" == '--update' ]] && update=1
|
|
|
|
local rules_dest bootstrap_root
|
|
rules_dest="$(get_cursor_rules_dest)"
|
|
bootstrap_root="$(get_cursor_bootstrap_root)" || {
|
|
echo 'Error: CursorRules not found. Run install.sh from Gitea first:' >&2
|
|
echo ' curl -fsSL https://git.kalinamall.ru/PapaTramp/Rules/raw/branch/main/install.sh | bash' >&2
|
|
return 1
|
|
}
|
|
|
|
local skip_clone=0
|
|
[[ -d "$rules_dest/.git" ]] && skip_clone=1
|
|
|
|
ROOT="$bootstrap_root" RULES_DEST="$rules_dest" SKIP_CLONE="$skip_clone" \
|
|
bash "$bootstrap_root/scripts/invoke-machine-bootstrap.sh"
|
|
|
|
if [[ "$update" -eq 1 && -d "$rules_dest/.git" ]]; then
|
|
echo '[*] Updating CursorRules from remote...'
|
|
if git -C "$rules_dest" pull --ff-only; then
|
|
echo '[+] CursorRules updated'
|
|
else
|
|
echo '[!] git pull failed in CursorRules' >&2
|
|
fi
|
|
fi
|
|
}
|
|
|
|
install_cursor_in_current_project() {
|
|
local skip_git_remotes=0
|
|
while [[ $# -gt 0 ]]; do
|
|
case "$1" in
|
|
--skip-git-remotes) skip_git_remotes=1; shift ;;
|
|
*) shift ;;
|
|
esac
|
|
done
|
|
|
|
local source rules_src scripts
|
|
source="$(get_cursor_rules_dest)"
|
|
rules_src="$source/.cursor/rules"
|
|
scripts="$source/scripts"
|
|
|
|
if [[ -f "$source/.cursorignore" ]]; then
|
|
cp -f "$source/.cursorignore" .
|
|
echo '[+] .cursorignore copied'
|
|
fi
|
|
|
|
if [[ -d "$rules_src" ]]; then
|
|
mkdir -p ./.cursor/rules
|
|
cp -f "$rules_src"/*.mdc ./.cursor/rules/
|
|
echo '[+] .cursor/rules (*.mdc) copied'
|
|
fi
|
|
|
|
if [[ -d .git ]]; then
|
|
local gitignore='.gitignore' line
|
|
for line in '.cursor/' '.cursorignore'; do
|
|
if [[ ! -f "$gitignore" ]]; then
|
|
printf '%s\n' "$line" > "$gitignore"
|
|
echo "[+] .gitignore created ($line)"
|
|
elif ! grep -Fxq "$line" "$gitignore" 2>/dev/null; then
|
|
printf '%s\n' "$line" >> "$gitignore"
|
|
echo "[+] .gitignore updated ($line)"
|
|
fi
|
|
done
|
|
fi
|
|
|
|
if [[ "$skip_git_remotes" -eq 1 ]]; then
|
|
echo '[!] Git remotes skipped'
|
|
return 0
|
|
fi
|
|
|
|
if ! git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
|
|
echo '[!] Not a git repo - remotes skipped'
|
|
return 0
|
|
fi
|
|
|
|
if [[ -f "$scripts/Set-GitRemotes.sh" ]]; then
|
|
echo '[*] Configuring git remotes...'
|
|
bash "$scripts/Set-GitRemotes.sh" || true
|
|
fi
|
|
|
|
if [[ -f "$scripts/Ensure-GiteaHomeRepo.ps1" ]]; then
|
|
local repo_name
|
|
repo_name="$(basename "$(git rev-parse --show-toplevel)")"
|
|
echo "[*] Ensuring home Gitea repo: $repo_name"
|
|
if command -v pwsh >/dev/null 2>&1; then
|
|
pwsh -NoProfile -File "$scripts/Ensure-GiteaHomeRepo.ps1" -RepoName "$repo_name" || true
|
|
fi
|
|
fi
|
|
}
|
|
|
|
init-cursor() {
|
|
local update=0
|
|
local skip_git_remotes=0
|
|
while [[ $# -gt 0 ]]; do
|
|
case "$1" in
|
|
-Update|--update) update=1; shift ;;
|
|
-SkipGitRemotes|--skip-git-remotes) skip_git_remotes=1; shift ;;
|
|
*) shift ;;
|
|
esac
|
|
done
|
|
|
|
if [[ "$update" -eq 1 ]]; then
|
|
initialize_cursor_rules_environment --update || return 1
|
|
else
|
|
initialize_cursor_rules_environment || return 1
|
|
fi
|
|
|
|
if [[ "$skip_git_remotes" -eq 1 ]]; then
|
|
install_cursor_in_current_project --skip-git-remotes
|
|
else
|
|
install_cursor_in_current_project
|
|
fi
|
|
echo '[+] init-cursor complete'
|
|
}
|