// Import of plugins

plugins {
    id "org.jetbrains.dokka" version "0.9.17"
    id "com.jfrog.artifactory" version "4.5.4"
    id "maven-publish"
}

// Global variable declarations

ext{
    bintrayUser = System.getenv("BINTRAY_API_USER")
    bintrayKey = System.getenv("BINTRAY_API_KEY")
    buildNumber = System.getenv("TRAVIS_BUILD_NUMBER")
    snapshot = Boolean.valueOf(System.getProperty("snapshot"))
}

group = 'de.adesso'
def junitInsightsVersion = '1.1.0' + (snapshot ? "-SNAPSHOT" : "")
version = junitInsightsVersion
sourceCompatibility = 1.8

// Build tasks

jar {
    enabled = true
    baseName = 'junit-insights'
    from {
        configurations.runtime.collect { it.isDirectory() ? it : zipTree(it) }
    }
    exclude("main.js")
}

task sourcesJar(type: Jar) {
    group = 'build'
    baseName = 'junit-insights'
    classifier = 'sources'
    from sourceSets.main.allSource
}

jar.dependsOn sourcesJar

bootJar {
    enabled = false
}

// Test tasks

test {
    useJUnitPlatform()
    systemProperty 'de.adesso.junitinsights.enabled', 'false'
}

// JavaDoc tasks

dokka {
    outputFormat = 'html'
    outputDirectory = "$buildDir/javadoc"
}

// Publishing tasks

artifacts {
    archives sourcesJar
}

def pomConfig = {
    licenses {
        license {
            name "MIT License"
            url "https://opensource.org/licenses/MIT"
            distribution "repo"
        }
    }
    developers {
        developer {
            id "thombergs"
            name "Tom Hombergs"
            email "tom.hombergs@gmail.com"
        }
    }

    scm {
        url "https://github.com/adessoAG/junit-insights"
    }
}

publishing {
    publications {
        mavenPublication(MavenPublication) {
            from components.java
            artifact sourcesJar {
                classifier "sources"
            }
            groupId 'de.adesso'
            artifactId 'junit-insights'
            version version
            pom.withXml {
                def root = asNode()
                root.appendNode('description', 'Analyze and optimize your JUnit tests')
                root.appendNode('name', 'JUnit Insights')
                root.appendNode('url', 'https://github.com/adessoAG/junit-insights')
                root.children().last() + pomConfig
            }
        }
    }
}

artifactory {
    contextUrl = 'http://oss.jfrog.org'
    publish {
        repository {
            repoKey = 'oss-snapshot-local'
            username = bintrayUser
            password = bintrayKey
        }
        defaults {
            publications('mavenPublication')
            publishArtifacts = true
            publishPom = true
            properties = [
                    'build.number': buildNumber,
                    'build.name': 'junit-insights'
            ]
        }
    }
    resolve {
        repoKey = 'jcenter'
    }
    clientConfig.info.setBuildNumber(buildNumber)
    clientConfig.info.setBuildName('junit-insights')
}

// npm specific tasks

task npmInstall(type: Exec) {
    inputs.files(
            'src/frontend/package-lock.json',
            'src/frontend/webpack.config.js'
    )
    outputs.dir 'src/frontend/node_modules/'

    workingDir = 'src/frontend/'
    if (System.getProperty('os.name').toLowerCase().contains('windows')) {
        commandLine 'cmd', '/c', 'npm install'
    } else {
        commandLine 'sh', '-c', 'npm install'
    }
}

task npmRunBuild(type: Exec) {
    dependsOn npmInstall
    inputs.dir 'src/frontend/src'
    outputs.file 'src/main/resources/index.html'
    workingDir = 'src/frontend'
    if (System.getProperty('os.name').toLowerCase().contains('windows')) {
        commandLine 'cmd', '/c', 'npm run build'
    } else {
        commandLine 'sh', '-c', 'npm run build'
    }
}

// Dependency declarations

dependencies {
    compile('org.jetbrains.kotlin:kotlin-reflect')
    compile('com.google.code.gson:gson:2.7')

    compileOnly('org.junit.jupiter:junit-jupiter-api:5.1.0')
    compileOnly('org.junit.platform:junit-platform-launcher:1.1.0')
    compileOnly('org.springframework.boot:spring-boot-starter')
    compileOnly('org.springframework:spring-test')

    testCompile('org.junit.jupiter:junit-jupiter-engine:5.1.0')
    testCompile('org.junit.platform:junit-platform-launcher:1.1.0')
    testCompile('org.springframework.boot:spring-boot-starter')

    testImplementation('com.nhaarman.mockitokotlin2:mockito-kotlin:2.0.0')
}
