buildscript {
	dependencies {
		classpath "com.android.tools.build:gradle:$agp_version"
		classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.5'
	}
}

apply plugin: 'com.android.library'
apply plugin: 'trikita.anvilgen'
apply from: "$rootProject.projectDir/copyDeps.gradle"

android {
	compileSdkVersion 27

	compileOptions {
		sourceCompatibility JavaVersion.VERSION_1_7
		targetCompatibility JavaVersion.VERSION_1_7
	}

	defaultConfig {
		minSdkVersion 15
		targetSdkVersion 27

		missingDimensionStrategy 'api', 'sdk15'
	}

	lintOptions {
		abortOnError false
	}

	testOptions {
		unitTests.returnDefaultValues = true
	}
}

apply from: 'attr-quirks.gradle'
anvilgen {
	type = "support"
	libraryName = "gridlayout-v7"
	camelCaseName = "GridLayoutv7"
	version = support_version
	dependencies = [
	  "support-core-ui": null,
	  "support-core-utils": null,
	  "support-compat": null,
	  "support-media-compat": null,
	  "support-fragment": null,
	]
	quirks = QUIRKS
}

dependencies {
	implementation project(path: ':anvil')
	implementation "com.android.support:gridlayout-v7:$support_version"

	testImplementation 'junit:junit:4.12'
	testImplementation "org.mockito:mockito-core:$mockito_version"
}

apply plugin: 'maven-publish'
apply plugin: 'com.jfrog.bintray'

def fixPom(pom) {
	pom.withXml {
		def root = asNode()
		root.appendNode('description', POM_DESCRIPTION)
		root.appendNode('name', POM_NAME)
		root.appendNode('url', POM_URL)
		def scm = root.appendNode('scm')
		scm.appendNode('url', POM_SCM_URL)
		scm.appendNode('connection', POM_SCM_CONNECTION)
		scm.appendNode('developerConnection', POM_SCM_DEV_CONNECTION)
		def developer = root.appendNode('developers').appendNode('developer')
		developer.appendNode('id', POM_DEVELOPER_ID)
		developer.appendNode('name', POM_DEVELOPER_NAME)
		developer.appendNode('email', POM_DEVELOPER_EMAIL)
		def license = root.appendNode('licenses').appendNode('license')
		license.appendNode('name', POM_LICENCE_NAME)
		license.appendNode('url', POM_LICENCE_URL)
		license.appendNode('distribution', POM_LICENCE_DIST)
	}
}

publishing {
	publications {
		gridlayoutv7(MavenPublication) {
			groupId project.group
			version project.version
			artifactId POM_ARTIFACT_ID
			artifact "$buildDir/outputs/aar/$POM_ARTIFACT_ID-release.aar"
			artifact sourcesJar
			artifact javadocJar
			fixPom(pom)
		}
	}
}

bintray {
	user = project.hasProperty('bintrayUser') ? bintrayUser : ''
	key = project.hasProperty('bintrayApiKey') ? bintrayApiKey : ''
	publications = ['gridlayoutv7']
	pkg {
		repo = 'maven'
		name = POM_PACKAGE_NAME
		websiteUrl = POM_URL
		vcsUrl = POM_SCM_URL
		licenses = [POM_LICENCE_SHORT_NAME]
		publish = true
		version {
			name = project.version
			gpg {
				sign = true
				passphrase = project.hasProperty('bintrayGpgPassword') ? bintrayGpgPassword : ''
			}
			mavenCentralSync {
				sync = true
				user = project.hasProperty('bintrayOssUser') ? bintrayOssUser : ''
				password = project.hasProperty('bintrayOssPassword') ? bintrayOssPassword : ''
				close = '1'
			}
		}
	}
}

task sourcesJar(type: Jar) {
	from android.sourceSets.main.java.srcDirs
	classifier = 'sources'
}

task javadoc(type: Javadoc) {
	source = android.sourceSets.main.java.srcDirs
	classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
}

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

artifacts {
	archives javadocJar
	archives sourcesJar
}

android.libraryVariants.all { variant ->
	task("generate${variant.name.capitalize()}Javadoc", type: Javadoc) {
		description "Generates Javadoc for $variant.name."
		source = variant.getCompileClasspath(null).getAsFileTree()
		ext.androidJar = "${android.sdkDirectory}/platforms/${android.compileSdkVersion}/android.jar"
		classpath = files(variant.getCompileClasspath(null)) + files(ext.androidJar)
	}
}
