-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathbuild-windows.ps1
More file actions
334 lines (262 loc) · 9.97 KB
/
build-windows.ps1
File metadata and controls
334 lines (262 loc) · 9.97 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
$ErrorActionPreference = 'Stop'
$repoRoot = Split-Path -Parent $MyInvocation.MyCommand.Path
function Get-IsccPath {
$candidates = @(
"C:\Users\$env:USERNAME\AppData\Local\Programs\Inno Setup 6\ISCC.exe",
"C:\Program Files (x86)\Inno Setup 6\ISCC.exe",
"C:\Program Files\Inno Setup 6\ISCC.exe"
)
foreach ($candidate in $candidates) {
if (Test-Path $candidate) { return $candidate }
}
$regPaths = @(
"HKCU:\Software\Microsoft\Windows\CurrentVersion\Uninstall\Inno Setup 6_is1",
"HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\Inno Setup 6_is1",
"HKLM:\Software\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\Inno Setup 6_is1"
)
foreach ($regPath in $regPaths) {
if (Test-Path $regPath) {
$appPath = (Get-ItemProperty $regPath -ErrorAction SilentlyContinue)."Inno Setup: App Path"
if ($appPath) {
$iscc = Join-Path $appPath "ISCC.exe"
if (Test-Path $iscc) { return $iscc }
}
}
}
throw "ISCC.exe not found. Install Inno Setup 6 first."
}
function Get-FlutterCommand {
$candidate = "C:\flutter\bin\flutter.bat"
if (Test-Path $candidate) { return $candidate }
$flutterCmd = Get-Command flutter -ErrorAction SilentlyContinue
if ($flutterCmd) { return $flutterCmd.Source }
throw "Flutter not found. Install Flutter or add it to PATH."
}
function Get-NormalizedVersion {
param([string]$RawVersion)
if ([string]::IsNullOrWhiteSpace($RawVersion)) {
throw "Version string is empty."
}
$mainPart = ($RawVersion -split '-', 2)[0].Trim()
$segments = $mainPart -split '\.'
if ($segments.Count -lt 3) {
$segments = @($segments + @('0', '0', '0'))[0..2]
}
return [Version]::Parse(($segments -join '.'))
}
function Assert-ToolchainVersions {
param([string]$FlutterExe)
$minFlutter = [Version]::Parse('3.41.0')
$minDart = [Version]::Parse('3.11.0')
$rawOutput = (& $FlutterExe --version --machine | Out-String)
if ($LASTEXITCODE -ne 0 -or [string]::IsNullOrWhiteSpace($rawOutput)) {
throw "Failed to query Flutter version. Run 'flutter --version' manually and verify your SDK installation."
}
# Flutter can emit setup chatter (e.g. "Running pub upgrade...") before JSON.
# Extract the JSON object from mixed output to avoid ConvertFrom-Json failures.
$jsonMatch = [regex]::Match($rawOutput, '\{[\s\S]*\}')
if (-not $jsonMatch.Success) {
throw "Failed to parse Flutter --version --machine output as JSON. Raw output: $rawOutput"
}
$versionInfo = $jsonMatch.Value | ConvertFrom-Json
$flutterVersion = Get-NormalizedVersion $versionInfo.frameworkVersion
$dartVersion = Get-NormalizedVersion $versionInfo.dartSdkVersion
if ($flutterVersion -lt $minFlutter) {
throw "Flutter SDK $($versionInfo.frameworkVersion) is too old. Required: 3.41.0+ (README requirement)."
}
if ($dartVersion -lt $minDart) {
throw "Dart SDK $($versionInfo.dartSdkVersion) is too old. Required: 3.11.0+ (README requirement)."
}
}
function Get-VcpkgCommand {
$candidates = @(
(Join-Path $repoRoot "vcpkg\vcpkg.exe"),
"C:\vcpkg\vcpkg.exe"
)
foreach ($candidate in $candidates) {
if (Test-Path $candidate) { return $candidate }
}
$vcpkgCmd = Get-Command vcpkg -ErrorAction SilentlyContinue
if ($vcpkgCmd) { return $vcpkgCmd.Source }
return $null
}
function Initialize-LibarchiveForWindows {
if (-not [Environment]::OSVersion.Platform.ToString().Contains('Win')) {
return
}
$vcpkgExe = Get-VcpkgCommand
if (-not $vcpkgExe) {
$bootstrapRoot = Join-Path $repoRoot "vcpkg"
$bootstrapScript = Join-Path $bootstrapRoot "bootstrap-vcpkg.bat"
Write-Host "vcpkg not found. Bootstrapping local copy at $bootstrapRoot..."
if (-not (Test-Path $bootstrapRoot)) {
$gitCmd = Get-Command git -ErrorAction SilentlyContinue
if (-not $gitCmd) {
throw "git not found. Install Git or install vcpkg manually, then run: vcpkg install libarchive:x64-windows"
}
& $gitCmd.Source clone https://github.com/microsoft/vcpkg $bootstrapRoot
if ($LASTEXITCODE -ne 0) {
throw "Failed to clone vcpkg repository."
}
}
if (-not (Test-Path $bootstrapScript)) {
throw "vcpkg bootstrap script not found at $bootstrapScript"
}
& $bootstrapScript
if ($LASTEXITCODE -ne 0) {
throw "vcpkg bootstrap failed with exit code $LASTEXITCODE"
}
$vcpkgExe = Join-Path $bootstrapRoot "vcpkg.exe"
if (-not (Test-Path $vcpkgExe)) {
throw "vcpkg executable not found after bootstrap: $vcpkgExe"
}
}
$vcpkgRoot = Split-Path -Parent $vcpkgExe
$libarchiveHeader = Join-Path $vcpkgRoot "installed\x64-windows\include\archive.h"
$libarchiveLib = Join-Path $vcpkgRoot "installed\x64-windows\lib\archive.lib"
if (-not (Test-Path $libarchiveHeader) -or -not (Test-Path $libarchiveLib)) {
Write-Host "Installing libarchive:x64-windows via vcpkg..."
& $vcpkgExe install libarchive:x64-windows
if ($LASTEXITCODE -ne 0) {
throw "vcpkg install libarchive:x64-windows failed with exit code $LASTEXITCODE"
}
}
$env:VCPKG_ROOT = $vcpkgRoot
}
function Copy-VcpkgRuntimeDlls {
param(
[string]$VcpkgRoot,
[string]$ReleaseDir
)
if ([string]::IsNullOrWhiteSpace($VcpkgRoot) -or -not (Test-Path $VcpkgRoot)) {
return
}
$binDir = Join-Path $VcpkgRoot "installed\x64-windows\bin"
if (-not (Test-Path $binDir)) {
return
}
$dlls = Get-ChildItem -Path $binDir -Filter *.dll -File -ErrorAction SilentlyContinue
if (-not $dlls) {
return
}
foreach ($dll in $dlls) {
Copy-Item -Path $dll.FullName -Destination (Join-Path $ReleaseDir $dll.Name) -Force
}
}
function Get-AppVersion {
$pubspecPath = Join-Path $repoRoot "pubspec.yaml"
if (-not (Test-Path $pubspecPath)) {
throw "pubspec.yaml not found at $pubspecPath"
}
$versionLine = Select-String -Path $pubspecPath -Pattern '^version\s*:\s*' | Select-Object -First 1 -ExpandProperty Line
if (-not $versionLine) {
throw "Could not find version in pubspec.yaml"
}
$fullVersion = ($versionLine -split ':', 2)[1].Trim()
$appVersion = ($fullVersion -split '\+', 2)[0].Trim()
if ([string]::IsNullOrWhiteSpace($appVersion)) {
throw "Invalid version value in pubspec.yaml: $fullVersion"
}
return $appVersion
}
function Invoke-CheckedCommand {
param(
[string]$Name,
[string]$FilePath,
[string[]]$Arguments = @()
)
& $FilePath @Arguments
if ($LASTEXITCODE -ne 0) {
throw "$Name failed with exit code $LASTEXITCODE"
}
}
function New-InnoScript {
param(
[string]$AppVersion,
[string]$InstallerBaseName,
[string]$OutputDir,
[string]$IconPath,
[string]$ReleaseDir,
[string]$IssPath
)
$iss = @"
#define MyAppName "Moonfin"
#define MyAppVersion "$AppVersion"
#define MyAppPublisher "Moonfin"
#define MyAppExeName "moonfin.exe"
[Setup]
AppId={{2B684544-2B56-47BE-B52F-6F7A94BCA4E1}
AppName={#MyAppName}
AppVersion={#MyAppVersion}
AppPublisher={#MyAppPublisher}
DefaultDirName={autopf}\Moonfin
DefaultGroupName={#MyAppName}
DisableProgramGroupPage=yes
OutputDir=$OutputDir
OutputBaseFilename=$InstallerBaseName
SetupIconFile=$IconPath
Compression=lzma2
SolidCompression=yes
WizardStyle=modern
PrivilegesRequired=admin
PrivilegesRequiredOverridesAllowed=dialog
UsePreviousPrivileges=yes
ArchitecturesAllowed=x64compatible
ArchitecturesInstallIn64BitMode=x64compatible
[Languages]
Name: "english"; MessagesFile: "compiler:Default.isl"
[Tasks]
Name: "desktopicon"; Description: "Create a desktop icon"; GroupDescription: "Additional icons:"; Flags: unchecked
[Files]
Source: "$ReleaseDir\*"; DestDir: "{app}"; Flags: ignoreversion recursesubdirs createallsubdirs
[Icons]
Name: "{autoprograms}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"
Name: "{autodesktop}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"; Tasks: desktopicon
[Run]
Filename: "{app}\{#MyAppExeName}"; Description: "Launch {#MyAppName}"; Flags: nowait postinstall skipifsilent
"@
Set-Content -Path $IssPath -Value $iss -Encoding UTF8
}
$flutterExe = Get-FlutterCommand
$isccExe = Get-IsccPath
$appVersion = Get-AppVersion
$installerBaseName = "Moonfin_Windows_v$appVersion"
Push-Location $repoRoot
try {
Write-Host "Moonfin version: $appVersion"
Assert-ToolchainVersions -FlutterExe $flutterExe
Initialize-LibarchiveForWindows
Write-Host "Cleaning previous Flutter outputs..."
Invoke-CheckedCommand -Name "flutter clean" -FilePath $flutterExe -Arguments @("clean")
Write-Host "Resolving Dart and Flutter packages..."
Invoke-CheckedCommand -Name "flutter pub get" -FilePath $flutterExe -Arguments @("pub", "get")
Write-Host "Building Windows x64 release..."
Invoke-CheckedCommand -Name "flutter build windows" -FilePath $flutterExe -Arguments @("build", "windows", "--release", "--dart-define=DISTRIBUTION_CHANNEL=windows")
$releaseDir = Join-Path $repoRoot "build\windows\x64\runner\Release"
$releaseExe = Join-Path $releaseDir "moonfin.exe"
if (-not (Test-Path $releaseExe)) {
throw "Missing release binary: $releaseExe"
}
Copy-VcpkgRuntimeDlls -VcpkgRoot $env:VCPKG_ROOT -ReleaseDir $releaseDir
$outputDir = Join-Path $repoRoot "build\windows\installer"
$iconPath = Join-Path $repoRoot "windows\runner\resources\app_icon.ico"
$issPath = Join-Path $outputDir "moonfin.generated.iss"
$outputExe = Join-Path $outputDir "$installerBaseName.exe"
$rootExe = Join-Path $repoRoot "$installerBaseName.exe"
New-Item -ItemType Directory -Force -Path $outputDir | Out-Null
if (-not (Test-Path $iconPath)) {
throw "Missing app icon: $iconPath"
}
New-InnoScript -AppVersion $appVersion -InstallerBaseName $installerBaseName -OutputDir $outputDir -IconPath $iconPath -ReleaseDir $releaseDir -IssPath $issPath
Write-Host "Building installer EXE..."
Invoke-CheckedCommand -Name "ISCC" -FilePath $isccExe -Arguments @($issPath)
if (-not (Test-Path $outputExe)) {
throw "Installer not found at expected path: $outputExe"
}
Copy-Item -Path $outputExe -Destination $rootExe -Force
Write-Host "Installer created:" $outputExe
Write-Host "Installer copied to root:" $rootExe
}
finally {
Pop-Location
}