fix(codex): macOS compatibility for install/update/uninstall scripts
- Replace grep -oP (Perl regex) with portable sed — fixes "grep: invalid option -- P" on macOS - Add sedi() wrapper for cross-platform sed -i (BSD vs GNU) - Detect macOS via uname and use apple-darwin binary suffix instead of linux-musl - Add is_native_binary() helper: checks both ELF (Linux) and Mach-O (macOS) - macOS env vars: use launchctl setenv + /etc/codex-env.sh + ~/.zshrc source line - Linux env vars: keep /etc/environment + /etc/profile.d/ as before - Wrapper script uses dynamic ENV_FILE path instead of hardcoded /etc/profile.d/ - Fix SUDO_USER handling for correct ~/.zshrc path when run via sudo - Uninstaller: also remove .codex-bin, /etc/codex-env.sh, launchctl vars, rc file entries - Uninstaller: scan /Users/* on macOS instead of /home/* - Fix CRLF line endings in ucodex_uninstall.sh Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -13,9 +13,32 @@ GITEA_TOKEN="${GITEA_TOKEN:-cadffcb0a6a3be728ac1ff619bb40c86588f6837}"
|
||||
REPO_RAW="https://git.sensey24.ru/aibot777/unlimitedcoding/raw/branch/master/codex"
|
||||
GITHUB_API="https://api.github.com/repos/openai/codex/releases/latest"
|
||||
|
||||
OS="$(uname -s)"
|
||||
IS_MACOS=false
|
||||
[ "$OS" = "Darwin" ] && IS_MACOS=true
|
||||
|
||||
CODEX_BIN="/usr/local/bin/.codex-bin"
|
||||
CODEX_WRAPPER="/usr/local/bin/codex"
|
||||
ENV_FILE="/etc/profile.d/codex-env.sh"
|
||||
if $IS_MACOS; then
|
||||
ENV_FILE="/etc/codex-env.sh"
|
||||
else
|
||||
ENV_FILE="/etc/profile.d/codex-env.sh"
|
||||
fi
|
||||
|
||||
# Cross-platform sed -i (macOS BSD sed requires -i '' while GNU sed uses -i)
|
||||
sedi() {
|
||||
if $IS_MACOS; then
|
||||
sed -i '' "$@"
|
||||
else
|
||||
sed -i "$@"
|
||||
fi
|
||||
}
|
||||
|
||||
# Cross-platform: check if file is a native binary (ELF on Linux, Mach-O on macOS)
|
||||
is_native_binary() {
|
||||
local f="$1"
|
||||
[ -f "$f" ] && file "$f" | grep -qE "ELF|Mach-O"
|
||||
}
|
||||
|
||||
RED='\033[0;31m'
|
||||
GREEN='\033[0;32m'
|
||||
@@ -71,13 +94,21 @@ log "Python3 $(python3 --version | awk '{print $2}')"
|
||||
echo -e "\n${BOLD}Step 1: Installing Codex CLI binary...${NC}"
|
||||
|
||||
ARCH=$(uname -m)
|
||||
case "$ARCH" in
|
||||
x86_64) BINARY_SUFFIX="x86_64-unknown-linux-musl" ;;
|
||||
aarch64|arm64) BINARY_SUFFIX="aarch64-unknown-linux-musl" ;;
|
||||
*) err "Unsupported architecture: $ARCH"; exit 1 ;;
|
||||
esac
|
||||
if $IS_MACOS; then
|
||||
case "$ARCH" in
|
||||
x86_64) BINARY_SUFFIX="x86_64-apple-darwin" ;;
|
||||
arm64|aarch64) BINARY_SUFFIX="aarch64-apple-darwin" ;;
|
||||
*) err "Unsupported architecture: $ARCH"; exit 1 ;;
|
||||
esac
|
||||
else
|
||||
case "$ARCH" in
|
||||
x86_64) BINARY_SUFFIX="x86_64-unknown-linux-musl" ;;
|
||||
aarch64|arm64) BINARY_SUFFIX="aarch64-unknown-linux-musl" ;;
|
||||
*) err "Unsupported architecture: $ARCH"; exit 1 ;;
|
||||
esac
|
||||
fi
|
||||
|
||||
LATEST_VER=$(curl -s "$GITHUB_API" | grep -oP '"tag_name":\s*"rust-v\K[0-9]+\.[0-9]+\.[0-9]+' | head -1)
|
||||
LATEST_VER=$(curl -s "$GITHUB_API" | sed -n 's/.*"tag_name":[[:space:]]*"rust-v\([0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\)".*/\1/p' | head -1)
|
||||
if [ -z "$LATEST_VER" ]; then
|
||||
err "Could not fetch latest version from GitHub"
|
||||
exit 1
|
||||
@@ -85,7 +116,7 @@ fi
|
||||
info "Latest version: $LATEST_VER ($BINARY_SUFFIX)"
|
||||
|
||||
# Migrate: if old codex is a real binary (not our wrapper), move it
|
||||
if [ -f "$CODEX_WRAPPER" ] && file "$CODEX_WRAPPER" | grep -q "ELF"; then
|
||||
if is_native_binary "$CODEX_WRAPPER"; then
|
||||
info "Migrating existing binary to $CODEX_BIN..."
|
||||
mv -f "$CODEX_WRAPPER" "$CODEX_BIN"
|
||||
fi
|
||||
@@ -125,7 +156,7 @@ else
|
||||
fi
|
||||
|
||||
# Create temporary wrapper so patcher can find codex via `which codex`
|
||||
if [ -f "$CODEX_BIN" ] && { [ ! -f "$CODEX_WRAPPER" ] || file "$CODEX_WRAPPER" | grep -q "ELF"; }; then
|
||||
if [ -f "$CODEX_BIN" ] && { [ ! -f "$CODEX_WRAPPER" ] || is_native_binary "$CODEX_WRAPPER"; }; then
|
||||
cat > "$CODEX_WRAPPER" << 'TWEOF'
|
||||
#!/usr/bin/env bash
|
||||
exec /usr/local/bin/.codex-bin "$@"
|
||||
@@ -158,35 +189,70 @@ echo -e "\n${BOLD}Step 3: Setting environment variables...${NC}"
|
||||
API_KEY=$(python3 -c "import json; print(json.load(open('$INSTALL_DIR/codex_config.json'))['api_key'])")
|
||||
BASE_URL=$(python3 -c "import json; print(json.load(open('$INSTALL_DIR/codex_config.json'))['base_url'])")
|
||||
|
||||
# /etc/environment (all users, all sessions including cron)
|
||||
ETC_ENV="/etc/environment"
|
||||
for kv in "OPENAI_API_KEY=\"$API_KEY\"" "OPENAI_BASE_URL=\"${BASE_URL}/v1\""; do
|
||||
KEY="${kv%%=*}"
|
||||
if grep -q "^${KEY}=" "$ETC_ENV" 2>/dev/null; then
|
||||
sed -i "s|^${KEY}=.*|${kv}|" "$ETC_ENV"
|
||||
else
|
||||
echo "$kv" >> "$ETC_ENV"
|
||||
fi
|
||||
done
|
||||
log "Env vars written to $ETC_ENV"
|
||||
if $IS_MACOS; then
|
||||
# macOS: no /etc/environment, no /etc/profile.d
|
||||
# Set launchctl env vars (GUI apps + new shells)
|
||||
launchctl setenv OPENAI_API_KEY "$API_KEY" 2>/dev/null || true
|
||||
launchctl setenv OPENAI_BASE_URL "${BASE_URL}/v1" 2>/dev/null || true
|
||||
log "Env vars set via launchctl"
|
||||
|
||||
# /etc/profile.d/ (login shells)
|
||||
cat > "$ENV_FILE" << ENVEOF
|
||||
# Also write env file that wrapper will source
|
||||
cat > "$ENV_FILE" << ENVEOF
|
||||
export OPENAI_API_KEY="$API_KEY"
|
||||
export OPENAI_BASE_URL="${BASE_URL}/v1"
|
||||
ENVEOF
|
||||
chmod 644 "$ENV_FILE"
|
||||
log "Env file: $ENV_FILE"
|
||||
chmod 644 "$ENV_FILE"
|
||||
log "Env file: $ENV_FILE"
|
||||
|
||||
# Add to shell rc files for interactive shells (macOS default is zsh)
|
||||
# When run via sudo, SUDO_USER has the real user; HOME may be /var/root
|
||||
REAL_HOME="$HOME"
|
||||
if [ -n "${SUDO_USER:-}" ] && [ "$SUDO_USER" != "root" ]; then
|
||||
REAL_HOME=$(eval echo "~$SUDO_USER")
|
||||
fi
|
||||
for rc_file in "$REAL_HOME/.zshrc" "$REAL_HOME/.bashrc"; do
|
||||
if [ -f "$rc_file" ] || [ "$rc_file" = "$REAL_HOME/.zshrc" ]; then
|
||||
# Remove old entries first
|
||||
if [ -f "$rc_file" ]; then
|
||||
sedi '/# Codex env/d' "$rc_file"
|
||||
sedi '/codex-env\.sh/d' "$rc_file"
|
||||
fi
|
||||
echo "[ -f $ENV_FILE ] && . $ENV_FILE # Codex env" >> "$rc_file"
|
||||
log "Added source to $rc_file"
|
||||
fi
|
||||
done
|
||||
else
|
||||
# Linux: /etc/environment (all users, all sessions including cron)
|
||||
ETC_ENV="/etc/environment"
|
||||
for kv in "OPENAI_API_KEY=\"$API_KEY\"" "OPENAI_BASE_URL=\"${BASE_URL}/v1\""; do
|
||||
KEY="${kv%%=*}"
|
||||
if grep -q "^${KEY}=" "$ETC_ENV" 2>/dev/null; then
|
||||
sedi "s|^${KEY}=.*|${kv}|" "$ETC_ENV"
|
||||
else
|
||||
echo "$kv" >> "$ETC_ENV"
|
||||
fi
|
||||
done
|
||||
log "Env vars written to $ETC_ENV"
|
||||
|
||||
# /etc/profile.d/ (login shells)
|
||||
mkdir -p /etc/profile.d
|
||||
cat > "$ENV_FILE" << ENVEOF
|
||||
export OPENAI_API_KEY="$API_KEY"
|
||||
export OPENAI_BASE_URL="${BASE_URL}/v1"
|
||||
ENVEOF
|
||||
chmod 644 "$ENV_FILE"
|
||||
log "Env file: $ENV_FILE"
|
||||
fi
|
||||
|
||||
# ---- Step 4: Create wrapper (auto-loads env) ----
|
||||
|
||||
echo -e "\n${BOLD}Step 4: Creating wrapper...${NC}"
|
||||
|
||||
cat > "$CODEX_WRAPPER" << 'WRAPPER_EOF'
|
||||
cat > "$CODEX_WRAPPER" << WRAPPER_EOF
|
||||
#!/usr/bin/env bash
|
||||
# Auto-generated wrapper — loads env vars before running codex binary
|
||||
[ -f /etc/profile.d/codex-env.sh ] && . /etc/profile.d/codex-env.sh
|
||||
exec /usr/local/bin/.codex-bin "$@"
|
||||
[ -f ${ENV_FILE} ] && . ${ENV_FILE}
|
||||
exec /usr/local/bin/.codex-bin "\$@"
|
||||
WRAPPER_EOF
|
||||
chmod +x "$CODEX_WRAPPER"
|
||||
log "Wrapper: $CODEX_WRAPPER -> $CODEX_BIN"
|
||||
|
||||
Reference in New Issue
Block a user