build.ps1 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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 'UI wix 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 'Util wix 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 'Util wix 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. 0 { "x86" }
  67. 5 { "ARM" }
  68. 9 { "x64 (AMD64)" }
  69. 12 { "ARM64" }
  70. default { "Unknown or Other ($archCode)" }
  71. }
  72. switch ($archName) {
  73. 'ARM64' {
  74. $javafxBaseJmod = Join-Path $Env:JAVA_HOME "jmods\javafx.base.jmod"
  75. if (!(Test-Path $javafxBaseJmod)) {
  76. Write-Error "JavaFX module not found in JDK. Please ensure full JDK (including jmods) is installed."
  77. exit 1
  78. }
  79. $jmodPaths = "$Env:JAVA_HOME/jmods"
  80. }
  81. 'x64 (AMD64)' {
  82. $javaFxVersion='24.0.1'
  83. $javaFxJmodsUrl = "https://download2.gluonhq.com/openjfx/${javaFxVersion}/openjfx-${javaFxVersion}_windows-x64_bin-jmods.zip"
  84. $javaFxJmodsSHA256 = 'f13d17c7caf88654fc835f1b4e75a9b0f34a888eb8abef381796c0002e63b03f'
  85. $javaFxJmods = '.\resources\jfxJmods.zip'
  86. if( !(Test-Path -Path $javaFxJmods) ) {
  87. Write-Output "Downloading ${javaFxJmodsUrl}..."
  88. Invoke-WebRequest $javaFxJmodsUrl -OutFile $javaFxJmods # redirects are followed by default
  89. }
  90. $jmodsChecksumActual = $(Get-FileHash -Path $javaFxJmods -Algorithm SHA256).Hash.ToLower()
  91. if( $jmodsChecksumActual -ne $javaFxJmodsSHA256 ) {
  92. Write-Error "Checksum mismatch for jfxJmods.zip. Expected: $javaFxJmodsSHA256
  93. , actual: $jmodsChecksumActual"
  94. exit 1;
  95. }
  96. Expand-Archive -Path $javaFxJmods -Force -DestinationPath ".\resources\"
  97. Remove-Item -Recurse -Force -Path ".\resources\javafx-jmods" -ErrorAction Ignore
  98. Move-Item -Force -Path ".\resources\javafx-jmods-*" -Destination ".\resources\javafx-jmods" -ErrorAction Stop
  99. $jmodPaths="$buildDir/resources/javafx-jmods";
  100. }
  101. default {
  102. Write-Error "Unsupported architecture: $arch"
  103. exit 1
  104. }
  105. }
  106. ## create custom runtime
  107. ### check for JEP 493
  108. if ((& "$Env:JAVA_HOME\bin\jlink" --help | Select-String -Pattern "Linking from run-time image enabled" -SimpleMatch | Measure-Object).Count -eq 0 ) {
  109. $jmodPaths="$Env:JAVA_HOME/jmods;" + $jmodPaths;
  110. }
  111. ### create runtime
  112. & "$Env:JAVA_HOME\bin\jlink" `
  113. --verbose `
  114. --output runtime `
  115. --module-path $jmodPaths `
  116. --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 `
  117. --strip-native-commands `
  118. --no-header-files `
  119. --no-man-pages `
  120. --strip-debug `
  121. --compress "zip-0" #do not compress and use msi compression
  122. $appPath = ".\$AppName"
  123. if ($clean -and (Test-Path -Path $appPath)) {
  124. Remove-Item -Path $appPath -Force -Recurse
  125. }
  126. # create app dir
  127. & "$Env:JAVA_HOME\bin\jpackage" `
  128. --verbose `
  129. --type app-image `
  130. --runtime-image runtime `
  131. --input ../../target/libs `
  132. --module-path ../../target/mods `
  133. --module $ModuleAndMainClass `
  134. --dest . `
  135. --name $AppName `
  136. --vendor $Vendor `
  137. --copyright $copyright `
  138. --java-options "--enable-preview" `
  139. --java-options "--enable-native-access=javafx.graphics,org.cryptomator.jfuse.win,org.cryptomator.integrations.win" `
  140. --java-options "-Xss5m" `
  141. --java-options "-Xmx256m" `
  142. --java-options "-Dcryptomator.appVersion=`"$semVerNo`"" `
  143. --app-version "$semVerNo.$revisionNo" `
  144. --java-options "-Dfile.encoding=`"utf-8`"" `
  145. --java-options "-Djava.net.useSystemProxies=true" `
  146. --java-options "-Dcryptomator.logDir=`"@{localappdata}/$AppName`"" `
  147. --java-options "-Dcryptomator.pluginDir=`"@{appdata}/$AppName/Plugins`"" `
  148. --java-options "-Dcryptomator.settingsPath=`"@{appdata}/$AppName/settings.json;@{userhome}/AppData/Roaming/$AppName/settings.json`"" `
  149. --java-options "-Dcryptomator.ipcSocketPath=`"@{localappdata}/$AppName/ipc.socket`"" `
  150. --java-options "-Dcryptomator.p12Path=`"@{appdata}/$AppName/key.p12;@{userhome}/AppData/Roaming/$AppName/key.p12`"" `
  151. --java-options "-Dcryptomator.mountPointsDir=`"@{userhome}/$AppName`"" `
  152. --java-options "-Dcryptomator.loopbackAlias=`"$LoopbackAlias`"" `
  153. --java-options "-Dcryptomator.integrationsWin.autoStartShellLinkName=`"$AppName`"" `
  154. --java-options "-Dcryptomator.integrationsWin.keychainPaths=`"@{appdata}/$AppName/keychain.json;@{userhome}/AppData/Roaming/$AppName/keychain.json`"" `
  155. --java-options "-Dcryptomator.integrationsWin.windowsHelloKeychainPaths=`"@{appdata}/$AppName/windowsHelloKeychain.json`"" `
  156. --java-options "-Dcryptomator.showTrayIcon=true" `
  157. --java-options "-Dcryptomator.buildNumber=`"msi-$revisionNo`"" `
  158. --resource-dir resources `
  159. --icon resources/$AppName.ico
  160. #Create RTF license for msi
  161. &mvn -B -f $buildDir/../../pom.xml license:add-third-party "-Djavafx.platform=win" `
  162. "-Dlicense.thirdPartyFilename=license.rtf" `
  163. "-Dlicense.fileTemplate=$buildDir\resources\licenseTemplate.ftl" `
  164. "-Dlicense.outputDirectory=$buildDir\resources\" `
  165. "-Dlicense.includedScopes=compile" `
  166. "-Dlicense.excludedGroups=^org\.cryptomator" `
  167. "-Dlicense.failOnMissing=true" `
  168. "-Dlicense.licenseMergesUrl=file:///$buildDir/../../license/merges"
  169. # patch app dir
  170. Copy-Item "contrib\*" -Destination "$AppName"
  171. attrib -r "$AppName\$AppName.exe"
  172. # patch batch script to set hostfile
  173. $webDAVPatcher = "$AppName\patchWebDAV.bat"
  174. try {
  175. (Get-Content $webDAVPatcher ) -replace '::REPLACE ME', "SET LOOPBACK_ALIAS=`"$LoopbackAlias`"" | Set-Content $webDAVPatcher
  176. } catch {
  177. Write-Host "Failed to set LOOPBACK_ALIAS for patchWebDAV.bat"
  178. exit 1
  179. }
  180. # create .msi
  181. $Env:JP_WIXWIZARD_RESOURCES = "$buildDir\resources"
  182. $Env:JP_WIXHELPER_DIR = "."
  183. & "$Env:JAVA_HOME\bin\jpackage" `
  184. --verbose `
  185. --type msi `
  186. --win-upgrade-uuid $UpgradeUUID `
  187. --app-image $AppName `
  188. --dest installer `
  189. --name $AppName `
  190. --vendor $Vendor `
  191. --copyright $copyright `
  192. --app-version "$semVerNo.$revisionNo" `
  193. --win-menu `
  194. --win-dir-chooser `
  195. --win-shortcut-prompt `
  196. --win-menu-group $AppName `
  197. --resource-dir resources `
  198. --license-file resources/license.rtf `
  199. --win-update-url $UpdateUrl `
  200. --about-url $AboutUrl `
  201. --file-associations resources/FAvaultFile.properties
  202. if ($LASTEXITCODE -ne 0) {
  203. Write-Error "jpackage MSI failed with exit code $LASTEXITCODE"
  204. return 1;
  205. }
  206. #Create RTF license for bundle
  207. &mvn -B -f $buildDir/../../pom.xml license:add-third-party "-Djavafx.platform=win" `
  208. "-Dlicense.thirdPartyFilename=license.rtf" `
  209. "-Dlicense.fileTemplate=$buildDir\bundle\resources\licenseTemplate.ftl" `
  210. "-Dlicense.outputDirectory=$buildDir\bundle\resources\" `
  211. "-Dlicense.includedScopes=compile" `
  212. "-Dlicense.excludedGroups=^org\.cryptomator" `
  213. "-Dlicense.failOnMissing=true" `
  214. "-Dlicense.licenseMergesUrl=file:///$buildDir/../../license/merges"
  215. # download Winfsp
  216. $winfspMsiUrl= 'https://github.com/winfsp/winfsp/releases/download/v2.0/winfsp-2.0.23075.msi'
  217. Write-Output "Downloading ${winfspMsiUrl}..."
  218. Invoke-WebRequest $winfspMsiUrl -OutFile ".\bundle\resources\winfsp.msi" # redirects are followed by default
  219. # download legacy-winfsp uninstaller
  220. $winfspUninstaller= 'https://github.com/cryptomator/winfsp-uninstaller/releases/latest/download/winfsp-uninstaller.exe'
  221. Write-Output "Downloading ${winfspUninstaller}..."
  222. Invoke-WebRequest $winfspUninstaller -OutFile ".\bundle\resources\winfsp-uninstaller.exe" # redirects are followed by default
  223. # copy MSI to bundle resources
  224. Copy-Item ".\installer\$AppName-*.msi" -Destination ".\bundle\resources\$AppName.msi" -Force
  225. # create bundle including winfsp
  226. & wix build `
  227. -define BundleName="$AppName" `
  228. -define BundleVersion="$semVerNo.$revisionNo" `
  229. -define BundleVendor="$Vendor" `
  230. -define BundleCopyright="$copyright" `
  231. -define AboutUrl="$AboutUrl" `
  232. -define HelpUrl="$HelpUrl" `
  233. -define UpdateUrl="$UpdateUrl" `
  234. -ext "WixToolset.Util.wixext" `
  235. -ext "WixToolset.BootstrapperApplications.wixext" `
  236. .\bundle\bundleWithWinfsp.wxs `
  237. -out "installer\$AppName-Installer.exe"