apply plugin: 'scala'

description = 'com.logicalhacking.dasca.crosslanguage.test'

def osName = System.getProperty('os.name')
ext.isWindows = osName.startsWith('Windows ')

task downloadAndroidSdk(type: VerifiedDownload) {
	def sdkOs
	switch (osName) {
		case ~/Linux/:
			sdkOs = 'linux'
			checksum '444e22ce8ca0f67353bda4b85175ed3731cae3ffa695ca18119cbacef1c1bea0'
			break
		case ~/Mac OS X/:
			sdkOs = 'darwin'
			checksum '4a81754a760fce88cba74d69c364b05b31c53d57b26f9f82355c61d5fe4b9df9'
			break
		case ~/Windows.*/:
			sdkOs = 'windows'
			checksum '7f6037d3a7d6789b4fdc06ee7af041e071e9860c51f66f7a4eb5913df9871fd2'
			break
	}
	def archive = "sdk-tools-$sdkOs-3859397.zip"
	src "https://dl.google.com/android/repository/$archive"
	dest "$temporaryDir/$archive"
	algorithm 'SHA-256'
}

task installAndroidSdk(type: Sync, dependsOn: downloadAndroidSdk) {
	from zipTree(downloadAndroidSdk.dest)
	into temporaryDir

	def buildToolsVersion = '26.0.2'
	ext {
		components = [
			'build-tools': buildToolsVersion,
			'platforms': "android-${buildToolsVersion.tokenize('.')[0]}"
		]
	}

	doLast {
		exec {
			def shell, shellFlags, yes, semicolon, discard
			if (project.isWindows) {
				shell = 'PowerShell'
				shellFlags = '-Command'
				yes = 'echo y'
				semicolon = '`;'
				discard = '$null'
			} else {
				shell = 'sh'
				shellFlags = '-ceu'
				yes = 'yes 2>/dev/null'
				semicolon = /\;/
				discard = '/dev/null'
			}

			def componentArgs = components.collect { "$it.key$semicolon$it.value" }.join ' '
			commandLine shell, shellFlags, "$yes | $temporaryDir/tools/bin/sdkmanager $componentArgs >$discard"
		}
	}
	outputs.cacheIf { true }
}

task copyAndroidJar(type: Sync, dependsOn: installAndroidSdk) {
	from "${installAndroidSdk.destinationDir}/platforms/${installAndroidSdk.components['platforms']}/android.jar"
        into temporaryDir
}

processResources {
    with copySpec {
        from copyAndroidJar
        rename { "android-dasca.jar" }
    }
}

dependencies {
    implementation group: 'org.scala-lang', name: 'scala-library', version:'2.12.3'
    implementation group: 'junit', name: 'junit', version: '4.12'
    implementation group: 'org.scalatest', name: 'scalatest_2.12', version: '3.0.5'
    implementation group: 'com.ibm.wala', name: 'com.ibm.wala.core' , version:walaVersion
    implementation group: 'com.ibm.wala', name: 'com.ibm.wala.shrike' , version:walaVersion
    implementation group: 'com.ibm.wala', name: 'com.ibm.wala.util' , version:walaVersion
    implementation group: 'com.ibm.wala', name: 'com.ibm.wala.cast' , version:walaVersion
    implementation group: 'com.ibm.wala', name: 'com.ibm.wala.cast.js' , version:walaVersion
    implementation group: 'com.ibm.wala', name: 'com.ibm.wala.cast.js.rhino' , version:walaVersion
    implementation group: 'com.ibm.wala', name: 'com.ibm.wala.dalvik' , version:walaVersion
    implementation project(':com.logicalhacking.dasca.crosslanguage')
    testRuntime group: 'org.slf4j', name: 'slf4j-api', version: '1.7.2'
    testRuntime group: 'org.smali', name: 'dexlib2', version: '2.2.5'
    testRuntime group: 'net.htmlparser.jericho', name: 'jericho-html', version:'3.2'
    testRuntime group: 'commons-io', name: 'commons-io', version:'2.4'
    testRuntime group: 'com.google.guava', name: 'guava', version: '23.0'
    testRuntime group: 'org.mozilla', name: 'rhino', version: '1.7.10'
    testRuntime group: 'com.typesafe.scala-logging', name: 'scala-logging_2.12', version: '3.5.0'
    testRuntime group: 'apktool', name: 'apktool', version:'2.3.0'
}


