Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inject a log variable into executed scripts #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,23 @@

import java.io.File;

import org.apache.maven.plugin.logging.Log;
import org.apache.maven.project.MavenProject;
import org.slf4j.Logger;
import org.slf4j.impl.MavenLoggerFactory;

/**
* groovy script executor which exposes maven project
*/
public class CarrotGroovyRunner {

private final MavenProject project;

private final Log log;

public CarrotGroovyRunner(final MavenProject project) {
public CarrotGroovyRunner(final MavenProject project, Log log) {
this.project = project;
this.log = log;
}

public Binding binding() {
Expand All @@ -37,7 +43,7 @@ public Binding binding() {

public Object execute(final File script) throws Exception {

final GroovyShell shell = new GroovyShell(binding());
final GroovyShell shell = createShell();

final Object result = shell.evaluate(script);

Expand All @@ -47,12 +53,25 @@ public Object execute(final File script) throws Exception {

public Object execute(final String script) throws Exception {

final GroovyShell shell = new GroovyShell(binding());
final GroovyShell shell = createShell();

final Object result = shell.evaluate(script);

return result;

}

public GroovyShell createShell() {

final GroovyShell shell = new GroovyShell(binding());
shell.getContext().setProperty("log", createLogger());
return shell;
}

private Logger createLogger() {
String loggerName = String.format("%s.%s.Script", project.getGroupId(),
project.getArtifactId());
return MavenLoggerFactory.getLogger(loggerName, log);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ public abstract class GroovyBase extends CarrotMojo {
* @parameter default-value=""
*/
protected String groovyText;

/**
* whether to log the script text during the build (The script might be long,
* or contain sensitive data)
*
* @parameter default-value="true"
*/
protected boolean logText;

/**
* should load all system properties from {@link System#getenv()}
Expand Down Expand Up @@ -72,7 +80,7 @@ protected CarrotGroovyRunner newRunner() throws Exception {

final MavenProject project = new MavenProjectAdaptor(mavenProps);

final CarrotGroovyRunner runner = new CarrotGroovyRunner(project);
final CarrotGroovyRunner runner = new CarrotGroovyRunner(project, getLog());

return runner;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {

if (groovyText != null && groovyText.length() != 0) {

getLog().info("groovy exec text : " + singleLine(groovyText));
getLog().info("groovy exec text : " + (logText? singleLine(groovyText): "[suppressed]"));

runner.execute(groovyText);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
import java.util.Properties;

import org.apache.commons.io.FileUtils;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.project.MavenProject;
import org.junit.Test;
import org.mockito.Mockito;

public class TestCarrotGroovyRunner {

Expand All @@ -31,7 +33,7 @@ public void testFile() throws Exception {

when(project.getProperties()).thenReturn(properties);

final CarrotGroovyRunner runner = new CarrotGroovyRunner(project);
final CarrotGroovyRunner runner = new CarrotGroovyRunner(project, Mockito.mock(Log.class));

final File script = new File("./src/test/resources/script.groovy");

Expand All @@ -40,7 +42,7 @@ public void testFile() throws Exception {
assertEquals(result, "result");

assertEquals(properties.get("prop-key"), "prop-value-2");

}

@Test
Expand All @@ -55,7 +57,7 @@ public void testString() throws Exception {

when(project.getProperties()).thenReturn(properties);

final CarrotGroovyRunner runner = new CarrotGroovyRunner(project);
final CarrotGroovyRunner runner = new CarrotGroovyRunner(project, Mockito.mock(Log.class));

final File file = new File("./src/test/resources/script.groovy");

Expand Down
2 changes: 2 additions & 0 deletions carrot-maven-aws-plugin/src/test/resources/script.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,7 @@ def result = "result"

println "result = $result"

log.info "testing the logger"

return result