JAL-3989 Changed to using com.google.common.hash for SHA256 sum, for gradle 7 compati...
[jalview.git] / build.gradle
index 420d37c..76ccefd 100644 (file)
@@ -23,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 {
@@ -44,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 {
@@ -97,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"
@@ -195,6 +201,7 @@ ext {
   getdownFullArchiveDir = null
   getdownTextLines = []
   getdownLaunchJvl = null
+  getdownVersionLaunchJvl = null
   buildDist = true
   buildProperties = null
 
@@ -1054,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)
 }
 
@@ -1804,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"
 
@@ -1851,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()
@@ -2012,11 +2019,11 @@ 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")
+  def hugoDataJsonFile = file("${jalviewDir}/${install4jBuildDir}/installers-${VERSION_UNDERSCORES}.json")
   doFirst {
     delete installersOutputTxt
     delete installersSha256
-    delete installersJsonFile
+    delete hugoDataJsonFile
   }
 }
 
@@ -2132,7 +2139,18 @@ task installerFiles(type: com.install4j.gradle.Install4jTask) {
   outputs.dir("${jalviewDir}/${install4j_build_dir}/${JAVA_VERSION}")
 }
 
-def writeInstallersJsonFile(File installersOutputTxt, File installersSha256, File installersJsonFile) {
+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;
@@ -2141,7 +2159,12 @@ def writeInstallersJsonFile(File installersOutputTxt, File installersSha256, Fil
     throw new GradleException("Required input file '${installersSha256.getPath()}' doesn't exist.")
     return null;
   }
-  def hash = [:]
+  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("#")) {
@@ -2166,26 +2189,26 @@ def writeInstallersJsonFile(File installersOutputTxt, File installersSha256, Fil
       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")
+  // executable JAR
+  def jarHash = getDataHash(file(shadowJar.archiveFile))
+  if (jarHash != null) {
+    hash."JAR" = jarHash;
+  }
 
-  inputs.file(installersOutputTxt)
-  if (install4jCheckSums) {
-    inputs.file(installersSha256)
+  // version JVL
+  def jvlHash =getDataHash(file(getdownVersionLaunchJvl))
+  if (jvlHash != null) {
+    hash."JVL" = jvlHash;
   }
-  outputs.file(installersJsonFile)
 
-  doFirst {
-    writeInstallersJsonFile(installersOutputTxt, installersSha256, installersJsonFile)
+  // source TGZ
+  def tarHash = getDataHash(file(sourceDist.archiveFile))
+  if (tarHash != null) {
+    hash."SOURCE" = tarHash;
   }
+
+  return hugoDataJsonFile.write(new JsonBuilder(hash).toPrettyString())
 }
 
 task staticMakeInstallersJsonFile {
@@ -2196,13 +2219,12 @@ task staticMakeInstallersJsonFile {
     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))
+    writeDataJsonFile(file(output), file(sha256), file(json))
   }
 }
 
 task installers {
   dependsOn installerFiles
-  dependsOn makeInstallersJsonFile
 }
 
 
@@ -2340,6 +2362,27 @@ 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 VERSION_UNDERSCORES = JALVIEW_VERSION.replaceAll("\\.", "_")
+  def hugoDataJsonFile = file("${jalviewDir}/${hugo_build_dir}/data/installers-${VERSION_UNDERSCORES}.json")
+
+  inputs.file(installersOutputTxt)
+  if (install4jCheckSums) {
+    inputs.file(installersSha256)
+  }
+  outputs.file(hugoDataJsonFile)
+
+  doFirst {
+    writeDataJsonFile(installersOutputTxt, installersSha256, hugoDataJsonFile)
+  }
+}
 
 task helppages {
   dependsOn copyHelp