buildscript {
	repositories {
		jcenter()
		mavenCentral()
		mavenLocal()
	}
}

ext {
	groovyVersion     = "2.4.15"
	hamcrestVersion   = "1.3"
	junitVersion      = "4.12"
	jrubyVersion      = "9.2.7.0"
	jsonVersion       = "20160810"
	mockitoVersion    = "2.28.2"
	slf4jVersion      = "1.7.26"
	springBootVersion = "2.1.6.RELEASE"
	ext.javadocLinks = [
			"http://docs.oracle.com/javase/7/docs/api/",
			"http://docs.oracle.com/javaee/7/api/"
	] as String[]
}

ext.coreprojects = subprojects.findAll { project -> !project.name.startsWith("rivescript-samples") }
ext.sampleprojects = subprojects.findAll { project -> project.name.startsWith("rivescript-samples") }

configure(allprojects) { project ->

	group = "com.rivescript"

	apply plugin: "java"
	apply plugin: "idea"
	apply plugin: "maven"
	apply plugin: "signing"

	sourceSets {
		integrationTest {
			java {
				compileClasspath += main.output + test.output
				runtimeClasspath += main.output + test.output
				srcDir file("src/integration-test/java")
			}
			resources.srcDir file("src/integration-test/resources")
		}
	}

	configurations {
		integrationTestCompile.extendsFrom testCompile
		integrationTestRuntime.extendsFrom testRuntime
	}

	compileJava {
		sourceCompatibility = 1.7
		targetCompatibility = 1.7
		options.encoding = "UTF-8"
		options.compilerArgs += "-Xlint:none"
	}

	compileTestJava {
		sourceCompatibility = 1.8
		targetCompatibility = 1.8
		options.encoding = "UTF-8"
		options.compilerArgs += "-Xlint:none"
	}

	repositories {
		jcenter()
		mavenCentral()
		mavenLocal()
	}

	dependencies {
		compile "org.slf4j:slf4j-api:${slf4jVersion}"
		testCompile "junit:junit:${junitVersion}"
		testCompile "org.hamcrest:hamcrest-core:${hamcrestVersion}"
		testCompile "org.hamcrest:hamcrest-library:${hamcrestVersion}"
		testCompile "org.mockito:mockito-core:${mockitoVersion}"
		testCompile "org.slf4j:slf4j-simple:${slf4jVersion}"
	}

	idea {
		module {
			downloadJavadoc = true
			downloadSources = true
		}
	}

	task integrationTest(type: Test) {
		testClassesDirs = sourceSets.integrationTest.output.classesDirs
		classpath = sourceSets.integrationTest.runtimeClasspath
	}

	check.dependsOn integrationTest
	integrationTest.mustRunAfter test

	tasks.withType(Test) {
		outputs.upToDateWhen { false }
		reports.html.destination = file("${reporting.baseDir}/${name}")
		testLogging {
			events "passed", "skipped", "failed"
		}
	}
}

