fix: wrapper scripts for all CLIs — env vars work immediately without source

Problem: `sudo bash install.sh` runs in child process, `export` inside it
never reaches the user's current shell. /etc/environment and /etc/profile.d/
only work for NEW sessions. So `codex`/`gemini`/`qwen` fail with
"Missing environment variable" right after install.

Solution: wrapper scripts that auto-source env file before exec'ing binary.
- codex: /usr/local/bin/codex (wrapper) -> /usr/local/bin/.codex-bin (real)
- gemini: /usr/local/bin/gemini (wrapper) -> node .../dist/index.js
- qwen: /usr/local/bin/qwen (wrapper) -> node .../dist/index.js

Works immediately in ANY shell, no manual `source` needed.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
delta-cloud-208e
2026-03-08 10:51:56 +00:00
parent be048ee873
commit d37772d67c
6 changed files with 260 additions and 98 deletions

View File

@@ -1,15 +1,19 @@
#!/usr/bin/env bash
# Qwen Code — Updater
# Re-installs latest version from registry + re-applies patches.
# Uses wrapper script so env vars work immediately in any shell.
#
# Usage: sudo bash uqwen_update.sh
set -euo pipefail
GITEA_TOKEN="${GITEA_TOKEN:-cadffcb0a6a3be728ac1ff619bb40c86588f6837}"
REGISTRY_URL="https://npm.sensey24.ru/"
NPM_SCOPE="@qwen-code"
NPM_PACKAGE="@qwen-code/qwen-code"
REPO_RAW="https://git.sensey24.ru/aibot777/unlimitedcoding/raw/branch/master/qwen"
ENV_FILE="/etc/profile.d/qwen-code.sh"
GREEN="\033[92m"
CYAN="\033[96m"
YELLOW="\033[93m"
@@ -22,6 +26,42 @@ info() { echo -e "${CYAN}[i]${RESET} $*"; }
warn() { echo -e "${YELLOW}[~]${RESET} $*"; }
err() { echo -e "${RED}[!]${RESET} $*" >&2; }
create_wrapper() {
local bin_name="$1"
local bin_path
bin_path=$(command -v "$bin_name" 2>/dev/null || echo "/usr/local/bin/$bin_name")
local npm_root
npm_root=$(npm root -g 2>/dev/null || echo "/usr/lib/node_modules")
local real_bin
real_bin=$(readlink -f "$bin_path" 2>/dev/null || true)
if [ -z "$real_bin" ] || [ "$real_bin" = "$bin_path" ] || [ ! -f "$real_bin" ]; then
for candidate_pkg in "@qwen-code/qwen-code" "qwen-code"; do
for entry in "dist/index.js" "bin/index.js" "index.js"; do
if [ -f "$npm_root/$candidate_pkg/$entry" ]; then
real_bin="$npm_root/$candidate_pkg/$entry"
break 2
fi
done
done
fi
if [ -z "$real_bin" ] || [ ! -f "$real_bin" ]; then
warn "Could not find $bin_name entry point"
return 1
fi
rm -f "$bin_path"
cat > "$bin_path" << WEOF
#!/usr/bin/env bash
[ -f $ENV_FILE ] && . $ENV_FILE
exec node "$real_bin" "\$@"
WEOF
chmod +x "$bin_path"
log "Wrapper: $bin_path -> $real_bin"
}
echo -e "${BOLD}"
echo " +--------------------------------------+"
echo " | Qwen Code — Updater |"
@@ -69,9 +109,6 @@ for candidate in qwen qwen-code; do
fi
done
NEW_VER=$($QWEN_BIN --version 2>/dev/null || echo "unknown")
log "Version: $OLD_VER$NEW_VER"
# ---- Download and apply patches ----
TEMP_DIR=$(mktemp -d)
@@ -79,7 +116,6 @@ cleanup() { rm -rf "$TEMP_DIR" 2>/dev/null || true; }
trap cleanup EXIT
info "Downloading patcher..."
GITEA_TOKEN="${GITEA_TOKEN:-cadffcb0a6a3be728ac1ff619bb40c86588f6837}"
curl -fsSL -H "Authorization: token ${GITEA_TOKEN}" "$REPO_RAW/qwen_patcher.py" -o "$TEMP_DIR/qwen_patcher.py"
curl -fsSL -H "Authorization: token ${GITEA_TOKEN}" "$REPO_RAW/qwen_config.json" -o "$TEMP_DIR/qwen_config.json"
@@ -92,7 +128,8 @@ if [ $PATCH_EXIT -ne 0 ]; then
python3 "$TEMP_DIR/qwen_patcher.py" --apply --config "$TEMP_DIR/qwen_config.json"
fi
# Set env vars system-wide (all users, all sessions)
# ---- Set env vars system-wide ----
API_KEY=$(python3 -c "import json; print(json.load(open('$TEMP_DIR/qwen_config.json'))['api_key'])")
BASE_URL=$(python3 -c "import json; print(json.load(open('$TEMP_DIR/qwen_config.json'))['base_url'])")
@@ -106,17 +143,21 @@ for kv in "QWEN_API_KEY=\"$API_KEY\"" "QWEN_BASE_URL=\"$BASE_URL\""; do
fi
done
cat > /etc/profile.d/qwen-code.sh << PROF_EOF
cat > "$ENV_FILE" << PROF_EOF
export QWEN_API_KEY="$API_KEY"
export QWEN_BASE_URL="$BASE_URL"
PROF_EOF
chmod 644 /etc/profile.d/qwen-code.sh
chmod 644 "$ENV_FILE"
export QWEN_API_KEY="$API_KEY"
export QWEN_BASE_URL="$BASE_URL"
# ---- Create wrapper (auto-loads env) ----
info "Env vars set system-wide (/etc/environment + /etc/profile.d/qwen-code.sh)"
if [ -n "$QWEN_BIN" ]; then
create_wrapper "$QWEN_BIN"
fi
NEW_VER=$($QWEN_BIN --version 2>/dev/null || echo "unknown")
log "Version: $OLD_VER -> $NEW_VER"
log "Update complete!"
echo -e "For current shell: ${CYAN}source /etc/profile.d/qwen-code.sh${RESET}"
echo -e "Qwen works immediately — no need to source anything."
echo ""