buildscript {
	repositories {
		mavenCentral()
	}
	dependencies {
		classpath "org.springframework.boot:spring-boot-gradle-plugin:$springBootVersion"
	}
}

apply from: JAVA_GRADLE
apply plugin: "application"
apply plugin: 'org.springframework.boot'

tasks.findByPath("artifactoryPublish")?.enabled = false

sonarqube {
	skipProject = true
}

dependencies {
	compile project(':spring-session-data-gemfire'),
		"org.springframework.boot:spring-boot-starter-thymeleaf",
		"org.springframework.boot:spring-boot-starter-web",
		"org.webjars:bootstrap:$bootstrapVersion",
		"org.webjars:webjars-locator"

	runtime "org.springframework.shell:spring-shell:1.0.0.RELEASE"

	testCompile "org.springframework.boot:spring-boot-starter-test"

	integrationTestCompile gebDependencies,
		"org.spockframework:spock-spring:$spockVersion"

	integrationTestRuntime "org.springframework.shell:spring-shell:1.0.0.RELEASE"
}

run {
	doFirst {
		mainClassName = 'sample.server.GemFireServer'
	}
}

springBoot {
	mainClass = 'sample.client.Application'
}

task runGemFireServer() << {
	println 'STARTING GEMFIRE SERVER...'

	ext.port = reservePort()

	String classpath = sourceSets.main.runtimeClasspath.collect { it }.join(File.pathSeparator)

	String[] commandLine = ['java', '-server', '-ea', '-classpath', classpath,
							"-Dgemfire.cache.server.port=$port",
							"-Dgemfire.log-level=" + System.getProperty('gemfire.log.level', 'warning'),
							'sample.server.GemFireServer']

	//println commandLine

	ext.process = commandLine.execute()
	process.in.close()
	process.out.close()
	process.err.close()
}


integrationTest {
	dependsOn runGemFireServer
	doFirst {
		def port = reservePort()
		def host = 'localhost:' + port
		systemProperties['server.port'] = port
		systemProperties['management.port'] = 0
		systemProperties['geb.build.baseUrl'] = 'http://'+host+'/'
		systemProperties['geb.build.reportsDir'] = 'build/geb-reports'
		systemProperties['gemfire.cache.server.port'] = runGemFireServer.port
	}
	doLast {
		println 'STOPPING GEMFIRE SERVER...'
		runGemFireServer.process?.destroyForcibly()
	}
}


def reservePort() {
	def socket = new ServerSocket(0)
	def result = socket.localPort
	socket.close()
	result
}
