configurations {
    startScriptGenerator
}

dependencies {
    groovy libraries.groovy

    compile project(':core')
    compile project(':cli')
    compile project(':ui')
    compile project(':toolingApi')
    compile project(':native')

    compile libraries.slf4j_api

    startScriptGenerator project(':plugins')
}

useTestFixtures()

jar {
    manifest.mainAttributes('Main-Class': "org.gradle.launcher.GradleMain")
    doFirst {
        jar.manifest.mainAttributes('Class-Path': "${project(':core').jar.archivePath.name}")
    }
}

task startScripts(type: StartScriptGenerator) {
    startScriptsDir = new File("$buildDir/startScripts")
    classpath = configurations.startScriptGenerator
    launcherJar = jar.outputs.files
}

class StartScriptGenerator extends DefaultTask {
    File startScriptsDir

    @InputFiles
    FileCollection classpath

    @InputFiles
    FileCollection launcherJar

    @OutputFile
    File getShellScript() {
        return new File(startScriptsDir, "gradle")
    }

    @OutputFile
    File getBatchFile() {
        return new File(startScriptsDir, "gradle.bat")
    }

    @TaskAction
    def generate() {
        logging.captureStandardOutput(LogLevel.INFO)
        def factory = services.get(ClassLoaderFactory)
        def classLoader = factory.createIsolatedClassLoader(classpath.collect { it.toURI().toURL() })
        def generator = classLoader.loadClass('org.gradle.api.internal.plugins.StartScriptGenerator').newInstance()
        generator.applicationName = 'Gradle'
        generator.optsEnvironmentVar = 'GRADLE_OPTS'
        generator.exitEnvironmentVar = 'GRADLE_EXIT_CONSOLE'
        generator.mainClassName = 'org.gradle.launcher.GradleMain'
        generator.scriptRelPath = 'bin/gradle'
        generator.classpath = ["lib/${launcherJar.singleFile.name}" as String]
        generator.appNameSystemProperty = 'org.gradle.appname'
        generator.generateUnixScript(shellScript)
        generator.generateWindowsScript(batchFile)
    }
}