JAL-3449 Work in progress improving XML replacements by gradle
[jalview.git] / build.gradle
index 336bc42..32816f3 100644 (file)
@@ -3,8 +3,10 @@ import org.gradle.internal.os.OperatingSystem
 import org.gradle.plugins.ide.eclipse.model.Output
 import org.gradle.plugins.ide.eclipse.model.Library
 import java.security.MessageDigest
-
 import groovy.transform.ExternalizeMethods
+import groovy.util.XmlSlurper
+import groovy.util.XmlParser
+import groovy.xml.XmlUtil
 
 buildscript {
   dependencies {
@@ -17,7 +19,7 @@ plugins {
   id 'application'
   id 'eclipse'
   id 'com.github.johnrengelman.shadow' version '4.0.3'
-  id 'com.install4j.gradle' version '7.0.9'
+  id 'com.install4j.gradle' version '8.0.2'
   id 'com.dorongold.task-tree' version '1.4' // only needed to display task dependency tree with  gradle task1 [task2 ...] taskTree
 }
 
@@ -1208,66 +1210,22 @@ install4j {
   installDir(file(install4jHomeDir))
 
   mediaTypes = Arrays.asList(install4j_media_types.split(","))
-  if (install4j_faster.equals("true")) {
-    faster = true
-  }
 }
 
 
 task copyInstall4jTemplate(type: Copy) {
   dependsOn setGitVals
 
-  def install4jTemplateMd5 = ""
-  def digest = MessageDigest.getInstance("MD5")
-  digest.update(
-    (file("${install4jDir}/${install4j_template}").text + 
-    file("${install4jDir}/${install4j_info_plist_file_associations}").text +
-    file("${install4jDir}/${install4j_installer_file_associations}").text).bytes)
-  def filesMd5 = new BigInteger(1, digest.digest()).toString(16)
-  if (filesMd5.length() >= 8) {
-    filesMd5 = filesMd5.substring(0,8)
-  }
-  install4jTemplateMd5 += filesMd5
-  install4jTemplateMd5 += "_${gitHash}"
-
   inputs.file("${install4jDir}/${install4j_template}")
-  inputs.file("${install4jDir}/${install4j_info_plist_file_associations}")
   inputs.file("${install4jDir}/${install4j_installer_file_associations}")
   outputs.files(install4jConfFile)
 
   from (install4jDir) {
     include install4j_template
     rename (install4j_template, install4jConfFileName)
-    filter(ReplaceTokens,
-      beginToken: '',
-      endToken: '',
-      tokens: [
-        '0000000000': JAVA_MIN_VERSION,
-        '9999999999': JAVA_MAX_VERSION
-      ]
-    )
+    // TODO change these ReplaceTokens to something better.  Possibly ${compiler:variables} but also the ant.replaceregexp below, or perhaps a proper xmldom parse
 
-    filter(ReplaceTokens,
-      beginToken: '$$',
-      endToken: '$$',
-      tokens: [
-        'JAVA_VERSION': JAVA_VERSION,
-        'JAVA_INTEGER_VERSION': JAVA_INTEGER_VERSION,
-        'VERSION': JALVIEW_VERSION,
-        'MACOS_JAVA_VM_DIR': macosJavaVMDir,
-        'MACOS_JAVA_VM_TGZ': macosJavaVMTgz,
-        'WINDOWS_JAVA_VM_DIR': windowsJavaVMDir,
-        'WINDOWS_JAVA_VM_TGZ': windowsJavaVMTgz,
-        'INSTALL4JINFOPLISTFILEASSOCIATIONS': install4j_info_plist_file_associations,
-        'INSTALLER_TEMPLATE_MD5': install4jTemplateMd5,
-        'COPYRIGHT_MESSAGE': install4j_copyright_message,
-        'MACOS_BUNDLE_ID': install4j_macOS_bundle_id,
-        'GETDOWN_RESOURCE_DIR': getdown_resource_dir,
-        'GETDOWN_DIST_DIR': getdown_app_dir,
-        'GETDOWN_ALT_DIR': getdown_app_dir_alt,
-        'GETDOWN_INSTALL_DIR': getdown_install_dir
-      ]
-    )
+    /*
     if (OSX_KEYPASS == "") {
       filter(ReplaceTokens,
         beginToken: 'codeSigning macEnabled="',
@@ -1284,19 +1242,61 @@ task copyInstall4jTemplate(type: Copy) {
         ]
       )
     }
+    */
   }
   into install4jDir
 
   doLast {
     // include file associations in installer
-    def installerFileAssociationsXml = file("${install4jDir}/${install4j_installer_file_associations}").text
+    def install4jFileAssociationsFile = file("${install4jDir}/${install4j_installer_file_associations}")
+    def install4jFileAssociationsText = install4jFileAssociationsFile.text
+/*
     ant.replaceregexp(
       byline: false,
       flags: "s",
       match: '<action name="EXTENSIONS_REPLACED_BY_GRADLE".*?</action>',
-      replace: installerFileAssociationsXml,
+      replace: install4jFileAssociationsText,
       file: install4jConfFile
     )
+*/
+
+    def install4jFileAssociations = new XmlParser().parseText('''
+<install4jFileAssociations>
+${install4jFileAssociationsText}
+</install4jFileAssociations>
+'''
+    )
+
+println("TEXT=${install4jFileAssociationsText}")
+println("ACTIONS")
+install4jFileAssociationsXml.action.each { a ->
+  println("ACTION="+XmlUtil.serialize(a))
+}
+
+
+    def install4j = new XmlParser().parse(install4jConfFile)
+
+    if (OSX_KEYPASS == "") {
+      install4j.application.codeSigning.each { codeSigning ->
+        codeSigning.'@macEnabled' = "false"
+      }
+      install4j.mediaSets.windows.each { windows ->
+        windows.'@runPostProcessor' = "false"
+      }
+    }
+    // reparse
+    //install4j = new XmlSlurper().parseText(XmlUtil.serialize(install4j))
+
+    install4j.'**'.action.each { a ->
+      if (a.'@name' == 'EXTENSIONS_REPLACED_BY_GRADLE') {
+        //a.replaceNode(install4jFileAssociationsXml.action[0])
+      }
+    }
+    // reparse
+    //install4j = new XmlSlurper().parseText(XmlUtil.serialize(install4j))
+
+    file(install4jConfFile).text = XmlUtil.serialize(install4j)
+
     /*
     // include uninstaller applescript app files in dmg
     def installerDMGUninstallerXml = file("$install4jDir/$install4j_DMG_uninstaller_app_files").text
@@ -1325,10 +1325,54 @@ task installers(type: com.install4j.gradle.Install4jTask) {
   dependsOn getdown
   dependsOn copyInstall4jTemplate
   projectFile = file(install4jConfFile)
-  variables = [majorVersion: version.substring(2, 11), build: 001, OSX_KEYSTORE: OSX_KEYSTORE, JSIGN_SH: JSIGN_SH]
+
+  def install4jTemplateMd5 = ""
+  def digest = MessageDigest.getInstance("MD5")
+  digest.update(
+    (file("${install4jDir}/${install4j_template}").text + 
+    file("${install4jDir}/${install4j_info_plist_file_associations}").text +
+    file("${install4jDir}/${install4j_installer_file_associations}").text).bytes)
+  def filesMd5 = new BigInteger(1, digest.digest()).toString(16)
+  if (filesMd5.length() >= 8) {
+    filesMd5 = filesMd5.substring(0,8)
+  }
+  install4jTemplateMd5 += filesMd5
+  install4jTemplateMd5 += "_${gitHash}"
+  def install4jTemplateVersion = "${JALVIEW_VERSION}_${install4jTemplateMd5}"
+  def infoPlistFileAssociations = file("${install4jDir}/${install4j_info_plist_file_associations}").text
+
+  variables = [
+    'OSX_KEYSTORE': OSX_KEYSTORE,
+    'JSIGN_SH': JSIGN_SH,
+    'JALVIEW_VERSION': JALVIEW_VERSION,
+    'JAVA_MIN_VERSION': JAVA_MIN_VERSION,
+    'JAVA_MAX_VERSION': JAVA_MAX_VERSION,
+    'JAVA_VERSION': JAVA_VERSION,
+    'JAVA_INTEGER_VERSION': JAVA_INTEGER_VERSION,
+    'VERSION': JALVIEW_VERSION,
+    'MACOS_JAVA_VM_DIR': macosJavaVMDir,
+    'MACOS_JAVA_VM_TGZ': macosJavaVMTgz,
+    'WINDOWS_JAVA_VM_DIR': windowsJavaVMDir,
+    'WINDOWS_JAVA_VM_TGZ': windowsJavaVMTgz,
+    'INSTALL4JINFOPLISTFILEASSOCIATIONS': install4j_info_plist_file_associations,
+    'INSTALLER_TEMPLATE_VERSION': install4jTemplateVersion,
+    'COPYRIGHT_MESSAGE': install4j_copyright_message,
+    'MACOS_BUNDLE_ID': install4j_macOS_bundle_id,
+    'GETDOWN_RESOURCE_DIR': getdown_resource_dir,
+    'GETDOWN_DIST_DIR': getdown_app_dir,
+    'GETDOWN_ALT_DIR': getdown_app_dir_alt,
+    'GETDOWN_INSTALL_DIR': getdown_install_dir,
+    'INFO_PLIST_FILE_ASSOCIATIONS': infoPlistFileAssociations
+  ]
   destination = "${jalviewDir}/${install4j_build_dir}/${JAVA_VERSION}"
   buildSelected = true
 
+  if (install4j_faster.equals("true") || CHANNEL.startsWith("DEVELOP") || CHANNEL.startsWith("LOCAL")) {
+    // this doesn't seem to work
+    faster = true
+    disableSigning = true
+  }
+
   if (OSX_KEYPASS) {
     macKeystorePassword=OSX_KEYPASS
   }
@@ -1339,6 +1383,7 @@ task installers(type: com.install4j.gradle.Install4jTask) {
 
   inputs.dir(getdownWebsiteDir)
   inputs.file(install4jConfFile)
+  inputs.file("${install4jDir}/${install4j_info_plist_file_associations}")
   inputs.dir(macosJavaVMDir)
   inputs.dir(windowsJavaVMDir)
   outputs.dir("${jalviewDir}/${install4j_build_dir}/${JAVA_VERSION}")