JAL-3210 need this .gitignore addition for the new location of .build_properties
[jalview.git] / build.gradle
index 2612fdb..43f2c1c 100644 (file)
@@ -2,6 +2,7 @@ import org.apache.tools.ant.filters.ReplaceTokens
 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
 
@@ -82,10 +83,17 @@ ext {
   } else {
     println("HEADLESS BUILD")
   }
-  /*
+  
+  J2S_ENABLED = (project.hasProperty('j2s.compiler.status') && project['j2s.compiler.status'] != null && project['j2s.compiler.status'] == "enable")
+  if (J2S_ENABLED) {
+    println("J2S ENABLED")
+  }
+  
+  /* *-/
   System.properties.sort { it.key }.each {
     key, val -> println("SYSTEM PROPERTY ${key}='${val}'")
   }
+  /-* *-/
   if (false && IN_ECLIPSE) {
     jalviewDir = jalviewDirAbsolutePath
   }
@@ -210,6 +218,7 @@ ext {
     break
 
   }
+buildProperties = string("${resourceDir}/${build_properties_file}")
 
   getdownAppDir = string("${getdownWebsiteDir}/${getdown_app_dir}")
   //getdownJ11libDir = "${getdownWebsiteDir}/${getdown_j11lib_dir}"
@@ -429,6 +438,9 @@ eclipse {
         def removeTheseToo = []
         HashMap<String, Boolean> alreadyAddedSrcPath = new HashMap<>();
         cp.entries.each { entry ->
+          // This conditional removes all src classpathentries that a) have already been added or b) aren't "src" or "test".
+          // e.g. this removes the resources dir being copied into bin/main, bin/test AND bin/clover
+          // we add the resources and help/help dirs in as libs afterwards (see below)
           if (entry.kind == 'src') {
             if (alreadyAddedSrcPath.getAt(entry.path) || !(entry.path == bareSourceDir || entry.path == bareTestSourceDir)) {
               removeTheseToo += entry
@@ -436,10 +448,11 @@ eclipse {
               alreadyAddedSrcPath.putAt(entry.path, true)
             }
           }
+
         }
         cp.entries.removeAll(removeTheseToo)
 
-        cp.entries += new Output("${eclipse_bin_dir}/main")
+        //cp.entries += new Output("${eclipse_bin_dir}/main")
         if (file(helpSourceDir).isDirectory()) {
           cp.entries += new Library(fileReference(helpSourceDir))
         }
@@ -452,8 +465,8 @@ eclipse {
         sourceSets.main.compileClasspath.findAll { it.name.endsWith(".jar") }.each {
           //don't want to add outputDir as eclipse is using its own output dir in bin/main
           if (it.isDirectory() || ! it.exists()) {
-            // don't add dirs to classpath
-            return
+            // don't add dirs to classpath, especially if they don't exist
+            return false // groovy "continue" in .any closure
           }
           def itPath = it.toString()
           if (itPath.startsWith("${jalviewDirAbsolutePath}/")) {
@@ -525,7 +538,8 @@ eclipse {
   if (IN_ECLIPSE) {
     // Don't want these to be activated if in headless build
     synchronizationTasks "eclipseSynchronizationTask"
-    autoBuildTasks "eclipseAutoBuildTask"
+    //autoBuildTasks "eclipseAutoBuildTask"
+
   }
 }
 
@@ -657,6 +671,8 @@ task setGitVals {
 
 
 task createBuildProperties(type: WriteProperties) {
+  group = "build"
+  description = "Create the ${buildProperties} file"
   dependsOn setGitVals
   inputs.dir(sourceDir)
   inputs.dir(resourceDir)
@@ -672,6 +688,13 @@ task createBuildProperties(type: WriteProperties) {
 }
 
 
+clean {
+  doFirst {
+       delete buildProperties
+  }
+}
+
+
 task cleanBuildingHTML(type: Delete) {
   doFirst {
     delete buildingHTML
@@ -702,7 +725,7 @@ task convertBuildingMD(type: Exec) {
         commandLine pandoc, '-s', '-o', buildingHTML, '--metadata', 'pagetitle="Building Jalview from Source"', '--toc', '-H', css, buildingMD
     } else {
         println("Cannot find pandoc. Skipping convert building.md to HTML")
-        throw new StopExecutionException()
+        throw new StopExecutionException("Cannot find pandoc. Skipping convert building.md to HTML")
     }
   }
 
@@ -767,6 +790,7 @@ task syncLib(type: Sync) {
 
 
 task syncResources(type: Sync) {
+  dependsOn createBuildProperties
   from resourceDir
   include "**/*.*"
   into "${classesDir}"
@@ -878,6 +902,7 @@ task cleanPackageDir(type: Delete) {
   }
 }
 
