JAL-3978 clean up unnecessary local variables for JALVIEW_VERSION_UNDERSCORES
[jalview.git] / build.gradle
index ec30174..100bc59 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
@@ -22,6 +23,9 @@ import com.vladsch.flexmark.ext.gfm.strikethrough.StrikethroughExtension
 import com.vladsch.flexmark.ext.autolink.AutolinkExtension
 import com.vladsch.flexmark.ext.anchorlink.AnchorLinkExtension
 import com.vladsch.flexmark.ext.toc.TocExtension
+import com.google.common.hash.HashCode
+import com.google.common.hash.Hashing
+import com.google.common.io.Files
 
 buildscript {
   repositories {
@@ -43,6 +47,8 @@ plugins {
   id 'com.install4j.gradle' version '9.0.6'
   id 'com.dorongold.task-tree' version '1.5' // only needed to display task dependency tree with  gradle task1 [task2 ...] taskTree
   id 'com.palantir.git-version' version '0.13.0' apply false
+//  id 'org.gradle.crypto.checksum' version '1.4.0'
+
 }
 
 repositories {
@@ -96,6 +102,7 @@ def overrideProperties(String propsFileName, boolean output = false) {
 ext {
   jalviewDirAbsolutePath = file(jalviewDir).getAbsolutePath()
   jalviewDirRelativePath = jalviewDir
+  date = new Date()
 
   getdownChannelName = CHANNEL.toLowerCase()
   // default to "default". Currently only has different cosmetics for "develop", "release", "default"
@@ -194,6 +201,7 @@ ext {
   getdownFullArchiveDir = null
   getdownTextLines = []
   getdownLaunchJvl = null
+  getdownVersionLaunchJvl = null
   buildDist = true
   buildProperties = null
 
@@ -217,6 +225,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 +337,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
@@ -1050,7 +1061,6 @@ cleanTest {
 
 // format is a string like date.format("dd MMMM yyyy")
 def getDate(format) {
-  def date = new Date()
   return date.format(format)
 }
 
@@ -1800,7 +1810,8 @@ task getdownArchiveBuild() {
   def v = "v${JALVIEW_VERSION_UNDERSCORES}"
   def vDir = "${getdownArchiveDir}/${v}"
   getdownFullArchiveDir = "${vDir}/getdown"
-  def vLaunchVersionJvl = "${vDir}/jalview-${v}.jvl"
+  getdownVersionLaunchJvl = "${vDir}/jalview-${v}.jvl"
+
   def vAltDir = "alt_${v}"
   def archiveImagesDir = "${jalviewDir}/${channel_properties_dir}/old/images"
 
@@ -1847,7 +1858,7 @@ task getdownArchiveBuild() {
 
     getdownArchiveTxt.write(getdownArchiveTextLines.join("\n"))
 
-    def vLaunchJvl = file(vLaunchVersionJvl)
+    def vLaunchJvl = file(getdownVersionLaunchJvl)
     vLaunchJvl.getParentFile().mkdirs()
     vLaunchJvl.write("appbase=${getdownFullArchiveAppBase}\n")
     def vLaunchJvlPath = vLaunchJvl.toPath().toAbsolutePath()
@@ -1951,11 +1962,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 +2015,23 @@ clean {
   }
 }
 
+task cleanInstallersDataFiles {
+  def installersOutputTxt = file("${jalviewDir}/${install4jBuildDir}/output.txt")
+  def installersSha256 = file("${jalviewDir}/${install4jBuildDir}/sha256sums")
+  def hugoDataJsonFile = file("${jalviewDir}/${install4jBuildDir}/installers-${JALVIEW_VERSION_UNDERSCORES}.json")
+  doFirst {
+    delete installersOutputTxt
+    delete installersSha256
+    delete hugoDataJsonFile
+  }
+}
 
-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 +2046,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 +2138,94 @@ task installers(type: com.install4j.gradle.Install4jTask) {
   outputs.dir("${jalviewDir}/${install4j_build_dir}/${JAVA_VERSION}")
 }
 
+def getDataHash(File myFile) {
+  HashCode hash = Files.asByteSource(myFile).hash(Hashing.sha256())
+  return myFile.exists()
+  ? [
+      "filename" : myFile.getName(),
+      "filesize" : myFile.length(),
+      "sha256" : hash.toString()
+    ]
+  : null
+}
+
+def writeDataJsonFile(File installersOutputTxt, File installersSha256, File hugoDataJsonFile) {
+  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 = [
+    "channel" : getdownChannelName,
+    "date" : getDate("yyyy-MM-dd HH:mm:ss"),
+    "git-commit" : "${gitHash} [${gitBranch}]",
+    "version" : JALVIEW_VERSION
+  ]
+  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]
+    }
+  }
+
+  // executable JAR
+  def jarHash = getDataHash(file(shadowJar.archiveFile))
+  if (jarHash != null) {
+    hash."JAR" = jarHash;
+  }
+
+  // version JVL
+  def jvlHash =getDataHash(file(getdownVersionLaunchJvl))
+  if (jvlHash != null) {
+    hash."JVL" = jvlHash;
+  }
+
+  // source TGZ
+  def tarHash = getDataHash(file(sourceDist.archiveFile))
+  if (tarHash != null) {
+    hash."SOURCE" = tarHash;
+  }
+
+  return hugoDataJsonFile.write(new JsonBuilder(hash).toPrettyString())
+}
+
+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=...")
+    }
+    writeDataJsonFile(file(output), file(sha256), file(json))
+  }
+}
+
+task installers {
+  dependsOn installerFiles
+}
+
 
 spotless {
   java {
@@ -2257,6 +2361,26 @@ task sourceDist(type: Tar) {
   }
 }
 
+task makeDataJsonFile {
+  dependsOn installerFiles
+  dependsOn sourceDist
+  dependsOn getdownArchive
+  dependsOn shadowJar
+
+  def installersOutputTxt = file("${jalviewDir}/${install4jBuildDir}/output.txt")
+  def installersSha256 = file("${jalviewDir}/${install4jBuildDir}/sha256sums")
+  def hugoDataJsonFile = file("${jalviewDir}/${hugo_build_dir}/data/installers-${JALVIEW_VERSION_UNDERSCORES}.json")
+
+  inputs.file(installersOutputTxt)
+  if (install4jCheckSums) {
+    inputs.file(installersSha256)
+  }
+  outputs.file(hugoDataJsonFile)
+
+  doFirst {
+    writeDataJsonFile(installersOutputTxt, installersSha256, hugoDataJsonFile)
+  }
+}
 
 task helppages {
   dependsOn copyHelp