ant.importBuild 'buildSrc/src/main/resources/codesign.xml'

def String getSystemProperty(String propertyName, String defaultValue)
{
	return System.properties.getProperty(propertyName, defaultValue)
}

def String getAnanasEnv() {
	return getSystemProperty('ANANAS_ENV', 'development')
}

def getPackrPlatform() {
	switch(project.platform) {
		case 'linux':
			return 'linux64'
		case 'mac':
			return 'mac'
		case 'win':
			return 'windows64'
		default: 
			return 'linux64'
	}
}


if (!project.hasProperty('platform')) {
	project.ext.platform = ''	
}

if (!project.hasProperty('JDK_HOME')) {
	project.ext.JDK_HOME = ''
}

task printVersion {
	doLast {
		println "Version: $project.version; Runner name: $project.runnerJarName"
	}
}

task copyRunner(type: Copy) {
	dependsOn ":runner:shadowJar"

	from file("runner/build/libs/${project.runnerJarName}.jar")

	into file("ui/resource")
}

task packRunner(type: Exec) {
	dependsOn ":ui:cleanRunnerBinary"
	dependsOn ":runner:clean"
	dependsOn ":runner:shadowJar"

	println "Packing settings: JDK_HOME=[${project.JDK_HOME}] for platform [${project.platform}]"

    println "Jar: ${project.runnerJarName}"

	workingDir "."

	commandLine "java", "-jar", "buildSrc/src/main/resources/packr.jar",
		"--platform", "${getPackrPlatform()}",
		"--jdk", "$project.JDK_HOME",
		"--executable", "ananas",
		"--classpath", "runner/build/libs/${project.runnerJarName}.jar",
		// "--removelibs", "runner/build/libs/${project.runnerJarName}.jar",
        // "--minimizejre", "soft",
		"--mainclass", "org.ananas.runner.api.Main",
		"--vmargs", "Xmx4G",
        "--bundle", "com.ananas.desktop",
		"--output", "ui/resources/runner"
}

task pack(type: Exec) {
	dependsOn ':ui:build'
	dependsOn packRunner

	inputs.property 'NODE_ENV', getAnanasEnv()

	println "Binary version: ${project.version}"

	println "Environment: ${getAnanasEnv()}"

	workingDir "./ui"

	environment "NODE_ENV", "${getAnanasEnv()}"

    // environment "CSC_IDENTITY_AUTO_DISCOVERY", "${System.properties.getProperty('CODE_SIGN', 'false')}"

	commandLine "./node_modules/.bin/electron-builder", "--${project.platform}"
}

task packMac(type: Exec) {
	dependsOn ':ui:build'
    // don't depend on packRunner task

	inputs.property 'NODE_ENV', getAnanasEnv()

	println "Binary version: ${project.version}"

	println "Environment: ${getAnanasEnv()}"

	workingDir "./ui"

	environment "NODE_ENV", "${getAnanasEnv()}"

    environment "CSC_IDENTITY_AUTO_DISCOVERY", "false"

	commandLine "./node_modules/.bin/electron-builder", "--mac"
}

