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