-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
285 lines (248 loc) · 11.4 KB
/
install.sh
File metadata and controls
285 lines (248 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#!/bin/bash
set -euo pipefail
REPO_URL="https://github.com/anthonyivn2/fmapi-codingagent-setup.git"
INSTALL_DIR="${FMAPI_HOME:-${HOME}/.fmapi-codingagent-setup}"
BRANCH=""
USER_SET_REF=false
# ── Colors (respect NO_COLOR) ────────────────────────────────────────────────
BOLD='\033[1m' DIM='\033[2m' GREEN='\033[32m' CYAN='\033[36m' RED='\033[31m' YELLOW='\033[33m' RESET='\033[0m'
if [[ ! -t 1 ]] || [[ -n "${NO_COLOR:-}" ]]; then
BOLD='' DIM='' GREEN='' CYAN='' RED='' YELLOW='' RESET=''
fi
info() { echo -e " ${CYAN}::${RESET} $1"; }
success() { echo -e " ${GREEN}${BOLD}ok${RESET} $1"; }
warn() { echo -e " ${YELLOW}${BOLD}!!${RESET}${YELLOW} $1${RESET}"; }
error() { echo -e "\n ${RED}${BOLD}!! ERROR${RESET}${RED} $1${RESET}\n" >&2; }
CLI_COMMANDS=("setup-fmapi-codex" "setup-fmapi-claudecode")
_print_cli_commands() {
local cli
for cli in "${CLI_COMMANDS[@]}"; do
echo -e " ${CYAN}${cli}${RESET}"
done
}
_print_cli_examples() {
local cli="$1"
echo -e " ${CYAN}${cli}${RESET}"
echo -e " ${CYAN}${cli} --host https://your-workspace.cloud.databricks.com${RESET}"
echo -e " ${CYAN}${cli} self-update${RESET}"
}
_is_tag_ref() {
local repo="$1"
local ref="$2"
git -C "$repo" show-ref --verify --quiet "refs/tags/$ref"
}
_is_remote_tag_ref() {
local repo="$1"
local ref="$2"
git -C "$repo" ls-remote --exit-code --tags origin "refs/tags/$ref" >/dev/null 2>&1
}
_fetch_ref() {
local repo="$1"
local ref="$2"
if _is_remote_tag_ref "$repo" "$ref"; then
git -C "$repo" fetch --quiet origin "+refs/tags/$ref:refs/tags/$ref"
else
git -C "$repo" fetch --quiet origin "$ref"
fi
}
_checkout_ref() {
local repo="$1"
local ref="$2"
if _is_tag_ref "$repo" "$ref"; then
local tag_commit
tag_commit=$(git -C "$repo" rev-parse "refs/tags/$ref^{}")
git -C "$repo" checkout --quiet "$tag_commit" >/dev/null 2>&1
else
git -C "$repo" checkout --quiet "$ref"
fi
}
# ── Helpers ──────────────────────────────────────────────────────────────────
# Detect existing FMAPI config in agent settings
_has_fmapi_config() {
local claude_settings="$HOME/.claude/settings.json"
local codex_settings="$HOME/.codex/config.toml"
if [[ -f "$codex_settings" ]]; then
grep -q "fmapi-key-helper\.sh\|databricks_fmapi\|model_providers" "$codex_settings" 2>/dev/null && return 0
fi
[[ -f "$claude_settings" ]] || return 1
if command -v jq &>/dev/null; then
local helper=""
helper=$(jq -r '.apiKeyHelper // empty' "$claude_settings" 2>/dev/null) || true
[[ -n "$helper" ]] && return 0
return 1
fi
grep -q "apiKeyHelper" "$claude_settings" 2>/dev/null
}
# ── Parse flags ──────────────────────────────────────────────────────────────
while [[ $# -gt 0 ]]; do
case "$1" in
--version)
if [[ -n "$BRANCH" ]]; then error "--version and --branch are mutually exclusive."; exit 1; fi
BRANCH="${2:-}"; if [[ -z "$BRANCH" ]]; then error "--version requires a value."; exit 1; fi
# Accept with or without 'v' prefix
[[ "$BRANCH" != v* ]] && BRANCH="v${BRANCH}"
USER_SET_REF=true; shift 2 ;;
--branch)
if [[ -n "$BRANCH" ]]; then error "--version and --branch are mutually exclusive."; exit 1; fi
BRANCH="${2:-}"; if [[ -z "$BRANCH" ]]; then error "--branch requires a value."; exit 1; fi
USER_SET_REF=true; shift 2 ;;
-h|--help)
echo "Usage: bash <(curl -sL .../install.sh) [OPTIONS]"
echo ""
echo "Options:"
echo " --version VER Install a specific release version (e.g., 0.1.0 or v0.1.0)"
echo " --branch NAME Install from a git branch or tag (e.g., main, v0.1.0)"
echo " -h, --help Show this help"
echo ""
echo "By default, the latest release tag is installed. Falls back to main if"
echo "no releases exist."
echo ""
echo "Environment variables:"
echo " FMAPI_HOME Install location (default: ~/.fmapi-codingagent-setup)"
exit 0 ;;
*) error "Unrecognized option: $1"; exit 1 ;;
esac
done
# ── Check Xcode CLT (macOS only, non-blocking) ─────────────────────────────
if [[ "$(uname -s)" == "Darwin" ]]; then
if xcode-select -p &>/dev/null; then
success "Xcode Command Line Tools installed."
else
warn "Xcode Command Line Tools not found. Fix: xcode-select --install"
fi
fi
# ── Require git ──────────────────────────────────────────────────────────────
if ! command -v git &>/dev/null; then
error "git is required but not installed."
exit 1
fi
echo -e "\n${BOLD} FMAPI Codingagent Setup — Installer${RESET}\n"
# ── Resolve version ─────────────────────────────────────────────────────────
if [[ -z "$BRANCH" ]]; then
# Auto-detect latest release tag via git ls-remote
LATEST_TAG=$(git ls-remote --tags --sort=-v:refname "$REPO_URL" 'v*' 2>/dev/null \
| head -1 | sed 's/.*refs\/tags\///' | sed 's/\^{}//')
if [[ -n "$LATEST_TAG" ]]; then
BRANCH="$LATEST_TAG"
info "Latest release: ${BOLD}${LATEST_TAG}${RESET}"
else
BRANCH="main"
warn "No release tags found. Installing from main."
fi
fi
# ── Clone or update ──────────────────────────────────────────────────────────
IS_UPDATE=false
pre_version=""
post_version=""
if [[ -d "${INSTALL_DIR}/.git" ]]; then
IS_UPDATE=true
# Capture pre-update version
if [[ -f "${INSTALL_DIR}/VERSION" ]]; then
pre_version=$(tr -d '[:space:]' < "${INSTALL_DIR}/VERSION")
fi
info "Existing installation found at ${DIM}${INSTALL_DIR}${RESET}"
info "Updating..."
_fetch_ref "$INSTALL_DIR" "$BRANCH"
_checkout_ref "$INSTALL_DIR" "$BRANCH"
if ! _is_tag_ref "$INSTALL_DIR" "$BRANCH"; then
git -C "$INSTALL_DIR" pull --quiet origin "$BRANCH"
fi
# Capture post-update version
if [[ -f "${INSTALL_DIR}/VERSION" ]]; then
post_version=$(tr -d '[:space:]' < "${INSTALL_DIR}/VERSION")
fi
if [[ -n "$pre_version" && -n "$post_version" && "$pre_version" != "$post_version" ]]; then
success "Updated from v${pre_version} → v${post_version}."
else
success "Already up to date at v${post_version:-${pre_version:-unknown}}."
fi
else
if [[ -d "$INSTALL_DIR" ]]; then
error "${INSTALL_DIR} exists but is not a git repo. Remove it first or set FMAPI_HOME."
exit 1
fi
info "Cloning to ${DIM}${INSTALL_DIR}${RESET}..."
git clone --quiet "$REPO_URL" "$INSTALL_DIR"
_checkout_ref "$INSTALL_DIR" "$BRANCH"
success "Installed."
fi
# ── Verify ───────────────────────────────────────────────────────────────────
# Verify the Python package exists
if [[ ! -f "${INSTALL_DIR}/pyproject.toml" ]]; then
error "Installation verification failed: pyproject.toml not found in ${INSTALL_DIR}."
exit 1
fi
local_version="unknown"
if [[ -f "${INSTALL_DIR}/VERSION" ]]; then
local_version=$(tr -d '[:space:]' < "${INSTALL_DIR}/VERSION")
fi
# ── Install uv if not available ──────────────────────────────────────────────
if command -v uv &>/dev/null; then
success "uv already installed."
else
info "Installing uv (Python package manager) ..."
curl -LsSf https://astral.sh/uv/install.sh | sh
# Ensure uv is on PATH for this session
export PATH="$HOME/.local/bin:$PATH"
if command -v uv &>/dev/null; then
success "uv installed."
else
error "Failed to install uv. Install manually: https://docs.astral.sh/uv/"
exit 1
fi
fi
# ── Python info ──────────────────────────────────────────────────────────────
python_info=$(uv python list --only-installed 2>/dev/null | head -1 || true)
if [[ -n "$python_info" ]]; then
info "Python available: ${DIM}${python_info}${RESET}"
else
info "No Python installation detected — uv will download one automatically."
fi
# ── Install CLI as global tool ───────────────────────────────────────────────
info "Installing CLIs ..."
# Ensure uv tool bin is on PATH for this session
UV_TOOL_BIN_DIR="$(uv tool dir --bin 2>/dev/null || echo "$HOME/.local/bin")"
export PATH="${UV_TOOL_BIN_DIR}:$PATH"
uv tool install "$INSTALL_DIR" --force --reinstall --no-cache --quiet 2>/dev/null || \
uv tool install "$INSTALL_DIR" --force --reinstall --no-cache
success "CLIs installed globally as setup-fmapi-codex and setup-fmapi-claudecode."
# Check if the tool bin directory is on the user's default PATH
if ! echo "$PATH" | tr ':' '\n' | grep -qx "$UV_TOOL_BIN_DIR" 2>/dev/null || \
! command -v setup-fmapi-codex &>/dev/null; then
echo ""
echo -e " ${RED}${BOLD}NOTE:${RESET} ${UV_TOOL_BIN_DIR} may not be on your PATH."
echo -e " Add it to your shell profile to use the CLI in new terminals:"
echo ""
echo -e " ${CYAN}echo 'export PATH=\"${UV_TOOL_BIN_DIR}:\$PATH\"' >> ~/.zshrc${RESET}"
echo -e " ${DIM}(or ~/.bashrc for bash)${RESET}"
echo ""
fi
# ── Print next steps ─────────────────────────────────────────────────────────
echo ""
success "fmapi-codingagent-setup v${local_version} installed to ${INSTALL_DIR}"
echo ""
echo -e " ${BOLD}Available CLI commands:${RESET}"
_print_cli_commands
echo ""
echo -e " ${BOLD}Hint format:${RESET}"
echo -e " ${DIM}<cli-command>${RESET}"
echo -e " ${DIM}<cli-command> --host https://your-workspace.cloud.databricks.com${RESET}"
echo -e " ${DIM}<cli-command> self-update${RESET}"
echo ""
echo -e " ${BOLD}Example using setup-fmapi-codex:${RESET}"
_print_cli_examples "setup-fmapi-codex"
# ── Reinstall hint (update + existing config) ─────────────────────────────────
if [[ "$IS_UPDATE" == true ]] && _has_fmapi_config; then
echo ""
echo -e " ${BOLD}Already configured?${RESET} If needed, re-run refresh manually:"
echo -e " ${DIM}<cli-command>${RESET} ${CYAN}reinstall${RESET}"
fi
# ── Installation files hint ───────────────────────────────────────────────────
echo ""
echo -e " ${DIM}Installation files:${RESET}"
echo -e " ${DIM}Repository clone ${INSTALL_DIR}${RESET}"
echo -e " ${DIM}CLI tools ${UV_TOOL_BIN_DIR}/setup-fmapi-codex, ${UV_TOOL_BIN_DIR}/setup-fmapi-claudecode${RESET}"
echo ""
echo -e " ${DIM}To uninstall everything later:${RESET}"
echo -e " ${DIM}<cli-command>${RESET} ${CYAN}uninstall${RESET}"
echo ""