package toGit

import org.codehaus.groovy.control.CompilerConfiguration
import org.slf4j.LoggerFactory
import toGit.migration.MigrationManager

public class Executor {

    final static log = LoggerFactory.getLogger(this.class)

    GroovyShell execute(String[] args) {
        File commandFile = null
        if (!args) {
            log.error("Missing command file parameter")
            System.exit(1)
        } else {
            commandFile = new File(args[0])
            if (!commandFile.exists() || commandFile.isDirectory()) {
                log.error("Cannot find command file '$commandFile'")
                System.exit(1)
            }
        }

        def scriptDir = new File(getClass().protectionDomain.codeSource.location.path).parent
        def config = new CompilerConfiguration()
        config.setClasspath(scriptDir)
        config.scriptBaseClass = 'toGit.ScriptBase'
        def binding = new Binding(args as String[])
        if (args.length > 1) {
            parseParameters(binding, args[1..-1] as String[])
        }

        MigrationManager.instance.reset()
        def shell = new GroovyShell(binding, config)
        shell.evaluate(commandFile)
        return shell
    }

    static def parseParameters(Binding binding, String[] args) {
        args.each {
            if (!it.contains('=')) return
            def split = it.split('=')
            binding.setVariable(split[0], split[1..-1].join('='))
        }
    }
}

package toGit

import org.codehaus.groovy.control.CompilerConfiguration
import org.slf4j.LoggerFactory
import toGit.migration.MigrationManager

public class Executor {

    final static log = LoggerFactory.getLogger(this.class)

    GroovyShell execute(String[] args) {
        if (!args) {
            log.error("Missing migration script parameter")
            System.exit(1)
        }

        File migrationScript = new File(args[0])

        if (!migrationScript.exists() || migrationScript.isDirectory()) {
            log.error("Cannot find migration script '$migrationScript'")
            System.exit(1)
        }

        def config = new CompilerConfiguration(
                scriptBaseClass: 'toGit.ScriptBase',
                classpath: migrationScript.absoluteFile.parent
        )

        def binding = new Binding(args as String[])
        if (args.length > 1) {
            parseParameters(binding, args[1..-1] as String[])
        }

        MigrationManager.instance.reset()
        def shell = new GroovyShell(this.class.classLoader, binding, config)
        shell.evaluate(migrationScript)
        return shell
    }

    static def parseParameters(Binding binding, String[] args) {
        args.each {
            if (!it.contains('=')) return
            def split = it.split('=')
            binding.setVariable(split[0], split[1..-1].join('='))
        }
    }
}

Fix 127. Add script location to classpath.
Fix 127. Add script location to classpath.
