JAL-3313 don't double count mapped codons
[jalview.git] / build.gradle
1 import org.apache.tools.ant.filters.ReplaceTokens
2 //import org.apache.tools.ant.filters.ReplaceRegexp
3 import org.gradle.internal.os.OperatingSystem
4 import org.gradle.plugins.ide.eclipse.model.*
5
6
7 import groovy.transform.ExternalizeMethods
8
9 buildscript {
10   dependencies {
11     classpath 'org.openclover:clover:4.3.1'
12     classpath 'org.apache.commons:commons-compress:1.18'
13   }
14 }
15
16 plugins {
17   id 'java'
18   id 'application'
19   id 'eclipse'
20   id 'com.github.johnrengelman.shadow' version '4.0.3'
21   id 'com.install4j.gradle' version '7.0.9'
22 }
23
24 repositories {
25   jcenter()
26   mavenCentral()
27   mavenLocal()
28   flatDir {
29     dirs gradlePluginsDir
30   }
31 }
32
33 mainClassName = launcherClass
34 def cloverInstrDir = file("$buildDir/$cloverSourcesInstrDir")
35 def classes = "$jalviewDir/$classesDir"
36
37 if (clover.equals("true")) {
38   use_clover = true
39   classes = "$buildDir/$cloverClassesDir"
40 } else {
41   use_clover = false
42   classes = "$jalviewDir/$classesDir"
43 }
44
45 // configure classpath/args for j8/j11 compilation
46
47 def jalviewDirAbsolutePath = file(jalviewDir).getAbsolutePath()
48 def libDir
49 def libDistDir
50 def compile_source_compatibility
51 def compile_target_compatibility
52
53 ext {
54   // where the getdown channel will be built.
55   // TODO: consider allowing this expression to  be overridden by -P arg
56   getdownWebsiteDir = jalviewDir + '/' + getdown_website_dir + '/' + JAVA_VERSION
57   getdownAppDir = getdownWebsiteDir + '/' + getdown_app_dir
58   //getdownJ11libDir = getdownWebsiteDir + '/' + getdown_j11lib_dir
59   getdownResourceDir = getdownWebsiteDir + '/' + getdown_resource_dir
60   getdownLauncher = jalviewDir + '/' + getdown_launcher
61   getdownFilesDir = jalviewDir + '/' + getdown_files_dir + '/' + JAVA_VERSION + '/'
62   getdown_app_base = getdown_channel_base+"/"+getdown_channel_name+"/"+JAVA_VERSION+"/"
63   modules_compileClasspath = fileTree(dir: "$jalviewDir/$j11modDir", include: ["*.jar"])
64   modules_runtimeClasspath = modules_compileClasspath
65   gitHash = ""
66   gitBranch = ""
67 }
68
69 def JAVA_INTEGER_VERSION
70 def additional_compiler_args = []
71 // these are getdown.txt properties defined dependent on the JAVA_VERSION
72 def getdown_alt_java_min_version
73 // this property is assigned below and expanded to multiple lines in the getdown task
74 def getdown_alt_multi_java_location
75 // this property is for the Java library used in eclipse
76 def eclipse_java_runtime_name
77 if (JAVA_VERSION.equals("1.8")) {
78   JAVA_INTEGER_VERSION = "8"
79   //libDir = j8libDir
80   libDir = j11libDir
81   libDistDir = j8libDir
82   compile_source_compatibility = 1.8
83   compile_target_compatibility = 1.8
84   getdown_alt_java_min_version = getdown_alt_java8_min_version
85   getdown_alt_multi_java_location = getdown_alt_java8_txt_multi_java_location
86   eclipse_java_runtime_name = "JavaSE-1.8"
87 } else if (JAVA_VERSION.equals("11")) {
88   JAVA_INTEGER_VERSION = "11"
89   libDir = j11libDir
90   libDistDir = j11libDir
91   compile_source_compatibility = 11
92   compile_target_compatibility = 11
93   getdown_alt_java_min_version = getdown_alt_java11_min_version
94   getdown_alt_multi_java_location = getdown_alt_java11_txt_multi_java_location
95   eclipse_java_runtime_name = "JavaSE-11"
96   additional_compiler_args += [
97     '--module-path', ext.modules_compileClasspath.asPath,
98     '--add-modules', j11modules
99   ]
100 } else {
101   throw new GradleException("JAVA_VERSION=$JAVA_VERSION not currently supported by Jalview")
102 }
103
104 sourceSets {
105
106   main {
107     java {
108       srcDirs "$jalviewDir/$sourceDir"
109       outputDir = file("$classes")
110     }
111
112     resources {
113       srcDirs "$jalviewDir/$resourceDir"
114     }
115
116     jar.destinationDir = file("$jalviewDir/$packageDir")
117
118     compileClasspath = files(sourceSets.main.java.outputDir)
119     compileClasspath += fileTree(dir: "$jalviewDir/$libDir", include: ["*.jar"])
120
121     runtimeClasspath = compileClasspath
122   }
123   clover {
124     java {
125       srcDirs = [ cloverInstrDir ]
126       outputDir = file("${buildDir}/${cloverClassesDir}")
127     }
128
129     resources {
130       srcDirs = sourceSets.main.resources.srcDirs
131     }
132     compileClasspath = configurations.cloverRuntime + files( sourceSets.clover.java.outputDir )
133     compileClasspath += files(sourceSets.main.java.outputDir)
134     compileClasspath += sourceSets.main.compileClasspath
135     compileClasspath += fileTree(dir: "$jalviewDir/$utilsDir", include: ["**/*.jar"])
136     compileClasspath += fileTree(dir: "$jalviewDir/$libDir", include: ["*.jar"])
137
138     runtimeClasspath = compileClasspath
139   }
140
141   test {
142     java {
143       srcDirs "$jalviewDir/$testSourceDir"
144       outputDir = file("$jalviewDir/$testOutputDir")
145     }
146
147     resources {
148       srcDirs = sourceSets.main.resources.srcDirs
149     }
150
151     compileClasspath = files( sourceSets.test.java.outputDir )
152
153     if (use_clover) {
154       compileClasspath += sourceSets.clover.compileClasspath
155     } else {
156       compileClasspath += files(sourceSets.main.java.outputDir)
157     }
158     
159     compileClasspath += fileTree(dir: "$jalviewDir/$utilsDir", include: ["**/*.jar"])
160     compileClasspath += fileTree(dir: "$jalviewDir/$libDir", include: ["*.jar"])
161
162     runtimeClasspath = compileClasspath
163   }
164 }
165
166 // clover bits
167 dependencies {
168   if (use_clover) {
169     cloverCompile 'org.openclover:clover:4.3.1'
170     testCompile 'org.openclover:clover:4.3.1'
171   }
172 }
173
174 configurations {
175   cloverRuntime
176   cloverRuntime.extendsFrom cloverCompile
177 }
178
179 eclipse {
180   project {
181     name = "Jalview with gradle build"
182
183     natures 'org.eclipse.jdt.core.javanature',
184         'org.eclipse.jdt.groovy.core.groovyNature',
185         'org.eclipse.buildship.core.gradleprojectnature'
186
187     buildCommand 'org.eclipse.jdt.core.javabuilder'
188     buildCommand 'org.eclipse.buildship.core.gradleprojectbuilder'
189   }
190
191   classpath {
192     //defaultOutputDir = sourceSets.main.java.outputDir
193     def removeThese = []
194     configurations.each{ if (it.isCanBeResolved()) {
195         removeThese += it
196       }
197     }
198
199     minusConfigurations += removeThese
200     plusConfigurations = [ ]
201     file {
202
203       whenMerged { cp ->
204         def removeTheseToo = []
205         HashMap<String, Boolean> addedSrcPath = new HashMap<>();
206         cp.entries.each { entry ->
207           if (entry.kind == 'src') {
208             if (addedSrcPath.getAt(entry.path) || !(entry.path == "src" || entry.path == "test")) {
209               removeTheseToo += entry
210             } else {
211               addedSrcPath.putAt(entry.path, true)
212             }
213           }
214         }
215         cp.entries.removeAll(removeTheseToo)
216         
217         print ("CP="+cp.inspect())
218         
219         cp.entries += new Output("bin/main")
220         cp.entries += new Library(fileReference(helpParentDir))
221         cp.entries += new Library(fileReference(resourceDir))
222         
223         HashMap<String, Boolean> addedLibPath = new HashMap<>();
224         
225         // changing from sourcesets.main.classpath to specific Java version lib
226         //sourceSets.main.compileClasspath.each{
227         fileTree("$jalviewDir/$libDistDir").include("**/*.jar").include("*.jar").each {
228           //don't want to add outputDir as eclipse is using its own output dir in bin/main
229           if (it.isDirectory() || ! it.exists()) {
230             // don't add dirs to classpath
231             return
232           }
233           def itPath = it.toString()
234           if (itPath.startsWith(jalviewDirAbsolutePath+"/")) {
235             itPath = itPath.substring(jalviewDirAbsolutePath.length()+1)
236           }
237           if (addedLibPath.get(itPath)) {
238             //println("Not adding duplicate entry "+itPath)
239           } else {
240             //println("Adding entry "+itPath)
241             cp.entries += new Library(fileReference(itPath))
242             addedLibPath.put(itPath, true)
243           }
244         }
245
246         // changing from sourcesets.main.classpath to specific Java version lib
247         //sourceSets.test.compileClasspath.each{
248         fileTree(dir: "$jalviewDir/$utilsDir", include: ["**/*.jar"]).each {
249           //if ((it.isDirectory() || ! it.exists()) && ! (it.equals(sourceSets.main.java.outputDir))) {
250           //no longer want to add outputDir as eclipse is using its own output dir in bin/main
251           if (it.isDirectory() || ! it.exists()) {
252             // don't add dirs to classpath
253             return false // groovy "break" in .each loop
254           }
255           def itPath = it.toString()
256           if (itPath.startsWith(jalviewDirAbsolutePath+"/")) {
257             itPath = itPath.substring(jalviewDirAbsolutePath.length()+1)
258           }
259           if (addedLibPath.get(itPath)) {
260             // don't duplicate
261           } else {
262             def lib = new Library(fileReference(itPath))
263             // this doesn't work... yet.  Adding test=true attribute using withXml below
264             //def attrs = new Node(null, 'attributes', ["test":"true"])
265             //lib.appendNode(attrs) //
266             cp.entries += lib
267             addedLibPath.put(itPath, true)
268           }
269         }
270       }  
271
272       // withXml changes ignored by buildship, these add the "test=true" attribute
273       withXml {
274         def node = it.asNode()
275         
276         def srcTestAttributes
277         node.children().each{ cpe ->
278           def attributes = cpe.attributes()
279           if (attributes.get("kind") == "src" && attributes.get("path") == "test") {
280             srcTestAttributes = cpe.find { a -> a.name() == "attributes" }
281             return
282           }
283         }
284         def addTestAttribute = true
285         srcTestAttributes.each{a ->
286           if (a.name() == "attribute" && a.attributes().getAt("name") == "test") {
287             addTestAttribute = false
288           }
289         }
290         if (addTestAttribute) {
291           srcTestAttributes.append(new Node(null, "attribute", [name:"test", value:"true"]))
292         }
293
294         node.children().each{ cpe ->
295           def attributes = cpe.attributes()
296           if (attributes.get("kind") == "lib" && attributes.get("path").startsWith("utils/")) {
297             cpe.appendNode('attributes')
298                 .appendNode('attribute', [name:"test", value:"true"])
299           }
300         }
301       } // withXML
302     } // file
303
304     containers 'org.eclipse.buildship.core.gradleclasspathcontainer'
305   } // classpath
306
307   jdt {
308     // for the IDE, use java 11 compatibility
309     sourceCompatibility = compile_source_compatibility
310     targetCompatibility = compile_target_compatibility
311     javaRuntimeName = eclipse_java_runtime_name
312
313     file {
314       withProperties { props ->
315         def jalview_prefs = new Properties()
316         def ins = new FileInputStream(jalviewDirAbsolutePath+"/"+eclipse_extra_jdt_prefs_file)
317         jalview_prefs.load(ins)
318         ins.close()
319         jalview_prefs.forEach { t, v ->
320           if (props.getAt(t) == null) {
321             props.putAt(t, v)
322           }
323         }
324       }
325     }
326   }
327   
328   //synchronizationTasks eclipseClasspath
329   //autoBuildTasks eclipseClasspath
330
331
332 task cloverInstr() {
333   // only instrument source, we build test classes as normal
334   inputs.files files (sourceSets.main.allJava) // , fileTree(dir:"$jalviewDir/$testSourceDir", include: ["**/*.java"]))
335   outputs.dir cloverInstrDir
336
337   doFirst {
338     delete cloverInstrDir
339     def argsList = ["--initstring", "${buildDir}/clover/clover.db",
340       "-d", "${buildDir}/${cloverSourcesInstrDir}"]
341     argsList.addAll(inputs.files.files.collect({ file ->
342       file.absolutePath
343     }))
344     String[] args = argsList.toArray()
345     println("About to instrument "+args.length +" files")
346     com.atlassian.clover.CloverInstr.mainImpl(args)
347   }
348 }
349   
350
351 task cloverReport {
352   group = "Verification"
353   description = "Createst the Clover report"
354   inputs.dir "${buildDir}/clover"
355   outputs.dir "${reportsDir}/clover"
356   onlyIf {
357     file("${buildDir}/clover/clover.db").exists()
358   }
359   doFirst {
360     def argsList = ["--initstring", "${buildDir}/clover/clover.db",
361       "-o", "${reportsDir}/clover"]
362     String[] args = argsList.toArray()
363     com.atlassian.clover.reporters.html.HtmlReporter.runReport(args)
364
365     // and generate ${reportsDir}/clover/clover.xml
366     args = ["--initstring", "${buildDir}/clover/clover.db",
367       "-o", "${reportsDir}/clover/clover.xml"].toArray()
368     com.atlassian.clover.reporters.xml.XMLReporter.runReport(args)
369   }
370 }
371
372 // end clover bits
373
374
375 compileJava {
376
377   doFirst {
378     sourceCompatibility = compile_source_compatibility
379     targetCompatibility = compile_target_compatibility
380     options.compilerArgs = additional_compiler_args
381     print ("Setting target compatibility to "+targetCompatibility+"\n")
382   }
383
384 }
385
386 compileTestJava {
387   if (use_clover) {
388     dependsOn compileCloverJava
389     classpath += configurations.cloverRuntime
390   } else {
391     classpath += sourceSets.main.runtimeClasspath
392   }
393   doFirst {
394     sourceCompatibility = compile_source_compatibility
395     targetCompatibility = compile_target_compatibility
396     options.compilerArgs = additional_compiler_args
397     print ("Setting target compatibility to "+targetCompatibility+"\n")
398   }
399 }
400
401
402 compileCloverJava {
403
404   doFirst {
405     sourceCompatibility = compile_source_compatibility
406     targetCompatibility = compile_target_compatibility
407     options.compilerArgs += additional_compiler_args
408     print ("Setting target compatibility to "+targetCompatibility+"\n")
409   }
410   classpath += configurations.cloverRuntime
411 }
412
413 clean {
414   delete sourceSets.main.java.outputDir
415 }
416
417 cleanTest {
418   delete sourceSets.test.java.outputDir
419   delete cloverInstrDir
420 }
421
422 // format is a string like date.format("dd MMMM yyyy")
423 def getDate(format) {
424   def date = new Date()
425   return date.format(format)
426 }
427
428 task setGitVals {
429   def hashStdOut = new ByteArrayOutputStream()
430   exec {
431     commandLine "git", "rev-parse", "--short", "HEAD"
432     standardOutput = hashStdOut
433     ignoreExitValue true
434   }
435
436   def branchStdOut = new ByteArrayOutputStream()
437   exec {
438     commandLine "git", "rev-parse", "--abbrev-ref", "HEAD"
439     standardOutput = branchStdOut
440     ignoreExitValue true
441   }
442
443   project.ext.gitHash = hashStdOut.toString().trim()
444   project.ext.gitBranch = branchStdOut.toString().trim()
445
446   outputs.upToDateWhen { false }
447 }
448
449 task createBuildProperties(type: WriteProperties) {
450   dependsOn setGitVals
451   inputs.dir("$jalviewDir/$sourceDir")
452   inputs.dir("$classes")
453   inputs.dir("$jalviewDir/$resourceDir")
454   outputFile "$classes/$buildPropertiesFile"
455   // taking time specific comment out to allow better incremental builds
456   comment "--Jalview Build Details--\n"+getDate("yyyy-MM-dd HH:mm:ss")
457   //comment "--Jalview Build Details--\n"+getDate("yyyy-MM-dd")
458   property "BUILD_DATE", getDate("HH:mm:ss dd MMMM yyyy")
459   property "VERSION", JALVIEW_VERSION
460   property "INSTALLATION", INSTALLATION+" git-commit:"+project.ext.gitHash+" ["+project.ext.gitBranch+"]"
461   outputs.file(outputFile)
462 }
463
464 def buildingHTML = "$jalviewDir/$docDir/building.html"
465 task deleteBuildingHTML(type: Delete) {
466   delete buildingHTML
467 }
468
469 task convertBuildingMD(type: Exec) {
470   dependsOn deleteBuildingHTML
471   def buildingMD = "$jalviewDir/$docDir/building.md"
472   def css = "$jalviewDir/$docDir/github.css"
473
474   def pandoc = null
475   pandoc_exec.split(",").each {
476     if (file(it.trim()).exists()) {
477       pandoc = it.trim()
478       return true
479     }
480   }
481
482   def hostname = "hostname".execute().text.trim()
483   if ((pandoc == null || ! file(pandoc).exists()) && hostname.equals("jv-bamboo")) {
484     pandoc = System.getProperty("user.home")+"/buildtools/pandoc/bin/pandoc"
485   }
486
487   if (pandoc != null && file(pandoc).exists()) {
488     commandLine pandoc, '-s', '-o', buildingHTML, '--metadata', 'pagetitle="Building Jalview from Source"', '--toc', '-H', css, buildingMD
489   } else {
490     commandLine "true"
491   }
492
493   ignoreExitValue true
494
495   inputs.file(buildingMD)
496   inputs.file(css)
497   outputs.file(buildingHTML)
498 }
499 clean {
500   delete buildingHTML
501 }
502
503 task syncDocs(type: Sync) {
504   dependsOn convertBuildingMD
505   def syncDir = "$classes/$docDir"
506   from fileTree("$jalviewDir/$docDir")
507   into syncDir
508
509 }
510
511 def helpFile = "$classes/$helpDir/help.jhm"
512
513 task copyHelp(type: Copy) {
514   def inputDir = "$jalviewDir/$helpParentDir/$helpDir"
515   def outputDir = "$classes/$helpDir"
516   from(inputDir) {
517     exclude '**/*.gif'
518     exclude '**/*.jpg'
519     exclude '**/*.png'
520     filter(ReplaceTokens, beginToken: '$$', endToken: '$$', tokens: ['Version-Rel': JALVIEW_VERSION])
521   }
522   from(inputDir) {
523     include '**/*.gif'
524     include '**/*.jpg'
525     include '**/*.png'
526   }
527   into outputDir
528
529   inputs.dir(inputDir)
530   outputs.files(helpFile)
531   outputs.dir(outputDir)
532 }
533
534 task syncLib(type: Sync) {
535   def syncDir = "$classes/$libDistDir"
536   from fileTree("$jalviewDir/$libDistDir")
537   into syncDir
538 }
539
540 task syncResources(type: Sync) {
541   from "$jalviewDir/$resourceDir"
542   include "**/*.*"
543   exclude "install4j"
544   into "$classes"
545   preserve {
546     include "**"
547   }
548 }
549
550 task prepare {
551   dependsOn syncResources
552   dependsOn syncDocs
553   dependsOn copyHelp
554 }
555
556
557 //testReportDirName = "test-reports" // note that test workingDir will be $jalviewDir
558 test {
559   dependsOn prepare
560   dependsOn compileJava
561   if (use_clover) {
562     dependsOn cloverInstr
563   }
564
565   print("Running tests " + (use_clover?"WITH":"WITHOUT") + " clover [clover="+use_clover+"]\n")
566
567   useTestNG() {
568     includeGroups testngGroups
569     preserveOrder true
570     useDefaultListeners=true
571   }
572
573   workingDir = jalviewDir
574   //systemProperties 'clover.jar' System.properties.clover.jar
575   sourceCompatibility = compile_source_compatibility
576   targetCompatibility = compile_target_compatibility
577   jvmArgs += additional_compiler_args
578   print ("Setting target compatibility to "+targetCompatibility+"\n")
579 }
580
581 task buildIndices(type: JavaExec) {
582   dependsOn copyHelp
583   classpath = sourceSets.main.compileClasspath
584   main = "com.sun.java.help.search.Indexer"
585   workingDir = "$classes/$helpDir"
586   def argDir = "html"
587   args = [ argDir ]
588   inputs.dir("$workingDir/$argDir")
589
590   outputs.dir("$classes/doc")
591   outputs.dir("$classes/help")
592   outputs.file("$workingDir/JavaHelpSearch/DOCS")
593   outputs.file("$workingDir/JavaHelpSearch/DOCS.TAB")
594   outputs.file("$workingDir/JavaHelpSearch/OFFSETS")
595   outputs.file("$workingDir/JavaHelpSearch/POSITIONS")
596   outputs.file("$workingDir/JavaHelpSearch/SCHEMA")
597   outputs.file("$workingDir/JavaHelpSearch/TMAP")
598 }
599
600 task compileLinkCheck(type: JavaCompile) {
601   options.fork = true
602   classpath = files("$jalviewDir/$utilsDir")
603   destinationDir = file("$jalviewDir/$utilsDir")
604   source = fileTree(dir: "$jalviewDir/$utilsDir", include: ["HelpLinksChecker.java", "BufferedLineReader.java"])
605
606   inputs.file("$jalviewDir/$utilsDir/HelpLinksChecker.java")
607   inputs.file("$jalviewDir/$utilsDir/HelpLinksChecker.java")
608   outputs.file("$jalviewDir/$utilsDir/HelpLinksChecker.class")
609   outputs.file("$jalviewDir/$utilsDir/BufferedLineReader.class")
610 }
611
612 def helplinkscheckeroutputfile = file("$jalviewDir/$utilsDir/HelpLinksChecker.out")
613 task linkCheck(type: JavaExec) {
614   dependsOn prepare, compileLinkCheck
615   classpath = files("$jalviewDir/$utilsDir")
616   main = "HelpLinksChecker"
617   workingDir = jalviewDir
618   def help = "$classes/$helpDir"
619   args = [ "$classes/$helpDir", "-nointernet" ]
620
621   doFirst {
622     helplinkscheckeroutputfile.createNewFile()
623     standardOutput new FileOutputStream(helplinkscheckeroutputfile, false)
624   }
625
626   outputs.file(helplinkscheckeroutputfile)
627 }
628
629 task cleanPackageDir(type: Delete) {
630   delete fileTree("$jalviewDir/$packageDir").include("*.jar")
631 }
632
633 jar {
634   dependsOn linkCheck
635   dependsOn buildIndices
636   dependsOn createBuildProperties
637
638   manifest {
639     attributes "Main-Class": mainClass,
640     "Permissions": "all-permissions",
641     "Application-Name": "Jalview Desktop",
642     "Codebase": application_codebase
643   }
644
645   destinationDir = file("$jalviewDir/$packageDir")
646   archiveName = rootProject.name+".jar"
647
648   exclude "cache*/**"
649   exclude "*.jar"
650   exclude "*.jar.*"
651   exclude "**/*.jar"
652   exclude "**/*.jar.*"
653
654   inputs.dir("$classes")
655   outputs.file("$jalviewDir/$packageDir/$archiveName")
656 }
657
658 task copyJars(type: Copy) {
659   from fileTree("$classes").include("**/*.jar").include("*.jar").files
660   into "$jalviewDir/$packageDir"
661 }
662
663 // doing a Sync instead of Copy as Copy doesn't deal with "outputs" very well
664 task syncJars(type: Sync) {
665   from fileTree("$jalviewDir/$libDistDir").include("**/*.jar").include("*.jar").files
666   into "$jalviewDir/$packageDir"
667   preserve {
668     include jar.archiveName
669   }
670 }
671
672 task makeDist {
673   group = "build"
674   description = "Put all required libraries in dist"
675   // order of "cleanPackageDir", "copyJars", "jar" important!
676   jar.mustRunAfter cleanPackageDir
677   syncJars.mustRunAfter cleanPackageDir
678   dependsOn cleanPackageDir
679   dependsOn syncJars
680   dependsOn jar
681   outputs.dir("$jalviewDir/$packageDir")
682 }
683
684 task cleanDist {
685   dependsOn cleanPackageDir
686   dependsOn cleanTest
687   dependsOn clean
688 }
689
690 shadowJar {
691   group = "distribution"
692   dependsOn makeDist
693   from ("$jalviewDir/$libDistDir") {
694     include("*.jar")
695   }
696   mainClassName = shadowJarMainClass
697   mergeServiceFiles()
698   classifier = "all-"+JAVA_VERSION
699   minimize()
700 }
701
702 task getdownWebsite() {
703   group = "distribution"
704   description = "Create the getdown minimal app folder, and website folder for this version of jalview. Website folder also used for offline app installer"
705   dependsOn makeDist
706   def getdownWebsiteResourceFilenames = []
707   def getdownTextString = ""
708   def getdownResourceDir = project.ext.getdownResourceDir
709   def getdownAppDir = project.ext.getdownAppDir
710   def getdownResourceFilenames = []
711   doFirst {
712     // go through properties looking for getdown_txt_...
713     def props = project.properties.sort { it.key }
714     props.put("getdown_txt_java_min_version", getdown_alt_java_min_version)
715     props.put("getdown_txt_multi_java_location", getdown_alt_multi_java_location)
716
717     if (getdown_local == "true") {
718       getdown_app_base = file(getdownWebsiteDir).toURI().toString()
719     }
720     props.put("getdown_txt_appbase", getdown_app_base)
721     props.each{ prop, val ->
722       if (prop.startsWith("getdown_txt_") && val != null) {
723         if (prop.startsWith("getdown_txt_multi_")) {
724           def key = prop.substring(18)
725           val.split(",").each{ v ->
726             def line = key + " = " + v + "\n"
727             getdownTextString += line
728           }
729         } else {
730           // file values rationalised
731           if (val.indexOf('/') > -1) {
732             def r = null
733             if (val.indexOf('/') == 0) {
734               // absolute path
735               r = file(val)
736             } else if (val.indexOf('/') > 0) {
737               // relative path (relative to jalviewDir)
738               r = file( jalviewDir + '/' + val )
739             }
740             if (r.exists()) {
741               val = getdown_resource_dir + '/' + r.getName()
742               getdownWebsiteResourceFilenames += val
743               getdownResourceFilenames += r.getPath()
744             }
745           }
746           def line = prop.substring(12) + " = " + val + "\n"
747           getdownTextString += line
748         }
749       }
750     }
751
752     getdownWebsiteResourceFilenames.each{ filename ->
753       getdownTextString += "resource = "+filename+"\n"
754     }
755     getdownResourceFilenames.each{ filename ->
756       copy {
757         from filename
758         into project.ext.getdownResourceDir
759       }
760     }
761
762     def codeFiles = []
763     makeDist.outputs.files.each{ f ->
764       if (f.isDirectory()) {
765         def files = fileTree(dir: f, include: ["*"]).getFiles()
766         codeFiles += files
767       } else if (f.exists()) {
768         codeFiles += f
769       }
770     }
771     codeFiles.sort().each{f ->
772       def line = "code = " + getdown_app_dir + '/' + f.getName() + "\n"
773       getdownTextString += line
774       copy {
775         from f.getPath()
776         into project.ext.getdownAppDir
777       }
778     }
779
780     // NOT USING MODULES YET, EVERYTHING SHOULD BE IN dist
781     /*
782      if (JAVA_VERSION.equals("11")) {
783      def j11libFiles = fileTree(dir: "$jalviewDir/$j11libDir", include: ["*.jar"]).getFiles()
784      j11libFiles.sort().each{f ->
785      def line = "code = " + getdown_j11lib_dir + '/' + f.getName() + "\n"
786      getdownTextString += line
787      copy {
788      from f.getPath()
789      into project.ext.getdownJ11libDir
790      }
791      }
792      }
793      */
794
795     // getdown-launcher.jar should not be in main application class path so the main application can move it when updated.  Listed as a resource so it gets updated.
796     //getdownTextString += "class = " + file(getdownLauncher).getName() + "\n"
797     getdownTextString += "resource = " + file(getdownLauncher).getName() + "\n"
798     getdownTextString += "class = " + mainClass + "\n"
799
800     def getdown_txt = file(project.ext.getdownWebsiteDir + "/getdown.txt")
801     getdown_txt.write(getdownTextString)
802
803     copy {
804       from getdown_txt
805       into project.ext.getdownFilesDir
806     }
807
808     copy {
809       from getdownLauncher
810       into project.ext.getdownFilesDir
811     }
812
813     copy {
814       from getdownLauncher
815       into project.ext.getdownWebsiteDir
816     }
817
818     copy {
819       from jalviewDir + '/' + project.getProperty('getdown_txt_ui.background_image')
820       from jalviewDir + '/' + project.getProperty('getdown_txt_ui.error_background')
821       from jalviewDir + '/' + project.getProperty('getdown_txt_ui.progress_image')
822       from jalviewDir + '/' + project.getProperty('getdown_txt_ui.icon')
823       from jalviewDir + '/' + project.getProperty('getdown_txt_ui.mac_dock_icon')
824       into project.ext.getdownFilesDir + '/' + getdown_resource_dir
825     }
826   }
827
828   inputs.dir(jalviewDir + '/' + packageDir)
829   outputs.dir(project.ext.getdownWebsiteDir)
830   outputs.dir(project.ext.getdownFilesDir)
831 }
832
833 task getdownDigest(type: JavaExec) {
834   group = "distribution"
835   description = "Digest the getdown website folder"
836   dependsOn getdownWebsite
837   classpath = files(jalviewDir + '/' + getdown_core, jalviewDir+'/'+getdown_launcher)
838   main = "com.threerings.getdown.tools.Digester"
839   args project.ext.getdownWebsiteDir
840   inputs.dir(project.ext.getdownWebsiteDir)
841   outputs.file(project.ext.getdownWebsiteDir + '/' + "digest2.txt")
842 }
843
844 task getdown() {
845   group = "distribution"
846   description = "Create the minimal and full getdown app folder for installers and website and create digest file"
847   dependsOn getdownDigest
848 }
849
850 clean {
851   delete project.ext.getdownWebsiteDir
852   delete project.ext.getdownFilesDir
853 }
854
855 install4j {
856   def install4jHomeDir = "/opt/install4j"
857   def hostname = "hostname".execute().text.trim()
858   if (hostname.equals("jv-bamboo")) {
859     install4jHomeDir = System.getProperty("user.home")+"/buildtools/install4j"
860   } else if (OperatingSystem.current().isMacOsX()) {
861     install4jHomeDir = '/Applications/install4j.app/Contents/Resources/app'
862     if (! file(install4jHomeDir).exists()) {
863       install4jHomeDir = System.getProperty("user.home")+install4jHomeDir
864     }
865   } else if (OperatingSystem.current().isLinux()) {
866     install4jHomeDir = System.getProperty("user.home")+"/buildtools/install4j"
867   }
868   installDir = file(install4jHomeDir)
869   mediaTypes = Arrays.asList(install4jMediaTypes.split(","))
870   if (install4jFaster.equals("true")) {
871     faster = true
872   }
873 }
874
875 def install4jConf
876 def macosJavaVMDir
877 def macosJavaVMTgz
878 def windowsJavaVMDir
879 def windowsJavaVMTgz
880 def install4jDir = "$jalviewDir/$install4jResourceDir"
881 def install4jConfFile = "jalview-installers-java"+JAVA_VERSION+".install4j"
882 install4jConf = "$install4jDir/$install4jConfFile"
883
884 task copyInstall4jTemplate(type: Copy) {
885   macosJavaVMDir = System.env.HOME+"/buildtools/jre/openjdk-java_vm/getdown/macos-jre"+JAVA_VERSION+"/jre"
886   macosJavaVMTgz = System.env.HOME+"/buildtools/jre/openjdk-java_vm/install4j/tgz/macos-jre"+JAVA_VERSION+".tar.gz"
887   windowsJavaVMDir = System.env.HOME+"/buildtools/jre/openjdk-java_vm/getdown/windows-jre"+JAVA_VERSION+"/jre"
888   windowsJavaVMTgz = System.env.HOME+"/buildtools/jre/openjdk-java_vm/install4j/tgz/windows-jre"+JAVA_VERSION+".tar.gz"
889   from (install4jDir) {
890     include install4jTemplate
891     rename (install4jTemplate, install4jConfFile)
892     filter(ReplaceTokens, beginToken: '', endToken: '', tokens: ['9999999999': JAVA_VERSION])
893     filter(ReplaceTokens, beginToken: '$$', endToken: '$$',
894       tokens: [
895         'JAVA_VERSION': JAVA_VERSION,
896         'JAVA_INTEGER_VERSION': JAVA_INTEGER_VERSION,
897         'VERSION': JALVIEW_VERSION,
898         'MACOS_JAVA_VM_DIR': macosJavaVMDir,
899         'MACOS_JAVA_VM_TGZ': macosJavaVMTgz,
900         'WINDOWS_JAVA_VM_DIR': windowsJavaVMDir,
901         'WINDOWS_JAVA_VM_TGZ': windowsJavaVMTgz,
902         'INSTALL4JINFOPLISTFILEASSOCIATIONS': install4jInfoPlistFileAssociations,
903         'COPYRIGHT_MESSAGE': install4jCopyrightMessage,
904         'MACOS_BUNDLE_ID': install4jMacOSBundleId
905       ]
906     )
907     if (OSX_KEYPASS=="") {
908       filter(ReplaceTokens, beginToken: 'codeSigning macEnabled="', endToken: '"', tokens: ['true':'codeSigning macEnabled="false"'])
909       filter(ReplaceTokens, beginToken: 'runPostProcessor="true" ',endToken: 'Processor', tokens: ['post':'runPostProcessor="false" postProcessor'])
910     }
911   }
912   into install4jDir
913   outputs.files(install4jConf)
914
915   doLast {
916     // include file associations in installer
917     def installerFileAssociationsXml = file("$install4jDir/$install4jInstallerFileAssociations").text
918     ant.replaceregexp(
919       byline: false,
920       flags: "s",
921       match: '<action name="EXTENSIONS_REPLACED_BY_GRADLE".*?</action>',
922       replace: installerFileAssociationsXml,
923       file: install4jConf
924     )
925     /*
926     // include uninstaller applescript app files in dmg
927     def installerDMGUninstallerXml = file("$install4jDir/$install4jDMGUninstallerAppFiles").text
928     ant.replaceregexp(
929       byline: false,
930       flags: "s",
931       match: '<file name="UNINSTALL_OLD_JALVIEW_APP_REPLACED_IN_GRADLE" file=.*?>',
932       replace: installerDMGUninstallerXml,
933       file: install4jConf
934     )
935     */
936   }
937 }
938
939 task installers(type: com.install4j.gradle.Install4jTask) {
940   group = "distribution"
941   description = "Create the install4j installers"
942   dependsOn getdown
943   dependsOn copyInstall4jTemplate
944   projectFile = file(install4jConf)
945   println("Using projectFile "+projectFile)
946   variables = [majorVersion: version.substring(2, 11), build: 001, OSX_KEYSTORE: OSX_KEYSTORE, JSIGN_SH: JSIGN_SH]
947   destination = "$jalviewDir/$install4jBuildDir/$JAVA_VERSION"
948   buildSelected = true
949
950   if (OSX_KEYPASS) {
951     macKeystorePassword=OSX_KEYPASS
952     
953   }
954   
955   inputs.dir(project.ext.getdownWebsiteDir)
956   inputs.file(install4jConf)
957   inputs.dir(macosJavaVMDir)
958   inputs.dir(windowsJavaVMDir)
959   outputs.dir("$jalviewDir/$install4jBuildDir/$JAVA_VERSION")
960 }
961
962 clean {
963   delete install4jConf
964 }