group(System.getenv('GROUP') ?: "agorapulse.libs")

apply plugin: "groovy"
apply plugin: "maven-publish"
project.apply plugin: "codenarc"

repositories {
    mavenCentral()
}

dependencies {
    compile "org.codehaus.groovy:groovy-all:$groovyVersion"
    compile "org.codehaus.groovy:groovy-dateutil:$groovyVersion"
    compile "org.slf4j:slf4j-api:$slf4jVersion"
    compile "ch.qos.logback:logback-classic:$logbackVersion"
    compile "ch.qos.logback:logback-core:$logbackVersion"

    testCompile "org.spockframework:spock-core:1.3-groovy-2.5"
}

test {
    systemProperties System.properties
}

/*
 * Maven
 */

// custom tasks for creating source/javadoc jars
task sourcesJar(type: Jar, dependsOn: classes) {
    classifier = 'sources'
    from sourceSets.main.allSource
}

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

// add javadoc/source jar tasks as artifacts
artifacts {
    archives sourcesJar
    archives javadocJar
}

publishing {
    repositories {
        maven {
            name = "GitHubPackages"
            url = uri("https://maven.pkg.github.com/agorapulse/grails-aws-sdk")
            credentials {
                username = System.getenv("GITHUB_ACTOR")
                password = System.getenv("GITHUB_TOKEN")
            }
        }
        if (System.getenv('DEPLOYER_PASSWORD')) {
            maven {
                name = "JFrog"
                credentials {
                    username "deployer"
                    password System.getenv('DEPLOYER_PASSWORD')
                }
                if (project.version.endsWith('-SNAPSHOT')) {
                    url 'https://agorapulse.jfrog.io/agorapulse/libs-snapshots-local/'
                } else {
                    url 'https://agorapulse.jfrog.io/agorapulse/libs-releases-local/'
                }
            }
        }
    }
    publications {
        maven(MavenPublication) {
            artifactId project.name

            from components.java

            artifact sourcesJar {
                classifier "sources"
            }

            artifact javadocJar {
                classifier "javadoc"
            }

            pom.withXml {
                def Node root = asNode()
                root.appendNode('name', project.name)
                root.appendNode('description', project.project_description)
                root.appendNode('url', 'https://github.com/agorapulse/groovy-aws-sdk')

                def issues = root.appendNode( 'issueManagement' )
                issues.appendNode('system', 'github')
                issues.appendNode('url', 'https://github.com/agorapulse/groovy-aws-sdk/issues')

                def scm = root.appendNode('scm')
                scm.appendNode('url', 'https://github.com/agorapulse/groovy-aws-sdk')
                scm.appendNode('connection', 'scm:git:https://github.com/agorapulse/groovy-aws-sdk.git')
                scm.appendNode('developerConnection', 'scm:git:https://github.com/agorapulse/groovy-aws-sdk.git')

                def license = root.appendNode('licenses').appendNode('license')
                license.appendNode('name', 'The Apache Software License, Version 2.0')
                license.appendNode('url', 'http://www.apache.org/licenses/LICENSE-2.0.txt')
                license.appendNode('distribution', 'repo')

                def dev = root.appendNode('developers').appendNode('developer')
                dev.appendNode('id', 'agorapulse')
                dev.appendNode('name', 'agorapulse')
                dev.appendNode('organization', 'agorapulse')
                dev.appendNode('organizationUrl', 'http://agorapulse.com')
            }
        }
    }
}

/*
 * Codenarc
 */

codenarc {
    toolVersion = '0.25.2'
}

codenarc {
    configFile rootProject.file('gradle/codenarc/codenarc.groovy')
}

codenarcTest {
    configFile rootProject.file('gradle/codenarc/codenarcTest.groovy')
}
