plugins {
    id "com.moowork.node" version "1.1.1"
    id "org.jetbrains.kotlin.jvm" version "1.1.2"
}

repositories {
    mavenCentral()
    maven { url "https://jitpack.io" }
}

group 'org.andrejs'
version '0.4'

apply plugin: 'kotlin'
apply plugin: 'application'
apply plugin: 'maven'

sourceCompatibility = 1.8

mainClassName = 'JvmMon'

dependencies {
    compile 'com.eclipsesource.j2v8:j2v8_macosx_x86_64:4.6.0'
    compile 'com.eclipsesource.j2v8:j2v8_linux_x86_64:4.6.0'
    compile 'com.eclipsesource.j2v8:j2v8_win32_x86_64:4.6.0'
    compile 'com.github.ajermakovics:jvmtop:0.8.2'
    compile 'com.github.ajermakovics:json:1.2'
    compile "org.jetbrains.kotlin:kotlin-stdlib-jre8:1.1.2"

    testCompile 'junit:junit:4.12'
    testCompile files(org.gradle.internal.jvm.Jvm.current().toolsJar)
    testCompile 'org.assertj:assertj-core:3.6.2'
}

jar {
    manifest {
        attributes 'Implementation-Title': archivesBaseName, 'Implementation-Version': version
        attributes "Main-Class": mainClassName
        attributes 'Class-Path': configurations.runtime.collect { it.name }.join(' ')
    }
}

startScripts {
    classpath = files('$APP_HOME/lib/' + jar.archiveName, '$APP_HOME/lib/tools.jar')
    doLast {
        def unixScriptFile    = file getUnixScript()
        unixScriptFile.text = file("src/main/resources/jvm-mon").text
        def windowsScriptFile    = file getWindowsScript()
        windowsScriptFile.text  = windowsScriptFile.text.replace('%APP_HOME%\\lib\\tools.jar', '%JAVA_HOME%\\lib\\tools.jar')
    }
}

tasks.withType(Tar){
    compression = Compression.GZIP
}

task npmDeps(type: NpmTask) {
    args = ['install', 'blessed', 'blessed-contrib']
    doFirst {
        def node_modules = file("node_modules")
        node_modules.mkdirs()
    }
}

task createNodeModules(dependsOn: npmDeps) {
    def node_modules = file("node_modules")
    outputs.dir node_modules
}

distributions {
    main {
        contents {
            from(createNodeModules) {
                into "node_modules"
            }
        }
    }
}
