Search in sources :

Example 1 with BaseScript

use of org.commonjava.maven.ext.core.groovy.BaseScript in project pom-manipulation-ext by release-engineering.

the class GroovyManipulator method applyChanges.

/**
 * Apply the groovy script changes to the top level pom.
 */
@Override
public Set<Project> applyChanges(final List<Project> projects) throws ManipulationException {
    final GroovyState state = session.getState(GroovyState.class);
    if (!session.isEnabled() || !state.isEnabled()) {
        logger.debug(getClass().getSimpleName() + ": Nothing to do!");
        return Collections.emptySet();
    }
    final Set<Project> changed = new HashSet<>();
    for (File groovyScript : parseGroovyScripts(state.getGroovyScripts())) {
        GroovyShell shell = new GroovyShell();
        Script script;
        for (final Project project : projects) {
            if (project.isExecutionRoot()) {
                logger.info("Executing {} on {}", groovyScript, project);
                try {
                    script = shell.parse(groovyScript);
                    if (script instanceof BaseScript) {
                        ((BaseScript) script).setValues(session.getUserProperties(), projects, project);
                    } else {
                        throw new ManipulationException("Cannot cast " + groovyScript + " to a BaseScript to set values.");
                    }
                } catch (MissingMethodException e) {
                    try {
                        logger.debug("Failure when injecting into script {} ", FileUtils.readFileToString(groovyScript), e);
                    } catch (IOException e1) {
                        logger.debug("Unable to read script file {} for debugging! {} ", groovyScript, e1);
                    }
                    throw new ManipulationException("Unable to inject values into base script", e);
                } catch (CompilationFailedException e) {
                    try {
                        logger.debug("Failure when parsing script {} ", FileUtils.readFileToString(groovyScript), e);
                    } catch (IOException e1) {
                        logger.debug("Unable to read script file {} for debugging! {} ", groovyScript, e1);
                    }
                    throw new ManipulationException("Unable to parse script", e);
                } catch (IOException e) {
                    throw new ManipulationException("Unable to parse script", e);
                }
                try {
                    script.run();
                } catch (Exception e) {
                    throw new ManipulationException("Unable to parse script", e);
                }
                changed.add(project);
            }
        }
    }
    return changed;
}
Also used : Script(groovy.lang.Script) BaseScript(org.commonjava.maven.ext.core.groovy.BaseScript) CompilationFailedException(org.codehaus.groovy.control.CompilationFailedException) IOException(java.io.IOException) GroovyShell(groovy.lang.GroovyShell) MissingMethodException(groovy.lang.MissingMethodException) IOException(java.io.IOException) ManipulationException(org.commonjava.maven.ext.common.ManipulationException) CompilationFailedException(org.codehaus.groovy.control.CompilationFailedException) GroovyState(org.commonjava.maven.ext.core.state.GroovyState) Project(org.commonjava.maven.ext.common.model.Project) MissingMethodException(groovy.lang.MissingMethodException) BaseScript(org.commonjava.maven.ext.core.groovy.BaseScript) ManipulationException(org.commonjava.maven.ext.common.ManipulationException) File(java.io.File) HashSet(java.util.HashSet)

Aggregations

GroovyShell (groovy.lang.GroovyShell)1 MissingMethodException (groovy.lang.MissingMethodException)1 Script (groovy.lang.Script)1 File (java.io.File)1 IOException (java.io.IOException)1 HashSet (java.util.HashSet)1 CompilationFailedException (org.codehaus.groovy.control.CompilationFailedException)1 ManipulationException (org.commonjava.maven.ext.common.ManipulationException)1 Project (org.commonjava.maven.ext.common.model.Project)1 BaseScript (org.commonjava.maven.ext.core.groovy.BaseScript)1 GroovyState (org.commonjava.maven.ext.core.state.GroovyState)1