-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.ps1
More file actions
130 lines (112 loc) · 4.67 KB
/
Copy pathinstall.ps1
File metadata and controls
130 lines (112 loc) · 4.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# Thin bootstrap for remote installation.
#
# Review this file before running it with:
# irm <raw-url>/install.ps1 | iex
param(
[ValidateSet('Prompt', 'Codex', 'Claude', 'Both', 'None')]
[string]$Agent = 'Prompt',
[switch]$Yes,
[switch]$NonInteractive,
[switch]$SkipTools,
[switch]$SkipProfile,
[string]$Repo = 'CodingAgentSetup/CodingAgentSetup',
[string]$Ref = '',
[ValidateSet('sha', 'tag', 'branch')]
[string]$RefKind = 'sha'
)
$ErrorActionPreference = 'Stop'
Write-Warning 'This bootstrap downloads and executes setup-windows.ps1 from GitHub. Review the repository before running it on a machine you care about.'
function Test-Command {
param([Parameter(Mandatory = $true)][string]$Name)
return [bool](Get-Command $Name -ErrorAction SilentlyContinue)
}
function Get-PwshCommand {
$pwsh = Get-Command pwsh -ErrorAction SilentlyContinue
if ($pwsh) { return $pwsh }
$candidateRoots = @(
$env:ProgramFiles,
${env:ProgramFiles(x86)},
$(if ($env:LOCALAPPDATA) { Join-Path $env:LOCALAPPDATA 'Microsoft\WinGet\Links' })
) | Where-Object { $_ }
$candidates = foreach ($root in $candidateRoots) {
if ((Split-Path -Leaf $root) -eq 'Links') {
Join-Path $root 'pwsh.exe'
} else {
Join-Path $root 'PowerShell\7\pwsh.exe'
}
}
$candidates = @($candidates | Where-Object { Test-Path -LiteralPath $_ -PathType Leaf })
if ($candidates) {
$pwshPath = $candidates[0]
$pwshDir = Split-Path -Parent $pwshPath
if (($env:Path -split ';') -notcontains $pwshDir) {
$env:Path += ";$pwshDir"
}
return Get-Command $pwshPath -ErrorAction SilentlyContinue
}
return $null
}
try {
if ((Get-ExecutionPolicy -Scope CurrentUser) -ne 'RemoteSigned') {
Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy RemoteSigned -Force
}
} catch {
Write-Warning "Could not set CurrentUser execution policy to RemoteSigned: $($_.Exception.Message)"
}
if (-not (Get-PwshCommand)) {
if (-not (Test-Command winget)) {
throw 'PowerShell 7 (pwsh) is not installed, and winget is unavailable. Install PowerShell 7 from Microsoft.PowerShell or install App Installer, then rerun this bootstrap.'
}
Write-Host 'Installing PowerShell 7 (Microsoft.PowerShell) with winget'
winget install --id Microsoft.PowerShell -e --accept-package-agreements --accept-source-agreements
if ($LASTEXITCODE -ne 0) {
throw "winget failed to install PowerShell 7 with exit code $LASTEXITCODE."
}
}
if ([string]::IsNullOrWhiteSpace($Ref)) {
$runs = Invoke-RestMethod -Uri "https://api.github.com/repos/$Repo/actions/workflows/smoke.yml/runs?status=success&event=push&per_page=50"
$run = @($runs.workflow_runs | Where-Object { $_.head_branch -like 'ci-*' -and $_.head_sha } | Select-Object -First 1)
if (-not $run) {
throw @"
Could not find a successful CI-verified commit (ci-* branch) for $Repo.
This can happen if the repository uses a different CI naming convention or has no passing smoke runs yet.
Specify a ref explicitly, for example:
-Ref main -RefKind branch
-Ref v1.2.3 -RefKind tag
-Ref <commit-sha> -RefKind sha
"@
}
$ciTag = $run[0].head_branch
$Ref = $run[0].head_sha
$RefKind = 'sha'
}
if ($RefKind -eq 'sha') {
if ($ciTag) {
Write-Host "Using latest successful CI tag $ciTag at $Ref from $Repo"
} else {
Write-Host "Using commit $Ref from $Repo"
}
$archiveUrl = "https://github.com/$Repo/archive/$Ref.zip"
} else {
Write-Host "Using $RefKind $Ref from $Repo"
$refPath = if ($RefKind -eq 'tag') { 'tags' } else { 'heads' }
$archiveUrl = "https://github.com/$Repo/archive/refs/$refPath/$Ref.zip"
}
$tempRoot = Join-Path $env:TEMP ('CodingAgentSetup-' + [guid]::NewGuid().ToString('N'))
$zipPath = Join-Path $tempRoot 'repo.zip'
$extractPath = Join-Path $tempRoot 'repo'
New-Item -ItemType Directory -Force -Path $tempRoot | Out-Null
Invoke-WebRequest -Uri $archiveUrl -OutFile $zipPath
Expand-Archive -LiteralPath $zipPath -DestinationPath $extractPath -Force
$setup = Get-ChildItem -LiteralPath $extractPath -Recurse -Filter 'setup-windows.ps1' | Select-Object -First 1
if (-not $setup) { throw 'setup-windows.ps1 was not found in the downloaded archive.' }
$args = @('-ExecutionPolicy', 'Bypass', '-File', $setup.FullName, '-Agent', $Agent)
if ($Yes) { $args += '-Yes' }
if ($NonInteractive) { $args += '-NonInteractive' }
if ($SkipTools) { $args += '-SkipTools' }
if ($SkipProfile) { $args += '-SkipProfile' }
$pwsh = Get-PwshCommand
if (-not $pwsh) {
throw 'PowerShell 7 (pwsh) is still unavailable after installation.'
}
& $pwsh.Source @args