<%#
 Copyright 2013-2017 the original author or authors from the JBooter project.

 This file is part of the JBooter project, see https://jbooter.github.io/
 for more information.

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-%>
apply plugin: 'scala'

sourceSets {
    test {
        scala {
            srcDirs = ['<%= TEST_DIR %>gatling/simulations']
            output.classesDir = '<%= BUILD_DIR %>test-classes'
        }
    }
}

task manifestJar(dependsOn:'compileTestScala',type: Jar) {
    dependsOn configurations.testCompile
    archiveName 'gatlingBooter.jar'
    doFirst {
        manifest {
            // uri is just needed for Windows-compatibility
            attributes 'Class-Path': configurations.testCompile.files.collect{ project.uri(it) }.join(' ')
        }
    }
}

task gatlingRun(dependsOn:'manifestJar') {
    group = 'gatling'
    description = 'Run a single Gatling simulation. You must write the name of your Gatling simulation.'

    doLast {
        def String gatlingSimulationClass = "WriteTheNameOfYourGatlingSimulation"
        if (project.hasProperty('gatlingSimulationClass')) {
            gatlingSimulationClass = project.property('gatlingSimulationClass')
        }

        createGatlingRunTask(gatlingSimulationClass).dependsOn('manifestJar').execute()
    }
}

task gatlingRunAll(dependsOn: 'manifestJar') {
    group = 'gatling'
    description = 'Run all available Gatling simulations.'

    doLast {
        FileTree tree = fileTree(dir: './src/test/gatling/simulations')
        tree.include '**/*.scala'
        tree.each {File file ->
            def simulationClassName = file.name.replaceFirst(".scala", "")
            createGatlingRunTask(simulationClassName).dependsOn('manifestJar').execute();
        }
    }
}

def createGatlingRunTask(def gatlingSimulationClassName) {
    return tasks.create("gatlingRun${gatlingSimulationClassName}", JavaExec) {

        standardInput = System.in

        final def sourceSet = sourceSets.test

        def String gatlingDataFolder = "$project.rootDir.absolutePath/src/test/gatling/data"
        def String gatlingReportsFolder = "$project.buildDir.absolutePath/reports/gatling"
        def String gatlingBodiesFolder = "$project.rootDir.absolutePath/src/test/gatling/bodies"
        def String gatlingSimulationsFolder = "$project.rootDir.absolutePath/src/test/gatling/simulations"

        classpath sourceSet.output + files(manifestJar.archivePath) + files("src/test/gatling/conf")
        main = "io.gatling.app.Gatling"

        environment GATLING_HOME:''

        args '-df', gatlingDataFolder
        args '-rf', gatlingReportsFolder
        args '-bdf', gatlingBodiesFolder
        args '-sf', gatlingSimulationsFolder
        args '-m'
        args '-s', gatlingSimulationClassName
    }
}
