diff --git a/scripts/lib/plugins.ps1 b/scripts/lib/plugins.ps1 index 4badbef..cbf1edf 100644 --- a/scripts/lib/plugins.ps1 +++ b/scripts/lib/plugins.ps1 @@ -66,6 +66,17 @@ function Get-InstalledPlugins { } } +function Test-CliCommand { + param([Parameter(Mandatory=$true)][string]$Name) + if (Get-Command $Name -ErrorAction SilentlyContinue) { return $true } + # npm's global bin directory is often missing from PATH on Windows; match managed-extensions.py find_executable. + if (-not (Get-Command npm -ErrorAction SilentlyContinue)) { return $false } + $prefix = @(& npm prefix --global 2>$null | ForEach-Object { "$_".Trim() } | Where-Object { $_ }) | Select-Object -Last 1 + if (-not $prefix) { return $false } + $directory = if ([Environment]::OSVersion.Platform -eq [PlatformID]::Win32NT) { $prefix } else { Join-Path $prefix 'bin' } + return [bool](@('', '.cmd', '.exe', '.ps1') | Where-Object { Test-Path -LiteralPath (Join-Path $directory ($Name + $_)) -PathType Leaf }) +} + function Test-InstalledMCPServer { param([string]$HomePath, [string]$Name, [string]$Url) $path = Join-Path $HomePath '.mcporter/mcporter.json' @@ -119,7 +130,7 @@ function Select-ConfiguredPlugins { if ($Mode -eq 'Update') { foreach ($referenceClient in $referenceClients) { $installed[$referenceClient] = @(Get-InstalledPlugins -Client $referenceClient -HomePath $HomePath) } } $checked = @($toggleable | ForEach-Object { if ($Mode -eq 'Install') { $true; return } - if ($_.codex_method -eq 'cli') { return $null -ne (Get-Command $_.codex_source -ErrorAction SilentlyContinue) } + if ($_.codex_method -eq 'cli') { return Test-CliCommand -Name $_.codex_source } $entry = $_ $found = if ($entry.mcporter_name -and $entry.mcporter_url) { Test-InstalledMCPServer -HomePath $HomePath -Name $entry.mcporter_name -Url $entry.mcporter_url diff --git a/scripts/lib/plugins.sh b/scripts/lib/plugins.sh index 841fa85..21df05f 100755 --- a/scripts/lib/plugins.sh +++ b/scripts/lib/plugins.sh @@ -25,6 +25,21 @@ except (ValueError, KeyError, TypeError) as error: ' "$client" "$plugin" } +cli_installed() { + local name=$1 prefix + command -v "$name" >/dev/null 2>&1 && return 0 + # npm's global bin directory is often missing from PATH; match managed-extensions.py find_executable. + command -v npm >/dev/null 2>&1 || return 1 + prefix=$(npm prefix --global 2>/dev/null | tr -d '\r' | tail -n 1) + [ -n "$prefix" ] || return 1 + case "$(uname -s 2>/dev/null)" in + MINGW*|MSYS*|CYGWIN*) + prefix=$(cygpath -u "$prefix" 2>/dev/null || printf '%s' "$prefix") + [ -f "$prefix/$name" ] || [ -f "$prefix/$name.cmd" ] || [ -f "$prefix/$name.exe" ] ;; + *) [ -x "$prefix/bin/$name" ] ;; + esac +} + run_plugin_command() { local dry_run=$1 shift @@ -151,7 +166,7 @@ select_configured_plugins() { continue fi if [ "$(plugin_field "$root" "$name" codex_method)" = cli ]; then - if command -v "$(plugin_field "$root" "$name" codex_source)" >/dev/null 2>&1; then checked+=(true); else checked+=(false); fi + if cli_installed "$(plugin_field "$root" "$name" codex_source)"; then checked+=(true); else checked+=(false); fi continue fi found=false diff --git a/scripts/tests/test-plugin-selection.ps1 b/scripts/tests/test-plugin-selection.ps1 index 0a90a66..b8568de 100644 --- a/scripts/tests/test-plugin-selection.ps1 +++ b/scripts/tests/test-plugin-selection.ps1 @@ -66,6 +66,19 @@ try { $selection = Select-ConfiguredPlugins -RepositoryRoot $root -Client Codex -Mode Update -HomePath $mcporterHome if (@($selection.Deselected | Where-Object { $_.name -eq 'Context7' }).Count -ne 1) { throw 'A different MCPorter server must not select Context7.' } } finally { Remove-Item -LiteralPath $mcporterHome -Recurse -Force -ErrorAction SilentlyContinue } +$npmPrefix = Join-Path $root ('.npm-test-' + [Guid]::NewGuid()) +$cliName = 'ai-config-test-cli-' + [Guid]::NewGuid().ToString('N') +try { + New-Item (Join-Path $npmPrefix 'bin') -ItemType Directory -Force | Out-Null + function npm { $npmPrefix } + if (Test-CliCommand -Name $cliName) { throw 'A missing CLI must not be detected.' } + New-Item (Join-Path $npmPrefix "$cliName.cmd") -ItemType File | Out-Null + New-Item (Join-Path $npmPrefix "bin/$cliName") -ItemType File | Out-Null + if (-not (Test-CliCommand -Name $cliName)) { throw 'A CLI in the npm global prefix must be detected when it is not on PATH.' } +} finally { + Remove-Item function:npm -ErrorAction SilentlyContinue + Remove-Item -LiteralPath $npmPrefix -Recurse -Force -ErrorAction SilentlyContinue +} $script:managedArguments = @() function Invoke-Python { $script:managedArguments = @($args); $global:LASTEXITCODE = 0 } $entry = [pscustomobject]@{ name = 'Test'; codex_method = 'plugin'; codex_marketplace = '-'; codex_plugin = 'test@market' } diff --git a/scripts/tests/test-plugin-selection.sh b/scripts/tests/test-plugin-selection.sh index 3856c15..e1ad1d0 100644 --- a/scripts/tests/test-plugin-selection.sh +++ b/scripts/tests/test-plugin-selection.sh @@ -48,6 +48,16 @@ select_configured_plugins "$root" codex Update true selected deselected printf '%s\n' '{"mcpServers":{"context7":{"baseUrl":"https://example.invalid/mcp"}}}' > "$test_home/.mcporter/mcporter.json" select_configured_plugins "$root" codex Update true selected deselected [[ " ${deselected[*]} " == *' Context7 '* ]] || { printf 'A different MCPorter server must not select Context7.\n' >&2; exit 1; } +npm_prefix=$(mktemp -d "$root/.npm-test.XXXXXX") +cli_name="ai-config-test-cli-${npm_prefix##*.}" +npm() { printf '%s\n' "$npm_prefix"; } +if cli_installed "$cli_name"; then rm -rf -- "$npm_prefix"; printf 'A missing CLI must not be detected.\n' >&2; exit 1; fi +mkdir -p "$npm_prefix/bin" +touch "$npm_prefix/$cli_name.cmd" "$npm_prefix/bin/$cli_name" +chmod +x "$npm_prefix/bin/$cli_name" +if ! cli_installed "$cli_name"; then rm -rf -- "$npm_prefix"; printf 'A CLI in the npm global prefix must be detected when it is not on PATH.\n' >&2; exit 1; fi +rm -rf -- "$npm_prefix" +unset -f npm # shellcheck disable=SC2034 selected=() plugin_installed() { printf 'Empty selection must not query installed plugins.\n' >&2; exit 1; }