Command line tool

I can see a few tools for checking build status, starting build and the ilk. What would be really useful to me is a tool that runs the build locally. It could assume that all the tools and dependencies are installed (as it’s a dev machine!).

This would help test build scripts I’ve put together a Groovy script, but I’d like something a bit more polished:

#!/usr/bin/env groovy
@Grapes(
        @Grab(group = 'org.yaml', module = 'snakeyaml', version = '1.18')
)
import org.yaml.snakeyaml.Yaml

def log(String line) {
    println "\033[0;1m$line \033[0m"
}

def exec(String command) {
    def process = new ProcessBuilder("bash", "-c", command)
            .redirectErrorStream(true)
            .start()

    def debug = []
    process.inputStream.eachLine {
        def s = it.substring(0, Math.min(it.length(), 80))
        print "                                                                                \r"
        print "$s\r"
        System.out.flush()
        debug.add(s)
    }
    print "                                                                                \r"
    process.waitFor()
    if (process.exitValue() != 0) {
        debug.each {println it}
        System.exit 1
    }
}

input = new FileInputStream(new File("circle.yml"))

def doc = new Yaml().load(input)

if (doc.compile == null) {
    doc.compile = [:]
}

if (doc.compile.override == null) {
    doc.compile.override = ["mvn clean compile"]
}

args.each { subcommand ->
    switch (subcommand) {
        case "compile":
            log "compile:"
            log "  override:"
            doc.compile.override.each {
                log "  - $it"
                exec(it)
            }
            break
        case "test":
            log "test:"
            log "  override:"
            doc.test.override.each {
                log "  - $it"
                exec(it)
            }
            break
        case "deploy":
            log "deploy:"
            doc.deployment.each { key, value ->
                log "  $key:"
                log "    commands:"
                value.commands.each {
                    log "     - $it"
                    exec(it)
                }
            }
            break
        default:
            throw new IllegalArgumentException(subcommand)
    }
}
1 Like

Thank you for sharing your script!

I’ve relocated your post to our Community/Hacks category so that other CircleCI hackers may find it more easily :slight_smile: