/*
 * Copyright 2016 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * To publish a driver to Maven local:
 *     ./gradlew -PPUBLISHING [driver_name]:clean [driver_name]:publishToMavenLocal
 *
 * To publish a driver to Bintray:
 * Set environmental variables BINTRAY_USER and BINTRAY_API_KEY to proper values and run
 *     ./gradlew -PPUBLISHING [driver_name]:clean [driver_name]:bintrayUpload
 *
 * Additionally, add the -PSNAPSHOT flag to replace artifact version names with "SNAPSHOT". Note
 * that bintrayUpload tasks will not upload the artifact if this flag is used.
 */

allprojects {
    apply plugin: 'maven-publish'
    apply plugin: 'com.jfrog.bintray'
}

String publicationName = 'driverPublish'
boolean snapshot = hasProperty "SNAPSHOT"

subprojects {
    if (!project.findProperty("ARTIFACT_VERSION")) {
        return
    }

    apply plugin: 'com.android.library'

    task genJavadocs(type: Javadoc) {
        // For each project, generate Javadoc and include it as an artifact
        source = android.sourceSets.main.java.sourceFiles
        // Manually select the current version of Android
        project.android.compileSdkVersion = 'android-' + DEFAULT_COMPILE_SDK_VERSION

        // Make sure compiled dependencies can be handled for Javadoc
	// e.g: for androidthings support library.
        configurations.compileOnly.setCanBeResolved(true)
        classpath += configurations.compileOnly

        // Make sure implemented dependencies can be handled for Javadoc
	// e.g: for annotation support library.
        configurations.implementation.setCanBeResolved(true)
        classpath += configurations.implementation
    }

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

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

    def artifactVersion = snapshot ? "LATEST" : ARTIFACT_VERSION

    publishing.publications {
            "$publicationName"(MavenPublication) {
                groupId 'com.google.android.things.contrib'
                artifactId "driver-$project.name"
                version artifactVersion
                artifact sourceJar
                artifact javadocJar
                artifact("$buildDir/outputs/aar/$project.name-release.aar")
                pom.withXml {
                    def dependenciesNode = asNode().appendNode('dependencies')
                    (configurations.compile.allDependencies + configurations.api.allDependencies).each {
                        if (it.group != null
                                && it.name != null && !"unspecified".equals(it.name)
                                && it.version != null && !"unspecified".equals(it.version)) {
                            def dependencyNode = dependenciesNode.appendNode('dependency')
                            dependencyNode.appendNode('groupId', it.group)
                            dependencyNode.appendNode('artifactId', it.name)
                            dependencyNode.appendNode('version', it.version)
                        }
                    }
                }
            }
    }

    bintray {
        user = System.getenv('BINTRAY_USER')
        key = System.getenv('BINTRAY_API_KEY')
        publications = [publicationName]

        publish = true
        dryRun = snapshot

        pkg {
            repo = 'androidthings'
            name = "contrib-driver-$project.name"
            userOrg = 'google'

            version {
                name = artifactVersion
                gpg {
                    sign = true
                }
            }
        }
    }

    afterEvaluate {
        tasks.withType(PublishToMavenRepository).all { publishTask ->
            publishTask.dependsOn build
        }
        tasks.withType(PublishToMavenLocal).all { publishTask ->
            publishTask.dependsOn build
        }
    }
}
