buildscript {
    ext.kotlin_version = '1.2.31'
    ext.coroutines_version = '0.22.5'
    ext.dokka_version = '0.9.16'
    ext.frontend_plugin_version = '0.0.29'
    ext.detekt_version = '1.0.0.RC6-4'
    ext.production = (findProperty('prod') ?: 'false') == 'true'
    ext.npmdeps = new File("npm.dependencies").getText()

    repositories {
        jcenter()
        maven { url = 'https://dl.bintray.com/kotlin/kotlin-eap' }
        maven { url = "https://plugins.gradle.org/m2/" }
        maven { url = 'https://bintray.com/kotlin/kotlin-eap/dokka' }
    }

    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlin_version}"
        classpath "org.jetbrains.kotlin:kotlin-frontend-plugin:${frontend_plugin_version}"
        classpath "gradle.plugin.io.gitlab.arturbosch.detekt:detekt-gradle-plugin:${detekt_version}"
        classpath "org.jetbrains.dokka:dokka-gradle-plugin:${dokka_version}"
    }
}

plugins {
    id "maven-publish"
    id "com.jfrog.bintray" version "1.7.3"
}

if (!project.gradle.startParameter.taskNames.contains("dokka")) {
    apply plugin: 'kotlin2js'
} else {
    apply plugin: 'kotlin'
}
apply plugin: 'org.jetbrains.kotlin.frontend'
apply plugin: "io.gitlab.arturbosch.detekt"
apply plugin: 'org.jetbrains.dokka'

group = 'pl.treksoft'
version = '0.0.11'

repositories {
    jcenter()
    maven { url = 'https://dl.bintray.com/gbaldeck/kotlin' }
    maven { url = 'https://dl.bintray.com/rjaros/kotlin' }
}

dependencies {
    if (!project.gradle.startParameter.taskNames.contains("dokka")) {
        compile "org.jetbrains.kotlin:kotlin-stdlib-js:$kotlin_version"
        compile "org.jetbrains.kotlin:kotlin-test-js:$kotlin_version" // for now only compile configuration is supported
    } else {
        compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
        compile "org.jetbrains.kotlin:kotlin-test:$kotlin_version" // for now only compile configuration is supported
    }
    compile "org.jetbrains.kotlinx:kotlinx-coroutines-core-js:$coroutines_version"
    compile "com.github.snabbdom:snabbdom-kotlin:0.1.1"
    compile "pl.treksoft:navigo-kotlin:0.0.2"
    compile "pl.treksoft:jquery-kotlin:0.0.3"
    compile "pl.treksoft:kotlin-observable-js:0.0.3"
}

kotlinFrontend {
    npm {
        dependency("css-loader")
        dependency("style-loader")
        dependency("less")
        dependency("less-loader")
        dependency("imports-loader")
        dependency("uglifyjs-webpack-plugin")
        dependency("file-loader")
        dependency("url-loader")
        dependency("expose-loader")
        dependency("jquery", "3.2.1")
        dependency("fecha", "2.3.2")
        dependency("snabbdom", "0.7.1")
        dependency("snabbdom-virtualize", "0.7.0")
        dependency("navigo", "7.0.0")
        npmdeps.eachLine { line ->
            def (name, version) = line.tokenize(" ")
            dependency(name, version)
        }
        devDependency("karma", "2.0.0")
        devDependency("karma-qunit", "1.2.1")
        devDependency("karma-chrome-launcher")
        devDependency("karma-firefox-launcher")
        devDependency("karma-webpack", "2.0.9")
        devDependency("webpack", "3.11.0")
        devDependency("webpack-dev-server", "2.11.0")
    }

    webpackBundle {
        bundleName = "main"
        contentPath = file('src/main/web')
    }

    karma {
        plugins = ["karma-chrome-launcher", "karma-firefox-launcher"]
//        browsers = [ "Chrome", "Firefox" ]
    }

    define "PRODUCTION", production

}

task cleanLibs(type: Delete) {
    delete 'build/js', 'build/libs'
}

if (project.gradle.startParameter.taskNames.contains("jar")) {
    compileKotlin2Js.dependsOn 'cleanLibs'
}

jar {
    duplicatesStrategy = DuplicatesStrategy.EXCLUDE
    excludes = ["package.json"]
}

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

detekt {
    version = "${detekt_version}"
    profile("main") {
        input = "$projectDir/src/main/kotlin"
        config = "$projectDir/detekt.yml"
        filters = ".*test.*,.*/resources/.*,.*/tmp/.*"
    }
}

dokka {
    includes = ['Module.md']
    classpath = [new File("dokka/kvision-dokka-helper.jar")]
    outputFormat = 'html'
    outputDirectory = "$buildDir/kdoc"
    reportUndocumented = false
    impliedPlatforms = ["JS"]
}

if (!project.gradle.startParameter.taskNames.contains("dokka")) {

    compileKotlin2Js {
        kotlinOptions.metaInfo = true
        kotlinOptions.outputFile = "$project.buildDir.path/js/${project.name}.js"
        kotlinOptions.sourceMap = !production
        kotlinOptions.moduleKind = 'commonjs'
    }

    compileTestKotlin2Js {
        kotlinOptions.metaInfo = true
        kotlinOptions.outputFile = "$project.buildDir.path/js-tests/${project.name}-tests.js"
        kotlinOptions.sourceMap = !production
        kotlinOptions.moduleKind = 'commonjs'
    }

}

task copyResources(type: Copy) {
    from "src/main/resources"
    into file(buildDir.path + "/js")
}

task copyResourcesForTests(type: Copy) {
    from "src/main/resources"
    into file(buildDir.path + "/js-tests/")
}

if (!project.gradle.startParameter.taskNames.contains("dokka")) {
    afterEvaluate {
        tasks.getByName("webpack-bundle") { dependsOn(copyResources) }
        tasks.getByName("webpack-run") { dependsOn(copyResources) }
        tasks.getByName("karma-start") { dependsOn(copyResources, copyResourcesForTests) }
    }
}

publishing {
    publications {
        maven(MavenPublication) {
            from components.java
            pom.withXml {
                asNode().dependencies.'*'.findAll() {
                    it.artifactId.text() == 'kotlin-test-js'
                }.each() {
                    it.scope*.value = 'test'
                }
            }
        }
    }
}

model {
    tasks.generatePomFileForMavenPublication {
        destination = file("$buildDir/libs/${project.name}-${version}.pom")
    }
}

bintray {
    user = findProperty('buser')
    key = findProperty('bkey')
    pkg {
        repo = 'kotlin'
        name = "${project.name}"
        licenses = ['MIT']
        vcsUrl = "https://github.com/rjaros/${project.name}.git"
        version {
            name = "${project.version}"
            desc = 'Object oriented Web UI framework for Kotlin/JS'
            released = new Date()
        }
    }
    filesSpec {
        from "${project.buildDir}/libs/"
        into "pl/treksoft/${project.name}/${project.version}"
    }
}

bintrayUpload {
    dependsOn "generatePomFileForMavenPublication"
}