# Installer revision: 2026-11-02.1 [CmdletBinding()] param( [string]$Version = '0.0.19', [switch]$SkipDependencies, [switch]$SkipPathUpdate, [string]$InstallPrefix = (Join-Path ([Environment]::GetFolderPath('LocalApplicationData')) 'Lambogenius\cli') ) $ErrorActionPreference = 'Stop' Set-StrictMode -Version Latest $releaseBase = 'https://tools.lambogenius.com/lambogenius-integration-migration' if ($env:OS -ne 'Windows_NT') { throw 'Use install-cli.sh on Linux/macOS.' } if ($Version -notmatch '^\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?$') { throw 'Invalid or unstamped release version.' } if ($releaseBase -notmatch '^https://') { throw 'Release URL must use HTTPS.' } if (-not [IO.Path]::IsPathRooted($InstallPrefix)) { throw 'InstallPrefix must be absolute.' } $InstallPrefix = [IO.Path]::GetFullPath($InstallPrefix) if ((Test-Path -LiteralPath $InstallPrefix) -and ((Get-Item -LiteralPath $InstallPrefix -Force).Attributes -band [IO.FileAttributes]::ReparsePoint)) { throw 'InstallPrefix must be a regular directory, not a filesystem link.' } [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 function Invoke-Checked([string]$Command, [string[]]$Arguments) { & $Command @Arguments if ($LASTEXITCODE -ne 0) { throw "$Command failed with exit code $LASTEXITCODE" } } function Test-OwnedExecutable([string]$File) { $item = Get-Item -LiteralPath $File -Force if ($item.PSIsContainer -or ($item.Attributes -band [IO.FileAttributes]::ReparsePoint)) { throw "Refusing to replace a linked or non-file launcher: $File" } $stream = [IO.File]::OpenRead($File) try { if ($stream.ReadByte() -ne 77 -or $stream.ReadByte() -ne 90) { return $false } $stream.Position = 0 $buffer = [byte[]]::new(262144) $marker = '@lambogenius/integration-migration-cli' $tail = '' while (($count = $stream.Read($buffer, 0, $buffer.Length)) -gt 0) { $text = $tail + [Text.Encoding]::UTF8.GetString($buffer, 0, $count) if ($text.Contains($marker)) { return $true } $tail = $text.Substring([Math]::Max(0, $text.Length - $marker.Length)) } return $false } finally { $stream.Dispose() } } function Backup-ExecutableInstallation { $native = Join-Path $InstallPrefix 'li.exe' if (-not (Test-Path -LiteralPath $native)) { return $null } if (-not (Test-OwnedExecutable $native)) { throw "An unrecognized li.exe exists at $native. Choose another InstallPrefix; it will not be overwritten." } $names = [Collections.Generic.List[string]]::new() $names.Add('li.exe') foreach ($name in @('li.cmd', 'lambogenius-integration-migrate.cmd')) { $file = Join-Path $InstallPrefix $name if (-not (Test-Path -LiteralPath $file)) { continue } $item = Get-Item -LiteralPath $file -Force if ($item.PSIsContainer -or ($item.Attributes -band [IO.FileAttributes]::ReparsePoint)) { throw "Refusing a linked or non-file launcher: $file" } if ($item.Length -le 4096 -and (Get-Content -LiteralPath $file -Raw).Trim() -eq '@"%~dp0li.exe" %*') { $names.Add($name) } } $backupParent = Join-Path $InstallPrefix '.installer-backups' if ((Test-Path -LiteralPath $backupParent) -and ((Get-Item -LiteralPath $backupParent -Force).Attributes -band [IO.FileAttributes]::ReparsePoint)) { throw 'The installer backup directory must not be a filesystem link.' } $directory = Join-Path $backupParent ('native-' + [guid]::NewGuid().ToString('N')) New-Item -ItemType Directory -Path $directory | Out-Null $backup = [pscustomobject]@{ Directory = $directory; Files = [Collections.Generic.List[string]]::new() } try { foreach ($name in $names) { $source = [IO.Path]::GetFullPath((Join-Path $InstallPrefix $name)) if ([IO.Path]::GetRelativePath($InstallPrefix, $source) -ne $name) { throw 'Launcher path leaves the intended installation directory.' } Move-Item -LiteralPath $source -Destination (Join-Path $directory $name) $backup.Files.Add($name) } } catch { Restore-ExecutableInstallation $backup; throw } Write-Host "Previous executable installation preserved at $directory" return $backup } function Restore-ExecutableInstallation($Backup) { if ($null -eq $Backup) { return } foreach ($name in $Backup.Files) { $saved = Join-Path $Backup.Directory $name $target = Join-Path $InstallPrefix $name if ((Test-Path -LiteralPath $saved -PathType Leaf) -and -not (Test-Path -LiteralPath $target)) { Move-Item -LiteralPath $saved -Destination $target } } Write-Warning "Installation failed; previous launchers were restored where possible. Backup: $($Backup.Directory)" } function Update-ProcessPath { $env:PATH = "$InstallPrefix;" + [Environment]::GetEnvironmentVariable('Path', 'Machine') + ';' + [Environment]::GetEnvironmentVariable('Path', 'User') $sevenZip = Join-Path $env:ProgramFiles '7-Zip' if (Test-Path -LiteralPath (Join-Path $sevenZip '7z.exe')) { $env:PATH += ";$sevenZip" } } function Install-Dependency([string]$Command, [string]$Package) { if (-not (Get-Command $Command -ErrorAction SilentlyContinue)) { Invoke-Checked 'winget' @('install', '--exact', '--id', $Package, '--source', 'winget', '--accept-source-agreements', '--accept-package-agreements') Update-ProcessPath } } if (-not $SkipDependencies) { Update-ProcessPath Write-Host 'Installing missing Node.js/npm, Git, PowerShell, Azure CLI/Bicep and 7-Zip. Windows may request administrator approval.' if (-not (Get-Command winget -ErrorAction SilentlyContinue)) { throw 'Install App Installer (WinGet) from Microsoft, then rerun, or use -SkipDependencies.' } Install-Dependency 'node' 'OpenJS.NodeJS.LTS' if ([int]((& node -p 'process.versions.node.split(/[.]/)[0]') | Select-Object -Last 1) -lt 20) { Invoke-Checked 'winget' @('upgrade', '--exact', '--id', 'OpenJS.NodeJS.LTS', '--source', 'winget', '--accept-source-agreements', '--accept-package-agreements') Update-ProcessPath } Install-Dependency 'git' 'Git.Git' Install-Dependency 'pwsh' 'Microsoft.PowerShell' Install-Dependency 'az' 'Microsoft.AzureCLI' Install-Dependency '7z' '7zip.7zip' Invoke-Checked 'az' @('bicep', 'install') } foreach ($command in @('node', 'npm.cmd')) { if (-not (Get-Command $command -ErrorAction SilentlyContinue)) { throw "$command is required. Reopen your terminal after dependency installation and rerun." } } Invoke-Checked 'node' @('-e', 'process.exit(Number(process.versions.node.split(/[.]/)[0]) >= 20 ? 0 : 1)') $tempRoot = [IO.Path]::GetFullPath([IO.Path]::GetTempPath()) $installTemp = Join-Path $tempRoot ('li-install-' + [guid]::NewGuid().ToString('N')) New-Item -ItemType Directory -Path $installTemp | Out-Null try { $manifest = Invoke-RestMethod "$releaseBase/$Version/release.json" if ($manifest.version -ne $Version -or $manifest.sha256 -notmatch '^[a-f0-9]{64}$') { throw 'Invalid release manifest.' } $archive = Join-Path $installTemp 'cli.tgz' Invoke-WebRequest "$releaseBase/$Version/lambogenius-integration-migration-cli-$Version.tgz" -OutFile $archive -UseBasicParsing if ((Get-FileHash -LiteralPath $archive -Algorithm SHA256).Hash.ToLowerInvariant() -ne $manifest.sha256) { throw 'CLI checksum mismatch.' } $backup = Backup-ExecutableInstallation try { Invoke-Checked 'npm.cmd' @('install', '--global', '--ignore-scripts', '--no-audit', '--no-fund', '--prefix', $InstallPrefix, $archive) $installed = "$(& (Join-Path $InstallPrefix 'li.cmd') --version | Select-Object -Last 1)".Trim() if ($LASTEXITCODE -ne 0 -or $installed -ne $Version) { throw "The installed CLI reported '$installed', expected $Version." } } catch { Restore-ExecutableInstallation $backup; throw } if (-not $SkipPathUpdate) { $userPath = [Environment]::GetEnvironmentVariable('Path', 'User') if ($InstallPrefix -notin ($userPath -split ';')) { [Environment]::SetEnvironmentVariable('Path', "$InstallPrefix;$userPath", 'User') } $sevenZip = Join-Path $env:ProgramFiles '7-Zip' $userPath = [Environment]::GetEnvironmentVariable('Path', 'User') if ((Test-Path -LiteralPath (Join-Path $sevenZip '7z.exe')) -and $sevenZip -notin ($userPath -split ';')) { [Environment]::SetEnvironmentVariable('Path', "$userPath;$sevenZip", 'User') } Update-ProcessPath } # Use the npm command explicitly when a profile alias or older li.exe shadows it. Invoke-Checked (Join-Path $InstallPrefix 'li.cmd') @('--version') Write-Host 'Installed. In PowerShell use li.cmd; reopen your terminal if PATH has changed.' Write-Host 'Next: li.cmd auth --help; select/authenticate an AI provider.' Write-Host 'Generate a code at https://lambogenius.com/app/licensing, then: li.cmd license activate --code ' } finally { $resolvedTemp = [IO.Path]::GetFullPath($installTemp) if ((Split-Path -Parent $resolvedTemp).TrimEnd('\', '/') -eq $tempRoot.TrimEnd('\', '/') -and [IO.Path]::GetFileName($resolvedTemp) -match '^li-install-[a-f0-9]{32}$' -and -not ((Get-Item -LiteralPath $resolvedTemp -Force).Attributes -band [IO.FileAttributes]::ReparsePoint)) { Remove-Item -LiteralPath $resolvedTemp -Recurse -Force } else { throw 'Refusing to clean an unexpected installer temporary directory.' } }