import org.codehaus.groovy.control.CompilerConfiguration

apply plugin: 'groovy'

repositories {
    jcenter()
}

ScriptHolder holder = new ScriptHolder()
CompilerConfiguration cc = new CompilerConfiguration()
cc.setScriptBaseClass(DelegatingScript.class.name)
GroovyShell sh = new GroovyShell(Project.class.classLoader, new Binding(), cc)

//The build file for the main project
File projectBuildFile = file('../gradle/dependencies.gradle')

//Use this parse command because Groovy wants to use the file name as the classname
//which fails if your Gradle build file has been renamed to contain an invalid character (i.e. '-')
DelegatingScript script = (DelegatingScript)sh.parse(projectBuildFile.text.replaceAll('shadow', 'compile'), 'GradlePlugins')
script.setDelegate(holder)

//Resolve the project main Gradle file against our ScriptHolder
script.run()

//Class for holding the evaluation of a Gradle script
//You may need to add some extra methods here depending on what you have all placed in build.gradle
class ScriptHolder {
    Closure dependencies

    void dependencies(Closure c) {
        this.dependencies = c
    }

    void apply(Map map) {

    }
}

//Grab the dependencies closure and resolve it against
//the buildSrc project dependencies
//This effectively applies the same dependenices from build.gradle into buildSrc/build.gradle
//This is required so that when buildSrc is compiled it has the dependencies to compile the source code
def closure = holder.dependencies.clone()
closure.delegate = project.dependencies
closure()

sourceSets {
    main {
        java.srcDirs = ['../src/main/java']
        groovy.srcDirs = ['../src/main/groovy']
        resources.srcDirs = ['../src/main/resources']
    }
}

project.tasks.withType(JavaCompile) {
    // This will be the default in Gradle 5.0
    if (!options.compilerArgs.contains("-processor")) {
        options.compilerArgs << '-proc:none'
    }
}

project.tasks.withType(GroovyCompile) {
    // This will be the default in Gradle 5.0
    if (!options.compilerArgs.contains("-processor")) {
        options.compilerArgs << '-proc:none'
    }
}
