Merge branch 'task/JAL-3553_improved_install4j_output_txt' into merge/JAL-3978+JAL...
[jalview.git] / build.gradle
index ec30174..420d37c 100644 (file)
@@ -12,6 +12,7 @@ import java.security.MessageDigest
 import groovy.transform.ExternalizeMethods
 import groovy.util.XmlParser
 import groovy.xml.XmlUtil
+import groovy.json.JsonBuilder
 import com.vladsch.flexmark.util.ast.Node
 import com.vladsch.flexmark.html.HtmlRenderer
 import com.vladsch.flexmark.parser.Parser
@@ -217,6 +218,8 @@ ext {
   install4jWindowsIconsFile = string("${install4j_images_dir}/${install4j_windows_icons_file}")
   install4jPngIconFile = string("${install4j_images_dir}/${install4j_png_icon_file}")
   install4jBackground = string("${install4j_images_dir}/${install4j_background}")
+  install4jBuildDir = "${install4j_build_dir}/${JAVA_VERSION}"
+  install4jCheckSums = true
   switch (CHANNEL) {
 
     case "BUILD":
@@ -327,6 +330,7 @@ ext {
     getdownArchiveAppBase = file("${jalviewDir}/${getdown_archive_dir}").toURI().toString()
     getdownLauncher = string("${jalviewDir}/${getdown_lib_dir}/${getdown_launcher_local}")
     install4jExtraScheme = "jalviewl"
+    install4jCheckSums = false
     break
 
     default: // something wrong specified
@@ -1951,11 +1955,7 @@ task copyInstall4jTemplate {
 
     // turn off checksum creation for LOCAL channel
     def e = install4jConfigXml.application[0]
-    if (CHANNEL == "LOCAL") {
-      e.'@createChecksums' = "false"
-    } else {
-      e.'@createChecksums' = "true"
-    }
+    e.'@createChecksums' = string(install4jCheckSums)
 
     // put file association actions where placeholder action is
     def install4jFileAssociationsText = install4jFileAssociationsFile.text
@@ -2008,12 +2008,24 @@ clean {
   }
 }
 
+task cleanInstallersDataFiles {
+  def installersOutputTxt = file("${jalviewDir}/${install4jBuildDir}/output.txt")
+  def installersSha256 = file("${jalviewDir}/${install4jBuildDir}/sha256sums")
+  def VERSION_UNDERSCORES = JALVIEW_VERSION.replaceAll("\\.", "_")
+  def installersJsonFile = file("${jalviewDir}/${install4jBuildDir}/installers-${VERSION_UNDERSCORES}.json")
+  doFirst {
+    delete installersOutputTxt
+    delete installersSha256
+    delete installersJsonFile
+  }
+}
 
-task installers(type: com.install4j.gradle.Install4jTask) {
+task installerFiles(type: com.install4j.gradle.Install4jTask) {
   group = "distribution"
   description = "Create the install4j installers"
   dependsOn getdown
   dependsOn copyInstall4jTemplate
+  dependsOn cleanInstallersDataFiles
 
   projectFile = install4jConfFile
 
@@ -2028,8 +2040,6 @@ task installers(type: com.install4j.gradle.Install4jTask) {
     filesMd5 = filesMd5.substring(0,8)
   }
   def install4jTemplateVersion = "${JALVIEW_VERSION}_F${filesMd5}_C${gitHash}"
-  // make install4jBuildDir relative to jalviewDir
-  def install4jBuildDir = "${install4j_build_dir}/${JAVA_VERSION}"
 
   variables = [
     'JALVIEW_NAME': jalview_name,
@@ -2122,6 +2132,79 @@ task installers(type: com.install4j.gradle.Install4jTask) {
   outputs.dir("${jalviewDir}/${install4j_build_dir}/${JAVA_VERSION}")
 }
 
+def writeInstallersJsonFile(File installersOutputTxt, File installersSha256, File installersJsonFile) {
+  if (!installersOutputTxt.exists()) {
+    throw new GradleException("Required input file '${installersOutputTxt.getPath()}' doesn't exist.")
+    return null;
+  }
+  if (install4jCheckSums && (!installersSha256)) {
+    throw new GradleException("Required input file '${installersSha256.getPath()}' doesn't exist.")
+    return null;
+  }
+  def hash = [:]
+  def idHash = [:]
+  installersOutputTxt.readLines().each { def line ->
+    if (line.startsWith("#")) {
+      return;
+    }
+    line.replaceAll("\n","")
+    def vals = line.split("\t")
+    def filename = vals[3]
+    def filesize = file(filename).length()
+    filename = filename.replaceAll(/^.*\//, "")
+    hash[vals[0]] = [ "id" : vals[0], "os" : vals[1], "name" : vals[2], "file" : filename, "filesize" : filesize ]
+    idHash."${filename}" = vals[0]
+  }
+  if (install4jCheckSums) {
+    installersSha256.readLines().each { def line ->
+      if (line.startsWith("#")) {
+        return;
+      }
+      line.replaceAll("\n","")
+      def vals = line.split(/\s+\*?/)
+      def filename = vals[1]
+      def innerHash = (hash.(idHash."${filename}"))."sha256" = vals[0]
+    }
+  }
+  return installersJsonFile.write(new JsonBuilder(hash).toPrettyString())
+}
+
+task makeInstallersJsonFile {
+  dependsOn installerFiles
+
+  def installersOutputTxt = file("${jalviewDir}/${install4jBuildDir}/output.txt")
+  def installersSha256 = file("${jalviewDir}/${install4jBuildDir}/sha256sums")
+  def VERSION_UNDERSCORES = JALVIEW_VERSION.replaceAll("\\.", "_")
+  def installersJsonFile = file("${jalviewDir}/${install4jBuildDir}/installers-${VERSION_UNDERSCORES}.json")
+
+  inputs.file(installersOutputTxt)
+  if (install4jCheckSums) {
+    inputs.file(installersSha256)
+  }
+  outputs.file(installersJsonFile)
+
+  doFirst {
+    writeInstallersJsonFile(installersOutputTxt, installersSha256, installersJsonFile)
+  }
+}
+
+task staticMakeInstallersJsonFile {
+  doFirst {
+    def output = findProperty("i4j_output")
+    def sha256 = findProperty("i4j_sha256")
+    def json = findProperty("i4j_json")
+    if (output == null || sha256 == null || json == null) {
+      throw new GradleException("Must provide paths to all of output.txt, sha256sums, and output.json with '-Pi4j_output=... -Pi4j_sha256=... -Pi4j_json=...")
+    }
+    writeInstallersJsonFile(file(output), file(sha256), file(json))
+  }
+}
+
+task installers {
+  dependsOn installerFiles
+  dependsOn makeInstallersJsonFile
+}
+
 
 spotless {
   java {