JAL-3456 re-use of existing jar files
[jalview.git] / build.gradle
1 import org.gradle.internal.os.OperatingSystem
2 import org.gradle.plugins.ide.eclipse.model.Output
3 import org.gradle.plugins.ide.eclipse.model.Library
4
5 import com.diffplug.gradle.GoomphCacheLocations
6
7 buildscript {
8 }
9
10 plugins {
11   id 'java'
12   id 'application'
13   id 'eclipse'
14
15   id 'com.diffplug.gradle.oomph.ide' version '3.18.1'
16   id 'com.diffplug.gradle.equinoxlaunch' version '3.18.1'
17   id 'com.diffplug.gradle.eclipse.mavencentral' version '3.18.1'
18   id 'com.diffplug.gradle.p2.asmaven' version '3.18.1'
19 }
20
21 def jalviewDirAbsolutePath = file(jalviewDir).getAbsolutePath()
22 def useIde = useGoomph.equals("true")
23
24 repositories {
25   jcenter()
26   mavenCentral()
27   flatDir {
28     dirs gradlePluginsDir
29   }
30   if (true || useIde) {
31     flatDir {
32       dirs jalviewjs_utils_dir+"/"+jalviewjs_eclipse_dropins_dir
33     }
34   }
35 }
36
37 configurations {
38   jalviewjsIde
39 }
40
41
42 if (useIde) {
43
44   p2AsMaven {
45     group 'eclipseDeps', {
46       repoEclipse goomph_eclipse_repo_version
47       //slicingOption 'latestVersionOnly', 'true'
48       iu 'org.eclipse.update.configurator'
49
50
51       iu 'org.eclipse.jdt.apt.core'
52       iu 'org.eclipse.jdt.core'
53       iu 'org.eclipse.jdt.annotation'
54
55     }
56     group 'ecjDeps', {
57       repo 'https://download.eclipse.org/eclipse/updates/4.13/R-4.13-201909161045/'
58       iu 'org.eclipse.debug.core'
59       iu 'org.eclipse.jdt.apt.pluggable.core'
60       iu 'org.eclipse.jdt.apt.ui'
61       iu 'org.eclipse.jdt.compiler.apt'
62       iu 'org.eclipse.jdt.compiler.tool'
63       iu 'org.eclipse.jdt.core.manipulation'
64       iu 'org.eclipse.jdt.debug'
65       iu 'org.eclipse.jdt.debug.ui'
66       iu 'org.eclipse.jdt.doc.user'
67       iu 'org.eclipse.jdt.junit'
68       iu 'org.eclipse.jdt.junit.core'
69       iu 'org.eclipse.jdt.junit4.runtime'
70       iu 'org.eclipse.jdt.junit4.runtime'
71       iu 'org.eclipse.jdt.junit5.runtime'
72       iu 'org.eclipse.jdt.launching'
73       iu 'org.eclipse.jdt.ui'
74
75       iu 'org.eclipse.jdt.launching.macosx'
76       iu 'org.eclipse.jdt.launching.ui.macosx'
77     }
78     group 'eclipseOrbit', {
79       repo goomph_orbit_repo
80       slicingOption 'latestVersionOnly', 'true'
81       iu 'javax.el'
82       iu 'javax.servlet'
83       iu 'javax.servlet.jsp'
84       iu 'org.apache.ant'
85       iu 'org.apache.batik.constants'
86       iu 'org.apache.batik.css'
87       iu 'org.apache.batik.i18n'
88       iu 'org.apache.batik.util'
89       iu 'org.apache.commons.beanutils'
90       iu 'org.apache.commons.collections'
91       iu 'org.apache.commons.jxpath'
92       iu 'org.apache.commons.io'
93       iu 'org.apache.commons.logging'
94       iu 'org.apache.xmlgraphics'
95       iu 'org.jdom'
96       iu 'org.w3c.dom.events'
97       iu 'org.w3c.dom.smil'
98       iu 'org.w3c.dom.svg'
99     }
100   }
101
102 } // if (useIde) {}
103
104 dependencies {
105 /*
106     source.addMaven('org.eclipse.platform:org.eclipse.osgi:3.14.0')
107     // ...
108     compile 'eclipseOrbit:org.apache.ant:1.10.5.v20190526-1402'
109     // ...
110     compile 'org.osgi:org.osgi.service.component:1.4.0'
111     // ...
112 */
113
114   jalviewjsIde name: 'net.sf.j2s.core-3.2.4'
115   jalviewjsIde name: 'com.seeq.eclipse.importprojects-1.4.0'
116
117 }
118
119
120
121
122
123
124 if (useIde) {
125
126   eclipseMavenCentral {
127     // TODO: How do I add these to the jalviewjsIde configuration and not the compile configuration?
128     release goomph_eclipse_repo_version, {
129
130       //def platformCode="cocoa.macosx.x86_64"
131       //compile 'org.eclipse.swt.'+platformCode
132       useNativesForRunningPlatform()
133       compile 'org.eclipse.swt'
134
135       compile 'org.eclipse.ant.core'
136       compile 'org.eclipse.compare.core'
137       compile 'org.eclipse.core.commands'
138       compile 'org.eclipse.core.contenttype'
139       compile 'org.eclipse.core.jobs'
140       compile 'org.eclipse.core.expressions'
141       compile 'org.eclipse.core.filesystem'
142       compile 'org.eclipse.core.resources'
143       compile 'org.eclipse.core.runtime'
144       compile 'org.eclipse.core.variables'
145       compile 'org.eclipse.equinox.common'
146       compile 'org.eclipse.equinox.ds'
147       compile 'org.eclipse.equinox.app'
148       compile 'org.eclipse.equinox.registry'
149       compile 'org.eclipse.equinox.preferences'
150       compile 'org.eclipse.jface'
151       compile 'org.eclipse.osgi'
152       compile 'org.eclipse.osgi.services'
153       compile 'org.eclipse.ui'
154       compile 'org.eclipse.ui.workbench'
155       compile 'org.eclipse.team.core'
156       compile 'org.eclipse.text'
157
158       compile 'org.eclipse.e4.core.commands'
159       compile 'org.eclipse.e4.core.contexts'
160       compile 'org.eclipse.e4.core.di'
161       compile 'org.eclipse.e4.core.di.annotations'
162       compile 'org.eclipse.e4.core.di.extensions.supplier'
163       compile 'org.eclipse.e4.core.services'
164       compile 'org.eclipse.e4.ui.bindings'
165       compile 'org.eclipse.e4.ui.model.workbench'
166       compile 'org.eclipse.e4.ui.workbench'
167       compile 'org.eclipse.e4.ui.workbench.swt'
168       compile 'org.eclipse.e4.ui.css.core'
169       compile 'org.eclipse.e4.ui.css.swt'
170       compile 'org.eclipse.e4.ui.css.swt.theme'
171       compile 'org.eclipse.e4.ui.services'
172       compile 'org.eclipse.e4.ui.di'
173
174       //compile 'org.eclipse.e4.core.di.internal.extensions'
175
176       // org.eclipse.update.configurator deliberately kept at 3.3.400 see https://github.com/diffplug/goomph/issues/69
177       // this module downloaded in equinoxLaunch configuration (to specify version)
178       //compile 'org.eclipse.update.configurator'
179
180
181     }
182   }
183
184 } // if (useIde) {}
185
186
187
188
189
190
191
192
193
194
195
196 mainClassName = launcherClass
197 def classes = "$jalviewDir/$classesDir"
198
199 // configure classpath/args for j8/j11 compilation
200
201 def libDir
202 def libDistDir
203 def compile_source_compatibility
204 def compile_target_compatibility
205
206 ext {
207   buildProperties = jalviewDir + "/" + classesDir +"/" + buildPropertiesFile
208
209   gitHash = ""
210   gitBranch = ""
211
212   jalviewjsServer = ""
213 }
214
215 def JAVA_INTEGER_VERSION
216 //def additional_compiler_args = []
217 // this property is for the Java library used in eclipse
218 def eclipse_java_runtime_name
219 if (JAVA_VERSION.equals("1.8")) {
220   JAVA_INTEGER_VERSION = "8"
221   //libDir = j8libDir
222   libDir = j11libDir
223   libDistDir = j8libDir
224   compile_source_compatibility = 1.8
225   compile_target_compatibility = 1.8
226   eclipse_java_runtime_name = "JavaSE-1.8"
227 } else if (JAVA_VERSION.equals("11")) {
228   JAVA_INTEGER_VERSION = "11"
229   libDir = j11libDir
230   libDistDir = j11libDir
231   compile_source_compatibility = 11
232   compile_target_compatibility = 11
233   eclipse_java_runtime_name = "JavaSE-11"
234   /* compile without modules -- using classpath libraries
235   additional_compiler_args += [
236   '--module-path', ext.modules_compileClasspath.asPath,
237   '--add-modules', j11modules
238   ]
239   */
240 } else if (JAVA_VERSION.equals("12") || JAVA_VERSION.equals("13")) {
241   JAVA_INTEGER_VERSION = JAVA_VERSION
242   libDir = j11libDir
243   libDistDir = j11libDir
244   compile_source_compatibility = JAVA_VERSION
245   compile_target_compatibility = JAVA_VERSION
246   eclipse_java_runtime_name = "JavaSE-11"
247   /* compile without modules -- using classpath libraries
248   additional_compiler_args += [
249   '--module-path', ext.modules_compileClasspath.asPath,
250   '--add-modules', j11modules
251   ]
252   */
253 } else {
254   throw new GradleException("JAVA_VERSION=$JAVA_VERSION not currently supported by Jalview")
255 }
256
257
258 sourceSets {
259
260   main {
261     java {
262       srcDirs "$jalviewDir/$sourceDir"
263       outputDir = file("$classes")
264     }
265
266     resources {
267       srcDirs "$jalviewDir/$resourceDir"
268     }
269
270     compileClasspath = files(sourceSets.main.java.outputDir)
271     compileClasspath += fileTree(dir: "$jalviewDir/$libDir", include: ["*.jar"])
272
273     runtimeClasspath = compileClasspath
274   }
275
276 }
277
278 eclipse {
279   project {
280     name = eclipse_project_name
281
282     natures 'org.eclipse.jdt.core.javanature',
283     'org.eclipse.buildship.core.gradleprojectnature'
284
285     buildCommand 'org.eclipse.jdt.core.javabuilder'
286     buildCommand 'org.eclipse.buildship.core.gradleprojectbuilder'
287   }
288
289   classpath {
290     //defaultOutputDir = sourceSets.main.java.outputDir
291     def removeThese = []
292     configurations.each{ if (it.isCanBeResolved()) {
293         removeThese += it
294       }
295     }
296
297     minusConfigurations += removeThese
298     plusConfigurations = [ ]
299     file {
300
301       whenMerged { cp ->
302         def removeTheseToo = []
303         HashMap<String, Boolean> addedSrcPath = new HashMap<>();
304         cp.entries.each { entry ->
305           if (entry.kind == 'src') {
306             if (addedSrcPath.getAt(entry.path) || !(entry.path == sourceDir || entry.path == "test")) {
307               removeTheseToo += entry
308             } else {
309               addedSrcPath.putAt(entry.path, true)
310             }
311           }
312         }
313         cp.entries.removeAll(removeTheseToo)
314
315         if (file(eclipse_bin_dir+"/main").isDirectory()) {
316                 cp.entries += new Output(eclipse_bin_dir+"/main")
317         }
318         if (file(helpParentDir).isDirectory()) {
319                 cp.entries += new Library(fileReference(helpParentDir))
320         }
321         if (file(resourceDir).isDirectory()) {
322                 cp.entries += new Library(fileReference(resourceDir))
323         }
324
325         HashMap<String, Boolean> addedLibPath = new HashMap<>();
326
327         // changing from sourcesets.main.classpath to specific Java version lib
328         //sourceSets.main.compileClasspath.each{
329         fileTree("$jalviewDir/$libDistDir").include("**/*.jar").include("*.jar").each {
330           //don't want to add outputDir as eclipse is using its own output dir in bin/main
331           if (it.isDirectory() || ! it.exists()) {
332             // don't add dirs to classpath
333             return
334           }
335           def itPath = it.toString()
336           if (itPath.startsWith(jalviewDirAbsolutePath+"/")) {
337             itPath = itPath.substring(jalviewDirAbsolutePath.length()+1)
338           }
339           if (addedLibPath.get(itPath)) {
340             //println("Not adding duplicate entry "+itPath)
341           } else {
342             //println("Adding entry "+itPath)
343             cp.entries += new Library(fileReference(itPath))
344             addedLibPath.put(itPath, true)
345           }
346         }
347       } // whenMerged
348
349     } // file
350
351     containers 'org.eclipse.buildship.core.gradleclasspathcontainer'
352   } // classpath
353
354   jdt {
355     // for the IDE, use java 11 compatibility
356     sourceCompatibility = compile_source_compatibility
357     targetCompatibility = compile_target_compatibility
358     javaRuntimeName = eclipse_java_runtime_name
359
360     /*
361     file {
362     withProperties { props ->
363     def jalview_prefs = new Properties()
364     def ins = new FileInputStream(jalviewDirAbsolutePath+"/"+eclipse_extra_jdt_prefs_file)
365     jalview_prefs.load(ins)
366     ins.close()
367     jalview_prefs.forEach { t, v ->
368     if (props.getAt(t) == null) {
369     props.putAt(t, v)
370     }
371     }
372     }
373     }
374      */
375   } // jdt
376
377   //synchronizationTasks eclipseClasspath
378   //autoBuildTasks eclipseClasspath
379 } // eclipse
380
381 /*
382 compileJava {
383
384   doFirst {
385     sourceCompatibility = compile_source_compatibility
386     targetCompatibility = compile_target_compatibility
387     //options.compilerArgs = additional_compiler_args
388     print ("Setting target compatibility to "+targetCompatibility+"\n")
389   }
390
391 }
392
393 clean {
394   delete sourceSets.main.java.outputDir
395 }
396 */
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418 def eclipseWorkspace
419 task jalviewjsSetEclipseWorkspace {
420   def eclipseWsDir = useIde?goomph_workspace:jalviewjs_eclipse_workspace
421   eclipseWorkspace = file(eclipseWsDir)
422   if (!eclipseWorkspace.exists()) {
423     eclipseWorkspace = file("${buildDir}/tmp/eclipse-workspace")
424     //eclipseWorkspace.deleteOnExit()
425   } else if (useIde) {
426     com.diffplug.gradle.GoomphCacheLocations.override_workspaces = file(eclipseWorkspace.getAbsolutePath())
427     eclipseWorkspace = oomphIde.workspaceRegistry.workspaceDir(project, file(oomphIde.ideDir))
428   }
429   println("ECLIPSE WORKSPACE: "+eclipseWorkspace.getPath())
430 }
431
432
433 if (useIde) {
434
435   oomphIde {
436     repoEclipse goomph_eclipse_version
437     jdt {
438       /*
439       installedJre {
440       //markDefault = true
441       //executionEnvironments = [ eclipse.jdt.javaRuntimeName ]
442       }
443        */
444       //compilerComplianceLevel( JAVA_INTEGER_VERSION )
445     }
446
447     repo "https://download.eclipse.org/releases/${goomph_eclipse_version_name}/"
448     feature goomph_eclipse_jee_feature, goomph_eclipse_jee_version
449
450     thirdParty {
451       buildship {
452         repo goomph_repo_buildship
453         feature goomph_feature_buildship
454       }
455
456     }
457
458     workspaceProp '.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.jdt.launching.prefs', {
459       it.put('org.eclipse.jdt.launching.PREF_VM_XML', goomph_PREF_VM_XML)
460     }
461
462
463     // CLI ARGS HERE!
464   }
465
466   equinoxLaunch {
467     ideJalviewjsBuildSetup {
468
469       source.addConfiguration(configurations.compile)
470       source.addConfiguration(configurations.jalviewjsIde)
471
472       source.addMaven('eclipseOrbit:org.apache.ant:1.10.5.v20190526-1402')
473       source.addMaven('eclipseOrbit:javax.servlet:3.1.0.v201410161800')
474       source.addMaven('eclipseOrbit:javax.servlet.jsp:2.2.0.v201112011158')
475       source.addMaven('eclipseOrbit:javax.el:2.2.0.v201303151357')
476       source.addMaven('eclipseOrbit:org.apache.batik.constants:1.11.0.v20190515-0436')
477       source.addMaven('eclipseOrbit:org.apache.batik.css:1.11.0.v20190515-0436')
478       source.addMaven('eclipseOrbit:org.apache.batik.i18n:1.11.0.v20190515-0436')
479       source.addMaven('eclipseOrbit:org.apache.batik.util:1.11.0.v20190515-0436')
480       source.addMaven('eclipseOrbit:org.apache.commons.beanutils:1.8.0.v201205091237')
481       source.addMaven('eclipseOrbit:org.apache.commons.collections:3.2.2.v201511171945')
482       source.addMaven('eclipseOrbit:org.apache.xmlgraphics:2.3.0.v20190515-0436')
483       source.addMaven('eclipseOrbit:org.apache.commons.jxpath:1.3.0.v200911051830')
484       source.addMaven('eclipseOrbit:org.apache.commons.io:2.6.0.v20190123-2029')
485       source.addMaven('eclipseOrbit:org.apache.commons.logging:1.2.0.v20180409-1502')
486       source.addMaven('eclipseOrbit:org.jdom:1.1.1.v201101151400')
487       source.addMaven('eclipseOrbit:org.w3c.dom.svg:1.1.0.v201011041433')
488       source.addMaven('eclipseOrbit:org.w3c.dom.smil:1.0.1.v200903091627')
489       source.addMaven('eclipseOrbit:org.w3c.dom.events:3.0.0.draft20060413_v201105210656')
490
491       source.addMaven('org.eclipse.birt.runtime:com.ibm.icu:56.1.0.v201601250100')
492       source.addMaven('javax.inject:com.springsource.javax.inject:1.0.0')
493       source.addMaven('org.apache.felix:org.apache.felix.scr:2.1.16')
494       source.addMaven('org.osgi:org.osgi.framework:1.9.0')
495       source.addMaven('org.osgi:org.osgi.service.component:1.4.0')
496       source.addMaven('org.osgi:org.osgi.service.event:1.4.0')
497       source.addMaven('org.osgi:org.osgi.util.promise:1.1.1')
498       source.addMaven('org.osgi:org.osgi.util.function:1.1.0')
499
500       source.addMaven('ecjDeps:org.eclipse.debug.core:3.14.0.v20190812-1404')
501       source.addMaven('ecjDeps:org.eclipse.jdt.annotation:2.2.300.v20190328-1431')
502       source.addMaven('ecjDeps:org.eclipse.jdt.apt.core:3.6.500.v20190703-0914')
503       source.addMaven('ecjDeps:org.eclipse.jdt.core:3.19.0.v20190903-0936')
504       source.addMaven('ecjDeps:org.eclipse.jdt.apt.pluggable.core:1.2.400.v20190626-1334')
505       //source.addMaven('ecjDeps:org.eclipse.jdt.apt.ui:3.6.300.v20190727-1243')
506       source.addMaven('ecjDeps:org.eclipse.jdt.compiler.apt:1.3.700.v20190704-1731')
507       source.addMaven('ecjDeps:org.eclipse.jdt.compiler.tool:1.2.600.v20190322-0450')
508       //source.addMaven('ecjDeps:org.eclipse.jdt.core.manipulation:1.12.100.v20190904-0645')
509       source.addMaven('ecjDeps:org.eclipse.jdt.debug:3.13.100.v20190902-1050')
510       //source.addMaven('ecjDeps:org.eclipse.jdt.debug.ui:3.10.400.v20190815-2202')
511       source.addMaven('ecjDeps:org.eclipse.jdt.doc.user:3.15.400.v20190904-0724')
512       //source.addMaven('ecjDeps:org.eclipse.jdt.junit:3.11.500.v20190809-1613')
513       //source.addMaven('ecjDeps:org.eclipse.jdt.junit.core:3.10.400.v20190625-1910')
514       //source.addMaven('ecjDeps:org.eclipse.jdt.junit4.runtime:1.1.1000.v20190510-0840')
515       //source.addMaven('ecjDeps:org.eclipse.jdt.junit4.runtime:1.1.1000.v20190510-0840')
516       //source.addMaven('ecjDeps:org.eclipse.jdt.junit5.runtime:1.0.600.v20190610-0439')
517       source.addMaven('ecjDeps:org.eclipse.jdt.launching:3.15.0.v20190826-1639')
518       //source.addMaven('ecjDeps:org.eclipse.jdt.ui:3.19.0.v20190827-1733')
519
520       //source.addMaven('ecjDeps:org.eclipse.jdt.launching.macosx:3.4.300.v20190510-0639')
521       //source.addMaven('ecjDeps:org.eclipse.jdt.launching.ui.macosx:1.2.300.v20190510-0639')
522
523       // org.eclipse.update.configurator deliberately kept at 3.3.400 see https://github.com/diffplug/goomph/issues/69
524       source.addMaven('org.eclipse.platform:org.eclipse.update.configurator:3.3.400')
525
526
527       launchTask 'ideJalviewjsImport', {
528         it.args = ["-consoleLog", "-nosplash", "--launcher.suppressErrors", "-application", "com.seeq.eclipse.importprojects.headlessimport", "-data", eclipseWorkspace.getPath(), "-import", jalviewDirAbsolutePath ]
529       }
530
531       launchTask 'ideJalviewjsBuild', {
532
533         oomphIde.workspaceProp '.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.jdt.launching.prefs', {
534           it.put('org.eclipse.jdt.launching.PREF_VM_XML', goomph_PREF_VM_XML)
535         }
536
537         def args = ["-consoleLog", "-nosplash", "--launcher.suppressErrors", "-application", "org.eclipse.jdt.apt.core.aptBuild", "-data", eclipseWorkspace.getPath(), "-"+jalviewjs_eclipseBuildArg, eclipse_project_name ]
538         println("ARGS="+args)
539
540         it.args = args
541
542       }
543     }
544   }
545
546   ideSetupWorkspace.dependsOn jalviewjsSetEclipseWorkspace
547   ideSetupWorkspace.outputs.upToDateWhen {file(eclipseWorkspace).exists()}
548
549   task ideSetup {
550     dependsOn ideSetupP2
551     dependsOn ideSetupWorkspace
552   }
553
554 } // if (useIde) {}
555
556
557
558
559
560
561
562
563
564
565
566
567
568 def jalviewjsBuildDir
569 def jalviewjsSiteDir
570 task jalviewjsSitePath {
571   if (jalviewjs_site_dir.startsWith("/")) {
572     jalviewjsSiteDir = jalviewjs_site_dir
573   } else {
574     def relativeBuildDir = file(jalviewDirAbsolutePath).toPath().relativize(buildDir.toPath())
575     jalviewjsBuildDir = "${relativeBuildDir}/jalviewjs"
576     jalviewjsSiteDir = jalviewjsBuildDir + "/" + jalviewjs_site_dir
577   }
578 }
579
580
581 task jalviewjsUnzipFiles {
582   dependsOn jalviewjsSitePath
583
584   def zipFiles = fileTree(dir: jalviewjs_utils_dir+"/"+jalviewjs_libjs_dir).include("*.zip")
585   zipFiles += jalviewjs_utils_dir+"/"+jalviewjs_swingjs_zip
586
587   doLast {
588     zipFiles.each { file_zip -> 
589       copy {
590         from zipTree(file_zip)
591         into jalviewjsSiteDir
592       }
593     }
594   }
595
596   inputs.files zipFiles
597   outputs.dir jalviewjsSiteDir
598 }
599
600 def eclipseDropinsDir
601 def eclipseBinary
602 task jalviewjsEclipsePaths {
603   def eclipseRoot
604   if (useIde) {
605     eclipseRoot = oomphIde.ideDir
606     eclipseDropinsDir = eclipseRoot+"/"+com.diffplug.gradle.FileMisc.macContentsEclipse()+"/dropins"
607     eclipseBinary = eclipseRoot+"/"+com.diffplug.common.swt.os.OS.getNative().winMacLinux("", "Contents/MacOS", "")+"/eclipse"
608   } else {
609     eclipseRoot = jalviewjs_eclipse_root
610     if (eclipseRoot.startsWith("~")) {
611       eclipseRoot = System.getProperty("user.home") + eclipseRoot.substring(1)
612     }
613     if (OperatingSystem.current().isMacOsX()) {
614       eclipseRoot += "/Eclipse.app"
615       eclipseDropinsDir = eclipseRoot+"/Contents/Eclipse/dropins"
616       eclipseBinary = eclipseRoot+"/Contents/MacOS/eclipse"
617     } else if (OperatingSystem.current().isWindows()) { // check these paths!!
618       eclipseDropinsDir = eclipseRoot+"/dropins"
619       eclipseBinary = eclipseRoot+"/eclipse"
620     } else { // linux or unix
621       eclipseDropinsDir = eclipseRoot+"/dropins"
622       eclipseBinary = eclipseRoot+"/eclipse"
623     }
624   }
625 }
626
627 task jalviewjsEclipseCopyDropins {
628   dependsOn jalviewjsEclipsePaths
629   def inputFiles = fileTree(dir: jalviewjs_utils_dir+"/"+jalviewjs_eclipse_dropins_dir, include: "*.jar")
630   def outputDir = eclipseDropinsDir
631
632   inputs.files inputFiles
633   inputFiles.each { file ->
634     outputs.file(outputDir+"/"+file.name)
635   }
636
637   doLast {
638     def outputFiles = []
639     inputFiles.each { file ->
640       copy {
641         from file
642         into outputDir
643       }
644     }
645   }
646 }
647
648 task jalviewjsEclipseSetup {
649   dependsOn jalviewjsEclipseCopyDropins
650   dependsOn jalviewjsSetEclipseWorkspace
651 }
652
653
654 task jalviewjsCreateJ2sSettings(type: WriteProperties) {
655   dependsOn jalviewjsSitePath
656   outputFile (jalviewDir+"/"+jalviewjs_j2s_settings)
657   def props = project.properties.sort { it.key }
658   def siteDirProperty = "j2s.site.directory"
659   def setSiteDir = false
660   props.each { prop, val ->
661     if (prop.startsWith("j2s.") && val != null) {
662       if (prop == siteDirProperty) {
663         if (!(val.startsWith("/") || val.startsWith("file://") )) {
664           val = jalviewjsSiteDir+"/"+val
665         }
666         setSiteDir = true
667       }
668       property(prop,val)
669     }
670     if (!setSiteDir) {
671       property(siteDirProperty,"${jalviewjsSiteDir}")
672     }
673   }
674   outputs.file(outputFile)
675 }
676
677 task jalviewjsCopyResources (type: Copy) {
678   dependsOn jalviewjsSitePath
679   def inputFiles = fileTree(dir: jalviewjs_resource_dir)
680   def outputDir = jalviewjsSiteDir+"/"+jalviewjs_j2s_subdir
681
682   from inputFiles
683   into outputDir
684   def outputFiles = []
685   rename { filename ->
686     outputFiles += outputDir+"/"+filename
687     null
688   }
689   outputs.files outputFiles
690   inputs.files inputFiles
691 }
692
693 task jalviewjsCopySiteResources (type: Copy) {
694   dependsOn jalviewjsSitePath
695   def inputFiles = fileTree(dir: jalviewjs_utils_dir+"/"+jalviewjs_site_resource_dir)
696   def outputDir = jalviewjsSiteDir
697
698   from inputFiles
699   into outputDir
700   def outputFiles = []
701   rename { filename ->
702     outputFiles += outputDir+"/"+filename
703     null
704   }
705   outputs.files outputFiles
706   inputs.files inputFiles
707 }
708
709 task cleanJalviewjs {
710   dependsOn jalviewjsSitePath
711   delete jalviewDir+"/"+jalviewjsSiteDir
712   //delete jalviewDir+"/"+eclipse_bin_dir
713   //delete file(eclipseWorkspace.getAbsolutePath()+"/.metadata")
714   //delete jalviewDir+"/"+jalviewjs_j2s_settings
715 }
716
717 task jalviewjsProjectImport(type: Exec) {
718   // work out how to do this!
719   dependsOn eclipseProject
720   dependsOn eclipseClasspath
721   dependsOn eclipseJdt
722   dependsOn jalviewjsEclipsePaths
723   dependsOn jalviewjsEclipseSetup
724   executable(eclipseBinary)
725   args(["-nosplash", "--launcher.suppressErrors", "-application", "com.seeq.eclipse.importprojects.headlessimport", "-data", eclipseWorkspace.getPath(), "-import", jalviewDirAbsolutePath])
726
727   def projdir = eclipseWorkspace.getPath()+"/.metadata/.plugins/org.eclipse.core.resources/.projects/jalview/org.eclipse.jdt.core"
728   inputs.file(jalviewDir+"/.project")
729   outputs.dir(projdir)
730   outputs.upToDateWhen { file(projdir).exists() }
731 }
732
733
734
735
736
737
738
739
740 task jalviewjsTranspile(type: Exec) {
741   dependsOn jalviewjsCreateJ2sSettings 
742   dependsOn jalviewjsProjectImport
743   dependsOn jalviewjsEclipsePaths
744   executable(eclipseBinary)
745   args(["-nosplash", "--launcher.suppressErrors", "-application", "org.eclipse.jdt.apt.core.aptBuild", "-data", eclipseWorkspace, "-"+jalviewjs_eclipseBuildArg, eclipse_project_name ])
746
747   def stdout
748   def stderr
749   doFirst {
750     stdout = new ByteArrayOutputStream()
751     stderr = new ByteArrayOutputStream()
752     standardOutput = new org.apache.tools.ant.util.TeeOutputStream(new FileOutputStream("${jalviewjsBuildDir}/${jalviewjs_j2s_stdout}"), stdout);
753     errorOutput = new org.apache.tools.ant.util.TeeOutputStream(new FileOutputStream("${jalviewjsBuildDir}/${jalviewjs_j2s_stderr}"), stderr);
754   }
755   doLast {
756     if (stdout.toString().contains("Error processing ")) {
757       // j2s did not complete transpile
758       throw new GradleException("Error during transpilation:\n${stderr}\nSee eclipse transpile log files '${jalviewjsBuildDir}/${jalviewjs_j2s_stdout}' and '${jalviewjsBuildDir}/${jalviewjs_j2s_stderr}'\n")
759     }
760   }
761
762   inputs.dir(sourceDir)
763   outputs.dir(eclipse_bin_dir+"/main")
764   outputs.files(
765     fileTree(
766       dir: jalviewjsSiteDir+"/"+jalviewjs_j2s_subdir,
767       includes: [ "**/*.js" ],
768       excludes: [
769         //jalviewjsSiteDir+"/"+jalviewjs_j2s_subdir+"/intervalstore/**",
770         //jalviewjsSiteDir+"/"+jalviewjs_j2s_subdir+"/org/json/**",
771         "intervalstore/**",
772         "org/json/**",
773       ]
774     ).files
775   )
776   outputs.file(jalviewjsSiteDir+jalviewjs_server_resource)
777
778 }
779
780
781
782
783
784
785 jalviewjsUnzipFiles.mustRunAfter jalviewjsTranspile
786 jalviewjsTranspile.mustRunAfter jalviewjsCopyResources
787 jalviewjsTranspile.mustRunAfter jalviewjsCopySiteResources
788
789 task jalviewjsPrepareSite {
790   group "JalviewJS"
791   description "Prepares the website folder including unzipping files and copying resources"
792   dependsOn jalviewjsSitePath
793   dependsOn jalviewjsUnzipFiles
794   dependsOn jalviewjsCopyResources
795   dependsOn jalviewjsCopySiteResources
796 }
797
798 task jalviewjsBuildSite {
799   group "JalviewJS"
800   description "Builds the whole website including transpiled code"
801   dependsOn jalviewjsPrepareSite
802   dependsOn jalviewjsTranspile
803 }
804
805 task jalviewjsSiteTar(type: Tar) {
806   group "JalviewJS"
807   description "Creates a tar.gz file for the website"
808   dependsOn jalviewjsBuildSite
809   def outputFilename = "jalviewjs-site-${JALVIEW_VERSION}.tar.gz"
810   try {
811     archiveFileName = outputFilename
812   } catch (Exception e) {
813     archiveName = outputFilename
814   }
815
816   compression Compression.GZIP
817
818   from jalviewjsSiteDir
819   into jalviewjs_site_dir // this is inside the tar file
820
821   inputs.dir(jalviewjsSiteDir)
822 }
823
824 def jalviewjsServer = null
825 task jalviewjsServerStart {
826   group "JalviewJS"
827   description "Starts a webserver on localhost to test the website"
828   dependsOn jalviewjsSitePath
829   doLast {
830
831     if (jalviewjsServer != null) {
832       println("SERVER ALREADY RUNNING. Go to "+jalviewjsServer.getResourceUrl(jalviewjs_server_resource)+" . Run  gradle jalviewjsServerStop  to stop.")
833     } else {
834
835       SimpleHttpFileServerFactory factory = new SimpleHttpFileServerFactory()
836       def port = Integer.valueOf(jalviewjs_server_port)
837       def start = port
838       def running = false
839       while(port < start+1000 && !running) {
840         try {
841           def doc_root = new File(jalviewDirAbsolutePath +"/"+ jalviewjsSiteDir)
842           jalviewjsServer = factory.start(doc_root, port)
843           running = true
844           println("SERVER STARTED with document root ${doc_root}.\nGo to "+jalviewjsServer.getResourceUrl(jalviewjs_server_resource)+" . Run  gradle --stop  to stop (kills all gradle daemons).")
845           //println("Ctrl-c to stop.");java.lang.Thread.sleep(Integer.valueOf(jalviewjs_server_wait)*1000);
846         } catch (Exception e) {
847           port++;
848         }
849       }
850
851     }
852
853   }
854
855 }
856
857 /* server persists in gradle daemon but reference is not accessible across builds
858 task jalviewjsServerStop {
859   doLast {
860
861     if (jalviewjsServer != null) {
862
863       println("SERVER ON PORT "+jalviewjsServer.getPort()+" STOPPING. Run  gradle jalviewjsServerStart  to start again.")
864       jalviewjsServer.stop()
865
866     } else {
867       println("SERVER NOT RUNNING. Run  gradle jalviewjsServerStart  to start.")
868     }
869
870   }
871 }
872 */
873
874 task ideBuildSite {
875   dependsOn jalviewjsSitePath
876   dependsOn jalviewjsUnzipFiles
877   dependsOn jalviewjsCopyResources
878   dependsOn jalviewjsCopySiteResources
879   /* see project.afterEvaluate
880   dependsOn ideJalviewjsBuild
881   */
882 }
883
884
885
886
887 task jalviewjs {
888   if (useIde) {
889     dependsOn ideBuildSite
890   } else {
891     dependsOn jalviewjsBuildSite
892   }
893 }
894
895
896 project.afterEvaluate {
897
898   tasks.findByName('eclipse').dependsOn tasks.findByName('jalviewjsCreateJ2sSettings')
899
900   if (useIde) {
901     tasks.findByName('ideJalviewjsImport').dependsOn eclipseProject
902     tasks.findByName('ideJalviewjsImport').dependsOn eclipseClasspath
903     tasks.findByName('ideJalviewjsImport').dependsOn eclipseJdt
904     tasks.findByName('ideJalviewjsImport').dependsOn jalviewjsSetEclipseWorkspace
905
906     tasks.findByName('ideJalviewjsBuild').dependsOn tasks.findByName('ideJalviewjsImport')
907     tasks.findByName('ideJalviewjsBuild').dependsOn tasks.findByName('jalviewjsCreateJ2sSettings')
908
909     tasks.findByName('ide').dependsOn eclipseProject
910     tasks.findByName('ide').dependsOn eclipseClasspath
911     tasks.findByName('ide').dependsOn eclipseJdt
912
913     ideBuildSite.dependsOn tasks.findByName('ideJalviewjsBuild')
914   }
915
916 }
917