configure(subprojects - sampleprojects) { subproject ->

	jar {
		manifest.attributes["Created-By"] = "${System.getProperty("java.version")} (${System.getProperty("java.specification.vendor")})"
		manifest.attributes["Implementation-Title"] = project.name
		manifest.attributes["Implementation-Version"] = project.version
	}

	javadoc {
		options.memberLevel = org.gradle.external.javadoc.JavadocMemberLevel.PROTECTED
		options.author = true
		options.header = "${project.name} ${project.version}"
		options.docTitle = "${options.header} API"
		options.addStringOption("Xdoclint:none", "-quiet")
	}

	task sourcesJar(type: Jar, dependsOn: classes) {
		classifier = "sources"
		from sourceSets.main.allSource
	}

	task javadocJar(type: Jar, dependsOn: javadoc) {
		classifier = "javadoc"
		from javadoc.destinationDir
	}

	artifacts {
		archives sourcesJar, javadocJar
	}

	signing {
		required { gradle.taskGraph.hasTask("uploadArchives") }
		sign configurations.archives
	}

	uploadArchives {
		repositories {
			mavenDeployer {

				beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) }

				repository(url: "https://oss.sonatype.org/service/local/staging/deploy/maven2/") {
					authentication(userName: System.getenv("SONATYPE_OSSRH_USERNAME"), password: System.getenv("SONATYPE_OSSRH_PASSWORD"))
				}

				snapshotRepository(url: "https://oss.sonatype.org/content/repositories/snapshots/") {
					authentication(userName: System.getenv("SONATYPE_OSSRH_USERNAME"), password: System.getenv("SONATYPE_OSSRH_PASSWORD"))
				}

				pom.project {
					name "RiveScript"
					packaging "jar"
					description "RiveScript interpreter for Java"
					url "https://github.com/aichaos/rivescript-java"

					scm {
						connection "git@github.com:aichaos/rivescript-java.git"
						developerConnection "git@github.com:aichaos/rivescript-java.git"
						url "https://github.com/aichaos/rivescript-java"
					}

					licenses {
						license {
							name "MIT License"
							url "http://www.opensource.org/licenses/mit-license.php"
						}
					}

					developers {
						developer {
							id "kirsle"
							name "Noah Petherbridge"
							email "root@kirsle.net"
						}
						developer {
							id "marceloverdijk"
							name "Marcel Overdijk"
							email "marcel@overdijk.me"
						}
					}
				}
			}
		}
	}
}

project("rivescript-core") {

	description = "RiveScript Core"

	dependencies {
		compileOnly "org.json:json:${jsonVersion}"
		integrationTestRuntime "org.codehaus.groovy:groovy-all:${groovyVersion}"
		integrationTestRuntime "org.jruby:jruby:${jrubyVersion}"
	}
}

project("rivescript-spring-boot-starter") {

	description = "RiveScript Spring Boot Starter"

	dependencies {
		compile project(":rivescript-core")
		compile "org.springframework.boot:spring-boot:${springBootVersion}"
		compile "org.springframework.boot:spring-boot-autoconfigure:${springBootVersion}"
		testCompile "org.springframework.boot:spring-boot-starter-test:${springBootVersion}"
	}
}

configure(rootProject) {

	description = "RiveScript"

	// don't publish the default jar for the root project
	configurations.archives.artifacts.clear()

	task api(type: Javadoc) {
		group = "Documentation"
		description = "Generates aggregated Javadoc API documentation."
		title = "${rootProject.description} ${version} API"

		dependsOn {
			coreprojects.collect {
				it.tasks.getByName("jar")
			}
		}
		options.memberLevel = org.gradle.external.javadoc.JavadocMemberLevel.PROTECTED
		options.author = true
		options.header = "${project.name} ${project.version}"
		options.overview = "src/api/overview.html"
		options.splitIndex = true
		options.links(javadocLinks)
		options.addStringOption("Xdoclint:none", "-quiet")

		source coreprojects.collect { project ->
			project.sourceSets.main.allJava
		}

		classpath += files(coreprojects.collect { it.sourceSets.main.compileClasspath })
		destinationDir = new File(buildDir, "api")
		maxMemory = "1024m"
	}

	task docs(dependsOn: "api") {
		group = "Documentation"
		description = "Generates aggregated Javadoc API and reference documentation."
	}

	task docsZip(type: Zip, dependsOn: "docs") {
		group = "Distribution"
		baseName = rootProject.name
		classifier = "docs"
		description = "Builds -${classifier} archive containing Javadoc API and reference documentation."

		from (api) {
			into "api"
		}
	}

	task distZip(type: Zip, dependsOn: "docsZip") {
		group = "Distribution"
		baseName = rootProject.name
		classifier = "dist"
		description = "Builds -${classifier} archive containing all jars and documentation."

		from(zipTree(docsZip.archivePath)) {
			into "docs"
		}

		coreprojects.each { subproject ->
			into ("libs") {
				from subproject.jar
				if (subproject.tasks.findByPath("sourcesJar")) {
					from subproject.sourcesJar
				}
				if (subproject.tasks.findByPath("javadocJar")) {
					from subproject.javadocJar
				}
			}
		}
	}

	artifacts {
		archives docsZip
		archives distZip
	}
}
