From e1d67c07b6a85e64702bc17dcd7398f84b5c90f9 Mon Sep 17 00:00:00 2001 From: somew1nd Date: Fri, 4 Sep 2026 11:47:19 -0700 Subject: [PATCH] fix: support interactive privilege prompts and zero-admin user-space fallbacks --- scripts/install_deps.ps1 | 99 +++++++++++++++++++- scripts/install_deps.sh | 197 ++++++++++++++++++++++++++++----------- scripts/start.ps1 | 102 +++++++++++++++++++- start.sh | 175 ++++++++++++++++++++++++++++------ 4 files changed, 481 insertions(+), 92 deletions(-) diff --git a/scripts/install_deps.ps1 b/scripts/install_deps.ps1 index 0472608..11e519d 100644 --- a/scripts/install_deps.ps1 +++ b/scripts/install_deps.ps1 @@ -43,6 +43,10 @@ function Update-EnvironmentPath { "$env:ProgramFiles\nodejs", "${env:ProgramFiles(x86)}\nodejs", "$env:APPDATA\npm", + "$env:LOCALAPPDATA\Programs\platform-tools", + "$env:LOCALAPPDATA\Programs\node", + "$env:USERPROFILE\.local\share\platform-tools", + "$env:USERPROFILE\.local\share\node", "C:\ProgramData\chocolatey\bin", "$env:USERPROFILE\scoop\shims", "$env:USERPROFILE\.local\bin", @@ -59,6 +63,62 @@ function Test-CommandExists { return ($null -ne $res) } +function Install-PortablePlatformTools { + $ptDir = "$env:LOCALAPPDATA\Programs\platform-tools" + if (Test-Path "$ptDir\adb.exe") { + $env:PATH = "$ptDir;$env:PATH" + return $true + } + Write-Host " [INFO] Installing Android platform-tools (adb) in user space..." -ForegroundColor Cyan + try { + $zipPath = "$env:TEMP\platform-tools-windows.zip" + Invoke-WebRequest -Uri "https://dl.google.com/android/repository/platform-tools-latest-windows.zip" -OutFile $zipPath -UseBasicParsing + New-Item -ItemType Directory -Path "$env:LOCALAPPDATA\Programs" -Force | Out-Null + Expand-Archive -Path $zipPath -DestinationPath "$env:LOCALAPPDATA\Programs" -Force + Remove-Item $zipPath -Force -ErrorAction SilentlyContinue + if (Test-Path "$ptDir\adb.exe") { + $env:PATH = "$ptDir;$env:PATH" + Write-Host " ✔ adb installed in user space." -ForegroundColor Green + return $true + } + } catch { + Write-Host " ⚠ Failed to install portable platform-tools: $_" -ForegroundColor DarkYellow + } + return $false +} + +function Install-PortableNode { + $nodeDir = "$env:LOCALAPPDATA\Programs\node" + if (Test-Path "$nodeDir\node.exe") { + $env:PATH = "$nodeDir;$env:PATH" + return $true + } + Write-Host " [INFO] Installing portable Node.js LTS in user space..." -ForegroundColor Cyan + try { + $zipPath = "$env:TEMP\node-v20-win-x64.zip" + Invoke-WebRequest -Uri "https://nodejs.org/dist/v20.18.3/node-v20.18.3-win-x64.zip" -OutFile $zipPath -UseBasicParsing + $extractDir = "$env:TEMP\node_extract" + New-Item -ItemType Directory -Path $extractDir -Force | Out-Null + Expand-Archive -Path $zipPath -DestinationPath $extractDir -Force + $extractedFolder = Get-ChildItem -Path $extractDir -Directory | Select-Object -First 1 + if ($extractedFolder) { + New-Item -ItemType Directory -Path "$env:LOCALAPPDATA\Programs" -Force | Out-Null + if (Test-Path $nodeDir) { Remove-Item $nodeDir -Recurse -Force -ErrorAction SilentlyContinue } + Move-Item -Path $extractedFolder.FullName -Destination $nodeDir -Force + } + Remove-Item $zipPath -Force -ErrorAction SilentlyContinue + Remove-Item $extractDir -Recurse -Force -ErrorAction SilentlyContinue + if (Test-Path "$nodeDir\node.exe") { + $env:PATH = "$nodeDir;$env:PATH" + Write-Host " ✔ Portable Node.js installed in user space." -ForegroundColor Green + return $true + } + } catch { + Write-Host " ⚠ Failed to install portable Node.js: $_" -ForegroundColor DarkYellow + } + return $false +} + # 0. Initial PATH refresh Update-EnvironmentPath @@ -74,9 +134,19 @@ if ($missingTools.Count -eq 0) { Write-Host " ✔ All system toolchains are already installed (ADB, FFmpeg, scrcpy)." -ForegroundColor Green } else { Write-Host " ! Missing toolchains: $($missingTools -join ', ')" -ForegroundColor DarkYellow - Write-Host " Attempting automatic installation via Windows Package Manager..." -ForegroundColor Gray - + $useWinGet = $false if (Test-CommandExists "winget") { + if ([Console]::IsInputRedirected -eq $false) { + $ans = Read-Host " Install system packages via WinGet? [Y/n]" + if ($ans -eq "" -or $ans -match "^[Yy]") { + $useWinGet = $true + } + } else { + $useWinGet = $true + } + } + + if ($useWinGet) { Write-Host " Detected WinGet. Installing packages..." -ForegroundColor Cyan if ($missingTools -contains "adb") { Write-Host " 📦 Installing Google.PlatformTools (ADB)..." -ForegroundColor Cyan @@ -90,15 +160,21 @@ if ($missingTools.Count -eq 0) { Write-Host " 📦 Installing Genymobile.scrcpy..." -ForegroundColor Cyan winget install --id Genymobile.scrcpy -e --accept-source-agreements --accept-package-agreements --silent 2>$null | Out-Null } + Update-EnvironmentPath } elseif (Test-CommandExists "choco") { Write-Host " Detected Chocolatey. Installing packages..." -ForegroundColor Cyan choco install ($missingTools -join " ") -y | Out-Null + Update-EnvironmentPath } elseif (Test-CommandExists "scoop") { Write-Host " Detected Scoop. Installing packages..." -ForegroundColor Cyan scoop install ($missingTools -join " ") | Out-Null + Update-EnvironmentPath } - Update-EnvironmentPath + # Zero-admin user-space fallback for adb if still missing + if (-not (Test-CommandExists "adb")) { + Install-PortablePlatformTools + } } # 2. Check and Install uv (Fast Python Package Manager) @@ -144,11 +220,28 @@ $ShowcaseIndexAlt1 = "$RootDir\apps\showcase_ui\dist\browser\index.html" $ShowcaseIndexAlt2 = "$RootDir\apps\showcase_ui\dist\index.html" if ((-not (Test-Path $ShowcaseIndex)) -and (-not (Test-Path $ShowcaseIndexAlt1)) -and (-not (Test-Path $ShowcaseIndexAlt2))) { if (-not (Test-CommandExists "npm")) { + $useWinGetNode = $false if (Test-CommandExists "winget") { + if ([Console]::IsInputRedirected -eq $false) { + $ans = Read-Host " Install Node.js via WinGet (may require Administrator)? [Y/n]" + if ($ans -eq "" -or $ans -match "^[Yy]") { + $useWinGetNode = $true + } + } else { + $useWinGetNode = $true + } + } + + if ($useWinGetNode) { Write-Host " ⚡ Node.js/npm not found. Installing Node.js LTS via WinGet..." -ForegroundColor Cyan winget install --id OpenJS.NodeJS.LTS -e --accept-source-agreements --accept-package-agreements --silent 2>$null | Out-Null Update-EnvironmentPath } + + # Zero-admin user-space fallback for Node.js if still not found + if (-not (Test-CommandExists "npm")) { + Install-PortableNode + } } if (Test-CommandExists "npm") { diff --git a/scripts/install_deps.sh b/scripts/install_deps.sh index 1bb4ead..354330d 100755 --- a/scripts/install_deps.sh +++ b/scripts/install_deps.sh @@ -50,6 +50,8 @@ STANDARD_PATHS=( "/usr/local/bin" "/usr/local/sbin" "${HOME}/.local/bin" + "${HOME}/.local/share/node/bin" + "${HOME}/.local/share/platform-tools" "${HOME}/.cargo/bin" "${HOME}/Library/Android/sdk/platform-tools" "${HOME}/Android/Sdk/platform-tools" @@ -68,6 +70,51 @@ has_cmd() { command -v "$1" >/dev/null 2>&1 } +# Sudo credential and choice state tracking +SUDO_AUTHENTICATED=false +SUDO_DECLINED=false + +# Helper to check, prompt for, or skip administrator (sudo) privileges gracefully +request_sudo() { + [ "$(id -u)" -eq 0 ] && return 0 + if ! has_cmd sudo; then + return 1 + fi + # If already authenticated or passwordless sudo works + if [ "${SUDO_AUTHENTICATED}" = true ] || sudo -n true >/dev/null 2>&1; then + SUDO_AUTHENTICATED=true + return 0 + fi + # If previously declined or failed in this session, don't nag again + if [ "${SUDO_DECLINED}" = true ]; then + return 1 + fi + # If interactive terminal, ask user whether to enter sudo password + if [ -t 0 ]; then + local action="${1:-install system packages}" + echo -e " ${CYAN}🔐 Administrator (sudo) privileges can be used to ${action}.${NC}" + local reply="" + read -r -p " Enter sudo password now? [Y/n]: " reply + reply=${reply:-Y} + if [[ "${reply}" =~ ^[Yy]$ ]]; then + if sudo -v; then + SUDO_AUTHENTICATED=true + echo -e " ${GREEN}✔ Sudo authenticated successfully.${NC}" + return 0 + else + echo -e " ${YELLOW}⚠ Sudo authentication failed or cancelled. Using user-space fallback.${NC}" + SUDO_DECLINED=true + return 1 + fi + else + echo -e " ${YELLOW}⏭️ Skipped sudo. Using user-space fallback.${NC}" + SUDO_DECLINED=true + return 1 + fi + fi + return 1 +} + echo -e "${BOLD}1. Detecting Operating System & Environment...${NC}" echo -e " Platform: ${BLUE}${OS_TYPE}${NC} (${ARCH_TYPE})" echo -e " Root Dir: ${DIM}${ROOT_DIR}${NC}" @@ -131,48 +178,59 @@ install_system_packages() { fi elif [ "${OS_TYPE}" = "Linux" ]; then - SUDO_CMD="" - if [ "$(id -u)" -ne 0 ]; then - if has_cmd sudo; then - SUDO_CMD="sudo" - else - echo -e " ${YELLOW}⚠ Root/sudo privileges not detected. Please install system tools with your package manager.${NC}" + local PKGS=() + [ "${need_adb}" = true ] && PKGS+=("adb") + [ "${need_ffmpeg}" = true ] && PKGS+=("ffmpeg") + [ "${need_scrcpy}" = true ] && PKGS+=("scrcpy") + + if request_sudo "install missing system components (${PKGS[*]})"; then + local SUDO_PREFIX="" + if [ "$(id -u)" -ne 0 ]; then + SUDO_PREFIX="sudo" + fi + + if has_cmd apt-get; then + echo -e " ${CYAN}Detected Debian/Ubuntu (apt-get). Synchronizing and installing...${NC}" + ${SUDO_PREFIX} apt-get update -qq && ${SUDO_PREFIX} apt-get install -y "${PKGS[@]}" || true + elif has_cmd dnf; then + echo -e " ${CYAN}Detected Fedora/RHEL (dnf). Installing packages...${NC}" + local DNF_PKGS=() + [ "${need_adb}" = true ] && DNF_PKGS+=("android-tools") + [ "${need_ffmpeg}" = true ] && DNF_PKGS+=("ffmpeg") + [ "${need_scrcpy}" = true ] && DNF_PKGS+=("scrcpy") + ${SUDO_PREFIX} dnf install -y "${DNF_PKGS[@]}" || true + elif has_cmd pacman; then + echo -e " ${CYAN}Detected Arch Linux (pacman). Installing packages...${NC}" + local PAC_PKGS=() + [ "${need_adb}" = true ] && PAC_PKGS+=("android-tools") + [ "${need_ffmpeg}" = true ] && PAC_PKGS+=("ffmpeg") + [ "${need_scrcpy}" = true ] && PAC_PKGS+=("scrcpy") + ${SUDO_PREFIX} pacman -S --noconfirm "${PAC_PKGS[@]}" || true fi fi - if has_cmd apt-get; then - echo -e " ${CYAN}Detected Debian/Ubuntu (apt-get). Synchronizing and installing...${NC}" - PKGS=() - [ "${need_adb}" = true ] && PKGS+=("adb") - [ "${need_ffmpeg}" = true ] && PKGS+=("ffmpeg") - [ "${need_scrcpy}" = true ] && PKGS+=("scrcpy") - PKGS+=("curl" "git") - - if [ -n "${SUDO_CMD}" ] || [ "$(id -u)" -eq 0 ]; then - ${SUDO_CMD} apt-get update -qq || true - ${SUDO_CMD} apt-get install -y "${PKGS[@]}" || true - fi - elif has_cmd dnf; then - echo -e " ${CYAN}Detected Fedora/RHEL (dnf). Installing packages...${NC}" - PKGS=() - [ "${need_adb}" = true ] && PKGS+=("android-tools") - [ "${need_ffmpeg}" = true ] && PKGS+=("ffmpeg") - [ "${need_scrcpy}" = true ] && PKGS+=("scrcpy") - PKGS+=("curl" "git") - - if [ -n "${SUDO_CMD}" ] || [ "$(id -u)" -eq 0 ]; then - ${SUDO_CMD} dnf install -y "${PKGS[@]}" || true - fi - elif has_cmd pacman; then - echo -e " ${CYAN}Detected Arch Linux (pacman). Installing packages...${NC}" - PKGS=() - [ "${need_adb}" = true ] && PKGS+=("android-tools") - [ "${need_ffmpeg}" = true ] && PKGS+=("ffmpeg") - [ "${need_scrcpy}" = true ] && PKGS+=("scrcpy") - PKGS+=("curl" "git") - - if [ -n "${SUDO_CMD}" ] || [ "$(id -u)" -eq 0 ]; then - ${SUDO_CMD} pacman -S --noconfirm "${PKGS[@]}" || true + # User-space fallback for adb if missing and no root privileges + if ! has_cmd adb; then + if [[ "${ARCH_TYPE}" = "x86_64" || "${ARCH_TYPE}" = "amd64" ]]; then + local PT_DIR="${HOME}/.local/share/platform-tools" + if [ ! -x "${PT_DIR}/adb" ]; then + echo -e " ${CYAN}📦 Installing Android platform-tools (adb) in user space...${NC}" + local TEMP_ZIP="/tmp/platform-tools-$$.zip" + if curl -fsSL "https://dl.google.com/android/repository/platform-tools-latest-linux.zip" -o "${TEMP_ZIP}" 2>/dev/null; then + mkdir -p "${HOME}/.local/share" "${HOME}/.local/bin" + if has_cmd unzip; then + unzip -q -o "${TEMP_ZIP}" -d "${HOME}/.local/share" 2>/dev/null || true + else + python3 -m zipfile -e "${TEMP_ZIP}" "${HOME}/.local/share" 2>/dev/null || true + fi + rm -f "${TEMP_ZIP}" + fi + fi + if [ -x "${PT_DIR}/adb" ]; then + ln -sf "${PT_DIR}/adb" "${HOME}/.local/bin/adb" + export PATH="${PT_DIR}:${PATH}" + echo -e " ${GREEN}✓ adb installed in user space.${NC}" + fi fi fi fi @@ -254,29 +312,56 @@ setup_showcase_ui() { local SHOWCASE_INDEX_ALT1="${ROOT_DIR}/apps/showcase_ui/dist/browser/index.html" local SHOWCASE_INDEX_ALT2="${ROOT_DIR}/apps/showcase_ui/dist/index.html" if [ ! -f "${SHOWCASE_INDEX}" ] && [ ! -f "${SHOWCASE_INDEX_ALT1}" ] && [ ! -f "${SHOWCASE_INDEX_ALT2}" ]; then - if ! has_cmd npm; then - echo -e " ${YELLOW}⚡ npm/Node.js not found. Auto-installing Node.js...${NC}" - if [ "${OS_TYPE}" = "Darwin" ] && has_cmd brew; then - brew install node >/dev/null 2>&1 || true - elif [ "${OS_TYPE}" = "Linux" ]; then - local SUDO_CMD="" - if [ "$(id -u)" -ne 0 ] && has_cmd sudo; then SUDO_CMD="sudo"; fi - if has_cmd apt-get; then - ${SUDO_CMD} apt-get update -qq && ${SUDO_CMD} apt-get install -y -qq nodejs npm >/dev/null 2>&1 || true - elif has_cmd dnf; then - ${SUDO_CMD} dnf install -y nodejs npm >/dev/null 2>&1 || true - elif has_cmd pacman; then - ${SUDO_CMD} pacman -S --noconfirm nodejs npm >/dev/null 2>&1 || true - fi - fi - fi - # Try loading nvm if available in user environment export NVM_DIR="${HOME}/.nvm" if [ -s "${NVM_DIR}/nvm.sh" ]; then # shellcheck disable=SC1090,SC1091 . "${NVM_DIR}/nvm.sh" 2>/dev/null || true fi + if [ -d "${HOME}/.local/share/node/bin" ] && [[ ":${PATH}:" != *":${HOME}/.local/share/node/bin:"* ]]; then + export PATH="${HOME}/.local/share/node/bin:${PATH}" + fi + + if ! has_cmd npm; then + echo -e " ${YELLOW}⚡ npm/Node.js not found. Auto-installing Node.js...${NC}" + if [ "${OS_TYPE}" = "Darwin" ] && has_cmd brew; then + brew install node >/dev/null 2>&1 || true + elif [ "${OS_TYPE}" = "Linux" ]; then + if request_sudo "install Node.js and npm"; then + local SUDO_PREFIX="" + if [ "$(id -u)" -ne 0 ]; then SUDO_PREFIX="sudo"; fi + if has_cmd apt-get; then + ${SUDO_PREFIX} apt-get update -qq && ${SUDO_PREFIX} apt-get install -y -qq nodejs npm || true + elif has_cmd dnf; then + ${SUDO_PREFIX} dnf install -y nodejs npm || true + elif has_cmd pacman; then + ${SUDO_PREFIX} pacman -S --noconfirm nodejs npm || true + fi + fi + + # If still not found, download portable Node.js into user space + if ! has_cmd npm; then + local ARCH="$(uname -m)" + local NODE_ARCH="" + case "${ARCH}" in + x86_64|amd64) NODE_ARCH="x64" ;; + aarch64|arm64) NODE_ARCH="arm64" ;; + esac + if [ -n "${NODE_ARCH}" ]; then + local NODE_VER="v20.18.3" + local NODE_DIR="${HOME}/.local/share/node" + echo -e " ${CYAN}📦 Installing portable Node.js ${NODE_VER} in user space (~/.local)...${NC}" + mkdir -p "${NODE_DIR}" "${HOME}/.local/bin" + if curl -fsSL "https://nodejs.org/dist/${NODE_VER}/node-${NODE_VER}-linux-${NODE_ARCH}.tar.gz" | tar -xz -C "${NODE_DIR}" --strip-components=1 2>/dev/null; then + ln -sf "${NODE_DIR}/bin/node" "${HOME}/.local/bin/node" + ln -sf "${NODE_DIR}/bin/npm" "${HOME}/.local/bin/npm" + ln -sf "${NODE_DIR}/bin/npx" "${HOME}/.local/bin/npx" + export PATH="${NODE_DIR}/bin:${HOME}/.local/bin:${PATH}" + fi + fi + fi + fi + fi if has_cmd npm; then echo -e " ${YELLOW}🎨 Building Angular Showcase UI...${NC}" diff --git a/scripts/start.ps1 b/scripts/start.ps1 index 35d4d8f..39d2485 100644 --- a/scripts/start.ps1 +++ b/scripts/start.ps1 @@ -38,6 +38,10 @@ function Update-EnvironmentPath { "$env:ProgramFiles\nodejs", "${env:ProgramFiles(x86)}\nodejs", "$env:APPDATA\npm", + "$env:LOCALAPPDATA\Programs\platform-tools", + "$env:LOCALAPPDATA\Programs\node", + "$env:USERPROFILE\.local\share\platform-tools", + "$env:USERPROFILE\.local\share\node", "C:\ProgramData\chocolatey\bin", "$env:USERPROFILE\scoop\shims", "$env:USERPROFILE\.local\bin", @@ -54,6 +58,62 @@ function Test-CommandExists { return ($null -ne $res) } +function Install-PortablePlatformTools { + $ptDir = "$env:LOCALAPPDATA\Programs\platform-tools" + if (Test-Path "$ptDir\adb.exe") { + $env:PATH = "$ptDir;$env:PATH" + return $true + } + Write-Host " [INFO] Installing Android platform-tools (adb) in user space..." -ForegroundColor Cyan + try { + $zipPath = "$env:TEMP\platform-tools-windows.zip" + Invoke-WebRequest -Uri "https://dl.google.com/android/repository/platform-tools-latest-windows.zip" -OutFile $zipPath -UseBasicParsing + New-Item -ItemType Directory -Path "$env:LOCALAPPDATA\Programs" -Force | Out-Null + Expand-Archive -Path $zipPath -DestinationPath "$env:LOCALAPPDATA\Programs" -Force + Remove-Item $zipPath -Force -ErrorAction SilentlyContinue + if (Test-Path "$ptDir\adb.exe") { + $env:PATH = "$ptDir;$env:PATH" + Write-Host " [OK] adb installed in user space." -ForegroundColor Green + return $true + } + } catch { + Write-Host " [WARN] Failed to install portable platform-tools: $_" -ForegroundColor DarkYellow + } + return $false +} + +function Install-PortableNode { + $nodeDir = "$env:LOCALAPPDATA\Programs\node" + if (Test-Path "$nodeDir\node.exe") { + $env:PATH = "$nodeDir;$env:PATH" + return $true + } + Write-Host " [INFO] Installing portable Node.js LTS in user space..." -ForegroundColor Cyan + try { + $zipPath = "$env:TEMP\node-v20-win-x64.zip" + Invoke-WebRequest -Uri "https://nodejs.org/dist/v20.18.3/node-v20.18.3-win-x64.zip" -OutFile $zipPath -UseBasicParsing + $extractDir = "$env:TEMP\node_extract" + New-Item -ItemType Directory -Path $extractDir -Force | Out-Null + Expand-Archive -Path $zipPath -DestinationPath $extractDir -Force + $extractedFolder = Get-ChildItem -Path $extractDir -Directory | Select-Object -First 1 + if ($extractedFolder) { + New-Item -ItemType Directory -Path "$env:LOCALAPPDATA\Programs" -Force | Out-Null + if (Test-Path $nodeDir) { Remove-Item $nodeDir -Recurse -Force -ErrorAction SilentlyContinue } + Move-Item -Path $extractedFolder.FullName -Destination $nodeDir -Force + } + Remove-Item $zipPath -Force -ErrorAction SilentlyContinue + Remove-Item $extractDir -Recurse -Force -ErrorAction SilentlyContinue + if (Test-Path "$nodeDir\node.exe") { + $env:PATH = "$nodeDir;$env:PATH" + Write-Host " [OK] Portable Node.js installed in user space." -ForegroundColor Green + return $true + } + } catch { + Write-Host " [WARN] Failed to install portable Node.js: $_" -ForegroundColor DarkYellow + } + return $false +} + # 1. Refresh PATH with standard toolchain paths Update-EnvironmentPath @@ -71,8 +131,21 @@ if (-not (Test-CommandExists "ffmpeg")) { $missingCore += "ffmpeg" } if (-not (Test-CommandExists "scrcpy")) { $missingCore += "scrcpy" } if ($missingCore.Count -gt 0) { + $useWinGet = $false if (Test-CommandExists "winget") { - Write-Host "[INFO] Auto-installing missing components ($($missingCore -join ', '))..." -ForegroundColor DarkYellow + if ([Console]::IsInputRedirected -eq $false) { + Write-Host " [INFO] Missing components detected: $($missingCore -join ', ')." -ForegroundColor Cyan + $ans = Read-Host " Install system packages via WinGet? [Y/n]" + if ($ans -eq "" -or $ans -match "^[Yy]") { + $useWinGet = $true + } + } else { + $useWinGet = $true + } + } + + if ($useWinGet) { + Write-Host " [INFO] Auto-installing missing components ($($missingCore -join ', '))..." -ForegroundColor DarkYellow if ($missingCore -contains "adb") { winget install --id Google.PlatformTools -e --accept-source-agreements --accept-package-agreements --silent 2>$null | Out-Null } @@ -84,6 +157,11 @@ if ($missingCore.Count -gt 0) { } Update-EnvironmentPath } + + # Zero-admin user-space fallback for adb if still missing + if (-not (Test-CommandExists "adb")) { + Install-PortablePlatformTools + } } # 4. Check or initialize .env @@ -106,11 +184,29 @@ $ShowcaseIndexAlt1 = "$RootDir\apps\showcase_ui\dist\browser\index.html" $ShowcaseIndexAlt2 = "$RootDir\apps\showcase_ui\dist\index.html" if ((-not (Test-Path $ShowcaseIndex)) -and (-not (Test-Path $ShowcaseIndexAlt1)) -and (-not (Test-Path $ShowcaseIndexAlt2))) { if (-not (Test-CommandExists "npm")) { + $useWinGetNode = $false if (Test-CommandExists "winget") { - Write-Host " [INFO] Node.js/npm not found. Installing Node.js LTS via WinGet..." -ForegroundColor Yellow + if ([Console]::IsInputRedirected -eq $false) { + Write-Host " [INFO] Node.js/npm not found (required for Showcase UI)." -ForegroundColor Cyan + $ans = Read-Host " Install Node.js via WinGet (may require Administrator)? [Y/n]" + if ($ans -eq "" -or $ans -match "^[Yy]") { + $useWinGetNode = $true + } + } else { + $useWinGetNode = $true + } + } + + if ($useWinGetNode) { + Write-Host " [INFO] Installing Node.js LTS via WinGet..." -ForegroundColor Yellow winget install --id OpenJS.NodeJS.LTS -e --accept-source-agreements --accept-package-agreements --silent 2>$null | Out-Null Update-EnvironmentPath } + + # Zero-admin user-space fallback for Node.js if still not found + if (-not (Test-CommandExists "npm")) { + Install-PortableNode + } } if (Test-CommandExists "npm") { @@ -130,7 +226,7 @@ if ((-not (Test-Path $ShowcaseIndex)) -and (-not (Test-Path $ShowcaseIndexAlt1)) Pop-Location } } else { - Write-Host " [WARN] Node.js/npm not found. Please install Node.js (>= 18) to compile Showcase UI." -ForegroundColor DarkYellow + Write-Host " [WARN] Node.js/npm not found. Showcase UI will show fallback notice on launch." -ForegroundColor DarkYellow } } diff --git a/start.sh b/start.sh index 0232e31..92114d3 100755 --- a/start.sh +++ b/start.sh @@ -37,6 +37,8 @@ STANDARD_PATHS=( "/usr/local/bin" "/usr/local/sbin" "${HOME}/.local/bin" + "${HOME}/.local/share/node/bin" + "${HOME}/.local/share/platform-tools" "${HOME}/.cargo/bin" "${HOME}/Library/Android/sdk/platform-tools" "${HOME}/Android/Sdk/platform-tools" @@ -47,6 +49,51 @@ for p in "${STANDARD_PATHS[@]}"; do fi done +# Sudo credential and choice state tracking +SUDO_AUTHENTICATED=false +SUDO_DECLINED=false + +# Helper to check, prompt for, or skip administrator (sudo) privileges gracefully +request_sudo() { + [ "$(id -u)" -eq 0 ] && return 0 + if ! command -v sudo >/dev/null 2>&1; then + return 1 + fi + # If already authenticated or passwordless sudo works + if [ "${SUDO_AUTHENTICATED}" = true ] || sudo -n true >/dev/null 2>&1; then + SUDO_AUTHENTICATED=true + return 0 + fi + # If previously declined or failed in this session, don't nag again + if [ "${SUDO_DECLINED}" = true ]; then + return 1 + fi + # If interactive terminal, ask user whether to enter sudo password + if [ -t 0 ]; then + local action="${1:-install system packages}" + echo -e " ${CYAN}🔐 Administrator (sudo) privileges can be used to ${action}.${NC}" + local reply="" + read -r -p " Enter sudo password now? [Y/n]: " reply + reply=${reply:-Y} + if [[ "${reply}" =~ ^[Yy]$ ]]; then + if sudo -v; then + SUDO_AUTHENTICATED=true + echo -e " ${GREEN}✔ Sudo authenticated successfully.${NC}" + return 0 + else + echo -e " ${YELLOW}⚠ Sudo authentication failed or cancelled. Using user-space fallback.${NC}" + SUDO_DECLINED=true + return 1 + fi + else + echo -e " ${YELLOW}⏭️ Skipped sudo. Using user-space fallback.${NC}" + SUDO_DECLINED=true + return 1 + fi + fi + return 1 +} + # 2. Check or install uv (Fast Python package manager) if ! command -v uv >/dev/null 2>&1; then echo -e "${YELLOW}⚡ uv not found. Installing Astral uv...${NC}" @@ -88,11 +135,41 @@ if [ ${#MISSING_CORE[@]} -gt 0 ]; then fi fi elif [ "${OS_NAME}" = "Linux" ]; then - if command -v apt-get >/dev/null 2>&1; then - if [ "$(id -u)" -eq 0 ]; then - apt-get update -qq && apt-get install -y -qq "${MISSING_CORE[@]}" >/dev/null 2>&1 || true - elif command -v sudo >/dev/null 2>&1; then - sudo -n apt-get update -qq && sudo -n apt-get install -y -qq "${MISSING_CORE[@]}" >/dev/null 2>&1 || true + if request_sudo "install missing system components (${MISSING_CORE[*]})"; then + local SUDO_PREFIX="" + if [ "$(id -u)" -ne 0 ]; then SUDO_PREFIX="sudo"; fi + if command -v apt-get >/dev/null 2>&1; then + ${SUDO_PREFIX} apt-get update -qq && ${SUDO_PREFIX} apt-get install -y -qq "${MISSING_CORE[@]}" || true + elif command -v dnf >/dev/null 2>&1; then + ${SUDO_PREFIX} dnf install -y "${MISSING_CORE[@]}" || true + elif command -v pacman >/dev/null 2>&1; then + ${SUDO_PREFIX} pacman -S --noconfirm "${MISSING_CORE[@]}" || true + fi + fi + + # Fallback: if adb is missing on Linux without root/sudo, install platform-tools in user space + if ! command -v adb >/dev/null 2>&1; then + ARCH="$(uname -m)" + if [[ "${ARCH}" = "x86_64" || "${ARCH}" = "amd64" ]]; then + PT_DIR="${HOME}/.local/share/platform-tools" + if [ ! -x "${PT_DIR}/adb" ]; then + echo -e " ${CYAN}📦 Installing Android platform-tools (adb) in user space...${NC}" + TEMP_ZIP="/tmp/platform-tools-$$.zip" + if curl -fsSL "https://dl.google.com/android/repository/platform-tools-latest-linux.zip" -o "${TEMP_ZIP}" 2>/dev/null; then + mkdir -p "${HOME}/.local/share" "${HOME}/.local/bin" + if command -v unzip >/dev/null 2>&1; then + unzip -q -o "${TEMP_ZIP}" -d "${HOME}/.local/share" 2>/dev/null || true + else + python3 -m zipfile -e "${TEMP_ZIP}" "${HOME}/.local/share" 2>/dev/null || true + fi + rm -f "${TEMP_ZIP}" + fi + fi + if [ -x "${PT_DIR}/adb" ]; then + ln -sf "${PT_DIR}/adb" "${HOME}/.local/bin/adb" + export PATH="${PT_DIR}:${PATH}" + echo -e " ${GREEN}✓ adb installed in user space.${NC}" + fi fi fi fi @@ -117,41 +194,59 @@ SHOWCASE_INDEX="${SCRIPT_DIR}/apps/showcase_ui/dist/frontend/browser/index.html" SHOWCASE_INDEX_ALT1="${SCRIPT_DIR}/apps/showcase_ui/dist/browser/index.html" SHOWCASE_INDEX_ALT2="${SCRIPT_DIR}/apps/showcase_ui/dist/index.html" if [ ! -f "${SHOWCASE_INDEX}" ] && [ ! -f "${SHOWCASE_INDEX_ALT1}" ] && [ ! -f "${SHOWCASE_INDEX_ALT2}" ]; then + # First: Try loading existing nvm or user-installed node in environment + export NVM_DIR="${HOME}/.nvm" + if [ -s "${NVM_DIR}/nvm.sh" ]; then + # shellcheck disable=SC1090,SC1091 + . "${NVM_DIR}/nvm.sh" 2>/dev/null || true + fi + if [ -d "${HOME}/.local/share/node/bin" ] && [[ ":${PATH}:" != *":${HOME}/.local/share/node/bin:"* ]]; then + export PATH="${HOME}/.local/share/node/bin:${PATH}" + fi + if ! command -v npm >/dev/null 2>&1; then OS_NAME="$(uname -s)" echo -e " ${YELLOW}⚡ npm/Node.js not found. Auto-installing Node.js for Showcase UI compilation...${NC}" if [ "${OS_NAME}" = "Darwin" ] && command -v brew >/dev/null 2>&1; then brew install node >/dev/null 2>&1 || true elif [ "${OS_NAME}" = "Linux" ]; then - if command -v apt-get >/dev/null 2>&1; then - if [ "$(id -u)" -eq 0 ]; then - apt-get update -qq && apt-get install -y -qq nodejs npm >/dev/null 2>&1 || true - elif command -v sudo >/dev/null 2>&1; then - sudo -n apt-get update -qq && sudo -n apt-get install -y -qq nodejs npm >/dev/null 2>&1 || true + if request_sudo "install Node.js and npm"; then + local SUDO_PREFIX="" + if [ "$(id -u)" -ne 0 ]; then SUDO_PREFIX="sudo"; fi + if command -v apt-get >/dev/null 2>&1; then + ${SUDO_PREFIX} apt-get update -qq && ${SUDO_PREFIX} apt-get install -y -qq nodejs npm || true + elif command -v dnf >/dev/null 2>&1; then + ${SUDO_PREFIX} dnf install -y nodejs npm || true + elif command -v pacman >/dev/null 2>&1; then + ${SUDO_PREFIX} pacman -S --noconfirm nodejs npm || true fi - elif command -v dnf >/dev/null 2>&1; then - if [ "$(id -u)" -eq 0 ]; then - dnf install -y nodejs npm >/dev/null 2>&1 || true - elif command -v sudo >/dev/null 2>&1; then - sudo -n dnf install -y nodejs npm >/dev/null 2>&1 || true - fi - elif command -v pacman >/dev/null 2>&1; then - if [ "$(id -u)" -eq 0 ]; then - pacman -S --noconfirm nodejs npm >/dev/null 2>&1 || true - elif command -v sudo >/dev/null 2>&1; then - sudo -n pacman -S --noconfirm nodejs npm >/dev/null 2>&1 || true + fi + + # If npm still not found (e.g. no root/sudo privileges on cloud workstation): + if ! command -v npm >/dev/null 2>&1; then + ARCH="$(uname -m)" + NODE_ARCH="" + case "${ARCH}" in + x86_64|amd64) NODE_ARCH="x64" ;; + aarch64|arm64) NODE_ARCH="arm64" ;; + esac + if [ -n "${NODE_ARCH}" ]; then + NODE_VER="v20.18.3" + NODE_DIR="${HOME}/.local/share/node" + echo -e " ${CYAN}📦 Installing portable Node.js ${NODE_VER} in user space (~/.local)...${NC}" + mkdir -p "${NODE_DIR}" "${HOME}/.local/bin" + if curl -fsSL "https://nodejs.org/dist/${NODE_VER}/node-${NODE_VER}-linux-${NODE_ARCH}.tar.gz" | tar -xz -C "${NODE_DIR}" --strip-components=1 2>/dev/null; then + ln -sf "${NODE_DIR}/bin/node" "${HOME}/.local/bin/node" + ln -sf "${NODE_DIR}/bin/npm" "${HOME}/.local/bin/npm" + ln -sf "${NODE_DIR}/bin/npx" "${HOME}/.local/bin/npx" + export PATH="${NODE_DIR}/bin:${HOME}/.local/bin:${PATH}" + echo -e " ${GREEN}✓ Portable Node.js installed in user space.${NC}" + fi fi fi fi fi - # Try loading nvm if available in user environment - export NVM_DIR="${HOME}/.nvm" - if [ -s "${NVM_DIR}/nvm.sh" ]; then - # shellcheck disable=SC1090,SC1091 - . "${NVM_DIR}/nvm.sh" 2>/dev/null || true - fi - if command -v npm >/dev/null 2>&1; then echo -e " ${YELLOW}🎨 Showcase UI build not found. Compiling Angular Showcase UI...${NC}" ( @@ -197,8 +292,28 @@ else fi echo "" -# 8. Launch unified Showcase UI & auto-open browser +# 8. Detect environment & launch unified Showcase UI +IS_REMOTE=false +if [ -n "${SSH_CONNECTION:-}" ] || [ -n "${SSH_CLIENT:-}" ] || [ -n "${SSH_TTY:-}" ] || [ -z "${DISPLAY:-}" ]; then + IS_REMOTE=true +fi + echo -e " ${GREEN}🚀 Launching Artemis Showcase UI & Admin Console...${NC}" + +OPEN_FLAG="--open" +if [ "${IS_REMOTE}" = true ]; then + HOSTNAME_STR="$(hostname 2>/dev/null || echo 'cloud-host')" + USER_STR="$(whoami 2>/dev/null || echo 'user')" + echo -e " ${CYAN}☁️ Cloud / Remote environment detected:${NC}" + echo -e " • Access locally via SSH tunnel: ${BOLD}ssh -L 8000:localhost:8000 ${USER_STR}@${HOSTNAME_STR}${NC}" + echo -e " • Or access via Cloudtop / VS Code / Cursor Port Forwarding (Port 8000)" + if [ -z "${DISPLAY:-}" ]; then + echo -e " • Headless session detected (browser auto-open disabled)." + OPEN_FLAG="--no-open" + fi + echo "" +fi + # Launch via `python -m artemis` (not the `artemis` console-script shim) so the # long-running server never pins .venv/Scripts/artemis[.exe] against reinstalls. -exec uv run python -m artemis ui --open "$@" +exec uv run python -m artemis ui "${OPEN_FLAG}" "$@"