JAL-3210 some tidying
[jalview.git] / build.gradle
1 import org.apache.tools.ant.filters.ReplaceTokens
2 import org.gradle.internal.os.OperatingSystem
3 import org.gradle.plugins.ide.eclipse.model.Output
4 import org.gradle.plugins.ide.eclipse.model.Library
5
6 import groovy.transform.ExternalizeMethods
7
8 buildscript {
9   dependencies {
10     classpath 'org.openclover:clover:4.3.1'
11   }
12 }
13
14 plugins {
15   id 'java'
16   id 'application'
17   id 'eclipse'
18   id 'com.github.johnrengelman.shadow' version '4.0.3'
19   id 'com.install4j.gradle' version '7.0.9'
20 }
21
22 repositories {
23   jcenter()
24   mavenCentral()
25   mavenLocal()
26   flatDir {
27     dirs gradlePluginsDir
28   }
29 }
30
31 dependencies {
32   compile 'org.apache.commons:commons-compress:1.18'
33 }
34
35 mainClassName = launcherClass
36 def cloverInstrDir = file("$buildDir/$cloverSourcesInstrDir")
37 def classes = "$jalviewDir/$classesDir"
38
39 if (clover.equals("true")) {
40   use_clover = true
41   classes = "$buildDir/$cloverClassesDir"
42 } else {
43   use_clover = false
44   classes = "$jalviewDir/$classesDir"
45 }
46
47 // configure classpath/args for j8/j11 compilation
48
49 def jalviewDirAbsolutePath = file(jalviewDir).getAbsolutePath()
50 def libDir
51 def libDistDir
52 def compile_source_compatibility
53 def compile_target_compatibility
54
55 ext {
56   getdownWebsiteDir = jalviewDir + '/' + getdown_website_dir + '/' + JAVA_VERSION
57   getdownDir = ""
58   reportRsyncCmd = false
59   buildDist = true
60   buildProperties = buildPropertiesFile
61   getdownLauncher = jalviewDir + '/' + getdown_lib_dir + '/' + getdown_launcher
62   switch (CHANNEL) {
63
64     case "BUILD":
65     // TODO: get bamboo build artifact URL for getdown artifacts
66     getdown_channel_base = bamboo_channelbase
67     getdown_channel_name = bamboo_planKey + '/'+JAVA_VERSION
68     getdown_app_base = bamboo_channelbase + '/'+ bamboo_planKey + bamboo_getdown_channel_suffix + '/'+JAVA_VERSION
69     getdown_app_dir = getdown_app_dir_alt
70     buildProperties = jalviewDir + "/" + classesDir +"/" + buildPropertiesFile
71     break
72
73     case "RELEASE":
74     getdown_channel_name = CHANNEL.toLowerCase()
75     getdownDir = getdown_channel_name + "/" + JAVA_VERSION
76     getdown_app_base = getdown_channel_base + "/" + getdownDir
77     getdown_app_dir = getdown_app_dir_release
78     buildProperties = jalviewDir + "/" + classesDir +"/" + buildPropertiesFile
79     reportRsyncCommand = true
80     break
81
82     case "ARCHIVE":
83     getdown_channel_name = CHANNEL.toLowerCase()+"/"+JALVIEW_VERSION
84     getdownDir = getdown_channel_name + "/" + JAVA_VERSION
85     getdown_app_base = getdown_channel_base + "/" + getdownDir
86     getdown_app_dir = getdown_app_dir_alt
87     if (!file(ARCHIVEDIR+"/"+packageDir).exists()) {
88       print "Must provide an ARCHIVEDIR value to produce an archive distribution"
89       exit
90     } else {
91       packageDir = ARCHIVEDIR + "/" + packageDir
92       buildProperties = ARCHIVEDIR +"/" + classesDir + "/" + buildPropertiesFile
93       buildDist = false
94     }
95     reportRsyncCommand = true
96     break
97
98     case "ARCHIVELOCAL":
99     getdown_channel_name = "archive" + "/" + JALVIEW_VERSION
100     getdownDir = getdown_channel_name + "/" + JAVA_VERSION
101     getdown_app_base = file(getdownWebsiteDir).toURI().toString()
102     getdown_app_dir = getdown_app_dir_alt
103     if (!file(ARCHIVEDIR+"/"+packageDir).exists()) {
104       print "Must provide an ARCHIVEDIR value to produce an archive distribution"
105       exit
106     } else {
107       packageDir = ARCHIVEDIR + "/" + packageDir
108       buildProperties = ARCHIVEDIR +"/" + classesDir + "/" + buildPropertiesFile
109       buildDist = false
110     }
111     reportRsyncCommand = true
112     getdownLauncher = jalviewDir + '/' + getdown_lib_dir + '/' + getdown_launcher_local
113     break
114
115     case "DEVELOP":
116     getdown_channel_name = CHANNEL.toLowerCase()
117     getdownDir = getdown_channel_name + "/" + JAVA_VERSION
118     getdown_app_base = getdown_channel_base + "/" + getdownDir
119     getdown_app_dir = getdown_app_dir_alt
120     buildProperties = jalviewDir + "/" + classesDir +"/" + buildPropertiesFile
121     reportRsyncCommand = true
122     break
123
124     case "TEST-RELEASE":
125     getdown_channel_name = CHANNEL.toLowerCase()
126     getdownDir = getdown_channel_name + "/" + JAVA_VERSION
127     getdown_app_base = getdown_channel_base + "/" + getdownDir
128     getdown_app_dir = getdown_app_dir_alt
129     buildProperties = jalviewDir + "/" + classesDir +"/" + buildPropertiesFile
130     reportRsyncCommand = true
131     break
132
133     case ~/^SCRATCH(|-[-\w]*)$/:
134     getdown_channel_name = CHANNEL
135     getdownDir = getdown_channel_name + "/" + JAVA_VERSION
136     getdown_app_base = getdown_channel_base + "/" + getdownDir
137     getdown_app_dir = getdown_app_dir_alt
138     buildProperties = jalviewDir + "/" + classesDir +"/" + buildPropertiesFile
139     reportRsyncCommand = true
140     break
141
142     case "LOCAL":
143     getdown_app_base = file(getdownWebsiteDir).toURI().toString()
144     getdown_app_dir = getdown_app_dir_alt
145     buildProperties = jalviewDir + "/" + classesDir +"/" + buildPropertiesFile
146     getdownLauncher = jalviewDir + '/' + getdown_lib_dir + '/' + getdown_launcher_local
147     break
148
149     default: // something wrong specified
150     print("CHANNEL must be one of BUILD, RELEASE, ARCHIVE, DEVELOP, TEST-RELEASE, SCRATCH-..., LOCAL [default]")
151     exit
152     break
153
154   }
155
156   println("Using a "+CHANNEL+" profile. appbase="+getdown_app_base)
157   getdownAppDir = getdownWebsiteDir + '/' + getdown_app_dir
158   //getdownJ11libDir = getdownWebsiteDir + '/' + getdown_j11lib_dir
159   getdownResourceDir = getdownWebsiteDir + '/' + getdown_resource_dir
160   getdownInstallDir = getdownWebsiteDir + '/' + getdown_install_dir
161   getdownFilesDir = jalviewDir + '/' + getdown_files_dir + '/' + JAVA_VERSION + '/'
162   getdownFilesInstallDir = getdownFilesDir+"/"+getdown_install_dir
163   /* compile without modules -- using classpath libraries
164   modules_compileClasspath = fileTree(dir: "$jalviewDir/$j11modDir", include: ["*.jar"])
165   modules_runtimeClasspath = modules_compileClasspath
166   */
167   gitHash = ""
168   gitBranch = ""
169
170 }
171
172 def JAVA_INTEGER_VERSION
173 def additional_compiler_args = []
174 // these are getdown.txt properties defined dependent on the JAVA_VERSION
175 def getdown_alt_java_min_version
176 def getdown_alt_java_max_version
177 // this property is assigned below and expanded to multiple lines in the getdown task
178 def getdown_alt_multi_java_location
179 // this property is for the Java library used in eclipse
180 def eclipse_java_runtime_name
181 if (JAVA_VERSION.equals("1.8")) {
182   JAVA_INTEGER_VERSION = "8"
183   //libDir = j8libDir
184   libDir = j11libDir
185   libDistDir = j8libDir
186   compile_source_compatibility = 1.8
187   compile_target_compatibility = 1.8
188   getdown_alt_java_min_version = getdown_alt_java8_min_version
189   getdown_alt_java_max_version = getdown_alt_java8_max_version
190   getdown_alt_multi_java_location = getdown_alt_java8_txt_multi_java_location
191   eclipse_java_runtime_name = "JavaSE-1.8"
192 } else if (JAVA_VERSION.equals("11")) {
193   JAVA_INTEGER_VERSION = "11"
194   libDir = j11libDir
195   libDistDir = j11libDir
196   compile_source_compatibility = 11
197   compile_target_compatibility = 11
198   getdown_alt_java_min_version = getdown_alt_java11_min_version
199   getdown_alt_java_max_version = getdown_alt_java11_max_version
200   getdown_alt_multi_java_location = getdown_alt_java11_txt_multi_java_location
201   eclipse_java_runtime_name = "JavaSE-11"
202   /* compile without modules -- using classpath libraries
203   additional_compiler_args += [
204   '--module-path', ext.modules_compileClasspath.asPath,
205   '--add-modules', j11modules
206   ]
207   */
208 } else if (JAVA_VERSION.equals("12") || JAVA_VERSION.equals("13")) {
209   JAVA_INTEGER_VERSION = JAVA_VERSION
210   libDir = j11libDir
211   libDistDir = j11libDir
212   compile_source_compatibility = JAVA_VERSION
213   compile_target_compatibility = JAVA_VERSION
214   getdown_alt_java_min_version = getdown_alt_java11_min_version
215   getdown_alt_java_max_version = getdown_alt_java11_max_version
216   getdown_alt_multi_java_location = getdown_alt_java11_txt_multi_java_location
217   eclipse_java_runtime_name = "JavaSE-11"
218   /* compile without modules -- using classpath libraries
219   additional_compiler_args += [
220   '--module-path', ext.modules_compileClasspath.asPath,
221   '--add-modules', j11modules
222   ]
223   */
224 } else {
225   throw new GradleException("JAVA_VERSION=$JAVA_VERSION not currently supported by Jalview")
226 }
227
228 sourceSets {
229
230   main {
231     java {
232       srcDirs "$jalviewDir/$sourceDir"
233       outputDir = file("$classes")
234     }
235
236     resources {
237       srcDirs "$jalviewDir/$resourceDir"
238     }
239
240     jar.destinationDir = file("$jalviewDir/$packageDir")
241
242     compileClasspath = files(sourceSets.main.java.outputDir)
243     compileClasspath += fileTree(dir: "$jalviewDir/$libDir", include: ["*.jar"])
244
245     runtimeClasspath = compileClasspath
246   }
247
248   clover {
249     java {
250       srcDirs = [ cloverInstrDir ]
251       outputDir = file("${buildDir}/${cloverClassesDir}")
252     }
253
254     resources {
255       srcDirs = sourceSets.main.resources.srcDirs
256     }
257     compileClasspath = configurations.cloverRuntime + files( sourceSets.clover.java.outputDir )
258     compileClasspath += files(sourceSets.main.java.outputDir)
259     compileClasspath += sourceSets.main.compileClasspath
260     compileClasspath += fileTree(dir: "$jalviewDir/$utilsDir", include: ["**/*.jar"])
261     compileClasspath += fileTree(dir: "$jalviewDir/$libDir", include: ["*.jar"])
262
263     runtimeClasspath = compileClasspath
264   }
265
266   test {
267     java {
268       srcDirs "$jalviewDir/$testSourceDir"
269       outputDir = file("$jalviewDir/$testOutputDir")
270     }
271
272     resources {
273       srcDirs = sourceSets.main.resources.srcDirs
274     }
275
276     compileClasspath = files( sourceSets.test.java.outputDir )
277
278     if (use_clover) {
279       compileClasspath += sourceSets.clover.compileClasspath
280     } else {
281       compileClasspath += files(sourceSets.main.java.outputDir)
282     }
283
284     compileClasspath += fileTree(dir: "$jalviewDir/$libDir", include: ["*.jar"])
285     compileClasspath += fileTree(dir: "$jalviewDir/$utilsDir/testnglibs", include: ["**/*.jar"])
286     compileClasspath += fileTree(dir: "$jalviewDir/$utilsDir/testlibs", include: ["**/*.jar"])
287
288     runtimeClasspath = compileClasspath
289   }
290 }
291
292 // clover bits
293 dependencies {
294   if (use_clover) {
295     cloverCompile 'org.openclover:clover:4.3.1'
296     testCompile 'org.openclover:clover:4.3.1'
297   }
298 }
299
300 configurations {
301   cloverRuntime
302   cloverRuntime.extendsFrom cloverCompile
303 }
304
305 eclipse {
306   project {
307     name = eclipse_project_name
308
309     natures 'org.eclipse.jdt.core.javanature',
310     'org.eclipse.jdt.groovy.core.groovyNature',
311     'org.eclipse.buildship.core.gradleprojectnature'
312
313     buildCommand 'org.eclipse.jdt.core.javabuilder'
314     buildCommand 'org.eclipse.buildship.core.gradleprojectbuilder'
315   }
316
317   classpath {
318     //defaultOutputDir = sourceSets.main.java.outputDir
319     def removeThese = []
320     configurations.each{
321       if (it.isCanBeResolved()) {
322         removeThese += it
323       }
324     }
325
326     minusConfigurations += removeThese
327     plusConfigurations = [ ]
328     file {
329
330       whenMerged { cp ->
331         def removeTheseToo = []
332         HashMap<String, Boolean> alreadyAddedSrcPath = new HashMap<>();
333         cp.entries.each { entry ->
334           if (entry.kind == 'src') {
335             if (alreadyAddedSrcPath.getAt(entry.path) || !(entry.path == "src" || entry.path == "test")) {
336               removeTheseToo += entry
337             } else {
338               alreadyAddedSrcPath.putAt(entry.path, true)
339             }
340           }
341         }
342         cp.entries.removeAll(removeTheseToo)
343
344         if (file(eclipse_bin_dir+"/main").isDirectory()) {
345           cp.entries += new Output(eclipse_bin_dir+"/main")
346         }
347         if (file(helpParentDir).isDirectory()) {
348           cp.entries += new Library(fileReference(helpParentDir))
349         }
350         if (file(resourceDir).isDirectory()) {
351           cp.entries += new Library(fileReference(resourceDir))
352         }
353
354         HashMap<String, Boolean> alreadyAddedLibPath = new HashMap<>();
355
356         sourceSets.main.compileClasspath.findAll { it.name.endsWith(".jar") }.each {
357           //don't want to add outputDir as eclipse is using its own output dir in bin/main
358           if (it.isDirectory() || ! it.exists()) {
359             // don't add dirs to classpath
360             return
361           }
362           def itPath = it.toString()
363           if (itPath.startsWith(jalviewDirAbsolutePath+"/")) {
364             // make relative path
365             itPath = itPath.substring(jalviewDirAbsolutePath.length()+1)
366           }
367           if (alreadyAddedLibPath.get(itPath)) {
368             //println("Not adding duplicate entry "+itPath)
369           } else {
370             //println("Adding entry "+itPath)
371             cp.entries += new Library(fileReference(itPath))
372             alreadyAddedLibPath.put(itPath, true)
373           }
374         }
375
376         //fileTree(dir: "$jalviewDir/$utilsDir", include: ["test*/*.jar"]).each {
377         sourceSets.test.compileClasspath.findAll { it.name.endsWith(".jar") }.each {
378           //no longer want to add outputDir as eclipse is using its own output dir in bin/main
379           if (it.isDirectory() || ! it.exists()) {
380             // don't add dirs to classpath
381             return false // groovy "break" in .each closure
382           }
383           def itPath = it.toString()
384           if (itPath.startsWith(jalviewDirAbsolutePath+"/")) {
385             itPath = itPath.substring(jalviewDirAbsolutePath.length()+1)
386           }
387           if (alreadyAddedLibPath.get(itPath)) {
388             // don't duplicate
389           } else {
390             def lib = new Library(fileReference(itPath))
391             lib.entryAttributes["test"] = "true"
392             cp.entries += lib
393             alreadyAddedLibPath.put(itPath, true)
394           }
395         }
396
397       } // whenMerged
398
399     } // file
400
401     containers 'org.eclipse.buildship.core.gradleclasspathcontainer'
402
403   } // classpath
404
405   jdt {
406     // for the IDE, use java 11 compatibility
407     sourceCompatibility = compile_source_compatibility
408     targetCompatibility = compile_target_compatibility
409     javaRuntimeName = eclipse_java_runtime_name
410
411     // add in jalview project specific properties/preferences into eclipse core preferences
412     file {
413       withProperties { props ->
414         def jalview_prefs = new Properties()
415         def ins = new FileInputStream(jalviewDirAbsolutePath+"/"+eclipse_extra_jdt_prefs_file)
416         jalview_prefs.load(ins)
417         ins.close()
418         jalview_prefs.forEach { t, v ->
419           if (props.getAt(t) == null) {
420             props.putAt(t, v)
421           }
422         }
423       }
424     }
425
426   } // jdt
427
428 }
429
430 task cloverInstr() {
431   // only instrument source, we build test classes as normal
432   inputs.files files (sourceSets.main.allJava) // , fileTree(dir:"$jalviewDir/$testSourceDir", include: ["**/*.java"]))
433   outputs.dir cloverInstrDir
434
435   doFirst {
436     delete cloverInstrDir
437     def argsList = ["--initstring", "${buildDir}/clover/clover.db",
438     "-d", "${buildDir}/${cloverSourcesInstrDir}"]
439     argsList.addAll(inputs.files.files.collect({ file ->
440       file.absolutePath
441     }))
442     String[] args = argsList.toArray()
443     println("About to instrument "+args.length +" files")
444     com.atlassian.clover.CloverInstr.mainImpl(args)
445   }
446 }
447
448
449 task cloverReport {
450   group = "Verification"
451     description = "Createst the Clover report"
452     inputs.dir "${buildDir}/clover"
453     outputs.dir "${reportsDir}/clover"
454     onlyIf {
455       file("${buildDir}/clover/clover.db").exists()
456     }
457   doFirst {
458     def argsList = ["--initstring", "${buildDir}/clover/clover.db",
459     "-o", "${reportsDir}/clover"]
460     String[] args = argsList.toArray()
461     com.atlassian.clover.reporters.html.HtmlReporter.runReport(args)
462
463     // and generate ${reportsDir}/clover/clover.xml
464     args = ["--initstring", "${buildDir}/clover/clover.db",
465     "-o", "${reportsDir}/clover/clover.xml"].toArray()
466     com.atlassian.clover.reporters.xml.XMLReporter.runReport(args)
467   }
468 }
469
470 // end clover bits
471
472
473 compileJava {
474
475   doFirst {
476     sourceCompatibility = compile_source_compatibility
477     targetCompatibility = compile_target_compatibility
478     options.compilerArgs = additional_compiler_args
479     print ("Setting target compatibility to "+targetCompatibility+"\n")
480   }
481
482 }
483
484 compileTestJava {
485   if (use_clover) {
486     dependsOn compileCloverJava
487     classpath += configurations.cloverRuntime
488   } else {
489     classpath += sourceSets.main.runtimeClasspath
490   }
491   doFirst {
492     sourceCompatibility = compile_source_compatibility
493     targetCompatibility = compile_target_compatibility
494     options.compilerArgs = additional_compiler_args
495     print ("Setting target compatibility to "+targetCompatibility+"\n")
496   }
497 }
498
499
500 compileCloverJava {
501
502   doFirst {
503     sourceCompatibility = compile_source_compatibility
504     targetCompatibility = compile_target_compatibility
505     options.compilerArgs += additional_compiler_args
506     print ("Setting target compatibility to "+targetCompatibility+"\n")
507   }
508   classpath += configurations.cloverRuntime
509 }
510
511 clean {
512   delete sourceSets.main.java.outputDir
513 }
514
515 cleanTest {
516   delete sourceSets.test.java.outputDir
517   delete cloverInstrDir
518 }
519
520 // format is a string like date.format("dd MMMM yyyy")
521 def getDate(format) {
522   def date = new Date()
523   return date.format(format)
524 }
525
526 task setGitVals {
527   def hashStdOut = new ByteArrayOutputStream()
528   exec {
529     commandLine "git", "rev-parse", "--short", "HEAD"
530     standardOutput = hashStdOut
531     ignoreExitValue true
532   }
533
534   def branchStdOut = new ByteArrayOutputStream()
535   exec {
536     commandLine "git", "rev-parse", "--abbrev-ref", "HEAD"
537     standardOutput = branchStdOut
538     ignoreExitValue true
539   }
540
541   project.ext.gitHash = hashStdOut.toString().trim()
542   project.ext.gitBranch = branchStdOut.toString().trim()
543
544   outputs.upToDateWhen { false }
545 }
546
547 task createBuildProperties(type: WriteProperties) {
548   dependsOn setGitVals
549   inputs.dir("$jalviewDir/$sourceDir")
550   inputs.dir("$classes")
551   inputs.dir("$jalviewDir/$resourceDir")
552   outputFile (buildProperties)
553   // taking time specific comment out to allow better incremental builds
554   comment "--Jalview Build Details--\n"+getDate("yyyy-MM-dd HH:mm:ss")
555   //comment "--Jalview Build Details--\n"+getDate("yyyy-MM-dd")
556   property "BUILD_DATE", getDate("HH:mm:ss dd MMMM yyyy")
557   property "VERSION", JALVIEW_VERSION
558   property "INSTALLATION", INSTALLATION+" git-commit:"+project.ext.gitHash+" ["+project.ext.gitBranch+"]"
559   outputs.file(outputFile)
560 }
561
562 def buildingHTML = "$jalviewDir/$docDir/building.html"
563 task deleteBuildingHTML(type: Delete) {
564   delete buildingHTML
565 }
566
567 task convertBuildingMD(type: Exec) {
568   dependsOn deleteBuildingHTML
569   def buildingMD = "$jalviewDir/$docDir/building.md"
570   def css = "$jalviewDir/$docDir/github.css"
571
572   def pandoc = null
573   pandoc_exec.split(",").each {
574     if (file(it.trim()).exists()) {
575       pandoc = it.trim()
576       return true
577     }
578   }
579
580   def hostname = "hostname".execute().text.trim()
581   if ((pandoc == null || ! file(pandoc).exists()) && hostname.equals("jv-bamboo")) {
582     pandoc = System.getProperty("user.home")+"/buildtools/pandoc/bin/pandoc"
583   }
584
585   doFirst {
586     if (pandoc != null && file(pandoc).exists()) {
587         commandLine pandoc, '-s', '-o', buildingHTML, '--metadata', 'pagetitle="Building Jalview from Source"', '--toc', '-H', css, buildingMD
588     } else {
589         println("Cannot find pandoc. Skipping convert building.md to HTML")
590         throw new StopExecutionException()
591     }
592   }
593
594   ignoreExitValue true
595
596   inputs.file(buildingMD)
597   inputs.file(css)
598   outputs.file(buildingHTML)
599 }
600 clean {
601   delete buildingHTML
602 }
603
604 task syncDocs(type: Sync) {
605   dependsOn convertBuildingMD
606   def syncDir = "$classes/$docDir"
607   from fileTree("$jalviewDir/$docDir")
608   into syncDir
609
610 }
611
612 def helpFile = "$classes/$helpDir/help.jhm"
613
614 task copyHelp(type: Copy) {
615   def inputDir = "$jalviewDir/$helpParentDir/$helpDir"
616   def outputDir = "$classes/$helpDir"
617   from(inputDir) {
618     exclude '**/*.gif'
619       exclude '**/*.jpg'
620       exclude '**/*.png'
621       filter(ReplaceTokens, beginToken: '$$', endToken: '$$', tokens: ['Version-Rel': JALVIEW_VERSION,'Year-Rel': getDate("yyyy")])
622   }
623   from(inputDir) {
624     include '**/*.gif'
625       include '**/*.jpg'
626       include '**/*.png'
627   }
628   into outputDir
629
630   inputs.dir(inputDir)
631   outputs.files(helpFile)
632   outputs.dir(outputDir)
633 }
634
635 task syncLib(type: Sync) {
636   def syncDir = "$classes/$libDistDir"
637   from fileTree("$jalviewDir/$libDistDir")
638   into syncDir
639 }
640
641 task syncResources(type: Sync) {
642   from "$jalviewDir/$resourceDir"
643   include "**/*.*"
644   exclude "install4j"
645   into "$classes"
646   preserve {
647     include "**"
648   }
649 }
650
651 task prepare {
652   dependsOn syncResources
653   dependsOn syncDocs
654   dependsOn copyHelp
655 }
656
657
658 //testReportDirName = "test-reports" // note that test workingDir will be $jalviewDir
659 test {
660   dependsOn prepare
661   dependsOn compileJava
662   if (use_clover) {
663     dependsOn cloverInstr
664   }
665
666   if (use_clover) {
667     print("Running tests " + (use_clover?"WITH":"WITHOUT") + " clover [clover="+use_clover+"]\n")
668   }
669
670   useTestNG() {
671     includeGroups testngGroups
672     preserveOrder true
673     useDefaultListeners=true
674   }
675
676   workingDir = jalviewDir
677   //systemProperties 'clover.jar' System.properties.clover.jar
678   sourceCompatibility = compile_source_compatibility
679   targetCompatibility = compile_target_compatibility
680   jvmArgs += additional_compiler_args
681   print ("Setting target compatibility to "+targetCompatibility+"\n")
682 }
683
684 task buildIndices(type: JavaExec) {
685   dependsOn copyHelp
686   classpath = sourceSets.main.compileClasspath
687   main = "com.sun.java.help.search.Indexer"
688   workingDir = "$classes/$helpDir"
689   def argDir = "html"
690   args = [ argDir ]
691   inputs.dir("$workingDir/$argDir")
692
693   outputs.dir("$classes/doc")
694   outputs.dir("$classes/help")
695   outputs.file("$workingDir/JavaHelpSearch/DOCS")
696   outputs.file("$workingDir/JavaHelpSearch/DOCS.TAB")
697   outputs.file("$workingDir/JavaHelpSearch/OFFSETS")
698   outputs.file("$workingDir/JavaHelpSearch/POSITIONS")
699   outputs.file("$workingDir/JavaHelpSearch/SCHEMA")
700   outputs.file("$workingDir/JavaHelpSearch/TMAP")
701 }
702
703 task compileLinkCheck(type: JavaCompile) {
704   options.fork = true
705   classpath = files("$jalviewDir/$utilsDir")
706   destinationDir = file("$jalviewDir/$utilsDir")
707   source = fileTree(dir: "$jalviewDir/$utilsDir", include: ["HelpLinksChecker.java", "BufferedLineReader.java"])
708
709   inputs.file("$jalviewDir/$utilsDir/HelpLinksChecker.java")
710   inputs.file("$jalviewDir/$utilsDir/HelpLinksChecker.java")
711   outputs.file("$jalviewDir/$utilsDir/HelpLinksChecker.class")
712   outputs.file("$jalviewDir/$utilsDir/BufferedLineReader.class")
713 }
714
715 def helplinkscheckertouchfile = file("$jalviewDir/$utilsDir/HelpLinksChecker.touch")
716 task linkCheck(type: JavaExec) {
717   dependsOn prepare, compileLinkCheck
718   classpath = files("$jalviewDir/$utilsDir")
719   main = "HelpLinksChecker"
720   workingDir = jalviewDir
721   def help = "$classes/$helpDir"
722   args = [ "$classes/$helpDir", "-nointernet" ]
723
724   doLast {
725     helplinkscheckertouchfile.createNewFile()
726   }
727
728   inputs.dir("$classes/$helpDir")
729   outputs.file(helplinkscheckertouchfile)
730 }
731
732 // import the pubhtmlhelp target
733 ant.properties.basedir = "$jalviewDir"
734 ant.properties.helpBuildDir = jalviewDirAbsolutePath+"/$classes/$helpDir"
735 ant.importBuild "$utilsDir/publishHelp.xml"
736
737
738 task cleanPackageDir(type: Delete) {
739   delete fileTree("$jalviewDir/$packageDir").include("*.jar")
740 }
741
742 jar {
743   dependsOn linkCheck
744   dependsOn buildIndices
745   dependsOn createBuildProperties
746
747   manifest {
748     attributes "Main-Class": mainClass,
749     "Permissions": "all-permissions",
750     "Application-Name": "Jalview Desktop",
751     "Codebase": application_codebase
752   }
753
754   destinationDir = file("$jalviewDir/$packageDir")
755   archiveName = rootProject.name+".jar"
756
757   exclude "cache*/**"
758   exclude "*.jar"
759   exclude "*.jar.*"
760   exclude "**/*.jar"
761   exclude "**/*.jar.*"
762
763   inputs.dir("$classes")
764   outputs.file("$jalviewDir/$packageDir/$archiveName")
765 }
766
767 task copyJars(type: Copy) {
768   from fileTree("$classes").include("**/*.jar").include("*.jar").files
769   into "$jalviewDir/$packageDir"
770 }
771
772 // doing a Sync instead of Copy as Copy doesn't deal with "outputs" very well
773 task syncJars(type: Sync) {
774   from fileTree("$jalviewDir/$libDistDir").include("**/*.jar").include("*.jar").files
775   into "$jalviewDir/$packageDir"
776   preserve {
777     include jar.archiveName
778   }
779 }
780
781 task makeDist {
782   group = "build"
783   description = "Put all required libraries in dist"
784   // order of "cleanPackageDir", "copyJars", "jar" important!
785   jar.mustRunAfter cleanPackageDir
786   syncJars.mustRunAfter cleanPackageDir
787   dependsOn cleanPackageDir
788   dependsOn syncJars
789   dependsOn jar
790   outputs.dir("$jalviewDir/$packageDir")
791 }
792
793 task cleanDist {
794   dependsOn cleanPackageDir
795   dependsOn cleanTest
796   dependsOn clean
797 }
798
799 shadowJar {
800   group = "distribution"
801   if (buildDist) {
802     dependsOn makeDist
803   }
804   from ("$jalviewDir/$libDistDir") {
805     include("*.jar")
806   }
807   manifest {
808     attributes 'Implementation-Version': JALVIEW_VERSION
809   }
810   mainClassName = shadowJarMainClass
811   mergeServiceFiles()
812   classifier = "all-"+JALVIEW_VERSION+"-j"+JAVA_VERSION
813   minimize()
814 }
815
816 task getdownWebsite() {
817   group = "distribution"
818   description = "Create the getdown minimal app folder, and website folder for this version of jalview. Website folder also used for offline app installer"
819   if (buildDist) {
820     dependsOn makeDist
821   }
822
823         // clean the getdown website and files dir before creating getdown folders
824         delete project.ext.getdownWebsiteDir
825         delete project.ext.getdownFilesDir
826
827   def getdownWebsiteResourceFilenames = []
828   def getdownTextString = ""
829   def getdownResourceDir = project.ext.getdownResourceDir
830   def getdownAppDir = project.ext.getdownAppDir
831   def getdownResourceFilenames = []
832
833   doFirst {
834     copy {
835       from buildProperties
836       rename(buildPropertiesFile, getdown_build_properties)
837       into project.ext.getdownAppDir
838     }
839     getdownWebsiteResourceFilenames += getdown_app_dir+"/"+getdown_build_properties
840
841     // go through properties looking for getdown_txt_...
842     def props = project.properties.sort { it.key }
843         if (getdown_alt_java_min_version.length() > 0) {
844                 props.put("getdown_txt_java_min_version", getdown_alt_java_min_version)
845         }
846         if (getdown_alt_java_max_version.length() > 0) {
847                 props.put("getdown_txt_java_max_version", getdown_alt_java_max_version)
848         }
849         props.put("getdown_txt_multi_java_location", getdown_alt_multi_java_location)
850
851     props.put("getdown_txt_appbase", getdown_app_base)
852     props.each{ prop, val ->
853       if (prop.startsWith("getdown_txt_") && val != null) {
854         if (prop.startsWith("getdown_txt_multi_")) {
855           def key = prop.substring(18)
856           val.split(",").each{ v ->
857             def line = key + " = " + v + "\n"
858             getdownTextString += line
859           }
860         } else {
861           // file values rationalised
862           if (val.indexOf('/') > -1 || prop.startsWith("getdown_txt_resource")) {
863             def r = null
864             if (val.indexOf('/') == 0) {
865               // absolute path
866               r = file(val)
867             } else if (val.indexOf('/') > 0) {
868               // relative path (relative to jalviewDir)
869               r = file( jalviewDir + '/' + val )
870             }
871             if (r.exists()) {
872               val = getdown_resource_dir + '/' + r.getName()
873               getdownWebsiteResourceFilenames += val
874               getdownResourceFilenames += r.getPath()
875             }
876           }
877           if (! prop.startsWith("getdown_txt_resource")) {
878             def line = prop.substring(12) + " = " + val + "\n"
879             getdownTextString += line
880           }
881         }
882       }
883     }
884
885     getdownWebsiteResourceFilenames.each{ filename ->
886       getdownTextString += "resource = "+filename+"\n"
887     }
888     getdownResourceFilenames.each{ filename ->
889       copy {
890         from filename
891         into project.ext.getdownResourceDir
892       }
893     }
894
895     def codeFiles = []
896     fileTree(file(packageDir)).each{ f ->
897       if (f.isDirectory()) {
898         def files = fileTree(dir: f, include: ["*"]).getFiles()
899         codeFiles += files
900       } else if (f.exists()) {
901         codeFiles += f
902       }
903     }
904     codeFiles.sort().each{f ->
905       def line = "code = " + getdown_app_dir + '/' + f.getName() + "\n"
906       getdownTextString += line
907       copy {
908         from f.getPath()
909         into project.ext.getdownAppDir
910       }
911     }
912
913     // NOT USING MODULES YET, EVERYTHING SHOULD BE IN dist
914     /*
915     if (JAVA_VERSION.equals("11")) {
916     def j11libFiles = fileTree(dir: "$jalviewDir/$j11libDir", include: ["*.jar"]).getFiles()
917     j11libFiles.sort().each{f ->
918     def line = "code = " + getdown_j11lib_dir + '/' + f.getName() + "\n"
919     getdownTextString += line
920     copy {
921     from f.getPath()
922     into project.ext.getdownJ11libDir
923     }
924     }
925     }
926      */
927
928     // 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.
929     //getdownTextString += "class = " + file(getdownLauncher).getName() + "\n"
930     getdownTextString += "resource = " + getdown_launcher_new + "\n"
931     getdownTextString += "class = " + mainClass + "\n"
932
933     def getdown_txt = file(project.ext.getdownWebsiteDir + "/getdown.txt")
934     getdown_txt.write(getdownTextString)
935
936     def launch_jvl = file(project.ext.getdownWebsiteDir + "/" + getdown_launch_jvl)
937     launch_jvl.write("appbase="+props.get("getdown_txt_appbase"))
938
939     copy {
940       from getdownLauncher
941       rename(file(getdownLauncher).getName(), getdown_launcher_new)
942       into project.ext.getdownWebsiteDir
943     }
944
945     copy {
946       from getdownLauncher
947       if (file(getdownLauncher).getName() != getdown_launcher) {
948         rename(file(getdownLauncher).getName(), getdown_launcher)
949       }
950       into project.ext.getdownWebsiteDir
951     }
952
953     if (! (CHANNEL.startsWith("ARCHIVE") || CHANNEL.startsWith("DEVELOP"))) {
954       copy {
955         from getdown_txt
956         from getdownLauncher
957         from getdownWebsiteDir+"/"+getdown_build_properties
958         if (file(getdownLauncher).getName() != getdown_launcher) {
959           rename(file(getdownLauncher).getName(), getdown_launcher)
960         }
961         into getdownInstallDir
962       }
963
964       copy {
965         from getdownInstallDir
966         into getdownFilesInstallDir
967       }
968     }
969
970     copy {
971       from getdown_txt
972       from launch_jvl
973       from getdownLauncher
974       from getdownWebsiteDir+"/"+getdown_build_properties
975       if (file(getdownLauncher).getName() != getdown_launcher) {
976         rename(file(getdownLauncher).getName(), getdown_launcher)
977       }
978       into getdownFilesDir
979     }
980
981     copy {
982           from getdownResourceDir
983       into project.ext.getdownFilesDir + '/' + getdown_resource_dir
984     }
985   }
986
987   if (buildDist) {
988     inputs.dir(jalviewDir + '/' + packageDir)
989   }
990   outputs.dir(project.ext.getdownWebsiteDir)
991   outputs.dir(project.ext.getdownFilesDir)
992 }
993
994 task getdownDigest(type: JavaExec) {
995   group = "distribution"
996   description = "Digest the getdown website folder"
997   dependsOn getdownWebsite
998   doFirst {
999     classpath = files(getdownWebsiteDir + '/' + getdown_launcher)
1000   }
1001   main = "com.threerings.getdown.tools.Digester"
1002   args project.ext.getdownWebsiteDir
1003   inputs.dir(project.ext.getdownWebsiteDir)
1004   outputs.file(project.ext.getdownWebsiteDir + '/' + "digest2.txt")
1005 }
1006
1007 task getdown() {
1008   group = "distribution"
1009   description = "Create the minimal and full getdown app folder for installers and website and create digest file"
1010   dependsOn getdownDigest
1011   doLast {
1012     if (reportRsyncCommand) {
1013       def fromDir = getdownWebsiteDir + (getdownWebsiteDir.endsWith("/")?"":"/")
1014       def toDir = getdown_rsync_dest + "/" + getdownDir + (getdownDir.endsWith("/")?"":"/")
1015       println "LIKELY RSYNC COMMAND:"
1016       println "mkdir -p '$toDir'\nrsync -avh --delete '$fromDir' '$toDir'"
1017       if (RUNRSYNC == "true") {
1018         exec {
1019           commandLine "mkdir", "-p", toDir
1020         }
1021         exec {
1022           commandLine "rsync", "-avh", "--delete", fromDir, toDir
1023         }
1024       }
1025     }
1026   }
1027 }
1028
1029 clean {
1030   delete project.ext.getdownWebsiteDir
1031   delete project.ext.getdownFilesDir
1032 }
1033
1034 install4j {
1035   def install4jHomeDir = "/opt/install4j"
1036   def hostname = "hostname".execute().text.trim()
1037   if (hostname.equals("jv-bamboo")) {
1038     install4jHomeDir = System.getProperty("user.home")+"/buildtools/install4j"
1039   } else if (OperatingSystem.current().isMacOsX()) {
1040     install4jHomeDir = '/Applications/install4j.app/Contents/Resources/app'
1041     if (! file(install4jHomeDir).exists()) {
1042       install4jHomeDir = System.getProperty("user.home")+install4jHomeDir
1043     }
1044   } else if (OperatingSystem.current().isLinux()) {
1045     install4jHomeDir = System.getProperty("user.home")+"/buildtools/install4j"
1046   }
1047   installDir = file(install4jHomeDir)
1048   mediaTypes = Arrays.asList(install4jMediaTypes.split(","))
1049   if (install4jFaster.equals("true")) {
1050     faster = true
1051   }
1052 }
1053
1054 def install4jConf
1055 def macosJavaVMDir
1056 def macosJavaVMTgz
1057 def windowsJavaVMDir
1058 def windowsJavaVMTgz
1059 def install4jDir = "$jalviewDir/$install4jResourceDir"
1060 def install4jConfFile = "jalview-installers-java"+JAVA_VERSION+".install4j"
1061 install4jConf = "$install4jDir/$install4jConfFile"
1062
1063 task copyInstall4jTemplate(type: Copy) {
1064   macosJavaVMDir = System.env.HOME+"/buildtools/jre/openjdk-java_vm/getdown/macos-jre"+JAVA_VERSION+"/jre"
1065   macosJavaVMTgz = System.env.HOME+"/buildtools/jre/openjdk-java_vm/install4j/tgz/macos-jre"+JAVA_VERSION+".tar.gz"
1066   windowsJavaVMDir = System.env.HOME+"/buildtools/jre/openjdk-java_vm/getdown/windows-jre"+JAVA_VERSION+"/jre"
1067   windowsJavaVMTgz = System.env.HOME+"/buildtools/jre/openjdk-java_vm/install4j/tgz/windows-jre"+JAVA_VERSION+".tar.gz"
1068   from (install4jDir) {
1069     include install4jTemplate
1070     rename (install4jTemplate, install4jConfFile)
1071     filter(ReplaceTokens, beginToken: '', endToken: '', tokens: ['9999999999': JAVA_VERSION])
1072     filter(ReplaceTokens, beginToken: '$$', endToken: '$$',
1073     tokens: [
1074     'JAVA_VERSION': JAVA_VERSION,
1075     'JAVA_INTEGER_VERSION': JAVA_INTEGER_VERSION,
1076     'VERSION': JALVIEW_VERSION,
1077     'MACOS_JAVA_VM_DIR': macosJavaVMDir,
1078     'MACOS_JAVA_VM_TGZ': macosJavaVMTgz,
1079     'WINDOWS_JAVA_VM_DIR': windowsJavaVMDir,
1080     'WINDOWS_JAVA_VM_TGZ': windowsJavaVMTgz,
1081     'INSTALL4JINFOPLISTFILEASSOCIATIONS': install4jInfoPlistFileAssociations,
1082     'COPYRIGHT_MESSAGE': install4jCopyrightMessage,
1083     'MACOS_BUNDLE_ID': install4jMacOSBundleId,
1084     'GETDOWN_RESOURCE_DIR': getdown_resource_dir,
1085     'GETDOWN_DIST_DIR': getdown_app_dir,
1086     'GETDOWN_ALT_DIR': getdown_app_dir_alt,
1087     'GETDOWN_INSTALL_DIR': getdown_install_dir
1088     ]
1089     )
1090     if (OSX_KEYPASS=="") {
1091       filter(ReplaceTokens, beginToken: 'codeSigning macEnabled="', endToken: '"', tokens: ['true':'codeSigning macEnabled="false"'])
1092       filter(ReplaceTokens, beginToken: 'runPostProcessor="true" ',endToken: 'Processor', tokens: ['post':'runPostProcessor="false" postProcessor'])
1093     }
1094   }
1095   into install4jDir
1096   outputs.files(install4jConf)
1097
1098   doLast {
1099     // include file associations in installer
1100     def installerFileAssociationsXml = file("$install4jDir/$install4jInstallerFileAssociations").text
1101     ant.replaceregexp(
1102       byline: false,
1103       flags: "s",
1104       match: '<action name="EXTENSIONS_REPLACED_BY_GRADLE".*?</action>',
1105       replace: installerFileAssociationsXml,
1106       file: install4jConf
1107     )
1108     /*
1109     // include uninstaller applescript app files in dmg
1110     def installerDMGUninstallerXml = file("$install4jDir/$install4jDMGUninstallerAppFiles").text
1111     ant.replaceregexp(
1112     byline: false,
1113     flags: "s",
1114     match: '<file name="UNINSTALL_OLD_JALVIEW_APP_REPLACED_IN_GRADLE" file=.*?>',
1115     replace: installerDMGUninstallerXml,
1116     file: install4jConf
1117     )
1118      */
1119   }
1120 }
1121
1122 task installers(type: com.install4j.gradle.Install4jTask) {
1123   group = "distribution"
1124   description = "Create the install4j installers"
1125   dependsOn getdown
1126   dependsOn copyInstall4jTemplate
1127   projectFile = file(install4jConf)
1128   println("Using projectFile "+projectFile)
1129   variables = [majorVersion: version.substring(2, 11), build: 001, OSX_KEYSTORE: OSX_KEYSTORE, JSIGN_SH: JSIGN_SH]
1130   destination = "$jalviewDir/$install4jBuildDir/$JAVA_VERSION"
1131   buildSelected = true
1132
1133   if (OSX_KEYPASS) {
1134     macKeystorePassword=OSX_KEYPASS
1135
1136   }
1137
1138   inputs.dir(project.ext.getdownWebsiteDir)
1139   inputs.file(install4jConf)
1140   inputs.dir(macosJavaVMDir)
1141   inputs.dir(windowsJavaVMDir)
1142   outputs.dir("$jalviewDir/$install4jBuildDir/$JAVA_VERSION")
1143
1144 }
1145
1146 clean {
1147   delete install4jConf
1148 }
1149
1150 task sourceDist (type: Tar) {
1151   
1152   def VERSION_UNDERSCORES = JALVIEW_VERSION.replaceAll("\\.", "_")
1153   def outputFileName = project.name + "_" + VERSION_UNDERSCORES + ".tar.gz"
1154   // cater for buildship < 3.1 [3.0.1 is max version in eclipse 2018-09]
1155   try {
1156     archiveFileName = outputFileName
1157   } catch (Exception e) {
1158     archiveName = outputFileName
1159   }
1160   
1161   compression Compression.GZIP
1162   
1163   into project.name
1164
1165   def EXCLUDE_FILES=["build/*","bin/*","test-output/","test-reports","tests","clover*/*"
1166   ,".*"
1167   ,"benchmarking/*"
1168   ,"**/.*"
1169   ,"*.class"
1170   ,"**/*.class","$j11modDir/**/*.jar","appletlib","**/*locales"
1171   ,"*locales/**",
1172   ,"utils/InstallAnywhere"] 
1173   def PROCESS_FILES=[   "AUTHORS",
1174   "CITATION",
1175   "FEATURETODO",
1176   "JAVA-11-README",
1177   "FEATURETODO",
1178   "LICENSE",
1179   "**/README",
1180   "RELEASE",
1181   "THIRDPARTYLIBS","TESTNG",
1182   "build.gradle",
1183   "gradle.properties",
1184   "**/*.java",
1185   "**/*.html",
1186   "**/*.xml",
1187   "**/*.gradle",
1188   "**/*.groovy",
1189   "**/*.properties",
1190   "**/*.perl",
1191   "**/*.sh"]
1192
1193   from(jalviewDir) {
1194     exclude (EXCLUDE_FILES)
1195     include (PROCESS_FILES)
1196     filter(ReplaceTokens, beginToken: '$$', endToken: '$$', tokens: ['Version-Rel': JALVIEW_VERSION,'Year-Rel': getDate("yyyy")])
1197   }
1198   from(jalviewDir) {
1199     exclude (EXCLUDE_FILES)
1200     exclude (PROCESS_FILES)
1201   exclude ("appletlib")
1202   exclude ("**/*locales")
1203   exclude ("*locales/**")
1204   exclude ("utils/InstallAnywhere")
1205
1206     exclude (getdown_files_dir)
1207   exclude (getdown_website_dir)
1208
1209   // exluding these as not using jars as modules yet
1210   exclude ("$j11modDir/**/*.jar")
1211 }
1212 //  from (jalviewDir) {
1213 //    // explicit includes for stuff that seemed to not get included
1214 //    include(fileTree("test/**/*."))
1215 //    exclude(EXCLUDE_FILES)
1216 //    exclude(PROCESS_FILES)
1217 //  }
1218 }
1219
1220 task helppages  {
1221   dependsOn copyHelp
1222   dependsOn pubhtmlhelp
1223   
1224   inputs.dir("$classes/$helpDir")
1225   outputs.dir("$helpOutputDir")
1226 }
1227
1228 def jalviewjsBuildDir
1229 def jalviewjsSiteDir
1230 def jalviewjsTransferSiteDir
1231 task jalviewjsSitePath {
1232   def relativeBuildDir = file(jalviewDirAbsolutePath).toPath().relativize(buildDir.toPath())
1233   jalviewjsBuildDir = "${relativeBuildDir}/jalviewjs"
1234   if (jalviewjs_site_dir.startsWith("/")) {
1235     jalviewjsSiteDir = jalviewjs_site_dir
1236   } else {
1237     jalviewjsSiteDir = jalviewjsBuildDir + "/" + jalviewjs_site_dir
1238   }
1239   jalviewjsTransferSiteDir = jalviewjsBuildDir + "/tmp/site"
1240 }
1241
1242 def eclipseWorkspace
1243 task jalviewjsSetEclipseWorkspace {
1244   def propKey = "jalviewjs_eclipse_workspace"
1245   def propsFileName = "${jalviewjsBuildDir}/${jalviewjs_eclipse_workspace_location_file}"
1246   def props = new Properties()
1247   def eclipseWsDir
1248   def propVal = null
1249   if (project.hasProperty(propKey)) {
1250     propVal = project.getProperty(propKey)
1251     eclipseWsDir = propVal
1252   }
1253   if (( eclipseWsDir == null || !file(eclipseWsDir).exists() ) && file(propsFileName).exists()) {
1254     def ins = new FileInputStream("${jalviewDirAbsolutePath}/${propsFileName}")
1255     props.load(ins)
1256     ins.close()
1257     if (props.getProperty(propKey, null) != null) {
1258       eclipseWsDir = props.getProperty(propKey)
1259     }
1260   }
1261
1262   if (eclipseWsDir == null || !file(eclipseWsDir).exists()) {
1263     def tempDir = File.createTempDir()
1264     eclipseWsDir = tempDir.getAbsolutePath()
1265     props.setProperty(propKey, eclipseWsDir)
1266     def propsFile = file(propsFileName)
1267     propsFile.parentFile.mkdirs()
1268     propsFile.createNewFile() // doesn't affect existing file
1269     def outs = new FileOutputStream(propsFile, false)
1270     props.store(outs, null)
1271     outs.close()
1272   }
1273
1274   eclipseWorkspace = file(eclipseWsDir)
1275     
1276   println("ECLIPSE WORKSPACE: "+eclipseWorkspace.getPath())
1277
1278   inputs.property(propKey, propVal)
1279   outputs.file(propsFileName)
1280 }
1281
1282
1283 task jalviewjsUnzipFiles {
1284   dependsOn jalviewjsSitePath
1285
1286   def zipFiles = fileTree(dir: jalviewjs_utils_dir+"/"+jalviewjs_libjs_dir).include("*.zip")
1287   zipFiles += jalviewjs_utils_dir+"/"+jalviewjs_swingjs_zip
1288
1289   doLast {
1290     zipFiles.each { file_zip -> 
1291       copy {
1292         from zipTree(file_zip)
1293         into jalviewjsSiteDir
1294       }
1295     }
1296   }
1297
1298   inputs.files zipFiles
1299   outputs.dir jalviewjsSiteDir
1300 }
1301
1302 def eclipseDropinsDir
1303 def eclipseBinary
1304 def eclipseVersion
1305 def fromDropinsDir
1306 task jalviewjsEclipsePaths {
1307   def eclipseRoot
1308   def eclipseProduct
1309   eclipseRoot = jalviewjs_eclipse_root
1310   if (eclipseRoot.startsWith("~")) {
1311     eclipseRoot = System.getProperty("user.home") + eclipseRoot.substring(1)
1312   }
1313   if (OperatingSystem.current().isMacOsX()) {
1314     eclipseRoot += "/Eclipse.app"
1315     eclipseDropinsDir = eclipseRoot+"/Contents/Eclipse/dropins"
1316     eclipseBinary = eclipseRoot+"/Contents/MacOS/eclipse"
1317     eclipseProduct = eclipseRoot+"/Contents/Eclipse/.eclipseproduct"
1318   } else if (OperatingSystem.current().isWindows()) { // check these paths!!
1319     if (file(eclipseRoot+"/eclipse").isDirectory() && file(eclipseRoot+"/eclipse/.eclipseproduct").exists()) {
1320       eclipseRoot += "/eclipse"
1321     }
1322     eclipseDropinsDir = eclipseRoot+"/dropins"
1323     eclipseBinary = eclipseRoot+"/eclipse"
1324     eclipseProduct = eclipseRoot+"/.eclipseproduct"
1325   } else { // linux or unix
1326     if (file(eclipseRoot+"/eclipse").isDirectory() && file(eclipseRoot+"/eclipse/.eclipseproduct").exists()) {
1327       eclipseRoot += "/eclipse"
1328     }
1329     eclipseDropinsDir = eclipseRoot+"/dropins"
1330     eclipseBinary = eclipseRoot+"/eclipse"
1331     eclipseProduct = eclipseRoot+"/.eclipseproduct"
1332   }
1333
1334   def fis = new FileInputStream(eclipseProduct)
1335   def props = new Properties()
1336   props.load(fis)
1337   eclipseVersion = props.getProperty("version")
1338   fis.close()
1339   println("ECLIPSE_VERSION=${eclipseVersion}")
1340   String[] v = eclipseVersion.split("\\.")
1341   def v0 = Integer.valueOf(v[0])
1342   def v1 = Integer.valueOf(v[1])
1343   if (v0 < 4 || ( v0 == 4 && v1 < 13 )) {
1344     fromDropinsDir = "eclipse/dropins_4.12"
1345   } else {
1346     fromDropinsDir = "eclipse/dropins_4.13"
1347   } 
1348 }
1349
1350 task jalviewjsEclipseCopyDropins {
1351   dependsOn jalviewjsEclipsePaths
1352   def inputFiles = fileTree(dir: jalviewjs_utils_dir+"/"+fromDropinsDir, include: "*.jar")
1353   def outputDir = eclipseDropinsDir
1354
1355   inputs.files inputFiles
1356   inputFiles.each { file ->
1357     outputs.file(outputDir+"/"+file.name)
1358   }
1359
1360   doLast {
1361     def outputFiles = []
1362     inputFiles.each { file ->
1363       copy {
1364         from file
1365         into outputDir
1366       }
1367     }
1368   }
1369 }
1370
1371 task jalviewjsCreateJ2sSettings(type: WriteProperties) {
1372   dependsOn jalviewjsSitePath
1373   outputFile (jalviewDir+"/"+jalviewjs_j2s_settings)
1374   def props = project.properties.sort { it.key }
1375   def siteDirProperty = "j2s.site.directory"
1376   def setSiteDir = false
1377   props.each { prop, val ->
1378     if (prop.startsWith("j2s.") && val != null) {
1379       if (prop == siteDirProperty) {
1380         if (!(val.startsWith("/") || val.startsWith("file://") )) {
1381           val = jalviewjsTransferSiteDir+"/"+val
1382         }
1383         setSiteDir = true
1384       }
1385       property(prop,val)
1386     }
1387     if (!setSiteDir) {
1388       property(siteDirProperty,jalviewjsTransferSiteDir)
1389     }
1390   }
1391   outputs.file(outputFile)
1392 }
1393
1394 task jalviewjsEclipseSetup {
1395   dependsOn jalviewjsEclipseCopyDropins
1396   dependsOn jalviewjsSetEclipseWorkspace
1397   dependsOn jalviewjsCreateJ2sSettings
1398 }
1399
1400 task jalviewjsCopyResources (type: Copy) {
1401   dependsOn jalviewjsSitePath
1402   def inputFiles = fileTree(dir: jalviewjs_resource_dir)
1403   def outputDir = jalviewjsSiteDir+"/"+jalviewjs_j2s_subdir
1404
1405   from inputFiles
1406   into outputDir
1407   def outputFiles = []
1408   rename { filename ->
1409     outputFiles += outputDir+"/"+filename
1410     null
1411   }
1412   outputs.files outputFiles
1413   inputs.files inputFiles
1414 }
1415
1416 task jalviewjsCopySiteResources (type: Copy) {
1417   dependsOn jalviewjsSitePath
1418   def inputFiles = fileTree(dir: jalviewjs_utils_dir+"/"+jalviewjs_site_resource_dir)
1419   def outputDir = jalviewjsSiteDir
1420
1421   from inputFiles
1422   into outputDir
1423   def outputFiles = []
1424   rename { filename ->
1425     outputFiles += outputDir+"/"+filename
1426     null
1427   }
1428   outputs.files outputFiles
1429   inputs.files inputFiles
1430 }
1431
1432 task jalviewjsTotalClean {
1433   group "JalviewJS"
1434   description "Delete everything to do with JalviewJS build"
1435   //delete jalviewjsBuildDir
1436   delete jalviewDir+"/"+eclipse_bin_dir
1437   delete file(eclipseWorkspace.getAbsolutePath()+"/.metadata")
1438   delete jalviewDir+"/"+jalviewjs_j2s_settings
1439 }
1440
1441 task jalviewjsProjectImport(type: Exec) {
1442   // work out how to do this!
1443   dependsOn eclipseProject
1444   dependsOn eclipseClasspath
1445   dependsOn eclipseJdt
1446   dependsOn jalviewjsEclipsePaths
1447   dependsOn jalviewjsEclipseSetup
1448   executable(eclipseBinary)
1449   args(["-nosplash", "--launcher.suppressErrors", "-application", "com.seeq.eclipse.importprojects.headlessimport", "-data", eclipseWorkspace.getPath(), "-import", jalviewDirAbsolutePath])
1450
1451   def projdir = eclipseWorkspace.getPath()+"/.metadata/.plugins/org.eclipse.core.resources/.projects/jalview/org.eclipse.jdt.core"
1452   inputs.file(jalviewDir+"/.project")
1453   outputs.dir(projdir)
1454   outputs.upToDateWhen { file(projdir).exists() }
1455 }
1456
1457 task jalviewjsTranspile(type: Exec) {
1458   dependsOn jalviewjsEclipseSetup 
1459   dependsOn jalviewjsProjectImport
1460   dependsOn jalviewjsEclipsePaths
1461   executable(eclipseBinary)
1462   args(["-nosplash", "--launcher.suppressErrors", "-application", "org.eclipse.jdt.apt.core.aptBuild", "-data", eclipseWorkspace, "-"+jalviewjs_eclipseBuildArg, eclipse_project_name ])
1463
1464   def stdout
1465   def stderr
1466   doFirst {
1467     stdout = new ByteArrayOutputStream()
1468     stderr = new ByteArrayOutputStream()
1469
1470     def logOutFileName = "${jalviewDirAbsolutePath}/${jalviewjsBuildDir}/${jalviewjs_j2s_stdout}"
1471     def logOutFile = file(logOutFileName)
1472     logOutFile.createNewFile()
1473     def logOutFOS = new FileOutputStream(logOutFile, false)
1474     //def logErrFileName = "${jalviewjsBuildDir}/${jalviewjs_j2s_stderr}"
1475     //def logErrFile = file(logFileName)
1476     //logErrFile.createNewFile()
1477     //def logErrFOS = new FileErrputStream(logErrFile, false)
1478     // combine stdout and stderr
1479     def logErrFOS = logOutFOS
1480     if (jalviewjs_j2s_to_console.equals("true")) {
1481       standardOutput = new org.apache.tools.ant.util.TeeOutputStream(
1482         new org.apache.tools.ant.util.TeeOutputStream(
1483           logOutFOS,
1484           stdout),
1485         standardOutput)
1486       errorOutput = new org.apache.tools.ant.util.TeeOutputStream(
1487         new org.apache.tools.ant.util.TeeOutputStream(
1488           logErrFOS,
1489           stderr),
1490         errorOutput)
1491     } else {
1492       standardOutput = new org.apache.tools.ant.util.TeeOutputStream(
1493         logOutFOS,
1494         stdout)
1495       errorOutput = new org.apache.tools.ant.util.TeeOutputStream(
1496         logErrFOS,
1497         stderr)
1498     }
1499   }
1500   doLast {
1501     if (stdout.toString().contains("Error processing ")) {
1502       // j2s did not complete transpile
1503       throw new GradleException("Error during transpilation:\n${stderr}\nSee eclipse transpile log file '${jalviewjsBuildDir}/${jalviewjs_j2s_stdout}'")
1504     }
1505   }
1506
1507   inputs.dir(sourceDir)
1508   outputs.dir(eclipse_bin_dir+"/main")
1509   outputs.dir(jalviewjsTransferSiteDir)
1510
1511 }
1512
1513 task jalviewjsCopyTransferSite(type: Copy) {
1514   dependsOn jalviewjsTranspile
1515   from jalviewjsTransferSiteDir
1516   into jalviewjsSiteDir
1517 }
1518
1519 jalviewjsUnzipFiles.mustRunAfter jalviewjsCopyTransferSite
1520 jalviewjsCopyResources.mustRunAfter jalviewjsCopyTransferSite
1521 jalviewjsCopySiteResources.mustRunAfter jalviewjsCopyTransferSite
1522
1523 task jalviewjsPrepareSite {
1524   group "JalviewJS"
1525   description "Prepares the website folder including unzipping files and copying resources"
1526   dependsOn jalviewjsSitePath
1527   dependsOn jalviewjsUnzipFiles
1528   dependsOn jalviewjsCopyResources
1529   dependsOn jalviewjsCopySiteResources
1530 }
1531
1532 task jalviewjsBuildSite {
1533   group "JalviewJS"
1534   description "Builds the whole website including transpiled code"
1535   dependsOn jalviewjsCopyTransferSite
1536   dependsOn jalviewjsPrepareSite
1537 }
1538
1539 task jalviewjsSiteClean {
1540   //delete jalviewjsTransferSiteDir
1541   //delete jalviewjsSiteDir
1542 }
1543
1544 task jalviewjsSiteTar(type: Tar) {
1545   group "JalviewJS"
1546   description "Creates a tar.gz file for the website"
1547   dependsOn jalviewjsBuildSite
1548   def outputFilename = "jalviewjs-site-${JALVIEW_VERSION}.tar.gz"
1549   try {
1550     archiveFileName = outputFilename
1551   } catch (Exception e) {
1552     archiveName = outputFilename
1553   }
1554
1555   compression Compression.GZIP
1556
1557   from jalviewjsSiteDir
1558   into jalviewjs_site_dir // this is inside the tar file
1559
1560   inputs.dir(jalviewjsSiteDir)
1561 }
1562
1563 task jalviewjsServer {
1564   group "JalviewJS"
1565   description "Starts a webserver on localhost to test the website"
1566   dependsOn jalviewjsSitePath
1567   def htmlFile = "${jalviewDirAbsolutePath}/jalviewjsTest.html"
1568   doLast {
1569
1570     SimpleHttpFileServerFactory factory = new SimpleHttpFileServerFactory()
1571     def port = Integer.valueOf(jalviewjs_server_port)
1572     def start = port
1573     def running = false
1574     def url
1575     while(port < start+1000 && !running) {
1576       try {
1577         def doc_root = new File(jalviewDirAbsolutePath +"/"+ jalviewjsSiteDir)
1578         def jalviewjsServer = factory.start(doc_root, port)
1579         running = true
1580         url = jalviewjsServer.getResourceUrl(jalviewjs_server_resource)
1581         println("SERVER STARTED with document root ${doc_root}.")
1582         println("Go to "+url+" . Run  gradle --stop  to stop (kills all gradle daemons).")
1583         println("For debug: "+url+"?j2sdebug")
1584
1585         file(htmlFile).text = """
1586         <p><a href="${url}">Jalview JS Test. &lt;${url}&gt;</a></p>
1587         <p><a href="${url}?j2sdebug">Jalview JS Test with debug. &lt;${url}?j2sdebug&lt;</a></p>
1588         """
1589
1590       } catch (Exception e) {
1591         port++;
1592       }
1593     }
1594
1595   }
1596
1597   outputs.file(htmlFile)
1598 }
1599
1600 task jalviewjs {
1601   group "JalviewJS"
1602   description "Build the site"
1603   dependsOn jalviewjsBuildSite
1604 }
1605
1606
1607 task jalviewjsIDECopyTransferSite(type: Copy) {
1608   from jalviewjsTransferSiteDir
1609   into jalviewjsSiteDir
1610 }
1611
1612 task jalviewjsIDEj2s {
1613   group "JalviewJS in Eclipse"
1614   description "Creates the .j2s file"
1615   dependsOn jalviewjsCreateJ2sSettings
1616 }
1617
1618 task jalviewjsIDEBuildSite {
1619   group "JalviewJS in Eclipse"
1620   description "Copies the Eclipse transpiled site and unzips supporting zipfiles"
1621   dependsOn jalviewjsIDECopyTransferSite
1622   dependsOn jalviewjsPrepareSite
1623 }
1624
1625 task jalviewjsIDESiteClean {
1626   group "JalviewJS in Eclipse"
1627   description "Deletes the Eclipse transpiled site"
1628   dependsOn jalviewjsSiteClean
1629 }
1630
1631 task jalviewjsIDEServer {
1632   group "JalviewJS in Eclipse"
1633   description "Starts a webserver on localhost to test the website"
1634   dependsOn jalviewjsServer
1635 }
1636