JAL-3210 Fixed cleaning tasks.
[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   doFirst {
513     delete sourceSets.main.java.outputDir
514   }
515 }
516
517 cleanTest {
518   doFirst {
519     delete sourceSets.test.java.outputDir
520     delete cloverInstrDir
521   }
522 }
523
524 // format is a string like date.format("dd MMMM yyyy")
525 def getDate(format) {
526   def date = new Date()
527   return date.format(format)
528 }
529
530 task setGitVals {
531   def hashStdOut = new ByteArrayOutputStream()
532   exec {
533     commandLine "git", "rev-parse", "--short", "HEAD"
534     standardOutput = hashStdOut
535     ignoreExitValue true
536   }
537
538   def branchStdOut = new ByteArrayOutputStream()
539   exec {
540     commandLine "git", "rev-parse", "--abbrev-ref", "HEAD"
541     standardOutput = branchStdOut
542     ignoreExitValue true
543   }
544
545   project.ext.gitHash = hashStdOut.toString().trim()
546   project.ext.gitBranch = branchStdOut.toString().trim()
547
548   outputs.upToDateWhen { false }
549 }
550
551 task createBuildProperties(type: WriteProperties) {
552   dependsOn setGitVals
553   inputs.dir("$jalviewDir/$sourceDir")
554   inputs.dir("$classes")
555   inputs.dir("$jalviewDir/$resourceDir")
556   outputFile (buildProperties)
557   // taking time specific comment out to allow better incremental builds
558   comment "--Jalview Build Details--\n"+getDate("yyyy-MM-dd HH:mm:ss")
559   //comment "--Jalview Build Details--\n"+getDate("yyyy-MM-dd")
560   property "BUILD_DATE", getDate("HH:mm:ss dd MMMM yyyy")
561   property "VERSION", JALVIEW_VERSION
562   property "INSTALLATION", INSTALLATION+" git-commit:"+project.ext.gitHash+" ["+project.ext.gitBranch+"]"
563   outputs.file(outputFile)
564 }
565
566 def buildingHTML = "$jalviewDir/$docDir/building.html"
567 task cleanBuildingHTML(type: Delete) {
568   doFirst {
569     delete buildingHTML
570   }
571 }
572
573 task convertBuildingMD(type: Exec) {
574   dependsOn cleanBuildingHTML
575   def buildingMD = "$jalviewDir/$docDir/building.md"
576   def css = "$jalviewDir/$docDir/github.css"
577
578   def pandoc = null
579   pandoc_exec.split(",").each {
580     if (file(it.trim()).exists()) {
581       pandoc = it.trim()
582       return true
583     }
584   }
585
586   def hostname = "hostname".execute().text.trim()
587   if ((pandoc == null || ! file(pandoc).exists()) && hostname.equals("jv-bamboo")) {
588     pandoc = System.getProperty("user.home")+"/buildtools/pandoc/bin/pandoc"
589   }
590
591   doFirst {
592     if (pandoc != null && file(pandoc).exists()) {
593         commandLine pandoc, '-s', '-o', buildingHTML, '--metadata', 'pagetitle="Building Jalview from Source"', '--toc', '-H', css, buildingMD
594     } else {
595         println("Cannot find pandoc. Skipping convert building.md to HTML")
596         throw new StopExecutionException()
597     }
598   }
599
600   ignoreExitValue true
601
602   inputs.file(buildingMD)
603   inputs.file(css)
604   outputs.file(buildingHTML)
605 }
606
607 clean {
608   doFirst {
609     delete buildingHTML
610   }
611 }
612
613 task syncDocs(type: Sync) {
614   dependsOn convertBuildingMD
615   def syncDir = "$classes/$docDir"
616   from fileTree("$jalviewDir/$docDir")
617   into syncDir
618
619 }
620
621 def helpFile = "$classes/$helpDir/help.jhm"
622
623 task copyHelp(type: Copy) {
624   def inputDir = "$jalviewDir/$helpParentDir/$helpDir"
625   def outputDir = "$classes/$helpDir"
626   from(inputDir) {
627     exclude '**/*.gif'
628       exclude '**/*.jpg'
629       exclude '**/*.png'
630       filter(ReplaceTokens, beginToken: '$$', endToken: '$$', tokens: ['Version-Rel': JALVIEW_VERSION,'Year-Rel': getDate("yyyy")])
631   }
632   from(inputDir) {
633     include '**/*.gif'
634       include '**/*.jpg'
635       include '**/*.png'
636   }
637   into outputDir
638
639   inputs.dir(inputDir)
640   outputs.files(helpFile)
641   outputs.dir(outputDir)
642 }
643
644 task syncLib(type: Sync) {
645   def syncDir = "$classes/$libDistDir"
646   from fileTree("$jalviewDir/$libDistDir")
647   into syncDir
648 }
649
650 task syncResources(type: Sync) {
651   from "$jalviewDir/$resourceDir"
652   include "**/*.*"
653   exclude "install4j"
654   into "$classes"
655   preserve {
656     include "**"
657   }
658 }
659
660 task prepare {
661   dependsOn syncResources
662   dependsOn syncDocs
663   dependsOn copyHelp
664 }
665
666
667 //testReportDirName = "test-reports" // note that test workingDir will be $jalviewDir
668 test {
669   dependsOn prepare
670   dependsOn compileJava
671   if (use_clover) {
672     dependsOn cloverInstr
673   }
674
675   if (use_clover) {
676     print("Running tests " + (use_clover?"WITH":"WITHOUT") + " clover [clover="+use_clover+"]\n")
677   }
678
679   useTestNG() {
680     includeGroups testngGroups
681     preserveOrder true
682     useDefaultListeners=true
683   }
684
685   workingDir = jalviewDir
686   //systemProperties 'clover.jar' System.properties.clover.jar
687   sourceCompatibility = compile_source_compatibility
688   targetCompatibility = compile_target_compatibility
689   jvmArgs += additional_compiler_args
690   print ("Setting target compatibility to "+targetCompatibility+"\n")
691 }
692
693 task buildIndices(type: JavaExec) {
694   dependsOn copyHelp
695   classpath = sourceSets.main.compileClasspath
696   main = "com.sun.java.help.search.Indexer"
697   workingDir = "$classes/$helpDir"
698   def argDir = "html"
699   args = [ argDir ]
700   inputs.dir("$workingDir/$argDir")
701
702   outputs.dir("$classes/doc")
703   outputs.dir("$classes/help")
704   outputs.file("$workingDir/JavaHelpSearch/DOCS")
705   outputs.file("$workingDir/JavaHelpSearch/DOCS.TAB")
706   outputs.file("$workingDir/JavaHelpSearch/OFFSETS")
707   outputs.file("$workingDir/JavaHelpSearch/POSITIONS")
708   outputs.file("$workingDir/JavaHelpSearch/SCHEMA")
709   outputs.file("$workingDir/JavaHelpSearch/TMAP")
710 }
711
712 task compileLinkCheck(type: JavaCompile) {
713   options.fork = true
714   classpath = files("$jalviewDir/$utilsDir")
715   destinationDir = file("$jalviewDir/$utilsDir")
716   source = fileTree(dir: "$jalviewDir/$utilsDir", include: ["HelpLinksChecker.java", "BufferedLineReader.java"])
717
718   inputs.file("$jalviewDir/$utilsDir/HelpLinksChecker.java")
719   inputs.file("$jalviewDir/$utilsDir/HelpLinksChecker.java")
720   outputs.file("$jalviewDir/$utilsDir/HelpLinksChecker.class")
721   outputs.file("$jalviewDir/$utilsDir/BufferedLineReader.class")
722 }
723
724 def helplinkscheckertouchfile = file("$jalviewDir/$utilsDir/HelpLinksChecker.touch")
725 task linkCheck(type: JavaExec) {
726   dependsOn prepare, compileLinkCheck
727   classpath = files("$jalviewDir/$utilsDir")
728   main = "HelpLinksChecker"
729   workingDir = jalviewDir
730   def help = "$classes/$helpDir"
731   args = [ "$classes/$helpDir", "-nointernet" ]
732
733   doLast {
734     helplinkscheckertouchfile.createNewFile()
735   }
736
737   inputs.dir("$classes/$helpDir")
738   outputs.file(helplinkscheckertouchfile)
739 }
740
741 // import the pubhtmlhelp target
742 ant.properties.basedir = "$jalviewDir"
743 ant.properties.helpBuildDir = jalviewDirAbsolutePath+"/$classes/$helpDir"
744 ant.importBuild "$utilsDir/publishHelp.xml"
745
746
747 task cleanPackageDir(type: Delete) {
748   doFirst {
749     delete fileTree("$jalviewDir/$packageDir").include("*.jar")
750   }
751 }
752
753 jar {
754   dependsOn linkCheck
755   dependsOn buildIndices
756   dependsOn createBuildProperties
757
758   manifest {
759     attributes "Main-Class": mainClass,
760     "Permissions": "all-permissions",
761     "Application-Name": "Jalview Desktop",
762     "Codebase": application_codebase
763   }
764
765   destinationDir = file("$jalviewDir/$packageDir")
766   archiveName = rootProject.name+".jar"
767
768   exclude "cache*/**"
769   exclude "*.jar"
770   exclude "*.jar.*"
771   exclude "**/*.jar"
772   exclude "**/*.jar.*"
773
774   inputs.dir("$classes")
775   outputs.file("$jalviewDir/$packageDir/$archiveName")
776 }
777
778 task copyJars(type: Copy) {
779   from fileTree("$classes").include("**/*.jar").include("*.jar").files
780   into "$jalviewDir/$packageDir"
781 }
782
783 // doing a Sync instead of Copy as Copy doesn't deal with "outputs" very well
784 task syncJars(type: Sync) {
785   from fileTree("$jalviewDir/$libDistDir").include("**/*.jar").include("*.jar").files
786   into "$jalviewDir/$packageDir"
787   preserve {
788     include jar.archiveName
789   }
790 }
791
792 task makeDist {
793   group = "build"
794   description = "Put all required libraries in dist"
795   // order of "cleanPackageDir", "copyJars", "jar" important!
796   jar.mustRunAfter cleanPackageDir
797   syncJars.mustRunAfter cleanPackageDir
798   dependsOn cleanPackageDir
799   dependsOn syncJars
800   dependsOn jar
801   outputs.dir("$jalviewDir/$packageDir")
802 }
803
804 task cleanDist {
805   dependsOn cleanPackageDir
806   dependsOn cleanTest
807   dependsOn clean
808 }
809
810 shadowJar {
811   group = "distribution"
812   if (buildDist) {
813     dependsOn makeDist
814   }
815   from ("$jalviewDir/$libDistDir") {
816     include("*.jar")
817   }
818   manifest {
819     attributes 'Implementation-Version': JALVIEW_VERSION
820   }
821   mainClassName = shadowJarMainClass
822   mergeServiceFiles()
823   classifier = "all-"+JALVIEW_VERSION+"-j"+JAVA_VERSION
824   minimize()
825 }
826
827 task getdownWebsite() {
828   group = "distribution"
829   description = "Create the getdown minimal app folder, and website folder for this version of jalview. Website folder also used for offline app installer"
830   if (buildDist) {
831     dependsOn makeDist
832   }
833
834   def getdownWebsiteResourceFilenames = []
835   def getdownTextString = ""
836   def getdownResourceDir = project.ext.getdownResourceDir
837   def getdownAppDir = project.ext.getdownAppDir
838   def getdownResourceFilenames = []
839
840   doFirst {
841     // clean the getdown website and files dir before creating getdown folders
842     delete project.ext.getdownWebsiteDir
843     delete project.ext.getdownFilesDir
844
845     copy {
846       from buildProperties
847       rename(buildPropertiesFile, getdown_build_properties)
848       into project.ext.getdownAppDir
849     }
850     getdownWebsiteResourceFilenames += getdown_app_dir+"/"+getdown_build_properties
851
852     // go through properties looking for getdown_txt_...
853     def props = project.properties.sort { it.key }
854         if (getdown_alt_java_min_version.length() > 0) {
855                 props.put("getdown_txt_java_min_version", getdown_alt_java_min_version)
856         }
857         if (getdown_alt_java_max_version.length() > 0) {
858                 props.put("getdown_txt_java_max_version", getdown_alt_java_max_version)
859         }
860         props.put("getdown_txt_multi_java_location", getdown_alt_multi_java_location)
861
862     props.put("getdown_txt_appbase", getdown_app_base)
863     props.each{ prop, val ->
864       if (prop.startsWith("getdown_txt_") && val != null) {
865         if (prop.startsWith("getdown_txt_multi_")) {
866           def key = prop.substring(18)
867           val.split(",").each{ v ->
868             def line = key + " = " + v + "\n"
869             getdownTextString += line
870           }
871         } else {
872           // file values rationalised
873           if (val.indexOf('/') > -1 || prop.startsWith("getdown_txt_resource")) {
874             def r = null
875             if (val.indexOf('/') == 0) {
876               // absolute path
877               r = file(val)
878             } else if (val.indexOf('/') > 0) {
879               // relative path (relative to jalviewDir)
880               r = file( jalviewDir + '/' + val )
881             }
882             if (r.exists()) {
883               val = getdown_resource_dir + '/' + r.getName()
884               getdownWebsiteResourceFilenames += val
885               getdownResourceFilenames += r.getPath()
886             }
887           }
888           if (! prop.startsWith("getdown_txt_resource")) {
889             def line = prop.substring(12) + " = " + val + "\n"
890             getdownTextString += line
891           }
892         }
893       }
894     }
895
896     getdownWebsiteResourceFilenames.each{ filename ->
897       getdownTextString += "resource = "+filename+"\n"
898     }
899     getdownResourceFilenames.each{ filename ->
900       copy {
901         from filename
902         into project.ext.getdownResourceDir
903       }
904     }
905
906     def codeFiles = []
907     fileTree(file(packageDir)).each{ f ->
908       if (f.isDirectory()) {
909         def files = fileTree(dir: f, include: ["*"]).getFiles()
910         codeFiles += files
911       } else if (f.exists()) {
912         codeFiles += f
913       }
914     }
915     codeFiles.sort().each{f ->
916       def line = "code = " + getdown_app_dir + '/' + f.getName() + "\n"
917       getdownTextString += line
918       copy {
919         from f.getPath()
920         into project.ext.getdownAppDir
921       }
922     }
923
924     // NOT USING MODULES YET, EVERYTHING SHOULD BE IN dist
925     /*
926     if (JAVA_VERSION.equals("11")) {
927     def j11libFiles = fileTree(dir: "$jalviewDir/$j11libDir", include: ["*.jar"]).getFiles()
928     j11libFiles.sort().each{f ->
929     def line = "code = " + getdown_j11lib_dir + '/' + f.getName() + "\n"
930     getdownTextString += line
931     copy {
932     from f.getPath()
933     into project.ext.getdownJ11libDir
934     }
935     }
936     }
937      */
938
939     // 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.
940     //getdownTextString += "class = " + file(getdownLauncher).getName() + "\n"
941     getdownTextString += "resource = " + getdown_launcher_new + "\n"
942     getdownTextString += "class = " + mainClass + "\n"
943
944     def getdown_txt = file(project.ext.getdownWebsiteDir + "/getdown.txt")
945     getdown_txt.write(getdownTextString)
946
947     def launch_jvl = file(project.ext.getdownWebsiteDir + "/" + getdown_launch_jvl)
948     launch_jvl.write("appbase="+props.get("getdown_txt_appbase"))
949
950     copy {
951       from getdownLauncher
952       rename(file(getdownLauncher).getName(), getdown_launcher_new)
953       into project.ext.getdownWebsiteDir
954     }
955
956     copy {
957       from getdownLauncher
958       if (file(getdownLauncher).getName() != getdown_launcher) {
959         rename(file(getdownLauncher).getName(), getdown_launcher)
960       }
961       into project.ext.getdownWebsiteDir
962     }
963
964     if (! (CHANNEL.startsWith("ARCHIVE") || CHANNEL.startsWith("DEVELOP"))) {
965       copy {
966         from getdown_txt
967         from getdownLauncher
968         from getdownWebsiteDir+"/"+getdown_build_properties
969         if (file(getdownLauncher).getName() != getdown_launcher) {
970           rename(file(getdownLauncher).getName(), getdown_launcher)
971         }
972         into getdownInstallDir
973       }
974
975       copy {
976         from getdownInstallDir
977         into getdownFilesInstallDir
978       }
979     }
980
981     copy {
982       from getdown_txt
983       from launch_jvl
984       from getdownLauncher
985       from getdownWebsiteDir+"/"+getdown_build_properties
986       if (file(getdownLauncher).getName() != getdown_launcher) {
987         rename(file(getdownLauncher).getName(), getdown_launcher)
988       }
989       into getdownFilesDir
990     }
991
992     copy {
993           from getdownResourceDir
994       into project.ext.getdownFilesDir + '/' + getdown_resource_dir
995     }
996   }
997
998   if (buildDist) {
999     inputs.dir(jalviewDir + '/' + packageDir)
1000   }
1001   outputs.dir(project.ext.getdownWebsiteDir)
1002   outputs.dir(project.ext.getdownFilesDir)
1003 }
1004
1005 task getdownDigest(type: JavaExec) {
1006   group = "distribution"
1007   description = "Digest the getdown website folder"
1008   dependsOn getdownWebsite
1009   doFirst {
1010     classpath = files(getdownWebsiteDir + '/' + getdown_launcher)
1011   }
1012   main = "com.threerings.getdown.tools.Digester"
1013   args project.ext.getdownWebsiteDir
1014   inputs.dir(project.ext.getdownWebsiteDir)
1015   outputs.file(project.ext.getdownWebsiteDir + '/' + "digest2.txt")
1016 }
1017
1018 task getdown() {
1019   group = "distribution"
1020   description = "Create the minimal and full getdown app folder for installers and website and create digest file"
1021   dependsOn getdownDigest
1022   doLast {
1023     if (reportRsyncCommand) {
1024       def fromDir = getdownWebsiteDir + (getdownWebsiteDir.endsWith("/")?"":"/")
1025       def toDir = getdown_rsync_dest + "/" + getdownDir + (getdownDir.endsWith("/")?"":"/")
1026       println "LIKELY RSYNC COMMAND:"
1027       println "mkdir -p '$toDir'\nrsync -avh --delete '$fromDir' '$toDir'"
1028       if (RUNRSYNC == "true") {
1029         exec {
1030           commandLine "mkdir", "-p", toDir
1031         }
1032         exec {
1033           commandLine "rsync", "-avh", "--delete", fromDir, toDir
1034         }
1035       }
1036     }
1037   }
1038 }
1039
1040 clean {
1041   doFirst {
1042     delete project.ext.getdownWebsiteDir
1043     delete project.ext.getdownFilesDir
1044   }
1045 }
1046
1047 install4j {
1048   def install4jHomeDir = "/opt/install4j"
1049   def hostname = "hostname".execute().text.trim()
1050   if (hostname.equals("jv-bamboo")) {
1051     install4jHomeDir = System.getProperty("user.home")+"/buildtools/install4j"
1052   } else if (OperatingSystem.current().isMacOsX()) {
1053     install4jHomeDir = '/Applications/install4j.app/Contents/Resources/app'
1054     if (! file(install4jHomeDir).exists()) {
1055       install4jHomeDir = System.getProperty("user.home")+install4jHomeDir
1056     }
1057   } else if (OperatingSystem.current().isLinux()) {
1058     install4jHomeDir = System.getProperty("user.home")+"/buildtools/install4j"
1059   }
1060   installDir = file(install4jHomeDir)
1061   mediaTypes = Arrays.asList(install4jMediaTypes.split(","))
1062   if (install4jFaster.equals("true")) {
1063     faster = true
1064   }
1065 }
1066
1067 def install4jConf
1068 def macosJavaVMDir
1069 def macosJavaVMTgz
1070 def windowsJavaVMDir
1071 def windowsJavaVMTgz
1072 def install4jDir = "$jalviewDir/$install4jResourceDir"
1073 def install4jConfFile = "jalview-installers-java"+JAVA_VERSION+".install4j"
1074 install4jConf = "$install4jDir/$install4jConfFile"
1075
1076 task copyInstall4jTemplate(type: Copy) {
1077   macosJavaVMDir = System.env.HOME+"/buildtools/jre/openjdk-java_vm/getdown/macos-jre"+JAVA_VERSION+"/jre"
1078   macosJavaVMTgz = System.env.HOME+"/buildtools/jre/openjdk-java_vm/install4j/tgz/macos-jre"+JAVA_VERSION+".tar.gz"
1079   windowsJavaVMDir = System.env.HOME+"/buildtools/jre/openjdk-java_vm/getdown/windows-jre"+JAVA_VERSION+"/jre"
1080   windowsJavaVMTgz = System.env.HOME+"/buildtools/jre/openjdk-java_vm/install4j/tgz/windows-jre"+JAVA_VERSION+".tar.gz"
1081   from (install4jDir) {
1082     include install4jTemplate
1083     rename (install4jTemplate, install4jConfFile)
1084     filter(ReplaceTokens, beginToken: '', endToken: '', tokens: ['9999999999': JAVA_VERSION])
1085     filter(ReplaceTokens, beginToken: '$$', endToken: '$$',
1086     tokens: [
1087     'JAVA_VERSION': JAVA_VERSION,
1088     'JAVA_INTEGER_VERSION': JAVA_INTEGER_VERSION,
1089     'VERSION': JALVIEW_VERSION,
1090     'MACOS_JAVA_VM_DIR': macosJavaVMDir,
1091     'MACOS_JAVA_VM_TGZ': macosJavaVMTgz,
1092     'WINDOWS_JAVA_VM_DIR': windowsJavaVMDir,
1093     'WINDOWS_JAVA_VM_TGZ': windowsJavaVMTgz,
1094     'INSTALL4JINFOPLISTFILEASSOCIATIONS': install4jInfoPlistFileAssociations,
1095     'COPYRIGHT_MESSAGE': install4jCopyrightMessage,
1096     'MACOS_BUNDLE_ID': install4jMacOSBundleId,
1097     'GETDOWN_RESOURCE_DIR': getdown_resource_dir,
1098     'GETDOWN_DIST_DIR': getdown_app_dir,
1099     'GETDOWN_ALT_DIR': getdown_app_dir_alt,
1100     'GETDOWN_INSTALL_DIR': getdown_install_dir
1101     ]
1102     )
1103     if (OSX_KEYPASS=="") {
1104       filter(ReplaceTokens, beginToken: 'codeSigning macEnabled="', endToken: '"', tokens: ['true':'codeSigning macEnabled="false"'])
1105       filter(ReplaceTokens, beginToken: 'runPostProcessor="true" ',endToken: 'Processor', tokens: ['post':'runPostProcessor="false" postProcessor'])
1106     }
1107   }
1108   into install4jDir
1109   outputs.files(install4jConf)
1110
1111   doLast {
1112     // include file associations in installer
1113     def installerFileAssociationsXml = file("$install4jDir/$install4jInstallerFileAssociations").text
1114     ant.replaceregexp(
1115       byline: false,
1116       flags: "s",
1117       match: '<action name="EXTENSIONS_REPLACED_BY_GRADLE".*?</action>',
1118       replace: installerFileAssociationsXml,
1119       file: install4jConf
1120     )
1121     /*
1122     // include uninstaller applescript app files in dmg
1123     def installerDMGUninstallerXml = file("$install4jDir/$install4jDMGUninstallerAppFiles").text
1124     ant.replaceregexp(
1125     byline: false,
1126     flags: "s",
1127     match: '<file name="UNINSTALL_OLD_JALVIEW_APP_REPLACED_IN_GRADLE" file=.*?>',
1128     replace: installerDMGUninstallerXml,
1129     file: install4jConf
1130     )
1131      */
1132   }
1133 }
1134
1135 task installers(type: com.install4j.gradle.Install4jTask) {
1136   group = "distribution"
1137   description = "Create the install4j installers"
1138   dependsOn getdown
1139   dependsOn copyInstall4jTemplate
1140   projectFile = file(install4jConf)
1141   println("Using projectFile "+projectFile)
1142   variables = [majorVersion: version.substring(2, 11), build: 001, OSX_KEYSTORE: OSX_KEYSTORE, JSIGN_SH: JSIGN_SH]
1143   destination = "$jalviewDir/$install4jBuildDir/$JAVA_VERSION"
1144   buildSelected = true
1145
1146   if (OSX_KEYPASS) {
1147     macKeystorePassword=OSX_KEYPASS
1148
1149   }
1150
1151   inputs.dir(project.ext.getdownWebsiteDir)
1152   inputs.file(install4jConf)
1153   inputs.dir(macosJavaVMDir)
1154   inputs.dir(windowsJavaVMDir)
1155   outputs.dir("$jalviewDir/$install4jBuildDir/$JAVA_VERSION")
1156
1157 }
1158
1159 clean {
1160   doFirst {
1161     delete install4jConf
1162   }
1163 }
1164
1165 task sourceDist (type: Tar) {
1166   
1167   def VERSION_UNDERSCORES = JALVIEW_VERSION.replaceAll("\\.", "_")
1168   def outputFileName = project.name + "_" + VERSION_UNDERSCORES + ".tar.gz"
1169   // cater for buildship < 3.1 [3.0.1 is max version in eclipse 2018-09]
1170   try {
1171     archiveFileName = outputFileName
1172   } catch (Exception e) {
1173     archiveName = outputFileName
1174   }
1175   
1176   compression Compression.GZIP
1177   
1178   into project.name
1179
1180   def EXCLUDE_FILES=["build/*","bin/*","test-output/","test-reports","tests","clover*/*"
1181   ,".*"
1182   ,"benchmarking/*"
1183   ,"**/.*"
1184   ,"*.class"
1185   ,"**/*.class","$j11modDir/**/*.jar","appletlib","**/*locales"
1186   ,"*locales/**",
1187   ,"utils/InstallAnywhere"] 
1188   def PROCESS_FILES=[   "AUTHORS",
1189   "CITATION",
1190   "FEATURETODO",
1191   "JAVA-11-README",
1192   "FEATURETODO",
1193   "LICENSE",
1194   "**/README",
1195   "RELEASE",
1196   "THIRDPARTYLIBS","TESTNG",
1197   "build.gradle",
1198   "gradle.properties",
1199   "**/*.java",
1200   "**/*.html",
1201   "**/*.xml",
1202   "**/*.gradle",
1203   "**/*.groovy",
1204   "**/*.properties",
1205   "**/*.perl",
1206   "**/*.sh"]
1207
1208   from(jalviewDir) {
1209     exclude (EXCLUDE_FILES)
1210     include (PROCESS_FILES)
1211     filter(ReplaceTokens, beginToken: '$$', endToken: '$$', tokens: ['Version-Rel': JALVIEW_VERSION,'Year-Rel': getDate("yyyy")])
1212   }
1213   from(jalviewDir) {
1214     exclude (EXCLUDE_FILES)
1215     exclude (PROCESS_FILES)
1216   exclude ("appletlib")
1217   exclude ("**/*locales")
1218   exclude ("*locales/**")
1219   exclude ("utils/InstallAnywhere")
1220
1221     exclude (getdown_files_dir)
1222   exclude (getdown_website_dir)
1223
1224   // exluding these as not using jars as modules yet
1225   exclude ("$j11modDir/**/*.jar")
1226 }
1227 //  from (jalviewDir) {
1228 //    // explicit includes for stuff that seemed to not get included
1229 //    include(fileTree("test/**/*."))
1230 //    exclude(EXCLUDE_FILES)
1231 //    exclude(PROCESS_FILES)
1232 //  }
1233 }
1234
1235 task helppages  {
1236   dependsOn copyHelp
1237   dependsOn pubhtmlhelp
1238   
1239   inputs.dir("$classes/$helpDir")
1240   outputs.dir("$helpOutputDir")
1241 }
1242
1243 def jalviewjsBuildDir
1244 def jalviewjsSiteDir
1245 def jalviewjsTransferSiteDir
1246 task jalviewjsSitePath {
1247   def relativeBuildDir = file(jalviewDirAbsolutePath).toPath().relativize(buildDir.toPath())
1248   jalviewjsBuildDir = "${relativeBuildDir}/jalviewjs"
1249   if (jalviewjs_site_dir.startsWith("/")) {
1250     jalviewjsSiteDir = jalviewjs_site_dir
1251   } else {
1252     jalviewjsSiteDir = jalviewjsBuildDir + "/" + jalviewjs_site_dir
1253   }
1254   jalviewjsTransferSiteDir = jalviewjsBuildDir + "/tmp/site"
1255 }
1256
1257 def eclipseWorkspace
1258 task jalviewjsSetEclipseWorkspace {
1259   def propKey = "jalviewjs_eclipse_workspace"
1260   def propsFileName = "${jalviewjsBuildDir}/${jalviewjs_eclipse_workspace_location_file}"
1261   def props = new Properties()
1262   def eclipseWsDir
1263   def propVal = null
1264   if (project.hasProperty(propKey)) {
1265     propVal = project.getProperty(propKey)
1266     eclipseWsDir = propVal
1267   }
1268   if (( eclipseWsDir == null || !file(eclipseWsDir).exists() ) && file(propsFileName).exists()) {
1269     def ins = new FileInputStream("${jalviewDirAbsolutePath}/${propsFileName}")
1270     props.load(ins)
1271     ins.close()
1272     if (props.getProperty(propKey, null) != null) {
1273       eclipseWsDir = props.getProperty(propKey)
1274     }
1275   }
1276
1277   if (eclipseWsDir == null || !file(eclipseWsDir).exists()) {
1278     def tempDir = File.createTempDir()
1279     eclipseWsDir = tempDir.getAbsolutePath()
1280     props.setProperty(propKey, eclipseWsDir)
1281     def propsFile = file(propsFileName)
1282     propsFile.parentFile.mkdirs()
1283     propsFile.createNewFile() // doesn't affect existing file
1284     def outs = new FileOutputStream(propsFile, false)
1285     props.store(outs, null)
1286     outs.close()
1287   }
1288
1289   eclipseWorkspace = file(eclipseWsDir)
1290     
1291   println("ECLIPSE WORKSPACE: "+eclipseWorkspace.getPath())
1292
1293   inputs.property(propKey, propVal)
1294   outputs.file(propsFileName)
1295 }
1296
1297
1298 task jalviewjsUnzipFiles {
1299   dependsOn jalviewjsSitePath
1300
1301   def zipFiles = fileTree(dir: jalviewjs_utils_dir+"/"+jalviewjs_libjs_dir).include("*.zip")
1302   zipFiles += jalviewjs_utils_dir+"/"+jalviewjs_swingjs_zip
1303
1304   doLast {
1305     zipFiles.each { file_zip -> 
1306       copy {
1307         from zipTree(file_zip)
1308         into jalviewjsSiteDir
1309       }
1310     }
1311   }
1312
1313   inputs.files zipFiles
1314   outputs.dir jalviewjsSiteDir
1315 }
1316
1317 def eclipseDropinsDir
1318 def eclipseBinary
1319 def eclipseVersion
1320 def fromDropinsDir
1321 task jalviewjsEclipsePaths {
1322   def eclipseRoot
1323   def eclipseProduct
1324   eclipseRoot = jalviewjs_eclipse_root
1325   if (eclipseRoot.startsWith("~")) {
1326     eclipseRoot = System.getProperty("user.home") + eclipseRoot.substring(1)
1327   }
1328   if (OperatingSystem.current().isMacOsX()) {
1329     eclipseRoot += "/Eclipse.app"
1330     eclipseDropinsDir = eclipseRoot+"/Contents/Eclipse/dropins"
1331     eclipseBinary = eclipseRoot+"/Contents/MacOS/eclipse"
1332     eclipseProduct = eclipseRoot+"/Contents/Eclipse/.eclipseproduct"
1333   } else if (OperatingSystem.current().isWindows()) { // check these paths!!
1334     if (file(eclipseRoot+"/eclipse").isDirectory() && file(eclipseRoot+"/eclipse/.eclipseproduct").exists()) {
1335       eclipseRoot += "/eclipse"
1336     }
1337     eclipseDropinsDir = eclipseRoot+"/dropins"
1338     eclipseBinary = eclipseRoot+"/eclipse"
1339     eclipseProduct = eclipseRoot+"/.eclipseproduct"
1340   } else { // linux or unix
1341     if (file(eclipseRoot+"/eclipse").isDirectory() && file(eclipseRoot+"/eclipse/.eclipseproduct").exists()) {
1342       eclipseRoot += "/eclipse"
1343     }
1344     eclipseDropinsDir = eclipseRoot+"/dropins"
1345     eclipseBinary = eclipseRoot+"/eclipse"
1346     eclipseProduct = eclipseRoot+"/.eclipseproduct"
1347   }
1348
1349   def fis = new FileInputStream(eclipseProduct)
1350   def props = new Properties()
1351   props.load(fis)
1352   eclipseVersion = props.getProperty("version")
1353   fis.close()
1354   println("ECLIPSE_VERSION=${eclipseVersion}")
1355   String[] v = eclipseVersion.split("\\.")
1356   def v0 = Integer.valueOf(v[0])
1357   def v1 = Integer.valueOf(v[1])
1358   if (v0 < 4 || ( v0 == 4 && v1 < 13 )) {
1359     fromDropinsDir = "eclipse/dropins_4.12"
1360   } else {
1361     fromDropinsDir = "eclipse/dropins_4.13"
1362   } 
1363 }
1364
1365 task OLDjalviewjsEclipseCopyDropins {
1366   dependsOn jalviewjsEclipsePaths
1367   def inputFiles = fileTree(dir: jalviewjs_utils_dir+"/"+fromDropinsDir, include: "*.jar")
1368   def outputDir = eclipseDropinsDir
1369
1370   inputs.files inputFiles
1371   inputFiles.each { file ->
1372     outputs.file(outputDir+"/"+file.name)
1373   }
1374
1375   doLast {
1376     def outputFiles = []
1377     inputFiles.each { file ->
1378       copy {
1379         from file
1380         into outputDir
1381       }
1382     }
1383   }
1384 }
1385
1386 // this version (type: Copy) will delete anything in the eclipse dropins folder that isn't in fromDropinsDir
1387 task jalviewjsEclipseCopyDropins(type: Copy) {
1388   from fromDropinsDir
1389   into eclipseDropinsDir
1390 }
1391
1392 task jalviewjsCreateJ2sSettings(type: WriteProperties) {
1393   dependsOn jalviewjsSitePath
1394   outputFile (jalviewDir+"/"+jalviewjs_j2s_settings)
1395   def props = project.properties.sort { it.key }
1396   def siteDirProperty = "j2s.site.directory"
1397   def setSiteDir = false
1398   props.each { prop, val ->
1399     if (prop.startsWith("j2s.") && val != null) {
1400       if (prop == siteDirProperty) {
1401         if (!(val.startsWith("/") || val.startsWith("file://") )) {
1402           val = jalviewjsTransferSiteDir+"/"+val
1403         }
1404         setSiteDir = true
1405       }
1406       property(prop,val)
1407     }
1408     if (!setSiteDir) {
1409       property(siteDirProperty,jalviewjsTransferSiteDir)
1410     }
1411   }
1412   inputs.properties(props.findAll( { k, v -> k.startsWith("j2s.") } ))
1413   outputs.file(outputFile)
1414 }
1415
1416 task jalviewjsEclipseSetup {
1417   dependsOn jalviewjsEclipseCopyDropins
1418   dependsOn jalviewjsSetEclipseWorkspace
1419   dependsOn jalviewjsCreateJ2sSettings
1420 }
1421
1422 task jalviewjsCopyResources (type: Copy) {
1423   dependsOn jalviewjsSitePath
1424   def inputFiles = fileTree(dir: jalviewjs_resource_dir)
1425   def outputDir = jalviewjsSiteDir+"/"+jalviewjs_j2s_subdir
1426
1427   from inputFiles
1428   into outputDir
1429   def outputFiles = []
1430   rename { filename ->
1431     outputFiles += outputDir+"/"+filename
1432     null
1433   }
1434   outputs.files outputFiles
1435   inputs.files inputFiles
1436 }
1437
1438 task jalviewjsCopySiteResources (type: Copy) {
1439   dependsOn jalviewjsSitePath
1440   def inputFiles = fileTree(dir: jalviewjs_utils_dir+"/"+jalviewjs_site_resource_dir)
1441   def outputDir = jalviewjsSiteDir
1442
1443   from inputFiles
1444   into outputDir
1445   def outputFiles = []
1446   rename { filename ->
1447     outputFiles += outputDir+"/"+filename
1448     null
1449   }
1450   outputs.files outputFiles
1451   inputs.files inputFiles
1452 }
1453
1454 task cleanJalviewjsAll {
1455   group "JalviewJS"
1456   description "Delete all configuration and build artifacts to do with JalviewJS build"
1457   dependsOn cleanJalviewjsSite
1458   
1459   doFirst {
1460     delete jalviewjsBuildDir
1461     delete jalviewDir+"/"+eclipse_bin_dir
1462     delete file(eclipseWorkspace.getAbsolutePath()+"/.metadata")
1463     delete jalviewDir+"/"+jalviewjs_j2s_settings
1464   }
1465 }
1466
1467 task jalviewjsProjectImport(type: Exec) {
1468   // work out how to do this!
1469   dependsOn eclipseProject
1470   dependsOn eclipseClasspath
1471   dependsOn eclipseJdt
1472   dependsOn jalviewjsEclipsePaths
1473   dependsOn jalviewjsEclipseSetup
1474   executable(eclipseBinary)
1475   args(["-nosplash", "--launcher.suppressErrors", "-application", "com.seeq.eclipse.importprojects.headlessimport", "-data", eclipseWorkspace.getPath(), "-import", jalviewDirAbsolutePath])
1476
1477   def projdir = eclipseWorkspace.getPath()+"/.metadata/.plugins/org.eclipse.core.resources/.projects/jalview/org.eclipse.jdt.core"
1478   inputs.file(jalviewDir+"/.project")
1479   outputs.dir(projdir)
1480   outputs.upToDateWhen { file(projdir).exists() }
1481 }
1482
1483 task jalviewjsTranspile(type: Exec) {
1484   dependsOn jalviewjsEclipseSetup 
1485   dependsOn jalviewjsProjectImport
1486   dependsOn jalviewjsEclipsePaths
1487   executable(eclipseBinary)
1488   args(["-nosplash", "--launcher.suppressErrors", "-application", "org.eclipse.jdt.apt.core.aptBuild", "-data", eclipseWorkspace, "-"+jalviewjs_eclipseBuildArg, eclipse_project_name ])
1489
1490   def stdout
1491   def stderr
1492   doFirst {
1493     stdout = new ByteArrayOutputStream()
1494     stderr = new ByteArrayOutputStream()
1495
1496     def logOutFileName = "${jalviewDirAbsolutePath}/${jalviewjsBuildDir}/${jalviewjs_j2s_stdout}"
1497     def logOutFile = file(logOutFileName)
1498     logOutFile.createNewFile()
1499     def logOutFOS = new FileOutputStream(logOutFile, false)
1500     //def logErrFileName = "${jalviewjsBuildDir}/${jalviewjs_j2s_stderr}"
1501     //def logErrFile = file(logFileName)
1502     //logErrFile.createNewFile()
1503     //def logErrFOS = new FileErrputStream(logErrFile, false)
1504     // combine stdout and stderr
1505     def logErrFOS = logOutFOS
1506     if (jalviewjs_j2s_to_console.equals("true")) {
1507       standardOutput = new org.apache.tools.ant.util.TeeOutputStream(
1508         new org.apache.tools.ant.util.TeeOutputStream(
1509           logOutFOS,
1510           stdout),
1511         standardOutput)
1512       errorOutput = new org.apache.tools.ant.util.TeeOutputStream(
1513         new org.apache.tools.ant.util.TeeOutputStream(
1514           logErrFOS,
1515           stderr),
1516         errorOutput)
1517     } else {
1518       standardOutput = new org.apache.tools.ant.util.TeeOutputStream(
1519         logOutFOS,
1520         stdout)
1521       errorOutput = new org.apache.tools.ant.util.TeeOutputStream(
1522         logErrFOS,
1523         stderr)
1524     }
1525   }
1526   doLast {
1527     if (stdout.toString().contains("Error processing ")) {
1528       // j2s did not complete transpile
1529       throw new GradleException("Error during transpilation:\n${stderr}\nSee eclipse transpile log file '${jalviewjsBuildDir}/${jalviewjs_j2s_stdout}'")
1530     }
1531   }
1532
1533   inputs.dir(sourceDir)
1534   outputs.dir(eclipse_bin_dir+"/main")
1535   outputs.dir(jalviewjsTransferSiteDir)
1536   outputs.upToDateWhen { file("${jalviewjsTransferSiteDir}${jalviewjs_server_resource}").exists() }
1537
1538 }
1539
1540 task jalviewjsCopyTransferSite(type: Copy) {
1541   dependsOn jalviewjsTranspile
1542   from jalviewjsTransferSiteDir
1543   into jalviewjsSiteDir
1544 }
1545
1546 jalviewjsUnzipFiles.mustRunAfter jalviewjsCopyTransferSite
1547 jalviewjsCopyResources.mustRunAfter jalviewjsCopyTransferSite
1548 jalviewjsCopySiteResources.mustRunAfter jalviewjsCopyTransferSite
1549
1550 task jalviewjsPrepareSite {
1551   group "JalviewJS"
1552   description "Prepares the website folder including unzipping files and copying resources"
1553   dependsOn jalviewjsSitePath
1554   dependsOn jalviewjsUnzipFiles
1555   dependsOn jalviewjsCopyResources
1556   dependsOn jalviewjsCopySiteResources
1557 }
1558
1559 task jalviewjsBuildSite {
1560   group "JalviewJS"
1561   description "Builds the whole website including transpiled code"
1562   dependsOn jalviewjsCopyTransferSite
1563   dependsOn jalviewjsPrepareSite
1564 }
1565
1566 task cleanJalviewjsSite {
1567   doFirst {
1568     delete jalviewjsTransferSiteDir
1569     delete jalviewjsSiteDir
1570   }
1571 }
1572
1573 task jalviewjsSiteTar(type: Tar) {
1574   group "JalviewJS"
1575   description "Creates a tar.gz file for the website"
1576   dependsOn jalviewjsBuildSite
1577   def outputFilename = "jalviewjs-site-${JALVIEW_VERSION}.tar.gz"
1578   try {
1579     archiveFileName = outputFilename
1580   } catch (Exception e) {
1581     archiveName = outputFilename
1582   }
1583
1584   compression Compression.GZIP
1585
1586   from jalviewjsSiteDir
1587   into jalviewjs_site_dir // this is inside the tar file
1588
1589   inputs.dir(jalviewjsSiteDir)
1590 }
1591
1592 task jalviewjsServer {
1593   group "JalviewJS"
1594   description "Starts a webserver on localhost to test the website"
1595   dependsOn jalviewjsSitePath
1596   def htmlFile = "${jalviewDirAbsolutePath}/jalviewjsTest.html"
1597   doLast {
1598
1599     SimpleHttpFileServerFactory factory = new SimpleHttpFileServerFactory()
1600     def port = Integer.valueOf(jalviewjs_server_port)
1601     def start = port
1602     def running = false
1603     def url
1604     while(port < start+1000 && !running) {
1605       try {
1606         def doc_root = new File(jalviewDirAbsolutePath +"/"+ jalviewjsSiteDir)
1607         def jalviewjsServer = factory.start(doc_root, port)
1608         running = true
1609         url = jalviewjsServer.getResourceUrl(jalviewjs_server_resource)
1610         println("SERVER STARTED with document root ${doc_root}.")
1611         println("Go to "+url+" . Run  gradle --stop  to stop (kills all gradle daemons).")
1612         println("For debug: "+url+"?j2sdebug")
1613
1614         file(htmlFile).text = """
1615         <p><a href="${url}">Jalview JS Test. &lt;${url}&gt;</a></p>
1616         <p><a href="${url}?j2sdebug">Jalview JS Test with debug. &lt;${url}?j2sdebug&lt;</a></p>
1617         """
1618
1619       } catch (Exception e) {
1620         port++;
1621       }
1622     }
1623
1624   }
1625
1626   outputs.file(htmlFile)
1627 }
1628
1629 task jalviewjs {
1630   group "JalviewJS"
1631   description "Build the site"
1632   dependsOn jalviewjsBuildSite
1633 }
1634
1635
1636 task jalviewjsIDECopyTransferSite(type: Copy) {
1637   from jalviewjsTransferSiteDir
1638   into jalviewjsSiteDir
1639 }
1640
1641 task jalviewjsIDEj2s {
1642   group "JalviewJS in Eclipse"
1643   description "Creates the .j2s file"
1644   dependsOn jalviewjsCreateJ2sSettings
1645 }
1646
1647 task jalviewjsIDEBuildSite {
1648   group "JalviewJS in Eclipse"
1649   description "Copies the Eclipse transpiled site and unzips supporting zipfiles"
1650   dependsOn jalviewjsIDECopyTransferSite
1651   dependsOn jalviewjsPrepareSite
1652 }
1653
1654 task jalviewjsIDESiteClean {
1655   group "JalviewJS in Eclipse"
1656   description "Deletes the Eclipse transpiled site"
1657   dependsOn cleanJalviewjsSite
1658 }
1659
1660 task jalviewjsIDEServer {
1661   group "JalviewJS in Eclipse"
1662   description "Starts a webserver on localhost to test the website"
1663   dependsOn jalviewjsServer
1664 }
1665