buildscript {
    repositories {
      mavenCentral()
    }
    dependencies {
    }
}

plugins {
    id "com.jfrog.bintray" version "1.8.4"
    id "com.android.library"
    id "maven-publish"
    id "signing"
}


Object[] versionInfo = screengrabVersion(rootProject.file('version.properties'))
String versionNameString = versionInfo[0] as String

Properties properties = new Properties()
def propertiesFile = project.rootProject.file('local.properties')
if (propertiesFile.exists()) {
 properties.load(propertiesFile.newDataInputStream())
}

android {
    compileSdkVersion 30

    defaultConfig {
        minSdkVersion 18
    }
}

dependencies {
    implementation "androidx.annotation:annotation:1.2.0"
    implementation 'androidx.test:core:1.3.0'
    implementation 'androidx.test:runner:1.3.0'
    implementation 'androidx.test:rules:1.3.0'
    implementation 'androidx.test.espresso:espresso-core:3.3.0'
    implementation 'androidx.test.uiautomator:uiautomator:2.2.0'
    implementation 'com.jraska:falcon:2.2.0'
}
gradle.projectsEvaluated {
    tasks.withType(JavaCompile){
        options.compilerArgs << "-Xlint:deprecation"
    }
}

afterEvaluate {
    publishing {
        publications {
            maven(MavenPublication) {
                groupId 'tools.fastlane'
                artifactId 'screengrab'
                version versionNameString

                artifact(bundleReleaseAar)
                pom.withXml {
                    asNode().appendNode('name', 'screengrab')
                    asNode().appendNode('url', 'https://github.com/fastlane/fastlane')
                    asNode().appendNode('description', 'Screengrab is a commandline tool that automates taking localized screenshots')

                    final licensesNode = asNode().appendNode('licenses')
                    final licenseNode = licensesNode.appendNode('license')
                    licenseNode.appendNode('name', 'MIT License')
                    licenseNode.appendNode('url', 'http://www.opensource.org/licenses/mit-license.php')

                    final scmNode = asNode().appendNode('scm')
                    scmNode.appendNode('url', 'https://github.com/fastlane/fastlane')
                    scmNode.appendNode('connection', 'scm:git:git://github.com/fastlane/fastlane.git')

                    final developersNode = asNode().appendNode('developers')
                    final developerNode = developersNode.appendNode('developer')
                    developerNode.appendNode('email', 'me@joshholtz.com')
                    developerNode.appendNode('name', 'Josh Holtz')
                    developerNode.appendNode('url', 'https://github.com/joshdholtz')
                    developerNode.appendNode('id', 'joshdholtz')

                    final dependenciesNode = asNode().appendNode('dependencies')

                    ext.addDependency = { Dependency dep, String scope ->
                        if (dep.group == null || dep.version == null || dep.name == null || dep.name == "unspecified")
                            return // invalid dependencies should be ignored

                        final dependencyNode = dependenciesNode.appendNode('dependency')
                        dependencyNode.appendNode('artifactId', dep.name)

                        if (dep.version == 'unspecified') {
                            dependencyNode.appendNode('groupId', project.ext.pomGroupID)
                            dependencyNode.appendNode('version', project.ext.pomVersion)
                            System.println("${project.ext.pomGroupID} ${dep.name} ${project.ext.pomVersion}")
                        } else {
                            dependencyNode.appendNode('groupId', dep.group)
                            dependencyNode.appendNode('version', dep.version)
                            System.println("${dep.group} ${dep.name} ${dep.version}")
                        }

                        dependencyNode.appendNode('scope', scope)
                        // Some dependencies may have types, such as aar, that should be mentioned in the POM file
                        def artifactsList = dep.properties['artifacts']
                        if (artifactsList != null && artifactsList.size() > 0) {
                            final artifact = artifactsList[0]
                            dependencyNode.appendNode('type', artifact.getType())
                        }

                        if (!dep.transitive) {
                            // In case of non transitive dependency, all its dependencies should be force excluded from them POM file
                            final exclusionNode = dependencyNode.appendNode('exclusions').appendNode('exclusion')
                            exclusionNode.appendNode('groupId', '*')
                            exclusionNode.appendNode('artifactId', '*')
                        } else if (!dep.properties.excludeRules.empty) {
                            // For transitive with exclusions, all exclude rules should be added to the POM file
                            final exclusions = dependencyNode.appendNode('exclusions')
                            dep.properties.excludeRules.each { ExcludeRule rule ->
                                final exclusionNode = exclusions.appendNode('exclusion')
                                exclusionNode.appendNode('groupId', rule.group ?: '*')
                                exclusionNode.appendNode('artifactId', rule.module ?: '*')
                            }
                        }
                    }

                    configurations.api.getDependencies().each { dep -> addDependency(dep, "compile") }
                    configurations.implementation.getDependencies().each { dep -> addDependency(dep, "runtime") }
                }
            }
        }
        repositories {
            maven {
                //url 'https://oss.sonatype.org/content/repositories/snapshots'
                //url 'https://oss.sonatype.org/service/local/staging/deploy/maven2/'
                url properties.getProperty('sonatypeStagingProfileId')
                credentials {
                    username properties.getProperty('ossrhUsername')
                    password properties.getProperty('ossrhPassword')
                }
            }
        }
    }
}

/*
 * Requires the following settings in ~/.gradle/gradle.properties
 * signing.enabled=true
 * signing.keyId=516466E9 # last 8 digits of signing key (can get with `gpg -K`) 
 * signing.password=
 * signing.secretKeyRingFile=/Users/josh/.gnupg/secring.gpg # export with `gpg --keyring secring.gpg --export-secret-keys > ~/.gnupg/secring.gpg`
 */
if (new Boolean(project.findProperty('signing.enabled'))) {
    signing {
        sign publishing.publications
    }
}