1[CmdletBinding()]
  2Param(
  3    [Parameter()][Alias('i')][switch]$Install,
  4    [Parameter()][Alias('h')][switch]$Help,
  5    [Parameter()][Alias('a')][string]$Architecture,
  6    [Parameter()][string]$Name
  7)
  8
  9. "$PSScriptRoot/lib/workspace.ps1"
 10
 11# https://stackoverflow.com/questions/57949031/powershell-script-stops-if-program-fails-like-bash-set-o-errexit
 12$ErrorActionPreference = 'Stop'
 13$PSNativeCommandUseErrorActionPreference = $true
 14
 15$buildSuccess = $false
 16
 17$OSArchitecture = switch ([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture) {
 18    "X64" { "x86_64" }
 19    "Arm64" { "aarch64" }
 20    default { throw "Unsupported architecture" }
 21}
 22
 23$Architecture = if ($Architecture) {
 24    $Architecture
 25} else {
 26    $OSArchitecture
 27}
 28
 29$CargoOutDir = "./target/$Architecture-pc-windows-msvc/release"
 30
 31function Get-VSArch {
 32    param(
 33        [string]$Arch
 34    )
 35
 36    switch ($Arch) {
 37        "x86_64" { "amd64" }
 38        "aarch64" { "arm64" }
 39    }
 40}
 41
 42Push-Location
 43& "C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\Tools\Launch-VsDevShell.ps1" -Arch (Get-VSArch -Arch $Architecture) -HostArch (Get-VSArch -Arch $OSArchitecture)
 44Pop-Location
 45
 46$target = "$Architecture-pc-windows-msvc"
 47
 48if ($Help) {
 49    Write-Output "Usage: test.ps1 [-Install] [-Help]"
 50    Write-Output "Build the installer for Windows.\n"
 51    Write-Output "Options:"
 52    Write-Output "  -Architecture, -a Which architecture to build (x86_64 or aarch64)"
 53    Write-Output "  -Install, -i      Run the installer after building."
 54    Write-Output "  -Help, -h         Show this help message."
 55    exit 0
 56}
 57
 58Push-Location -Path crates/zed
 59$channel = Get-Content "RELEASE_CHANNEL"
 60$env:ZED_RELEASE_CHANNEL = $channel
 61$env:RELEASE_CHANNEL = $channel
 62Pop-Location
 63
 64function CheckEnvironmentVariables {
 65    if(-not $env:CI) {
 66        return
 67    }
 68
 69    $requiredVars = @(
 70        'ZED_WORKSPACE', 'RELEASE_VERSION', 'ZED_RELEASE_CHANNEL',
 71        'AZURE_TENANT_ID', 'AZURE_CLIENT_ID', 'AZURE_CLIENT_SECRET',
 72        'ACCOUNT_NAME', 'CERT_PROFILE_NAME', 'ENDPOINT',
 73        'FILE_DIGEST', 'TIMESTAMP_DIGEST', 'TIMESTAMP_SERVER'
 74    )
 75
 76    foreach ($var in $requiredVars) {
 77        if (-not (Test-Path "env:$var")) {
 78            Write-Error "$var is not set"
 79            exit 1
 80        }
 81    }
 82}
 83
 84function PrepareForBundle {
 85    if (Test-Path "$innoDir") {
 86        Remove-Item -Path "$innoDir" -Recurse -Force
 87    }
 88    New-Item -Path "$innoDir" -ItemType Directory -Force
 89    Copy-Item -Path "$env:ZED_WORKSPACE\crates\zed\resources\windows\*" -Destination "$innoDir" -Recurse -Force
 90    New-Item -Path "$innoDir\make_appx" -ItemType Directory -Force
 91    New-Item -Path "$innoDir\appx" -ItemType Directory -Force
 92    New-Item -Path "$innoDir\bin" -ItemType Directory -Force
 93    New-Item -Path "$innoDir\tools" -ItemType Directory -Force
 94
 95    rustup target add $target
 96}
 97
 98function GenerateLicenses {
 99    $oldErrorActionPreference = $ErrorActionPreference
100    $ErrorActionPreference = 'Continue'
101    . $PSScriptRoot/generate-licenses.ps1
102    $ErrorActionPreference = $oldErrorActionPreference
103}
104
105function BuildZedAndItsFriends {
106    Write-Output "Building Zed and its friends, for channel: $channel"
107    # Build zed.exe, cli.exe and auto_update_helper.exe
108    cargo build --release --package zed --package cli --package auto_update_helper --target $target
109    Copy-Item -Path ".\$CargoOutDir\zed.exe" -Destination "$innoDir\Zed.exe" -Force
110    Copy-Item -Path ".\$CargoOutDir\cli.exe" -Destination "$innoDir\cli.exe" -Force
111    Copy-Item -Path ".\$CargoOutDir\auto_update_helper.exe" -Destination "$innoDir\auto_update_helper.exe" -Force
112    # Build explorer_command_injector.dll
113    switch ($channel) {
114        "stable" {
115            cargo build --release --features stable --no-default-features --package explorer_command_injector --target $target
116        }
117        "preview" {
118            cargo build --release --features preview --no-default-features --package explorer_command_injector --target $target
119        }
120        default {
121            cargo build --release --package explorer_command_injector --target $target
122        }
123    }
124    Copy-Item -Path ".\$CargoOutDir\explorer_command_injector.dll" -Destination "$innoDir\zed_explorer_command_injector.dll" -Force
125}
126
127function ZipZedAndItsFriendsDebug {
128    $items = @(
129        ".\$CargoOutDir\zed.pdb",
130        ".\$CargoOutDir\cli.pdb",
131        ".\$CargoOutDir\auto_update_helper.pdb",
132        ".\$CargoOutDir\explorer_command_injector.pdb"
133    )
134
135    Compress-Archive -Path $items -DestinationPath ".\$CargoOutDir\zed-$env:RELEASE_VERSION-$env:ZED_RELEASE_CHANNEL.dbg.zip" -Force
136}
137
138
139function UploadToSentry {
140    if (-not (Get-Command "sentry-cli" -ErrorAction SilentlyContinue)) {
141        Write-Output "sentry-cli not found. skipping sentry upload."
142        Write-Output "install with: 'winget install -e --id=Sentry.sentry-cli'"
143        return
144    }
145    if (-not (Test-Path "env:SENTRY_AUTH_TOKEN")) {
146        Write-Output "missing SENTRY_AUTH_TOKEN. skipping sentry upload."
147        return
148    }
149    Write-Output "Uploading zed debug symbols to sentry..."
150    sentry-cli debug-files upload --include-sources --wait -p zed -o zed-dev $CargoOutDir
151}
152
153function MakeAppx {
154    switch ($channel) {
155        "stable" {
156            $manifestFile = "$env:ZED_WORKSPACE\crates\explorer_command_injector\AppxManifest.xml"
157        }
158        "preview" {
159            $manifestFile = "$env:ZED_WORKSPACE\crates\explorer_command_injector\AppxManifest-Preview.xml"
160        }
161        default {
162            $manifestFile = "$env:ZED_WORKSPACE\crates\explorer_command_injector\AppxManifest-Nightly.xml"
163        }
164    }
165    Copy-Item -Path "$manifestFile" -Destination "$innoDir\make_appx\AppxManifest.xml"
166    # Add makeAppx.exe to Path
167    $sdk = "C:\Program Files (x86)\Windows Kits\10\bin\10.0.26100.0\x64"
168    $env:Path += ';' + $sdk
169    makeAppx.exe pack /d "$innoDir\make_appx" /p "$innoDir\zed_explorer_command_injector.appx" /nv
170}
171
172function SignZedAndItsFriends {
173    if (-not $env:CI) {
174        return
175    }
176
177    $files = "$innoDir\Zed.exe,$innoDir\cli.exe,$innoDir\auto_update_helper.exe,$innoDir\zed_explorer_command_injector.dll,$innoDir\zed_explorer_command_injector.appx"
178    & "$innoDir\sign.ps1" $files
179}
180
181function DownloadAMDGpuServices {
182    # If you update the AGS SDK version, please also update the version in `crates/gpui/src/platform/windows/directx_renderer.rs`
183    $url = "https://codeload.github.com/GPUOpen-LibrariesAndSDKs/AGS_SDK/zip/refs/tags/v6.3.0"
184    $zipPath = ".\AGS_SDK_v6.3.0.zip"
185    # Download the AGS SDK zip file
186    Invoke-WebRequest -Uri $url -OutFile $zipPath
187    # Extract the AGS SDK zip file
188    Expand-Archive -Path $zipPath -DestinationPath "." -Force
189}
190
191function DownloadConpty {
192    $url = "https://www.nuget.org/api/v2/package/CI.Microsoft.Windows.Console.ConPTY/1.22.250314001"
193    $zipPath = ".\conpty.zip"
194    Invoke-WebRequest -Uri $url -OutFile $zipPath
195    Expand-Archive -Path $zipPath -DestinationPath ".\conpty" -Force
196}
197
198function CollectFiles {
199    Move-Item -Path "$innoDir\zed_explorer_command_injector.appx" -Destination "$innoDir\appx\zed_explorer_command_injector.appx" -Force
200    Move-Item -Path "$innoDir\zed_explorer_command_injector.dll" -Destination "$innoDir\appx\zed_explorer_command_injector.dll" -Force
201    Move-Item -Path "$innoDir\cli.exe" -Destination "$innoDir\bin\zed.exe" -Force
202    Move-Item -Path "$innoDir\zed.sh" -Destination "$innoDir\bin\zed" -Force
203    Move-Item -Path "$innoDir\auto_update_helper.exe" -Destination "$innoDir\tools\auto_update_helper.exe" -Force
204    if($Architecture -eq "aarch64") {
205        New-Item -Type Directory -Path "$innoDir\arm64" -Force
206        Move-Item -Path ".\conpty\build\native\runtimes\arm64\OpenConsole.exe" -Destination "$innoDir\arm64\OpenConsole.exe" -Force
207        Move-Item -Path ".\conpty\runtimes\win10-arm64\native\conpty.dll" -Destination "$innoDir\conpty.dll" -Force
208    }
209    else {
210        New-Item -Type Directory -Path "$innoDir\x64" -Force
211        New-Item -Type Directory -Path "$innoDir\arm64" -Force
212        Move-Item -Path ".\AGS_SDK-6.3.0\ags_lib\lib\amd_ags_x64.dll" -Destination "$innoDir\amd_ags_x64.dll" -Force
213        Move-Item -Path ".\conpty\build\native\runtimes\x64\OpenConsole.exe" -Destination "$innoDir\x64\OpenConsole.exe" -Force
214        Move-Item -Path ".\conpty\build\native\runtimes\arm64\OpenConsole.exe" -Destination "$innoDir\arm64\OpenConsole.exe" -Force
215        Move-Item -Path ".\conpty\runtimes\win10-x64\native\conpty.dll" -Destination "$innoDir\conpty.dll" -Force
216    }
217}
218
219function BuildInstaller {
220    $issFilePath = "$innoDir\zed.iss"
221    switch ($channel) {
222        "stable" {
223            $appId = "{{2DB0DA96-CA55-49BB-AF4F-64AF36A86712}"
224            $appIconName = "app-icon"
225            $appName = "Zed"
226            $appDisplayName = "Zed"
227            $appSetupName = "Zed-$Architecture"
228            # The mutex name here should match the mutex name in crates\zed\src\zed\windows_only_instance.rs
229            $appMutex = "Zed-Stable-Instance-Mutex"
230            $appExeName = "Zed"
231            $regValueName = "Zed"
232            $appUserId = "ZedIndustries.Zed"
233            $appShellNameShort = "Z&ed"
234            $appAppxFullName = "ZedIndustries.Zed_1.0.0.0_neutral__japxn1gcva8rg"
235        }
236        "preview" {
237            $appId = "{{F70E4811-D0E2-4D88-AC99-D63752799F95}"
238            $appIconName = "app-icon-preview"
239            $appName = "Zed Preview"
240            $appDisplayName = "Zed Preview"
241            $appSetupName = "Zed-$Architecture"
242            # The mutex name here should match the mutex name in crates\zed\src\zed\windows_only_instance.rs
243            $appMutex = "Zed-Preview-Instance-Mutex"
244            $appExeName = "Zed"
245            $regValueName = "ZedPreview"
246            $appUserId = "ZedIndustries.Zed.Preview"
247            $appShellNameShort = "Z&ed Preview"
248            $appAppxFullName = "ZedIndustries.Zed.Preview_1.0.0.0_neutral__japxn1gcva8rg"
249        }
250        "nightly" {
251            $appId = "{{1BDB21D3-14E7-433C-843C-9C97382B2FE0}"
252            $appIconName = "app-icon-nightly"
253            $appName = "Zed Nightly"
254            $appDisplayName = "Zed Nightly"
255            $appSetupName = "Zed-$Architecture"
256            # The mutex name here should match the mutex name in crates\zed\src\zed\windows_only_instance.rs
257            $appMutex = "Zed-Nightly-Instance-Mutex"
258            $appExeName = "Zed"
259            $regValueName = "ZedNightly"
260            $appUserId = "ZedIndustries.Zed.Nightly"
261            $appShellNameShort = "Z&ed Editor Nightly"
262            $appAppxFullName = "ZedIndustries.Zed.Nightly_1.0.0.0_neutral__japxn1gcva8rg"
263        }
264        "dev" {
265            $appId = "{{8357632E-24A4-4F32-BA97-E575B4D1FE5D}"
266            $appIconName = "app-icon-dev"
267            $appName = "Zed Dev"
268            $appDisplayName = "Zed Dev"
269            $appSetupName = "Zed-$Architecture"
270            # The mutex name here should match the mutex name in crates\zed\src\zed\windows_only_instance.rs
271            $appMutex = "Zed-Dev-Instance-Mutex"
272            $appExeName = "Zed"
273            $regValueName = "ZedDev"
274            $appUserId = "ZedIndustries.Zed.Dev"
275            $appShellNameShort = "Z&ed Dev"
276            $appAppxFullName = "ZedIndustries.Zed.Dev_1.0.0.0_neutral__japxn1gcva8rg"
277        }
278        default {
279            Write-Error "can't bundle installer for $channel."
280            exit 1
281        }
282    }
283
284    # Windows runner 2022 default has iscc in PATH, https://github.com/actions/runner-images/blob/main/images/windows/Windows2022-Readme.md
285    # Currently, we are using Windows 2022 runner.
286    # Windows runner 2025 doesn't have iscc in PATH for now, https://github.com/actions/runner-images/issues/11228
287    $innoSetupPath = "C:\Program Files (x86)\Inno Setup 6\ISCC.exe"
288
289    $definitions = @{
290        "AppId"          = $appId
291        "AppIconName"    = $appIconName
292        "OutputDir"      = "$env:ZED_WORKSPACE\target"
293        "AppSetupName"   = $appSetupName
294        "AppName"        = $appName
295        "AppDisplayName" = $appDisplayName
296        "RegValueName"   = $regValueName
297        "AppMutex"       = $appMutex
298        "AppExeName"     = $appExeName
299        "ResourcesDir"   = "$innoDir"
300        "ShellNameShort" = $appShellNameShort
301        "AppUserId"      = $appUserId
302        "Version"        = "$env:RELEASE_VERSION"
303        "SourceDir"      = "$env:ZED_WORKSPACE"
304        "AppxFullName"   = $appAppxFullName
305    }
306
307    $defs = @()
308    foreach ($key in $definitions.Keys) {
309        $defs += "/d$key=`"$($definitions[$key])`""
310    }
311
312    $innoArgs = @($issFilePath) + $defs
313    if($env:CI) {
314        $signTool = "powershell.exe -ExecutionPolicy Bypass -File $innoDir\sign.ps1 `$f"
315        $innoArgs += "/sDefaultsign=`"$signTool`""
316    }
317
318    # Execute Inno Setup
319    Write-Host "🚀 Running Inno Setup: $innoSetupPath $innoArgs"
320    $process = Start-Process -FilePath $innoSetupPath -ArgumentList $innoArgs -NoNewWindow -Wait -PassThru
321
322    if ($process.ExitCode -eq 0) {
323        Write-Host "✅ Inno Setup successfully compiled the installer"
324        Write-Output "SETUP_PATH=target/$appSetupName.exe" >> $env:GITHUB_ENV
325        $script:buildSuccess = $true
326    }
327    else {
328        Write-Host "❌ Inno Setup failed: $($process.ExitCode)"
329        $script:buildSuccess = $false
330    }
331}
332
333ParseZedWorkspace
334$innoDir = "$env:ZED_WORKSPACE\inno\$Architecture"
335$debugArchive = "$CargoOutDir\zed-$env:RELEASE_VERSION-$env:ZED_RELEASE_CHANNEL.dbg.zip"
336$debugStoreKey = "$env:ZED_RELEASE_CHANNEL/zed-$env:RELEASE_VERSION-$env:ZED_RELEASE_CHANNEL.dbg.zip"
337
338CheckEnvironmentVariables
339PrepareForBundle
340GenerateLicenses
341BuildZedAndItsFriends
342MakeAppx
343SignZedAndItsFriends
344ZipZedAndItsFriendsDebug
345DownloadAMDGpuServices
346DownloadConpty
347CollectFiles
348BuildInstaller
349
350if($env:CI) {
351    UploadToSentry
352}
353
354if ($buildSuccess) {
355    Write-Output "Build successful"
356    if ($Install) {
357        Write-Output "Installing Zed..."
358        Start-Process -FilePath "$env:ZED_WORKSPACE/target/ZedEditorUserSetup-x64-$env:RELEASE_VERSION.exe"
359    }
360    exit 0
361}
362else {
363    Write-Output "Build failed"
364    exit 1
365}