import org.ajoberstar.grgit.*

buildscript {
    repositories {
        mavenCentral()
        maven {
            url "https://plugins.gradle.org/m2/"
        }
    }
    
    dependencies {
        classpath 'org.ajoberstar:gradle-git:0.11.2'
        classpath "com.gradle.publish:plugin-publish-plugin:0.10.0"
    }
}

apply plugin: "com.gradle.plugin-publish"
apply plugin: 'maven-publish'
apply plugin: 'groovy'
apply plugin: 'signing'
apply plugin: 'idea'

Properties props = new Properties()
props.load(new FileInputStream("$projectDir/../gradle.properties"))
group = props['group']
version = props['version']
ext.androidPluginVersion = props['androidPluginVersion']

sourceCompatibility = '1.7'

repositories {
    maven { url 'https://maven.google.com' }
    jcenter()
    mavenCentral()
}

configurations {
    provided
}

sourceSets {
    main {
        compileClasspath += configurations.provided
    }
}

dependencies {
    compile gradleApi()
    compile localGroovy()
    provided "com.android.tools.build:gradle:$androidPluginVersion"

    testCompile gradleTestKit()
    testCompile 'junit:junit:4.12'
    testCompile 'org.assertj:assertj-core:2.1.0'
    testCompile "com.android.tools.build:gradle:$androidPluginVersion"
    testCompile 'com.neenbedankt.gradle.plugins:android-apt:1.8'
}

idea {
    module {
        scopes.PROVIDED.plus += [configurations.provided]
    }
}

// Taken from https://docs.gradle.org/current/userguide/test_kit.html
// Write the plugin's classpath to a file to share with the tests
task createClasspathManifest {
    def outputDir = file("$buildDir/$name")

    inputs.files sourceSets.main.runtimeClasspath
    outputs.dir outputDir

    doLast {
        outputDir.mkdirs()
        file("$outputDir/plugin-classpath.txt").text = sourceSets.main.runtimeClasspath.join("\n")
    }
}

// Add the classpath file to the test runtime classpath
dependencies {
    testRuntime files(createClasspathManifest)
}

task javadocJar(type: Jar, dependsOn: javadoc) {
    classifier = 'javadoc'
    from 'build/docs/javadoc'
}

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

pluginBundle {
    website = 'https://github.com/evant/gradle-retrolambda'
    vcsUrl = 'https://github.com/evant/gradle-retrolambda.git'
    description = 'https://github.com/evant/gradle-retrolambda.git'
    tags = ['java', 'android', 'lambda']

    plugins {
        retrolambdaPlugin {
            id = 'me.tatarka.retrolambda'
            displayName = 'gradle-retrolambda'
        }
    }
}

publishing {
    publications {
        lib(MavenPublication) {
            artifact jar
            artifact sourcesJar
            artifact javadocJar
            pom {
                name = 'gradle-retrolambda'
                description = 'A gradle plugin for getting java lambda support in java 6, 7 and android'
                url = 'https://github.com/evant/gradle-retrolambda'
                scm {
                    url = 'git@github.com:evant/gradle-retrolambda.git'
                    connection = 'scm:git:git@github.com:evant/gradle-retrolambda.git'
                    developerConnection = 'scm:git:git@github.com:evant/gradle-retrolambda.git'
                }
                licenses {
                    license {
                        name = 'The Apache Software License, Version 2.0'
                        url = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
                        distribution = 'repo'
                    }
                }
                developers {
                    developer {
                        id = 'evant'
                        name = 'Evan Tatarka'
                    }
                }
            }
        }
    }
    repositories {
        maven {
            def releasesRepoUrl = 'https://oss.sonatype.org/service/local/staging/deploy/maven2/'
            def snapshotsRepoUrl = 'https://oss.sonatype.org/content/repositories/snapshots'
            url = version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl
            credentials {
                username project.findProperty('sonatype.username')
                password project.findProperty('sonatype.password')
            }
        }
    }
}

signing {
    sign publishing.publications.lib
}


def grgit = Grgit.open("$projectDir/..")

task checkRelease { doLast {
    def readmeVersion = file("$project.rootProject.projectDir/../README.md").readLines().find { it.contains('me.tatarka:gradle-retrolambda:') }?.trim()

    if (readmeVersion == null) {
        throw new ProjectConfigurationException("Missing README version string", null)
    }

    if (!readmeVersion.contains(":$version")) {
        throw new ProjectConfigurationException("README version string: $readmeVersion must match release version: $version", null)
    }

    def changelogVersion = file("$project.rootProject.projectDir/../CHANGELOG.md").readLines().find { it.contains("### $version") }
    if (changelogVersion == null) {
        throw new ProjectConfigurationException("CHANGELOG does not contain changes for release version: $version", null)
    }

    def branchName = grgit.branch.current.name
    if (branchName == 'beta') {
        if (!version.contains('beta')) {
            throw new ProjectConfigurationException("Beta branch must contain 'beta' in the version name", null)
        }
    } else if (branchName != 'master') {
        throw new ProjectConfigurationException("Branch: $branchName must be master", null)
    }
} }

task tagRelease(dependsOn: [checkRelease]) { doLast {
    grgit.tag.add {
        name = "v$version"
        message = "Release of $version"
    }
} }

if (!version.endsWith('SNAPSHOT')) {
    uploadArchives.dependsOn(tagRelease)
}