+
 jar {
   dependsOn linkCheck
   dependsOn buildIndices
@@ -1421,16 +1446,17 @@ task jalviewjsSetEclipseWorkspace {
   def eclipseWsDir = propVal
   def props = new Properties()
 
+  def writeProps = true
   if (( eclipseWsDir == null || !file(eclipseWsDir).exists() ) && propsFile.exists()) {
     def ins = new FileInputStream(propsFileName)
     props.load(ins)
     ins.close()
     if (props.getProperty(propKey, null) != null) {
       eclipseWsDir = props.getProperty(propKey)
+      writeProps = false
     }
   }
 
-  def writeProps = false
   if (eclipseWsDir == null || !file(eclipseWsDir).exists()) {
     def tempDir = File.createTempDir()
     eclipseWsDir = tempDir.getAbsolutePath()
@@ -1876,6 +1902,7 @@ def jalviewjsCallCore(String name, FileCollection list, String prefixFile, Strin
   javaexec {
     classpath = files(["${jalviewDir}/tools/closure_compiler.jar"])
     args = [ "--js", jsfile, "--js_output_file", zjsfile ]
+    maxHeapSize = "2g"
 
     msg = "\nRunning '"+commandLine.join(' ')+"'\n"
     println(msg)
@@ -2254,6 +2281,62 @@ task cleanJalviewjsAll {
 }
 
 
+task jalviewjsIDE_checkJ2sPlugin {
+  group "00 JalviewJS in Eclipse"
+  description "Compare the swingjs/net.sf.j2s.core.jar file with the Eclipse IDE's plugin version (found in the 'dropins' dir)"
+
+  doFirst {
+    def j2sPlugin = string("${jalviewDir}/${jalviewjs_j2s_plugin}")
+    def j2sPluginFile = file(j2sPlugin)
+    def copyPlugin = jalviewjs_eclipseIDE_auto_copy_j2s_plugin == "true"
+    def eclipseHome = System.properties["eclipse.home.location"]
+    def doCopy = false
+    if (eclipseHome == null || ! IN_ECLIPSE) {
+      throw new StopExecutionException("Cannot find running Eclipse home from System.properties['eclipse.home.location']. Skipping J2S Plugin Check.")
+    }
+    def eclipseJ2sPlugin = "${eclipseHome}/dropins/${j2sPluginFile.getName()}"
+    def eclipseJ2sPluginFile = file(eclipseJ2sPlugin)
+    if (!eclipseJ2sPluginFile.exists()) {
+      def msg = "Eclipse J2S Plugin is not installed"
+      println(msg)
+      if (! copyPlugin) {
+        throw new GradleException(msg)
+      }
+      doCopy = true
+    }
+
+    def digest = MessageDigest.getInstance("MD5")
+
+    digest.update(j2sPluginFile.text.bytes)
+    def j2sPluginMd5 = new BigInteger(1, digest.digest()).toString(16).padLeft(32, '0')
+
+    digest.update(eclipseJ2sPluginFile.text.bytes)
+    def eclipseJ2sPluginMd5 = new BigInteger(1, digest.digest()).toString(16).padLeft(32, '0')
+     
+    if (j2sPluginMd5 != eclipseJ2sPluginMd5) {
+      def msg = "WARNING! Eclipse J2S Plugin '${eclipseJ2sPlugin}' is different to this commit's version '${j2sPlugin}'"
+      println(msg)
+      if (! copyPlugin) {
+        throw new StopExecutionException(msg)
+      }
+      doCopy = true
+    }
+
+    if (doCopy) {
+      def msg = "WARNING! Auto-copying this commit's j2s plugin version '${j2sPlugin}' to Eclipse J2S Plugin '${eclipseJ2sPlugin}'\n         May require an Eclipse restart"
+      println(msg)
+      copy {
+        from j2sPlugin
+        eclipseJ2sPluginFile.getParentFile().mkdirs()
+        into eclipseJ2sPluginFile.getParent()
+      }
+    } else {
+      def msg = "Eclipse J2S Plugin is the same as '${j2sPlugin}'"
+      println(msg)
+    }
+  }
+}
+
 
 task jalviewjsIDE_j2sFile {
   group "00 JalviewJS in Eclipse"
@@ -2300,16 +2383,22 @@ task jalviewjsIDE_Server {
 }
 
 
-// buildship runs this at import
+// buildship runs this at import or gradle refresh
 task eclipseSynchronizationTask {
   //dependsOn eclipseSetup
-  dependsOn jalviewjsIDE_j2sFile
+  dependsOn createBuildProperties
+  if (J2S_ENABLED) {
+    dependsOn jalviewjsIDE_j2sFile
+    dependsOn jalviewjsIDE_checkJ2sPlugin
+    dependsOn jalviewjsIDE_PrepareSite
+  }
 }
 
 
-// buildship runs this at build time
+// buildship runs this at build time or project refresh
 task eclipseAutoBuildTask {
-  dependsOn jalviewjsIDE_PrepareSite
+  //dependsOn jalviewjsIDE_checkJ2sPlugin
+  //dependsOn jalviewjsIDE_PrepareSite
 }