& { $ErrorActionPreference = 'Stop' $packageId = 'Oscova.Agents.App' $env:DOTNET_CLI_TELEMETRY_OPTOUT = '1' # The .NET SDK reports usage to Microsoft unless opted out; keep every dotnet call in this install silent. $sdkUrl = 'https://builds.dotnet.microsoft.com/dotnet/Sdk/10.0.401/dotnet-sdk-10.0.401-win-x64.exe' $sdkHash = 'f0d8f8e7ec24efb05172a65dd80c4a9b1ef17efcebdbf0f57c15f436eea417960a7eeb6726c473a042373d6a8b94ac1adc7d680decbf7d2c45fa5c5662d62265' $manualUrl = 'https://learn.microsoft.com/dotnet/core/install/windows' function Invoke-DotnetText([string] $HostPath, [string[]] $Arguments) { $result = & $HostPath @Arguments if ($LASTEXITCODE -ne 0) { throw "dotnet $($Arguments[0]) failed. See the error above." } return ($result -join "`n") } function Get-ReadySdk([string] $HostPath) { $info = Invoke-DotnetText $HostPath @('--info') if ($info -notmatch '(?ms)Host:\s*.*?Architecture:\s*x64\s') { return $null } $sdks = Invoke-DotnetText $HostPath @('--list-sdks') $runtimes = Invoke-DotnetText $HostPath @('--list-runtimes') if ($runtimes -notmatch '(?m)^Microsoft\.NETCore\.App\s+10\.\d+\.\d+\s+' -or $runtimes -notmatch '(?m)^Microsoft\.AspNetCore\.App\s+10\.\d+\.\d+\s+') { return $null } $versions = @([regex]::Matches($sdks, '(?m)^(10\.\d+\.\d+)\s+\[') | ForEach-Object { [version]$_.Groups[1].Value } | Sort-Object -Descending) if ($versions.Count -eq 0) { return $null } return $versions[0].ToString() } if ([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture.ToString() -ne 'X64') { throw 'Oscova requires native Windows x64. Use a supported computer.' } $identity = [Security.Principal.WindowsIdentity]::GetCurrent() $principal = [Security.Principal.WindowsPrincipal]::new($identity) if ($principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) { throw 'Run this command in a normal PowerShell window, without Run as administrator.' } # Prefer the native host when a 32-bit host appears earlier on PATH. $nativeProgramFiles = if ($env:ProgramW6432) { $env:ProgramW6432 } else { $env:ProgramFiles } $nativeDotnet = Join-Path $nativeProgramFiles 'dotnet\dotnet.exe' $dotnet = if (Test-Path -LiteralPath $nativeDotnet -PathType Leaf) { $nativeDotnet } else { $command = Get-Command dotnet.exe -ErrorAction SilentlyContinue | Select-Object -First 1 if ($command) { $command.Source } else { $null } } if ($env:DOTNET_ROOT -and [IO.Path]::GetFullPath($env:DOTNET_ROOT).TrimEnd('\') -ine [IO.Path]::GetDirectoryName($nativeDotnet).TrimEnd('\')) { throw "DOTNET_ROOT points to a different .NET installation: $env:DOTNET_ROOT" } $sdk = if ($dotnet) { Get-ReadySdk $dotnet } else { $null } $tempDir = $null try { if (-not $sdk) { $answer = try { Read-Host 'Oscova needs the .NET 10 SDK. Install it now? [y/N]' } catch { '' } if ($answer -notmatch '^[Yy]$') { throw "The .NET prerequisite was declined or input is unavailable. Install it manually: $manualUrl" } $tempDir = Join-Path ([IO.Path]::GetTempPath()) ("oscova-install-" + [guid]::NewGuid().ToString('N')) New-Item -ItemType Directory -Path $tempDir | Out-Null $winget = Get-Command winget.exe -ErrorAction SilentlyContinue | Select-Object -First 1 if ($winget) { Write-Host 'WinGet will request administrator approval for the machine-wide .NET SDK.' & $winget.Source install --exact --id Microsoft.DotNet.SDK.10 --source winget --architecture x64 --scope machine --accept-package-agreements --accept-source-agreements if ($LASTEXITCODE -ne 0) { throw "WinGet could not install the .NET 10 SDK. Install it manually: $manualUrl" } } else { $installer = Join-Path $tempDir 'dotnet-sdk.exe' Write-Host 'Downloading the official .NET SDK. Its installer will request administrator approval.' Invoke-WebRequest -Uri $sdkUrl -OutFile $installer -UseBasicParsing if ((Get-FileHash -LiteralPath $installer -Algorithm SHA512).Hash -ine $sdkHash) { throw "The .NET SDK download failed SHA-512 verification. Install it manually: $manualUrl" } $process = Start-Process -FilePath $installer -Wait -PassThru if ($process.ExitCode -ne 0) { throw "The .NET SDK installer failed or was canceled. Install it manually: $manualUrl" } } $dotnet = $nativeDotnet if (-not (Test-Path -LiteralPath $dotnet -PathType Leaf)) { throw "The native .NET host was not found after setup. Install it manually: $manualUrl" } $sdk = Get-ReadySdk $dotnet if (-not $sdk) { throw "A stable x64 .NET 10 SDK and both runtimes were not found after setup. Install them manually: $manualUrl" } } if (-not $tempDir) { $tempDir = Join-Path ([IO.Path]::GetTempPath()) ("oscova-install-" + [guid]::NewGuid().ToString('N')) New-Item -ItemType Directory -Path $tempDir | Out-Null } # Select the usable SDK and public feed without changing caller configuration. [IO.File]::WriteAllText((Join-Path $tempDir 'global.json'), ('{{"sdk":{{"version":"{0}","rollForward":"disable","allowPrerelease":false}}}}' -f $sdk)) $nugetConfig = Join-Path $tempDir 'NuGet.Config' [IO.File]::WriteAllText($nugetConfig, '') Push-Location -LiteralPath $tempDir try { $toolList = Invoke-DotnetText $dotnet @('tool', 'list', '--global') $installed = @($toolList -split "`n" | Where-Object { $_ -match '^\s*Oscova\.Agents\.App\s+' }) if ($installed.Count -gt 1) { throw 'The installed Oscova package list is ambiguous.' } if ($installed.Count -eq 1) { if ($installed[0] -notmatch '^\s*Oscova\.Agents\.App\s+(\S+)\s+oscova\s*$') { throw 'Cannot read the installed Oscova package record safely.' } Write-Host "Oscova $($Matches[1]) is already installed for this account. Update instructions: https://oscova.ai/docs/articles/user-guide/getting-started/command-line.html#install" } else { & $dotnet tool install --global $packageId --no-cache --configfile $nugetConfig if ($LASTEXITCODE -ne 0) { throw 'NuGet tool installation failed. See the package error above.' } } } finally { Pop-Location } $toolsDir = Join-Path $env:USERPROFILE '.dotnet\tools' $tool = Join-Path $toolsDir 'oscova.cmd' if (-not (Test-Path -LiteralPath $tool -PathType Leaf)) { throw "The installed command is missing: $tool" } $version = & $tool --version if ($LASTEXITCODE -ne 0) { throw 'oscova --version failed.' } & $tool --help | Out-Null if ($LASTEXITCODE -ne 0) { throw 'oscova --help failed.' } $userPath = [Environment]::GetEnvironmentVariable('Path', 'User') if ($null -eq $userPath) { $userPath = '' } $hasToolsPath = @($userPath -split ';' | Where-Object { [Environment]::ExpandEnvironmentVariables($_).TrimEnd('\') -ieq $toolsDir.TrimEnd('\') }).Count -gt 0 if (-not $hasToolsPath) { [Environment]::SetEnvironmentVariable('Path', (($userPath.TrimEnd(';') + ';' + $toolsDir).TrimStart(';')), 'User') } if (@($env:Path -split ';' | Where-Object { $_.TrimEnd('\') -ieq $toolsDir.TrimEnd('\') }).Count -eq 0) { $env:Path = $env:Path.TrimEnd(';') + ';' + $toolsDir } $discovered = Get-Command oscova -ErrorAction SilentlyContinue | Select-Object -First 1 if (-not $discovered -or $discovered.Source -ine $tool) { throw "The installed shim works, but command lookup does not reach $tool. Move $toolsDir earlier on PATH." } Write-Host "Oscova $version is ready." $startNow = -not [Console]::IsInputRedirected -and -not [Console]::IsOutputRedirected if ($startNow) { Write-Host 'Starting Oscova. Next time, run: oscova start' } else { Write-Host 'Next: oscova start' } } finally { if ($tempDir -and (Test-Path -LiteralPath $tempDir)) { try { Remove-Item -LiteralPath $tempDir -Recurse -Force } catch { Write-Warning "Could not remove temporary installer files at ${tempDir}: $($_.Exception.Message)" } } } if ($startNow) { & $tool start --detach } }