// Tasks to perform when no task is specified
defaultTasks 'clean', 'build', 'remap'

// Plugins used
apply plugin: 'java' // Specifies a java project
apply plugin: 'application' // Adds the 'run' task
apply plugin: 'project-report' // Adds project report tasks
apply plugin: 'eclipse' // Adds eclipse project tasks
apply plugin: 'idea' // Adds idea project tasks
apply plugin: 'com.github.johnrengelman.shadow' // Provides dependency shading
apply plugin: 'net.glowstone.remapper' // Provides method remapping
apply plugin: 'checkstyle' // Checks code style
apply from: 'etc/publish.gradle'

// Methods to determine detailed version string
def gitDescribe() {
    try {
        // determine git-describe output
        def stdout = new ByteArrayOutputStream()
        exec {
            commandLine 'git', 'describe', '--always', '--dirty=*'
            standardOutput = stdout
        }
        return stdout.toString().trim()
    } catch (e) {
        return "unknown"
    }
}

def getVersionName() {
    try {
        // determine git-describe output
        def version = gitDescribe()

        // add on jenkins or travis build information
        def jenkins_build = System.getenv("BUILD_NUMBER")
        def travis_build = System.getenv("TRAVIS_BUILD_NUMBER")
        if (jenkins_build != null) {
            version += "-b" + jenkins_build
        } else if (travis_build != null) {
            version += "-trv" + travis_build
        } else {
            version += "-dev"
        }

        return version
    } catch (e) {
        return "unknown-version"
    }
}

// Basic project information
group = 'net.glowstone'
version = '0.0.1-SNAPSHOT'
description = "Glowstone"
mainClassName = "net.glowstone.GlowServer"

// Extended project information
ext.url = 'https://github.com/GlowstoneMC/Glowstone'
ext.bukkitVersion = '1.8-R1-SNAPSHOT'
ext.gitDescribe = getVersionName()
ext.lombok = 'org.projectlombok:lombok:1.14.8'

// Minimum Java version required
sourceCompatibility = '1.7'
targetCompatibility = '1.7'

// Compile encoding
compileJava {
    options.encoding = 'UTF-8'
}

// Simple build script information
buildscript {
    repositories {
        jcenter()
        maven { url "http://repo.glowstone.net/content/groups/public/" }
    }
    dependencies {
        classpath 'com.github.jengelman.gradle.plugins:shadow:1.2.0'
        classpath 'net.glowstone:remapper:1.1'
        classpath 'org.ow2.asm:asm:5.0.3'
    }
}

// Configuration settings to check for new snapshots
configurations.all {
    resolutionStrategy.cacheChangingModulesFor 0, 'seconds' // Always check for new snapshots
}

// Repositories storing our dependencies
repositories {
    mavenLocal()
    mavenCentral()
    maven { url "https://oss.sonatype.org/content/repositories/public/" }
    maven { url "http://repo.glowstone.net/content/groups/public/" }
}

// Dependencies used by our project
dependencies {
    compile group: 'net.glowstone', name: 'glowkit', version: project.ext.bukkitVersion
    compile 'com.flowpowered:flow-networking:1.0.0-SNAPSHOT'
    compile 'jline:jline:2.11'
    //compile 'mysql:mysql-connector-java:5.1.14'
    //compile 'org.xerial:sqlite-jdbc:3.7.2'
    compile 'org.slf4j:slf4j-jdk14:1.7.7'
    compile lombok
    testCompile 'junit:junit:4.8.1'
}

// Checkstyle plugin settings
checkstyle {
    configFile = file('etc/checkstyle.xml')
}

// Shadow plugin settings
shadowJar {
    dependencies {
        exclude(dependency(lombok))
    }
}

// Remap plugin settings
remap {
    mappingFile = file('etc/remapper.txt')
    inputTask = shadowJar
}

// Eclipse project name
eclipse {
    project {
        name = 'Glowstone'
    }
}

// Jar manifest information
jar.manifest.mainAttributes(
        'Main-Class': mainClassName,
        'Implementation-Title': description,
        'Implementation-Version': ext.gitDescribe,
        'Implementation-Vendor': 'Glowstone Project',
        'Specification-Title': 'Bukkit/Glowkit',
        'Specification-Version': ext.bukkitVersion,
        'Specification-Vendor': 'Glowstone Project',
        'Sealed': true
)
