All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.researchgate.release.cli.Executor.groovy Maven / Gradle / Ivy

Go to download

gradle-release is a plugin for providing a Maven-like release process to project using Gradle.

There is a newer version: 2.4.0
Show newest version
/*
 * This file is part of the gradle-release plugin.
 *
 * (c) Eric Berry
 * (c) ResearchGate GmbH
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

package net.researchgate.release.cli

import org.gradle.api.GradleException
import org.slf4j.Logger

class Executor {

    private Logger logger

    Executor(Logger logger = null) {
        this.logger = logger
    }

    String exec(
        Map options = [:],
        List commands
    ) {
        StringBuffer out = new StringBuffer()
        StringBuffer err = new StringBuffer()

        File directory = options['directory'] ? options['directory'] as File : null
        Map processEnv = options['env'] ? System.getenv() + (options['env'] as Map) : System.getenv()

        logger?.info("Running $commands in [$directory]")
        Process process = commands.execute(processEnv.collect { "$it.key=$it.value" }, directory)
        process.waitForProcessOutput(out, err)
        logger?.info("Running $commands produced output: [${out.toString().trim()}]")

        if (err.toString()) {
            def message = "Running $commands produced an error: [${err.toString().trim()}]"

            if (options['failOnStderr'] as boolean) {
                throw new GradleException(message)
            } else {
                logger?.warn(message)
            }
        }

        if (options['errorPatterns'] && [out, err]*.toString().any { String s -> (options['errorPatterns'] as List).any { s.contains(it) } }) {
            throw new GradleException("${ options['errorMessage'] ? options['errorMessage'] as String : 'Failed to run [' + commands.join(' ') + ']' } - [$out][$err]")
        }

        out.toString()
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy