buildscript {
    repositories {
        mavenCentral()
    }
}

plugins {
    id 'pl.allegro.tech.build.axion-release' version '0.9.9'
    id 'com.moowork.node' version '0.8'
    id 'com.github.johnrengelman.shadow' version '1.2.0'
}

ext {
    prestoVersion = "0.145"
    if (System.properties['airpal.prestoVersion'] != null) {
        prestoVersion = System.properties['airpal.prestoVersion']
    }
}

apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'com.github.johnrengelman.shadow'
apply plugin: 'com.moowork.node'

group = 'com.airbnb.airpal'

description = """airpal"""

sourceCompatibility = 1.8
targetCompatibility = 1.8


repositories {
    mavenCentral()
    maven { url "http://oss.sonatype.org/content/repositories/snapshots" }
}

project.version = scmVersion.version

configurations {
    provided
}

idea.module.scopes.COMPILE.plus += [configurations.provided]

sourceSets {
    main {
        compileClasspath += [configurations.provided]
    }
}

dependencies {
    compile group: 'org.eclipse.jetty', name: 'jetty-util', version: '9.3.9.M1'
    compile group: 'org.eclipse.jetty', name: 'jetty-client', version: '9.3.9.M1'
    compile group: 'com.google.guava', name: 'guava', version: '18.0'
    compile group: 'org.slf4j', name: 'slf4j-api'
    compile group: 'ch.qos.logback', name: 'logback-classic'
    compile group: 'io.dropwizard', name: 'dropwizard-core', version:'1.0.0-rc2'
    compile group: 'io.dropwizard', name: 'dropwizard-jdbi', version:'1.0.0-rc2'
    compile group: 'io.dropwizard', name: 'dropwizard-assets', version:'1.0.0-rc2'
    compile group: 'io.dropwizard', name: 'dropwizard-views-freemarker', version:'1.0.0-rc2'
    compile group: 'io.dropwizard', name: 'dropwizard-metrics-graphite', version:'1.0.0-rc2'
    compile group: 'io.dropwizard.modules', name: 'dropwizard-flyway', version:'0.9.0-1'
    compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind'
    compile(group: 'com.facebook.presto', name: 'presto-client', version:prestoVersion) {
        exclude(group: 'org.eclipse.jetty')
        exclude(group: 'org.slf4j')
        exclude(group: 'ch.qos.logback')
    }
    compile(group: 'com.facebook.presto', name: 'presto-spi', version:prestoVersion) {
        exclude(group: 'ch.qos.logback')
    }
    compile(group: 'com.facebook.presto', name: 'presto-main', version:prestoVersion) {
        exclude(module: 'javax.servlet-api')
        exclude(group: 'org.eclipse.jetty')
        exclude(group: 'org.slf4j')
        exclude(group: 'ch.qos.logback')
    }
    compile group: 'com.facebook.presto', name: 'presto-parser', version:prestoVersion
    compile group: 'com.hubspot.rosetta', name: 'RosettaJdbi', version:'2.1-SNAPSHOT'
    compile group: 'com.opencsv', name: 'opencsv', version:'3.2'
    compile group: 'com.amazonaws', name: 'aws-java-sdk', version:'1.10.6'
    compile group: 'org.apache.shiro', name: 'shiro-web', version:'1.2.4'
    compile group: 'org.apache.shiro', name: 'shiro-guice', version:'1.2.4'
    compile group: 'org.secnod.dropwizard', name: 'dropwizard-shiro', version:'0.2.0'
    compile group: 'mysql', name: 'mysql-connector-java', version:'5.1.34'
    compile group: 'com.h2database', name: 'h2', version:'1.4.190'

    provided group: 'org.projectlombok', name: 'lombok', version:'1.12.2'

    testCompile group: 'junit', name: 'junit', version:'4.11'
    testCompile group: 'org.mockito', name: 'mockito-all', version:'1.10.8'
}

node {
    version    = '0.10.33'
    npmVersion = '2.1.6'

    download   = System.properties['airpal.useLocalNode'] == null
}

task cleanAssets {
  delete file("src/main/resources/assets/build")
}

task installAssets(type: NpmTask) {
    args = ['install']

    execOverrides {
        it.workingDir = file('src/main/resources/assets/').absolutePath
    }
}

task buildAssets(type: NodeTask) {
    script = file('src/main/resources/assets/node_modules/gulp/bin/gulp.js')
    args = ['browserify']

    execOverrides {
        it.workingDir = file('src/main/resources/assets/').absolutePath
    }
}

task buildTar(type: Tar) {
  baseName = project.name
  compression = Compression.GZIP
  extension = "tar.gz"

  into("$baseName-$version/lib") {
    from jar.archivePath
    from configurations.runtime
  }

  into("$baseName-$version/bin") {
    from "scripts/airpal"
    fileMode 0755
  }

  into("$baseName-$version") {
    from "reference.example.yml"
    rename { String fileName ->
      fileName.replace('example.', '')
    }
  }
}

shadowJar {
    transform(com.github.jengelman.gradle.plugins.shadow.transformers.ServiceFileTransformer)
    exclude 'META-INF/*.DSA'
    exclude 'META-INF/*.RSA'
    exclude 'META-INF/LICENSE'

    zip64 true
}

buildAssets.dependsOn installAssets
compileJava.dependsOn buildAssets
buildTar.dependsOn jar
clean.dependsOn cleanAssets
