build.ps1 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. Param(
  2. [Parameter(Mandatory, HelpMessage="Please provide a name for the app")][string] $AppName,
  3. [Parameter(Mandatory, HelpMessage="Please provide the glob pattern to identify the main jar")][string] $MainJarGlob,
  4. [Parameter(Mandatory, HelpMessage="Please provide the module- and main class path to start the app")][string] $ModuleAndMainClass,
  5. [Parameter(Mandatory, HelpMessage="Please provide the windows upgrade uuid for the installer")][string] $UpgradeUUID,
  6. [Parameter(Mandatory, HelpMessage="Please provide the name of the vendor")][string] $Vendor,
  7. [Parameter(Mandatory, HelpMessage="Please provide the starting year for the copyright notice")][int] $CopyrightStartYear,
  8. [Parameter(Mandatory, HelpMessage="Please provide a help url")][string] $HelpUrl,
  9. [Parameter(Mandatory, HelpMessage="Please provide an update url")][string] $UpdateUrl,
  10. [Parameter(Mandatory, HelpMessage="Please provide an about url")][string] $AboutUrl,
  11. [Parameter(Mandatory, HelpMessage="Please provide an alias for localhost")][string] $LoopbackAlias,
  12. [bool] $clean
  13. )
  14. [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
  15. $ProgressPreference = 'SilentlyContinue' # disables Invoke-WebRequest's progress bar, which slows down downloads to a few bytes/s
  16. # check preconditions
  17. if ((Get-Command "git" -ErrorAction SilentlyContinue) -eq $null)
  18. {
  19. Write-Error "Unable to find git.exe in your PATH (try: choco install git)"
  20. exit 1
  21. }
  22. if ((Get-Command "mvn" -ErrorAction SilentlyContinue) -eq $null)
  23. {
  24. Write-Error "Unable to find mvn.cmd in your PATH (try: choco install maven)"
  25. exit 1
  26. }
  27. if ((Get-Command 'wix' -ErrorAction SilentlyContinue) -eq $null)
  28. {
  29. Write-Error 'Unable to find wix in your PATH (try: dotnet tool install --global wix --version 6.0.0)'
  30. exit 1
  31. }
  32. $wixExtensions = & wix.exe extension list --global | Out-String
  33. if ($wixExtensions -notmatch 'WixToolset.UI.wixext') {
  34. Write-Error 'Wix UI extension missing. Please install it with: wix.exe extension add WixToolset.UI.wixext/6.0.0 --global)'
  35. exit 1
  36. }
  37. if ($wixExtensions -notmatch 'WixToolset.Util.wixext') {
  38. Write-Error 'Wix Util extension missing. Please install it with: wix.exe extension add WixToolset.Util.wixext/6.0.0 --global)'
  39. exit 1
  40. }
  41. if ($wixExtensions -notmatch 'WixToolset.BootstrapperApplications.wixext') {
  42. Write-Error 'Wix Bootstrapper extension missing. Please install it with: wix.exe extension add WixToolset.BootstrapperApplications.wixext/6.0.0 --global)'
  43. exit 1
  44. }
  45. $buildDir = Split-Path -Parent $PSCommandPath
  46. $version = $(mvn -f $buildDir/../../pom.xml help:evaluate -Dexpression="project.version" -q -DforceStdout)
  47. $semVerNo = $version -replace '(\d+\.\d+\.\d+).*','$1'
  48. $revisionNo = $(git rev-list --count HEAD)
  49. Write-Output "`$version=$version"
  50. Write-Output "`$semVerNo=$semVerNo"
  51. Write-Output "`$revisionNo=$revisionNo"
  52. Write-Output "`$buildDir=$buildDir"
  53. Write-Output "`$Env:JAVA_HOME=$Env:JAVA_HOME"
  54. $copyright = "(C) $CopyrightStartYear - $((Get-Date).Year) $Vendor"
  55. # compile
  56. &mvn -B -f $buildDir/../../pom.xml clean package -DskipTests -Pwin "-Djavafx.platform=win"
  57. Copy-Item "$buildDir\..\..\target\$MainJarGlob.jar" -Destination "$buildDir\..\..\target\mods"
  58. # add runtime
  59. $runtimeImagePath = '.\runtime'
  60. if ($clean -and (Test-Path -Path $runtimeImagePath)) {
  61. Remove-Item -Path $runtimeImagePath -Force -Recurse
  62. }
  63. ## download jfx jmods for X64, while they are part of the Arm64 JDK
  64. $archCode = (Get-CimInstance Win32_Processor).Architecture
  65. $archName = switch ($archCode) {
  66. 9 { "x64 (AMD64)" }
  67. 12 { "ARM64" }
  68. default { "WMI Win32_Processor.Architecture code ($archCode)" }
  69. }
  70. switch ($archName) {
  71. 'ARM64' {
  72. $javafxBaseJmod = Join-Path $Env:JAVA_HOME "jmods\javafx.base.jmod"
  73. if (!(Test-Path $javafxBaseJmod)) {
  74. Write-Error "JavaFX module not found in JDK. Please ensure full JDK (including jmods) is installed."
  75. exit 1
  76. }
  77. $jmodPaths = "$Env:JAVA_HOME/jmods"
  78. }
  79. 'x64 (AMD64)' {
  80. $javaFxVersion='24.0.1'
  81. $javaFxJmodsUrl = "https://download2.gluonhq.com/openjfx/${javaFxVersion}/openjfx-${javaFxVersion}_windows-x64_bin-jmods.zip"
  82. $javaFxJmodsSHA256 = 'f13d17c7caf88654fc835f1b4e75a9b0f34a888eb8abef381796c0002e63b03f'
  83. $javaFxJmods = '.\resources\jfxJmods.zip'
  84. if( !(Test-Path -Path $javaFxJmods) ) {
  85. Write-Output "Downloading ${javaFxJmodsUrl}..."
  86. Invoke-WebRequest $javaFxJmodsUrl -OutFile $javaFxJmods # redirects are followed by default
  87. }
  88. $jmodsChecksumActual = $(Get-FileHash -Path $javaFxJmods -Algorithm SHA256).Hash.ToLower()
  89. if( $jmodsChecksumActual -ne $javaFxJmodsSHA256 ) {
  90. Write-Error "Checksum mismatch for jfxJmods.zip. Expected: $javaFxJmodsSHA256
  91. , actual: $jmodsChecksumActual"
  92. exit 1;
  93. }
  94. Expand-Archive -Path $javaFxJmods -Force -DestinationPath ".\resources\"
  95. Remove-Item -Recurse -Force -Path ".\resources\javafx-jmods" -ErrorAction Ignore
  96. Move-Item -Force -Path ".\resources\javafx-jmods-*" -Destination ".\resources\javafx-jmods" -ErrorAction Stop
  97. $jmodPaths="$buildDir/resources/javafx-jmods";
  98. }
  99. default {
  100. Write-Error "Unsupported architecture: $arch"
  101. exit 1
  102. }
  103. }
  104. ## create custom runtime
  105. ### check for JEP 493
  106. if ((& "$Env:JAVA_HOME\bin\jlink" --help | Select-String -Pattern "Linking from run-time image enabled" -SimpleMatch | Measure-Object).Count -eq 0 ) {
  107. $jmodPaths="$Env:JAVA_HOME/jmods;" + $jmodPaths;
  108. }
  109. ### create runtime
  110. & "$Env:JAVA_HOME\bin\jlink" `
  111. --verbose `
  112. --output runtime `
  113. --module-path $jmodPaths `
  114. --add-modules java.base,java.desktop,java.instrument,java.logging,java.naming,java.net.http,java.scripting,java.sql,java.xml,jdk.unsupported,jdk.accessibility,jdk.management.jfr,jdk.crypto.mscapi,java.compiler,javafx.base,javafx.graphics,javafx.controls,javafx.fxml `
  115. --strip-native-commands `
  116. --no-header-files `
  117. --no-man-pages `
  118. --strip-debug `
  119. --compress "zip-0" #do not compress and use msi compression
  120. $appPath = ".\$AppName"
  121. if ($clean -and (Test-Path -Path $appPath)) {
  122. Remove-Item -Path $appPath -Force -Recurse
  123. }
  124. # create app dir
  125. & "$Env:JAVA_HOME\bin\jpackage" `
  126. --verbose `
  127. --type app-image `
  128. --runtime-image runtime `
  129. --input ../../target/libs `
  130. --module-path ../../target/mods `
  131. --module $ModuleAndMainClass `
  132. --dest . `
  133. --name $AppName `
  134. --vendor $Vendor `
  135. --copyright $copyright `
  136. --java-options "--enable-preview" `
  137. --java-options "--enable-native-access=javafx.graphics,org.cryptomator.jfuse.win,org.cryptomator.integrations.win" `
  138. --java-options "-Xss5m" `
  139. --java-options "-Xmx256m" `
  140. --java-options "-Dcryptomator.appVersion=`"$semVerNo`"" `
  141. --app-version "$semVerNo.$revisionNo" `
  142. --java-options "-Dfile.encoding=`"utf-8`"" `
  143. --java-options "-Djava.net.useSystemProxies=true" `
  144. --java-options "-Dcryptomator.logDir=`"@{localappdata}/$AppName`"" `
  145. --java-options "-Dcryptomator.pluginDir=`"@{appdata}/$AppName/Plugins`"" `
  146. --java-options "-Dcryptomator.settingsPath=`"@{appdata}/$AppName/settings.json;@{userhome}/AppData/Roaming/$AppName/settings.json`"" `
  147. --java-options "-Dcryptomator.ipcSocketPath=`"@{localappdata}/$AppName/ipc.socket`"" `
  148. --java-options "-Dcryptomator.p12Path=`"@{appdata}/$AppName/key.p12;@{userhome}/AppData/Roaming/$AppName/key.p12`"" `
  149. --java-options "-Dcryptomator.mountPointsDir=`"@{userhome}/$AppName`"" `
  150. --java-options "-Dcryptomator.loopbackAlias=`"$LoopbackAlias`"" `
  151. --java-options "-Dcryptomator.integrationsWin.autoStartShellLinkName=`"$AppName`"" `
  152. --java-options "-Dcryptomator.integrationsWin.keychainPaths=`"@{appdata}/$AppName/keychain.json;@{userhome}/AppData/Roaming/$AppName/keychain.json`"" `
  153. --java-options "-Dcryptomator.integrationsWin.windowsHelloKeychainPaths=`"@{appdata}/$AppName/windowsHelloKeychain.json`"" `
  154. --java-options "-Dcryptomator.showTrayIcon=true" `
  155. --java-options "-Dcryptomator.buildNumber=`"msi-$revisionNo`"" `
  156. --resource-dir resources `
  157. --icon resources/$AppName.ico
  158. #Create RTF license for msi
  159. &mvn -B -f $buildDir/../../pom.xml license:add-third-party "-Djavafx.platform=win" `
  160. "-Dlicense.thirdPartyFilename=license.rtf" `
  161. "-Dlicense.fileTemplate=$buildDir\resources\licenseTemplate.ftl" `
  162. "-Dlicense.outputDirectory=$buildDir\resources\" `
  163. "-Dlicense.includedScopes=compile" `
  164. "-Dlicense.excludedGroups=^org\.cryptomator" `
  165. "-Dlicense.failOnMissing=true" `
  166. "-Dlicense.licenseMergesUrl=file:///$buildDir/../../license/merges"
  167. # patch app dir
  168. Copy-Item "contrib\*" -Destination "$AppName"
  169. attrib -r "$AppName\$AppName.exe"
  170. # patch batch script to set hostfile
  171. $webDAVPatcher = "$AppName\patchWebDAV.bat"
  172. try {
  173. (Get-Content $webDAVPatcher ) -replace '::REPLACE ME', "SET LOOPBACK_ALIAS=`"$LoopbackAlias`"" | Set-Content $webDAVPatcher
  174. } catch {
  175. Write-Host "Failed to set LOOPBACK_ALIAS for patchWebDAV.bat"
  176. exit 1
  177. }
  178. # create .msi
  179. $Env:JP_WIXWIZARD_RESOURCES = "$buildDir\resources"
  180. $Env:JP_WIXHELPER_DIR = "."
  181. & "$Env:JAVA_HOME\bin\jpackage" `
  182. --verbose `
  183. --type msi `
  184. --win-upgrade-uuid $UpgradeUUID `
  185. --app-image $AppName `
  186. --dest installer `
  187. --name $AppName `
  188. --vendor $Vendor `
  189. --copyright $copyright `
  190. --app-version "$semVerNo.$revisionNo" `
  191. --win-menu `
  192. --win-dir-chooser `
  193. --win-shortcut-prompt `
  194. --win-menu-group $AppName `
  195. --resource-dir resources `
  196. --license-file resources/license.rtf `
  197. --win-update-url $UpdateUrl `
  198. --about-url $AboutUrl `
  199. --file-associations resources/FAvaultFile.properties
  200. if ($LASTEXITCODE -ne 0) {
  201. Write-Error "jpackage MSI failed with exit code $LASTEXITCODE"
  202. return 1;
  203. }
  204. #Create RTF license for bundle
  205. &mvn -B -f $buildDir/../../pom.xml license:add-third-party "-Djavafx.platform=win" `
  206. "-Dlicense.thirdPartyFilename=license.rtf" `
  207. "-Dlicense.fileTemplate=$buildDir\bundle\resources\licenseTemplate.ftl" `
  208. "-Dlicense.outputDirectory=$buildDir\bundle\resources\" `
  209. "-Dlicense.includedScopes=compile" `
  210. "-Dlicense.excludedGroups=^org\.cryptomator" `
  211. "-Dlicense.failOnMissing=true" `
  212. "-Dlicense.licenseMergesUrl=file:///$buildDir/../../license/merges"
  213. # download Winfsp
  214. $winfspMsiUrl= 'https://github.com/winfsp/winfsp/releases/download/v2.1/winfsp-2.1.25156.msi'
  215. $winfspMsiHash = '073A70E00F77423E34BED98B86E600DEF93393BA5822204FAC57A29324DB9F7A'
  216. Write-Output "Downloading ${winfspMsiUrl}..."
  217. Invoke-WebRequest $winfspMsiUrl -OutFile ".\bundle\resources\winfsp.msi" # redirects are followed by default
  218. $computedHash = $(Get-FileHash -Path '.\bundle\resources\winfsp.msi' -Algorithm SHA256).Hash
  219. if (! $computedHash.Equals($winfspMsiHash)) {
  220. Write-Error -Category InvalidData -CategoryActivity "Data integrity check failed" -Message @"
  221. Downloaded Winfsp Installer does not match stored SHA256 checksum.
  222. Expected: $winfspMsiHash
  223. Actual: $computedHash
  224. "@
  225. exit 1
  226. }
  227. # download legacy-winfsp uninstaller
  228. $winfspUninstaller= 'https://github.com/cryptomator/winfsp-uninstaller/releases/latest/download/winfsp-uninstaller.exe'
  229. Write-Output "Downloading ${winfspUninstaller}..."
  230. Invoke-WebRequest $winfspUninstaller -OutFile ".\bundle\resources\winfsp-uninstaller.exe" # redirects are followed by default
  231. # copy MSI to bundle resources
  232. Copy-Item ".\installer\$AppName-*.msi" -Destination ".\bundle\resources\$AppName.msi" -Force
  233. # create bundle including winfsp
  234. & wix build `
  235. -define BundleName="$AppName" `
  236. -define BundleVersion="$semVerNo.$revisionNo" `
  237. -define BundleVendor="$Vendor" `
  238. -define BundleCopyright="$copyright" `
  239. -define AboutUrl="$AboutUrl" `
  240. -define HelpUrl="$HelpUrl" `
  241. -define UpdateUrl="$UpdateUrl" `
  242. -ext "WixToolset.Util.wixext" `
  243. -ext "WixToolset.BootstrapperApplications.wixext" `
  244. .\bundle\bundleWithWinfsp.wxs `
  245. -out "installer\$AppName-Installer.exe"
  246. Write-Output "Created EXE installer .\installer\$AppName-Installer.exe"