# Factory CLI Windows Installer # Usage: # curl.exe -b 'session=cookie_value' -fsSL https://app.factory.ai/cli/windows -o install.ps1 # powershell -ExecutionPolicy Bypass -File install.ps1 # del install.ps1 $ErrorActionPreference = 'Stop' # Function to write colored output function Write-Info($message) { Write-Host "$message" -ForegroundColor Green } function Write-Error($message) { Write-Host "$message" -ForegroundColor Red exit 1 } function Install-Binary($source, $destination) { $stagedPath = "$destination.next-$PID" $backupPath = $null try { # Stage beside the destination so the launch path never names a partial copy. # A crashed earlier run under the same pid is the only other writer of this name. Copy-Item -LiteralPath $source -Destination $stagedPath -Force -ErrorAction Stop # Running executables can be renamed; the ticks keep repeated installs unique. # A destination that is not there leaves nothing to move aside, which is # not this run's failure: ask by renaming rather than by Test-Path, so a # concurrent installer cannot slip between the check and the move. $candidate = "$destination.old-$PID-$([DateTime]::UtcNow.Ticks)" try { Move-Item -LiteralPath $destination -Destination $candidate -ErrorAction Stop } catch { if (Test-Path -LiteralPath $destination) { throw } } # Record the backup only once it exists, so a restore never targets a # path this run merely intended to create. if (Test-Path -LiteralPath $candidate) { $backupPath = $candidate } # Publish without -Force. Move-Item replaces an existing destination when # forced, which would overwrite a release that another producer put here # between the move-aside above and this call, and its bytes would be # gone. Unforced, the move refuses and reports it, so the catch below # restores this run's backup instead of consuming a stranger's release. Move-Item -LiteralPath $stagedPath -Destination $destination -ErrorAction Stop } catch { Remove-Item -LiteralPath $stagedPath -Force -ErrorAction SilentlyContinue # Restore only when publishing left no executable at the launch path, and # never let a failed restore become the error the user sees: the publish # failure is the one that explains what happened, so this warns and falls # through to the rethrow below. if ($backupPath -and -not (Test-Path -LiteralPath $destination)) { # Unforced for the same reason: something that arrived here is not # this run's to overwrite. try { Move-Item -LiteralPath $backupPath -Destination $destination -ErrorAction Stop } catch { Write-Warning "Could not restore the previous droid.exe from $backupPath" } } throw } # A running session maps the backup and refuses this; droid's startup sweep # deletes it once that session exits. if ($backupPath) { Remove-Item -LiteralPath $backupPath -Force -ErrorAction SilentlyContinue } } # Detect architecture # Prefer the native architecture when PowerShell is running under emulation $arch = $env:PROCESSOR_ARCHITEW6432 if ([string]::IsNullOrWhiteSpace($arch)) { $arch = $env:PROCESSOR_ARCHITECTURE } if ($arch -eq "AMD64" -or $arch -eq "X64") { $architecture = "x64" } elseif ($arch -eq "ARM64") { $architecture = "arm64" } else { Write-Error "Unsupported architecture: $arch" } # Detect AVX2 support for x64 only if ($architecture -eq "x64") { # Use Windows kernel32.dll API to check for AVX2 support # Feature ID 40 = PF_AVX2_INSTRUCTIONS_AVAILABLE # This is the same method Bun uses for baseline detection try { $hasAvx2 = (Add-Type -MemberDefinition '[DllImport("kernel32.dll")] public static extern bool IsProcessorFeaturePresent(int ProcessorFeature);' -Name 'Kernel32' -Namespace 'Win32' -PassThru -ErrorAction Stop)::IsProcessorFeaturePresent(40) } catch { # If type already exists or other error, try to use it directly try { $hasAvx2 = ([Win32.Kernel32]::IsProcessorFeaturePresent(40)) } catch { # If all else fails, assume no AVX2 support (safer default for compatibility) $hasAvx2 = $false } } if (-not $hasAvx2) { $architecture = "x64-baseline" } } $version = "1.23.0" $baseUrl = "https://downloads.factory.ai" $binaryName = "droid.exe" $url = "$baseUrl/factory-cli/releases/$version/windows/$architecture/$binaryName" $shaUrl = "$baseUrl/factory-cli/releases/$version/windows/$architecture/$binaryName.sha256" Write-Info "Downloading Factory CLI v$version for Windows-$architecture" # Create temporary directory $tempDir = New-TemporaryFile | ForEach-Object { Remove-Item $_; New-Item -ItemType Directory -Path $_ } $binaryPath = Join-Path $tempDir $binaryName try { # Check for curl if (!(Get-Command curl.exe -ErrorAction SilentlyContinue)) { Write-Error "curl is required but not found. Please install curl or upgrade to Windows 10 1803+ where it's built-in." } & curl.exe -fsSL -o $binaryPath $url if ($LASTEXITCODE -ne 0) { Write-Error "Download failed" } Write-Info "Fetching and verifying checksum" $expectedHash = (& curl.exe -fsSL $shaUrl).Trim() if ($LASTEXITCODE -ne 0) { Write-Error "Failed to fetch checksum" } # Verify checksum try { $actualHash = (Get-FileHash -Path $binaryPath -Algorithm SHA256).Hash.ToLower() if ($actualHash -ne $expectedHash.ToLower()) { Write-Error "Checksum verification failed" } Write-Info "Checksum verification passed" } catch { Write-Error "Could not verify checksum" } # Determine installation directories $installDir = $null # Try user's local bin directory first $userBin = Join-Path $env:USERPROFILE "bin" if (Test-Path $userBin) { $installDir = $userBin } else { # Create user bin directory New-Item -ItemType Directory -Path $userBin -Force | Out-Null $installDir = $userBin } $installPath = Join-Path $installDir $binaryName Install-Binary $binaryPath $installPath Write-Info "Factory CLI v$version installed successfully to $installPath" # Check if installation directory is in persistent PATH $userPath = [Environment]::GetEnvironmentVariable('Path','User') if ($userPath -notlike "*$installDir*") { # Add to persistent PATH [Environment]::SetEnvironmentVariable('Path', $userPath + ";$installDir", 'User') # Update current session $env:PATH = "$installDir;$env:PATH" Write-Info "Added $installDir to your PATH permanently" } else { Write-Info "PATH already configured" } Write-Info "Run 'droid' to get started!" } finally { # Clean up temporary directory Remove-Item $tempDir -Recurse -Force -ErrorAction SilentlyContinue }