bundle-windows.ps1

  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    . $PSScriptRoot/generate-licenses.ps1
100}
101
102function BuildZedAndItsFriends {
103    Write-Output "Building Zed and its friends, for channel: $channel"
104    # Build zed.exe, cli.exe and auto_update_helper.exe
105    cargo build --release --package zed --package cli --package auto_update_helper --target $target
106    Copy-Item -Path ".\$CargoOutDir\zed.exe" -Destination "$innoDir\Zed.exe" -Force
107    Copy-Item -Path ".\$CargoOutDir\cli.exe" -Destination "$innoDir\cli.exe" -Force
108    Copy-Item -Path ".\$CargoOutDir\auto_update_helper.exe" -Destination "$innoDir\auto_update_helper.exe" -Force
109    # Build explorer_command_injector.dll
110    switch ($channel) {
111        "stable" {
112            cargo build --release --features stable --no-default-features --package explorer_command_injector --target $target
113        }
114        "preview" {
115            cargo build --release --features preview --no-default-features --package explorer_command_injector --target $target
116        }
117        default {
118            cargo build --release --package explorer_command_injector --target $target
119        }
120    }
121    Copy-Item -Path ".\$CargoOutDir\explorer_command_injector.dll" -Destination "$innoDir\zed_explorer_command_injector.dll" -Force
122}
123
124function BuildRemoteServer {
125    Write-Output "Building remote_server for $target"
126    cargo build --release --package remote_server --target $target
127
128    # Create zipped remote server binary
129    $remoteServerSrc = (Resolve-Path ".\$CargoOutDir\remote_server.exe").Path
130
131    if ($env:CI) {
132        Write-Output "Code signing remote_server.exe"
133        & "$innoDir\sign.ps1" $remoteServerSrc
134    }
135
136    $remoteServerDst = "$env:ZED_WORKSPACE\target\zed-remote-server-windows-$Architecture.zip"
137    Write-Output "Compressing remote_server to $remoteServerDst"
138    Compress-Archive -Path $remoteServerSrc -DestinationPath $remoteServerDst -Force
139
140    Write-Output "Remote server compressed successfully"
141}
142
143function ZipZedAndItsFriendsDebug {
144    $items = @(
145        ".\$CargoOutDir\zed.pdb",
146        ".\$CargoOutDir\cli.pdb",
147        ".\$CargoOutDir\auto_update_helper.pdb",
148        ".\$CargoOutDir\explorer_command_injector.pdb",
149        ".\$CargoOutDir\remote_server.pdb"
150    )
151
152    Compress-Archive -Path $items -DestinationPath ".\$CargoOutDir\zed-$env:RELEASE_VERSION-$env:ZED_RELEASE_CHANNEL.dbg.zip" -Force
153}
154
155
156function UploadToSentry {
157    if (-not (Get-Command "sentry-cli" -ErrorAction SilentlyContinue)) {
158        Write-Output "sentry-cli not found. skipping sentry upload."
159        Write-Output "install with: 'winget install -e --id=Sentry.sentry-cli'"
160        return
161    }
162    if (-not (Test-Path "env:SENTRY_AUTH_TOKEN")) {
163        Write-Output "missing SENTRY_AUTH_TOKEN. skipping sentry upload."
164        return
165    }
166    Write-Output "Uploading zed debug symbols to sentry..."
167    for ($i = 1; $i -le 3; $i++) {
168        try {
169            sentry-cli debug-files upload --include-sources --wait -p zed -o zed-dev $CargoOutDir
170            break
171        }
172        catch {
173            Write-Output "Sentry upload attempt $i failed: $_"
174            if ($i -eq 3) {
175                Write-Output "All sentry upload attempts failed"
176                throw
177            }
178            Start-Sleep -Seconds 2
179        }
180    }
181}
182
183function MakeAppx {
184    switch ($channel) {
185        "stable" {
186            $manifestFile = "$env:ZED_WORKSPACE\crates\explorer_command_injector\AppxManifest.xml"
187        }
188        "preview" {
189            $manifestFile = "$env:ZED_WORKSPACE\crates\explorer_command_injector\AppxManifest-Preview.xml"
190        }
191        default {
192            $manifestFile = "$env:ZED_WORKSPACE\crates\explorer_command_injector\AppxManifest-Nightly.xml"
193        }
194    }
195    Copy-Item -Path "$manifestFile" -Destination "$innoDir\make_appx\AppxManifest.xml"
196    # Add makeAppx.exe to Path
197    $sdk = "C:\Program Files (x86)\Windows Kits\10\bin\10.0.26100.0\x64"
198    $env:Path += ';' + $sdk
199    makeAppx.exe pack /d "$innoDir\make_appx" /p "$innoDir\zed_explorer_command_injector.appx" /nv
200}
201
202function SignZedAndItsFriends {
203    if (-not $env:CI) {
204        return
205    }
206
207    $files = "$innoDir\Zed.exe,$innoDir\cli.exe,$innoDir\auto_update_helper.exe,$innoDir\zed_explorer_command_injector.dll,$innoDir\zed_explorer_command_injector.appx"
208    & "$innoDir\sign.ps1" $files
209}
210
211function DownloadAMDGpuServices {
212    # If you update the AGS SDK version, please also update the version in `crates/gpui/src/platform/windows/directx_renderer.rs`
213    $url = "https://codeload.github.com/GPUOpen-LibrariesAndSDKs/AGS_SDK/zip/refs/tags/v6.3.0"
214    $zipPath = ".\AGS_SDK_v6.3.0.zip"
215    # Download the AGS SDK zip file
216    Invoke-WebRequest -Uri $url -OutFile $zipPath
217    # Extract the AGS SDK zip file
218    Expand-Archive -Path $zipPath -DestinationPath "." -Force
219}
220
221function DownloadConpty {
222    $url = "https://github.com/microsoft/terminal/releases/download/v1.23.13503.0/Microsoft.Windows.Console.ConPTY.1.23.251216003.nupkg"
223    $zipPath = ".\Microsoft.Windows.Console.ConPTY.1.23.251216003.nupkg"
224    Invoke-WebRequest -Uri $url -OutFile $zipPath
225    Expand-Archive -Path $zipPath -DestinationPath ".\conpty" -Force
226}
227
228function CollectFiles {
229    Move-Item -Path "$innoDir\zed_explorer_command_injector.appx" -Destination "$innoDir\appx\zed_explorer_command_injector.appx" -Force
230    Move-Item -Path "$innoDir\zed_explorer_command_injector.dll" -Destination "$innoDir\appx\zed_explorer_command_injector.dll" -Force
231    Move-Item -Path "$innoDir\cli.exe" -Destination "$innoDir\bin\zed.exe" -Force
232    Move-Item -Path "$innoDir\zed.sh" -Destination "$innoDir\bin\zed" -Force
233    Move-Item -Path "$innoDir\auto_update_helper.exe" -Destination "$innoDir\tools\auto_update_helper.exe" -Force
234    if($Architecture -eq "aarch64") {
235        New-Item -Type Directory -Path "$innoDir\arm64" -Force
236        Move-Item -Path ".\conpty\build\native\runtimes\arm64\OpenConsole.exe" -Destination "$innoDir\arm64\OpenConsole.exe" -Force
237        Move-Item -Path ".\conpty\runtimes\win-arm64\native\conpty.dll" -Destination "$innoDir\conpty.dll" -Force
238    }
239    else {
240        New-Item -Type Directory -Path "$innoDir\x64" -Force
241        New-Item -Type Directory -Path "$innoDir\arm64" -Force
242        Move-Item -Path ".\AGS_SDK-6.3.0\ags_lib\lib\amd_ags_x64.dll" -Destination "$innoDir\amd_ags_x64.dll" -Force
243        Move-Item -Path ".\conpty\build\native\runtimes\x64\OpenConsole.exe" -Destination "$innoDir\x64\OpenConsole.exe" -Force
244        Move-Item -Path ".\conpty\build\native\runtimes\arm64\OpenConsole.exe" -Destination "$innoDir\arm64\OpenConsole.exe" -Force
245        Move-Item -Path ".\conpty\runtimes\win-x64\native\conpty.dll" -Destination "$innoDir\conpty.dll" -Force
246    }
247}
248
249function BuildInstaller {
250    $issFilePath = "$innoDir\zed.iss"
251    switch ($channel) {
252        "stable" {
253            $appId = "{{2DB0DA96-CA55-49BB-AF4F-64AF36A86712}"
254            $appIconName = "app-icon"
255            $appName = "Zed"
256            $appDisplayName = "Zed"
257            $appSetupName = "Zed-$Architecture"
258            # The mutex name here should match the mutex name in crates\zed\src\zed\windows_only_instance.rs
259            $appMutex = "Zed-Stable-Instance-Mutex"
260            $appExeName = "Zed"
261            $regValueName = "Zed"
262            $appUserId = "ZedIndustries.Zed"
263            $appShellNameShort = "Z&ed"
264            $appAppxFullName = "ZedIndustries.Zed_1.0.0.0_neutral__japxn1gcva8rg"
265        }
266        "preview" {
267            $appId = "{{F70E4811-D0E2-4D88-AC99-D63752799F95}"
268            $appIconName = "app-icon-preview"
269            $appName = "Zed Preview"
270            $appDisplayName = "Zed Preview"
271            $appSetupName = "Zed-$Architecture"
272            # The mutex name here should match the mutex name in crates\zed\src\zed\windows_only_instance.rs
273            $appMutex = "Zed-Preview-Instance-Mutex"
274            $appExeName = "Zed"
275            $regValueName = "ZedPreview"
276            $appUserId = "ZedIndustries.Zed.Preview"
277            $appShellNameShort = "Z&ed Preview"
278            $appAppxFullName = "ZedIndustries.Zed.Preview_1.0.0.0_neutral__japxn1gcva8rg"
279        }
280        "nightly" {
281            $appId = "{{1BDB21D3-14E7-433C-843C-9C97382B2FE0}"
282            $appIconName = "app-icon-nightly"
283            $appName = "Zed Nightly"
284            $appDisplayName = "Zed Nightly"
285            $appSetupName = "Zed-$Architecture"
286            # The mutex name here should match the mutex name in crates\zed\src\zed\windows_only_instance.rs
287            $appMutex = "Zed-Nightly-Instance-Mutex"
288            $appExeName = "Zed"
289            $regValueName = "ZedNightly"
290            $appUserId = "ZedIndustries.Zed.Nightly"
291            $appShellNameShort = "Z&ed Editor Nightly"
292            $appAppxFullName = "ZedIndustries.Zed.Nightly_1.0.0.0_neutral__japxn1gcva8rg"
293        }
294        "dev" {
295            $appId = "{{8357632E-24A4-4F32-BA97-E575B4D1FE5D}"
296            $appIconName = "app-icon-dev"
297            $appName = "Zed Dev"
298            $appDisplayName = "Zed Dev"
299            $appSetupName = "Zed-$Architecture"
300            # The mutex name here should match the mutex name in crates\zed\src\zed\windows_only_instance.rs
301            $appMutex = "Zed-Dev-Instance-Mutex"
302            $appExeName = "Zed"
303            $regValueName = "ZedDev"
304            $appUserId = "ZedIndustries.Zed.Dev"
305            $appShellNameShort = "Z&ed Dev"
306            $appAppxFullName = "ZedIndustries.Zed.Dev_1.0.0.0_neutral__japxn1gcva8rg"
307        }
308        default {
309            Write-Error "can't bundle installer for $channel."
310            exit 1
311        }
312    }
313
314    # Windows runner 2022 default has iscc in PATH, https://github.com/actions/runner-images/blob/main/images/windows/Windows2022-Readme.md
315    # Currently, we are using Windows 2022 runner.
316    # Windows runner 2025 doesn't have iscc in PATH for now, https://github.com/actions/runner-images/issues/11228
317    $innoSetupPath = "C:\Program Files (x86)\Inno Setup 6\ISCC.exe"
318
319    $definitions = @{
320        "AppId"          = $appId
321        "AppIconName"    = $appIconName
322        "OutputDir"      = "$env:ZED_WORKSPACE\target"
323        "AppSetupName"   = $appSetupName
324        "AppName"        = $appName
325        "AppDisplayName" = $appDisplayName
326        "RegValueName"   = $regValueName
327        "AppMutex"       = $appMutex
328        "AppExeName"     = $appExeName
329        "ResourcesDir"   = "$innoDir"
330        "ShellNameShort" = $appShellNameShort
331        "AppUserId"      = $appUserId
332        "Version"        = "$env:RELEASE_VERSION"
333        "SourceDir"      = "$env:ZED_WORKSPACE"
334        "AppxFullName"   = $appAppxFullName
335    }
336
337    $defs = @()
338    foreach ($key in $definitions.Keys) {
339        $defs += "/d$key=`"$($definitions[$key])`""
340    }
341
342    $innoArgs = @($issFilePath) + $defs
343    if($env:CI) {
344        $signTool = "powershell.exe -ExecutionPolicy Bypass -File $innoDir\sign.ps1 `$f"
345        $innoArgs += "/sDefaultsign=`"$signTool`""
346    }
347
348    # Execute Inno Setup
349    Write-Host "🚀 Running Inno Setup: $innoSetupPath $innoArgs"
350    $process = Start-Process -FilePath $innoSetupPath -ArgumentList $innoArgs -NoNewWindow -Wait -PassThru
351
352    if ($process.ExitCode -eq 0) {
353        Write-Host "✅ Inno Setup successfully compiled the installer"
354        Write-Output "SETUP_PATH=target/$appSetupName.exe" >> $env:GITHUB_ENV
355        $script:buildSuccess = $true
356    }
357    else {
358        Write-Host "❌ Inno Setup failed: $($process.ExitCode)"
359        $script:buildSuccess = $false
360    }
361}
362
363ParseZedWorkspace
364$innoDir = "$env:ZED_WORKSPACE\inno\$Architecture"
365$debugArchive = "$CargoOutDir\zed-$env:RELEASE_VERSION-$env:ZED_RELEASE_CHANNEL.dbg.zip"
366$debugStoreKey = "$env:ZED_RELEASE_CHANNEL/zed-$env:RELEASE_VERSION-$env:ZED_RELEASE_CHANNEL.dbg.zip"
367
368CheckEnvironmentVariables
369PrepareForBundle
370GenerateLicenses
371BuildZedAndItsFriends
372BuildRemoteServer
373MakeAppx
374SignZedAndItsFriends
375ZipZedAndItsFriendsDebug
376DownloadAMDGpuServices
377DownloadConpty
378CollectFiles
379BuildInstaller
380
381if($env:CI) {
382    UploadToSentry
383}
384
385if ($buildSuccess) {
386    Write-Output "Build successful"
387    if ($Install) {
388        Write-Output "Installing Zed..."
389        Start-Process -FilePath "$env:ZED_WORKSPACE/target/ZedEditorUserSetup-x64-$env:RELEASE_VERSION.exe"
390    }
391    exit 0
392}
393else {
394    Write-Output "Build failed"
395    exit 1
396}