d259e13ad723aae87e1f9ec1362fdd6b236db286
[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   getdownAppDir = "${getdownWebsiteDir}/${getdown_app_dir}"
157   //getdownJ11libDir = "${getdownWebsiteDir}/${getdown_j11lib_dir}"
158   getdownResourceDir = "${getdownWebsiteDir}/${getdown_resource_dir}"
159   getdownInstallDir = "${getdownWebsiteDir}/${getdown_install_dir}"
160   getdownFilesDir = "${jalviewDir}/${getdown_files_dir}/${JAVA_VERSION}/"
161   getdownFilesInstallDir = "${getdownFilesDir}/${getdown_install_dir}"
162   /* compile without modules -- using classpath libraries
163   modules_compileClasspath = fileTree(dir: "${jalviewDir}/${j11modDir}", include: ["*.jar"])
164   modules_runtimeClasspath = modules_compileClasspath
165   */
166   gitHash = ""
167   gitBranch = ""
168
169   println("Using a ${CHANNEL} profile. appbase=${getdown_app_base}")
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 == sourceDir || entry.path == testSourceDir)) {
336               removeTheseToo += entry
337             } else {
338               alreadyAddedSrcPath.putAt(entry.path, true)
339             }
340           }
341         }
342         cp.entries.removeAll(removeTheseToo)
343
344         if (file("${jalviewDir}/${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
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(dir: "${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(dir: "${classes}", 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(dir: "${jalviewDir}/${libDistDir}", 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 name = f.getName()
917       def line = "code = ${getdown_app_dir}/${name}\n"
918       getdownTextString += line
919       copy {
920         from f.getPath()
921         into project.ext.getdownAppDir
922       }
923     }
924
925     // NOT USING MODULES YET, EVERYTHING SHOULD BE IN dist
926     /*
927     if (JAVA_VERSION.equals("11")) {
928     def j11libFiles = fileTree(dir: "${jalviewDir}/${j11libDir}", include: ["*.jar"]).getFiles()
929     j11libFiles.sort().each{f ->
930     def name = f.getName()
931     def line = "code = ${getdown_j11lib_dir}/${name}\n"
932     getdownTextString += line
933     copy {
934     from f.getPath()
935     into project.ext.getdownJ11libDir
936     }
937     }
938     }
939      */
940
941     // 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.
942     //getdownTextString += "class = " + file(getdownLauncher).getName() + "\n"
943     getdownTextString += "resource = ${getdown_launcher_new}\n"
944     getdownTextString += "class = ${mainClass}\n"
945
946     def getdown_txt = file("${project.ext.getdownWebsiteDir}/getdown.txt")
947     getdown_txt.write(getdownTextString)
948
949     def launch_jvl = file("${project.ext.getdownWebsiteDir}/${getdown_launch_jvl}")
950     launch_jvl.write("appbase="+props.get("getdown_txt_appbase"))
951
952     copy {
953       from getdownLauncher
954       rename(file(getdownLauncher).getName(), getdown_launcher_new)
955       into project.ext.getdownWebsiteDir
956     }
957
958     copy {
959       from getdownLauncher
960       if (file(getdownLauncher).getName() != getdown_launcher) {
961         rename(file(getdownLauncher).getName(), getdown_launcher)
962       }
963       into project.ext.getdownWebsiteDir
964     }
965
966     if (! (CHANNEL.startsWith("ARCHIVE") || CHANNEL.startsWith("DEVELOP"))) {
967       copy {
968         from getdown_txt
969         from getdownLauncher
970         from "${getdownWebsiteDir}/${getdown_build_properties}"
971         if (file(getdownLauncher).getName() != getdown_launcher) {
972           rename(file(getdownLauncher).getName(), getdown_launcher)
973         }
974         into getdownInstallDir
975       }
976
977       copy {
978         from getdownInstallDir
979         into getdownFilesInstallDir
980       }
981     }
982
983     copy {
984       from getdown_txt
985       from launch_jvl
986       from getdownLauncher
987       from "${getdownWebsiteDir}/${getdown_build_properties}"
988       if (file(getdownLauncher).getName() != getdown_launcher) {
989         rename(file(getdownLauncher).getName(), getdown_launcher)
990       }
991       into getdownFilesDir
992     }
993
994     copy {
995       from getdownResourceDir
996       into "${project.ext.getdownFilesDir}/${getdown_resource_dir}"
997     }
998   }
999
1000   if (buildDist) {
1001     inputs.dir("${jalviewDir}/${packageDir}")
1002   }
1003   outputs.dir(project.ext.getdownWebsiteDir)
1004   outputs.dir(project.ext.getdownFilesDir)
1005 }
1006
1007 task getdownDigest(type: JavaExec) {
1008   group = "distribution"
1009   description = "Digest the getdown website folder"
1010   dependsOn getdownWebsite
1011   doFirst {
1012     classpath = files("${getdownWebsiteDir}/${getdown_launcher}")
1013   }
1014   main = "com.threerings.getdown.tools.Digester"
1015   args project.ext.getdownWebsiteDir
1016   inputs.dir(project.ext.getdownWebsiteDir)
1017   outputs.file("${project.ext.getdownWebsiteDir}/digest2.txt")
1018 }
1019
1020 task getdown() {
1021   group = "distribution"
1022   description = "Create the minimal and full getdown app folder for installers and website and create digest file"
1023   dependsOn getdownDigest
1024   doLast {
1025     if (reportRsyncCommand) {
1026       def fromDir = getdownWebsiteDir + (getdownWebsiteDir.endsWith('/')?'':'/')
1027       def toDir = "${getdown_rsync_dest}/${getdownDir}" + (getdownDir.endsWith('/')?'':'/')
1028       println "LIKELY RSYNC COMMAND:"
1029       println "mkdir -p '$toDir'\nrsync -avh --delete '$fromDir' '$toDir'"
1030       if (RUNRSYNC == "true") {
1031         exec {
1032           commandLine "mkdir", "-p", toDir
1033         }
1034         exec {
1035           commandLine "rsync", "-avh", "--delete", fromDir, toDir
1036         }
1037       }
1038     }
1039   }
1040 }
1041
1042 clean {
1043   doFirst {
1044     delete project.ext.getdownWebsiteDir
1045     delete project.ext.getdownFilesDir
1046   }
1047 }
1048
1049 install4j {
1050   def install4jHomeDir = "/opt/install4j"
1051   def hostname = "hostname".execute().text.trim()
1052   if (hostname.equals("jv-bamboo")) {
1053     install4jHomeDir = System.getProperty("user.home")+"/buildtools/install4j"
1054   } else if (OperatingSystem.current().isMacOsX()) {
1055     install4jHomeDir = '/Applications/install4j.app/Contents/Resources/app'
1056     if (! file(install4jHomeDir).exists()) {
1057       install4jHomeDir = System.getProperty("user.home")+install4jHomeDir
1058     }
1059   } else if (OperatingSystem.current().isLinux()) {
1060     install4jHomeDir = System.getProperty("user.home")+"/buildtools/install4j"
1061   }
1062   installDir = file(install4jHomeDir)
1063   mediaTypes = Arrays.asList(install4jMediaTypes.split(","))
1064   if (install4jFaster.equals("true")) {
1065     faster = true
1066   }
1067 }
1068
1069 def install4jConf
1070 def macosJavaVMDir
1071 def macosJavaVMTgz
1072 def windowsJavaVMDir
1073 def windowsJavaVMTgz
1074 def install4jDir = "${jalviewDir}/${install4jResourceDir}"
1075 def install4jConfFile = "jalview-installers-java${JAVA_VERSION}.install4j"
1076 install4jConf = "${install4jDir}/${install4jConfFile}"
1077
1078 task copyInstall4jTemplate(type: Copy) {
1079   macosJavaVMDir = "${System.env.HOME}/buildtools/jre/openjdk-java_vm/getdown/macos-jre${JAVA_VERSION}/jre"
1080   macosJavaVMTgz = "${System.env.HOME}/buildtools/jre/openjdk-java_vm/install4j/tgz/macos-jre${JAVA_VERSION}.tar.gz"
1081   windowsJavaVMDir = "${System.env.HOME}/buildtools/jre/openjdk-java_vm/getdown/windows-jre${JAVA_VERSION}/jre"
1082   windowsJavaVMTgz = "${System.env.HOME}/buildtools/jre/openjdk-java_vm/install4j/tgz/windows-jre${JAVA_VERSION}.tar.gz"
1083   from (install4jDir) {
1084     include install4jTemplate
1085     rename (install4jTemplate, install4jConfFile)
1086     filter(ReplaceTokens, beginToken: '', endToken: '', tokens: ['9999999999': JAVA_VERSION])
1087     filter(ReplaceTokens, beginToken: '$$', endToken: '$$',
1088     tokens: [
1089     'JAVA_VERSION': JAVA_VERSION,
1090     'JAVA_INTEGER_VERSION': JAVA_INTEGER_VERSION,
1091     'VERSION': JALVIEW_VERSION,
1092     'MACOS_JAVA_VM_DIR': macosJavaVMDir,
1093     'MACOS_JAVA_VM_TGZ': macosJavaVMTgz,
1094     'WINDOWS_JAVA_VM_DIR': windowsJavaVMDir,
1095     'WINDOWS_JAVA_VM_TGZ': windowsJavaVMTgz,
1096     'INSTALL4JINFOPLISTFILEASSOCIATIONS': install4jInfoPlistFileAssociations,
1097     'COPYRIGHT_MESSAGE': install4jCopyrightMessage,
1098     'MACOS_BUNDLE_ID': install4jMacOSBundleId,
1099     'GETDOWN_RESOURCE_DIR': getdown_resource_dir,
1100     'GETDOWN_DIST_DIR': getdown_app_dir,
1101     'GETDOWN_ALT_DIR': getdown_app_dir_alt,
1102     'GETDOWN_INSTALL_DIR': getdown_install_dir
1103     ]
1104     )
1105     if (OSX_KEYPASS=="") {
1106       filter(ReplaceTokens, beginToken: 'codeSigning macEnabled="', endToken: '"', tokens: ['true':'codeSigning macEnabled="false"'])
1107       filter(ReplaceTokens, beginToken: 'runPostProcessor="true" ',endToken: 'Processor', tokens: ['post':'runPostProcessor="false" postProcessor'])
1108     }
1109   }
1110   into install4jDir
1111   outputs.files(install4jConf)
1112
1113   doLast {
1114     // include file associations in installer
1115     def installerFileAssociationsXml = file("${install4jDir}/${install4jInstallerFileAssociations}").text
1116     ant.replaceregexp(
1117       byline: false,
1118       flags: "s",
1119       match: '<action name="EXTENSIONS_REPLACED_BY_GRADLE".*?</action>',
1120       replace: installerFileAssociationsXml,
1121       file: install4jConf
1122     )
1123     /*
1124     // include uninstaller applescript app files in dmg
1125     def installerDMGUninstallerXml = file("$install4jDir/$install4jDMGUninstallerAppFiles").text
1126     ant.replaceregexp(
1127     byline: false,
1128     flags: "s",
1129     match: '<file name="UNINSTALL_OLD_JALVIEW_APP_REPLACED_IN_GRADLE" file=.*?>',
1130     replace: installerDMGUninstallerXml,
1131     file: install4jConf
1132     )
1133      */
1134   }
1135 }
1136
1137 task installers(type: com.install4j.gradle.Install4jTask) {
1138   group = "distribution"
1139   description = "Create the install4j installers"
1140   dependsOn getdown
1141   dependsOn copyInstall4jTemplate
1142   projectFile = file(install4jConf)
1143   variables = [majorVersion: version.substring(2, 11), build: 001, OSX_KEYSTORE: OSX_KEYSTORE, JSIGN_SH: JSIGN_SH]
1144   destination = "${jalviewDir}/${install4jBuildDir}/${JAVA_VERSION}"
1145   buildSelected = true
1146
1147   if (OSX_KEYPASS) {
1148     macKeystorePassword=OSX_KEYPASS
1149   }
1150
1151   doFirst {
1152     println("Using projectFile "+projectFile)
1153   }
1154
1155   inputs.dir(project.ext.getdownWebsiteDir)
1156   inputs.file(install4jConf)
1157   inputs.dir(macosJavaVMDir)
1158   inputs.dir(windowsJavaVMDir)
1159   outputs.dir("${jalviewDir}/${install4jBuildDir}/${JAVA_VERSION}")
1160 }
1161
1162 clean {
1163   doFirst {
1164     delete install4jConf
1165   }
1166 }
1167
1168 task sourceDist (type: Tar) {
1169   
1170   def VERSION_UNDERSCORES = JALVIEW_VERSION.replaceAll("\\.", "_")
1171   def outputFileName = "${project.name}_${VERSION_UNDERSCORES}.tar.gz"
1172   // cater for buildship < 3.1 [3.0.1 is max version in eclipse 2018-09]
1173   try {
1174     archiveFileName = outputFileName
1175   } catch (Exception e) {
1176     archiveName = outputFileName
1177   }
1178   
1179   compression Compression.GZIP
1180   
1181   into project.name
1182
1183   def EXCLUDE_FILES=["build/*","bin/*","test-output/","test-reports","tests","clover*/*"
1184   ,".*"
1185   ,"benchmarking/*"
1186   ,"**/.*"
1187   ,"*.class"
1188   ,"**/*.class","${j11modDir}/**/*.jar","appletlib","**/*locales"
1189   ,"*locales/**",
1190   ,"utils/InstallAnywhere"] 
1191   def PROCESS_FILES=[   "AUTHORS",
1192   "CITATION",
1193   "FEATURETODO",
1194   "JAVA-11-README",
1195   "FEATURETODO",
1196   "LICENSE",
1197   "**/README",
1198   "RELEASE",
1199   "THIRDPARTYLIBS","TESTNG",
1200   "build.gradle",
1201   "gradle.properties",
1202   "**/*.java",
1203   "**/*.html",
1204   "**/*.xml",
1205   "**/*.gradle",
1206   "**/*.groovy",
1207   "**/*.properties",
1208   "**/*.perl",
1209   "**/*.sh"]
1210
1211   from(jalviewDir) {
1212     exclude (EXCLUDE_FILES)
1213     include (PROCESS_FILES)
1214     filter(ReplaceTokens, beginToken: '$$', endToken: '$$', tokens: ['Version-Rel': JALVIEW_VERSION,'Year-Rel': getDate("yyyy")])
1215   }
1216   from(jalviewDir) {
1217     exclude (EXCLUDE_FILES)
1218     exclude (PROCESS_FILES)
1219   exclude ("appletlib")
1220   exclude ("**/*locales")
1221   exclude ("*locales/**")
1222   exclude ("utils/InstallAnywhere")
1223
1224     exclude (getdown_files_dir)
1225   exclude (getdown_website_dir)
1226
1227   // exluding these as not using jars as modules yet
1228   exclude ("${j11modDir}/**/*.jar")
1229 }
1230 //  from (jalviewDir) {
1231 //    // explicit includes for stuff that seemed to not get included
1232 //    include(fileTree("test/**/*."))
1233 //    exclude(EXCLUDE_FILES)
1234 //    exclude(PROCESS_FILES)
1235 //  }
1236 }
1237
1238 task helppages  {
1239   dependsOn copyHelp
1240   dependsOn pubhtmlhelp
1241   
1242   inputs.dir("${classes}/${helpDir}")
1243   outputs.dir("${helpOutputDir}")
1244 }
1245
1246 def jalviewjsBuildDir
1247 def jalviewjsSiteDir
1248 def jalviewjsTransferSiteDir
1249 task jalviewjsSitePath {
1250   def relativeBuildDir = file(jalviewDirAbsolutePath).toPath().relativize(buildDir.toPath())
1251   jalviewjsBuildDir = "${relativeBuildDir}/jalviewjs"
1252   if (jalviewjs_site_dir.startsWith('/')) {
1253     jalviewjsSiteDir = jalviewjs_site_dir
1254   } else {
1255     jalviewjsSiteDir = "${jalviewjsBuildDir}/${jalviewjs_site_dir}"
1256   }
1257   jalviewjsTransferSiteDir = "${jalviewjsBuildDir}/tmp/site"
1258 }
1259
1260 def eclipseWorkspace
1261 task jalviewjsSetEclipseWorkspace {
1262   def propKey = "jalviewjs_eclipse_workspace"
1263   def propVal = null
1264   if (project.hasProperty(propKey)) {
1265     propVal = project.getProperty(propKey)
1266   }
1267   def propsFileName = "${jalviewjsBuildDir}/${jalviewjs_eclipse_workspace_location_file}"
1268   def eclipseWsDir = propVal
1269   def props = new Properties()
1270
1271   if (( eclipseWsDir == null || !file(eclipseWsDir).exists() ) && file(propsFileName).exists()) {
1272     def ins = new FileInputStream("${jalviewDirAbsolutePath}/${propsFileName}")
1273     props.load(ins)
1274     ins.close()
1275     if (props.getProperty(propKey, null) != null) {
1276       eclipseWsDir = props.getProperty(propKey)
1277     }
1278   }
1279
1280   if (eclipseWsDir == null || !file(eclipseWsDir).exists()) {
1281     def tempDir = File.createTempDir()
1282     eclipseWsDir = tempDir.getAbsolutePath()
1283     props.setProperty(propKey, eclipseWsDir)
1284     def propsFile = file(propsFileName)
1285     propsFile.parentFile.mkdirs()
1286     propsFile.createNewFile() // doesn't affect existing file
1287     def outs = new FileOutputStream(propsFile, false)
1288     props.store(outs, null)
1289     outs.close()
1290   }
1291
1292   eclipseWorkspace = file(eclipseWsDir)
1293
1294   doFirst {
1295     println("ECLIPSE WORKSPACE: "+eclipseWorkspace.getPath())
1296   }
1297   inputs.property(propKey, eclipseWsDir)
1298   outputs.file(propsFileName)
1299 }
1300
1301
1302 def eclipseDropinsDir
1303 def utilsDropinsDir
1304 def eclipseBinary
1305 def eclipseVersion
1306 def eclipseDebug = false
1307 def eclipseVersionSuffix = ""
1308 task jalviewjsEclipsePaths {
1309   def eclipseRoot
1310   def eclipseProduct
1311
1312   eclipseRoot = jalviewjs_eclipse_root
1313   if (eclipseRoot.startsWith("~")) {
1314     eclipseRoot = System.getProperty("user.home") + eclipseRoot.substring(1)
1315   }
1316   if (OperatingSystem.current().isMacOsX()) {
1317     eclipseRoot += "/Eclipse.app"
1318     eclipseDropinsDir = "${eclipseRoot}/Contents/Eclipse/dropins"
1319     eclipseBinary = "${eclipseRoot}/Contents/MacOS/eclipse"
1320     eclipseProduct = "${eclipseRoot}/Contents/Eclipse/.eclipseproduct"
1321   } else if (OperatingSystem.current().isWindows()) { // check these paths!!
1322     if (file("${eclipseRoot}/eclipse").isDirectory() && file("${eclipseRoot}/eclipse/.eclipseproduct").exists()) {
1323       eclipseRoot += "/eclipse"
1324     }
1325     eclipseDropinsDir = "${eclipseRoot}/dropins"
1326     eclipseBinary = "${eclipseRoot}/eclipse"
1327     eclipseProduct = "${eclipseRoot}/.eclipseproduct"
1328   } else { // linux or unix
1329     if (file("${eclipseRoot}/eclipse").isDirectory() && file("${eclipseRoot}/eclipse/.eclipseproduct").exists()) {
1330       eclipseRoot += "/eclipse"
1331     }
1332     eclipseDropinsDir = "${eclipseRoot}/dropins"
1333     eclipseBinary = "${eclipseRoot}/eclipse"
1334     eclipseProduct = "${eclipseRoot}/.eclipseproduct"
1335   }
1336
1337   eclipseVersion = "4.12" // default
1338   def assumedVersion = true
1339   if (file(eclipseProduct).exists()) {
1340     def fis = new FileInputStream(eclipseProduct)
1341     def props = new Properties()
1342     props.load(fis)
1343     eclipseVersion = props.getProperty("version")
1344     fis.close()
1345     assumedVersion = false
1346   }
1347   String[] v = eclipseVersion.split("\\.")
1348   def v0 = Integer.valueOf(v[0])
1349   def v1 = Integer.valueOf(v[1])
1350   if (v0 < 4 || ( v0 == 4 && v1 < 13 )) {
1351     eclipseVersionSuffix = "_4.12"
1352   } else {
1353     eclipseVersionSuffix = "_4.13"
1354   }
1355
1356   utilsDropinsDir = "${jalviewjs_utils_dir}/${jalviewjs_eclipse_dropins_dir}${eclipseVersionSuffix}"
1357   def propKey = "eclipse_debug"
1358   eclipseDebug = (project.hasProperty(propKey) && project.getProperty(propKey).equals("true"))
1359
1360   doFirst {
1361     if (!assumedVersion) {
1362       println("ECLIPSE_VERSION=${eclipseVersion}")
1363     }
1364   }
1365 }
1366
1367 task jalviewjsEclipseCopyDropins {
1368   dependsOn jalviewjsEclipsePaths
1369   def inputFiles = fileTree(dir: utilsDropinsDir, include: "*.jar")
1370   def outputDir = eclipseDropinsDir
1371
1372   inputs.files inputFiles
1373   inputFiles.each { file ->
1374     outputs.file("${outputDir}/${file.name}")
1375   }
1376
1377   doLast {
1378     inputFiles.each { file ->
1379       copy {
1380         from file
1381         into outputDir
1382       }
1383     }
1384   }
1385 }
1386
1387 // this version (type: Copy) will delete anything in the eclipse dropins folder that isn't in fromDropinsDir
1388 task NEWjalviewjsEclipseCopyDropins(type: Copy) {
1389   dependsOn jalviewjsEclipsePaths
1390   from utilsDropinsDir
1391   into eclipseDropinsDir
1392 }
1393
1394 task jalviewjsUnzipFiles {
1395   dependsOn jalviewjsSitePath
1396
1397   def zipFiles = fileTree(dir: "${jalviewjs_utils_dir}/${jalviewjs_libjs_dir}", include: "*.zip")
1398   zipFiles += "${jalviewjs_utils_dir}/${jalviewjs_swingjs_zip}${eclipseVersionSuffix}"
1399
1400   doLast {
1401     zipFiles.each { file_zip -> 
1402       copy {
1403         from zipTree(file_zip)
1404         into jalviewjsSiteDir
1405       }
1406     }
1407   }
1408
1409   inputs.files zipFiles
1410   outputs.dir jalviewjsSiteDir
1411 }
1412
1413 task jalviewjsCreateJ2sSettings(type: WriteProperties) {
1414   dependsOn jalviewjsSitePath
1415   outputFile ("${jalviewDir}/${jalviewjs_j2s_settings}")
1416   def j2s_props = project.properties.findAll { it.key.startsWith("j2s.") }.sort { it.key }
1417   def siteDirProperty = "j2s.site.directory"
1418   def setSiteDir = false
1419   j2s_props.each { prop, val ->
1420     if (val != null) {
1421       if (prop == siteDirProperty) {
1422         if (!(val.startsWith('/') || val.startsWith("file://") )) {
1423           val = "${jalviewjsTransferSiteDir}/${val}"
1424         }
1425         setSiteDir = true
1426       }
1427       property(prop,val)
1428     }
1429     if (!setSiteDir) { // default site location, don't override specifically set property
1430       property(siteDirProperty,jalviewjsTransferSiteDir)
1431     }
1432   }
1433   inputs.properties(j2s_props)
1434   outputs.file(outputFile)
1435 }
1436
1437 task jalviewjsEclipseSetup {
1438   dependsOn jalviewjsEclipseCopyDropins
1439   dependsOn jalviewjsSetEclipseWorkspace
1440   dependsOn jalviewjsCreateJ2sSettings
1441 }
1442
1443 task jalviewjsCopyResources (type: Copy) {
1444   dependsOn jalviewjsSitePath
1445   def inputFiles = fileTree(dir: jalviewjs_resource_dir)
1446   def outputDir = "${jalviewjsSiteDir}/${jalviewjs_j2s_subdir}"
1447
1448   from inputFiles
1449   into outputDir
1450   def outputFiles = []
1451   rename { filename ->
1452     outputFiles += "${outputDir}/${filename}"
1453     null
1454   }
1455   outputs.files outputFiles
1456   inputs.files inputFiles
1457 }
1458
1459 task jalviewjsCopySiteResources (type: Copy) {
1460   dependsOn jalviewjsSitePath
1461   def inputFiles = fileTree(dir: "${jalviewjs_utils_dir}/${jalviewjs_site_resource_dir}")
1462   def outputDir = jalviewjsSiteDir
1463
1464   from inputFiles
1465   into outputDir
1466   def outputFiles = []
1467   rename { filename ->
1468     outputFiles += "${outputDir}/${filename}"
1469     null
1470   }
1471   outputs.files outputFiles
1472   inputs.files inputFiles
1473 }
1474
1475 task jalviewjsProjectImport(type: Exec) {
1476   dependsOn eclipseProject
1477   dependsOn eclipseClasspath
1478   dependsOn eclipseJdt
1479   dependsOn jalviewjsEclipsePaths
1480   dependsOn jalviewjsEclipseSetup
1481
1482   def projdir = eclipseWorkspace.getPath()+"/.metadata/.plugins/org.eclipse.core.resources/.projects/jalview/org.eclipse.jdt.core"
1483   executable(eclipseBinary)
1484   args(["-nosplash", "--launcher.suppressErrors", "-application", "com.seeq.eclipse.importprojects.headlessimport", "-data", eclipseWorkspace.getPath(), "-import", jalviewDirAbsolutePath])
1485   if (eclipseDebug) {
1486     args += "-debug"
1487   }
1488
1489   doFirst {
1490     println("IMPORT ECLIPSE_DEBUG=${eclipseDebug}")
1491   }
1492
1493   inputs.file("${jalviewDir}/.project")
1494   outputs.dir(projdir)
1495   outputs.upToDateWhen { file(projdir).exists() }
1496 }
1497
1498 task jalviewjsTranspile(type: Exec) {
1499   dependsOn jalviewjsEclipseSetup 
1500   dependsOn jalviewjsProjectImport
1501   dependsOn jalviewjsEclipsePaths
1502
1503   executable(eclipseBinary)
1504   args(["-nosplash", "--launcher.suppressErrors", "-application", "org.eclipse.jdt.apt.core.aptBuild", "-data", eclipseWorkspace, "-${jalviewjs_eclipseBuildArg}", eclipse_project_name ])
1505   if (eclipseDebug) {
1506     args += "-debug"
1507   }
1508
1509   def stdout
1510   def stderr
1511   stdout = new ByteArrayOutputStream()
1512   stderr = new ByteArrayOutputStream()
1513
1514   def logOutFileName = "${jalviewDirAbsolutePath}/${jalviewjsBuildDir}/${jalviewjs_j2s_stdout}"
1515   def logOutFile = file(logOutFileName)
1516   logOutFile.createNewFile()
1517   def logOutFOS = new FileOutputStream(logOutFile, false)
1518   //def logErrFileName = "${jalviewjsBuildDir}/${jalviewjs_j2s_stderr}"
1519   //def logErrFile = file(logFileName)
1520   //logErrFile.createNewFile()
1521   //def logErrFOS = new FileErrputStream(logErrFile, false)
1522   // combine stdout and stderr
1523   def logErrFOS = logOutFOS
1524   if (jalviewjs_j2s_to_console.equals("true")) {
1525     standardOutput = new org.apache.tools.ant.util.TeeOutputStream(
1526       new org.apache.tools.ant.util.TeeOutputStream(
1527         logOutFOS,
1528         stdout),
1529       standardOutput)
1530       errorOutput = new org.apache.tools.ant.util.TeeOutputStream(
1531         new org.apache.tools.ant.util.TeeOutputStream(
1532           logErrFOS,
1533           stderr),
1534         errorOutput)
1535   } else {
1536     standardOutput = new org.apache.tools.ant.util.TeeOutputStream(
1537       logOutFOS,
1538       stdout)
1539       errorOutput = new org.apache.tools.ant.util.TeeOutputStream(
1540         logErrFOS,
1541         stderr)
1542   }
1543
1544   doLast {
1545     if (stdout.toString().contains("Error processing ")) {
1546       // j2s did not complete transpile
1547       throw new GradleException("Error during transpilation:\n${stderr}\nSee eclipse transpile log file '${jalviewjsBuildDir}/${jalviewjs_j2s_stdout}'")
1548     }
1549   }
1550
1551   doFirst {
1552     println("TRANSPILE ECLIPSE_DEBUG=${eclipseDebug}")
1553   }
1554
1555   inputs.dir(sourceDir)
1556   outputs.dir("${eclipse_bin_dir}/main")
1557   outputs.dir(jalviewjsTransferSiteDir)
1558   outputs.upToDateWhen { file("${jalviewjsTransferSiteDir}${jalviewjs_server_resource}").exists() }
1559
1560 }
1561
1562 task jalviewjsCopyTransferSite(type: Copy) {
1563   dependsOn jalviewjsTranspile
1564   from jalviewjsTransferSiteDir
1565   into jalviewjsSiteDir
1566 }
1567
1568 jalviewjsUnzipFiles.mustRunAfter jalviewjsCopyTransferSite
1569 jalviewjsCopyResources.mustRunAfter jalviewjsCopyTransferSite
1570 jalviewjsCopySiteResources.mustRunAfter jalviewjsCopyTransferSite
1571
1572 task jalviewjsPrepareSite {
1573   group "JalviewJS"
1574   description "Prepares the website folder including unzipping files and copying resources"
1575   dependsOn jalviewjsSitePath
1576   dependsOn jalviewjsUnzipFiles
1577   dependsOn jalviewjsCopyResources
1578   dependsOn jalviewjsCopySiteResources
1579 }
1580
1581 task jalviewjsBuildSite {
1582   group "JalviewJS"
1583   description "Builds the whole website including transpiled code"
1584   dependsOn jalviewjsCopyTransferSite
1585   dependsOn jalviewjsPrepareSite
1586 }
1587
1588 task cleanJalviewjsSite {
1589   doFirst {
1590     delete jalviewjsTransferSiteDir
1591     delete jalviewjsSiteDir
1592   }
1593 }
1594
1595 task jalviewjsSiteTar(type: Tar) {
1596   group "JalviewJS"
1597   description "Creates a tar.gz file for the website"
1598   dependsOn jalviewjsBuildSite
1599   def outputFilename = "jalviewjs-site-${JALVIEW_VERSION}.tar.gz"
1600   try {
1601     archiveFileName = outputFilename
1602   } catch (Exception e) {
1603     archiveName = outputFilename
1604   }
1605
1606   compression Compression.GZIP
1607
1608   from jalviewjsSiteDir
1609   into jalviewjs_site_dir // this is inside the tar file
1610
1611   inputs.dir(jalviewjsSiteDir)
1612 }
1613
1614 task jalviewjsServer {
1615   group "JalviewJS"
1616   description "Starts a webserver on localhost to test the website"
1617   dependsOn jalviewjsSitePath
1618   def htmlFile = "${jalviewDirAbsolutePath}/jalviewjsTest.html"
1619   doLast {
1620
1621     SimpleHttpFileServerFactory factory = new SimpleHttpFileServerFactory()
1622     def port = Integer.valueOf(jalviewjs_server_port)
1623     def start = port
1624     def running = false
1625     def url
1626     while(port < start+1000 && !running) {
1627       try {
1628         def doc_root = new File("${jalviewDirAbsolutePath}/${jalviewjsSiteDir}")
1629         def jalviewjsServer = factory.start(doc_root, port)
1630         running = true
1631         url = jalviewjsServer.getResourceUrl(jalviewjs_server_resource)
1632         println("SERVER STARTED with document root ${doc_root}.")
1633         println("Go to "+url+" . Run  gradle --stop  to stop (kills all gradle daemons).")
1634         println("For debug: "+url+"?j2sdebug")
1635
1636         file(htmlFile).text = """
1637         <p><a href="${url}">Jalview JS Test. &lt;${url}&gt;</a></p>
1638         <p><a href="${url}?j2sdebug">Jalview JS Test with debug. &lt;${url}?j2sdebug&lt;</a></p>
1639         """
1640
1641       } catch (Exception e) {
1642         port++;
1643       }
1644     }
1645
1646   }
1647
1648   outputs.file(htmlFile)
1649 }
1650
1651 task cleanJalviewjsAll {
1652   group "JalviewJS"
1653   description "Delete all configuration and build artifacts to do with JalviewJS build"
1654   dependsOn cleanJalviewjsSite
1655   dependsOn jalviewjsEclipsePaths
1656   
1657   doFirst {
1658     delete jalviewjsBuildDir
1659     delete "${jalviewDir}/${eclipse_bin_dir}"
1660     if (eclipseWorkspace != null && file(eclipseWorkspace.getAbsolutePath()+"/.metadata").exists()) {
1661       delete file(eclipseWorkspace.getAbsolutePath()+"/.metadata")
1662     }
1663     delete "${jalviewDir}/${jalviewjs_j2s_settings}"
1664   }
1665 }
1666
1667 task jalviewjs {
1668   group "JalviewJS"
1669   description "Build the site"
1670   dependsOn jalviewjsBuildSite
1671 }
1672
1673
1674 task jalviewjsIDECopyTransferSite(type: Copy) {
1675   from jalviewjsTransferSiteDir
1676   into jalviewjsSiteDir
1677 }
1678
1679 task jalviewjsIDEj2s {
1680   group "JalviewJS in Eclipse"
1681   description "Creates the .j2s file"
1682   dependsOn jalviewjsCreateJ2sSettings
1683 }
1684
1685 task jalviewjsIDEBuildSite {
1686   group "JalviewJS in Eclipse"
1687   description "Copies the Eclipse transpiled site and unzips supporting zipfiles"
1688   dependsOn jalviewjsIDECopyTransferSite
1689   dependsOn jalviewjsPrepareSite
1690 }
1691
1692 task jalviewjsIDESiteClean {
1693   group "JalviewJS in Eclipse"
1694   description "Deletes the Eclipse transpiled site"
1695   dependsOn cleanJalviewjsSite
1696 }
1697
1698 task jalviewjsIDEServer {
1699   group "JalviewJS in Eclipse"
1700   description "Starts a webserver on localhost to test the website"
1701   dependsOn jalviewjsServer
1702 }
1703