Skip to content

Commit

Permalink
Add support for groovy rules provider, fixes #19
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Feb 17, 2020
1 parent 17b8054 commit 4e684aa
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 2 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<groovyVersion>3.0.0</groovyVersion>
<jlineVersion>3.12.1</jlineVersion>
<junitVersion>4.12</junitVersion>
<logbackVersion>1.2.3</logbackVersion>
Expand Down Expand Up @@ -71,6 +72,11 @@
<artifactId>jline-terminal-jansi</artifactId>
<version>${jlineVersion}</version>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy</artifactId>
<version>${groovyVersion}</version>
</dependency>

<dependency>
<groupId>junit</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ static DependencyGraph<MavenProject> fromMaven(ProjectDependencyGraph graph, Str

if (rules != null) {
for (String rule : rules.split("\\s*;\\s*|\n")) {
if (rule.trim().isEmpty()) {
continue;
}
String[] parts = rule.split("\\s*->\\s*|\\s+before\\s+");
if (parts.length != 2) {
throw new IllegalArgumentException("Invalid rule: " + rule);
Expand Down
66 changes: 64 additions & 2 deletions src/main/java/org/jboss/fuse/mvnd/builder/SmartBuilder.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
package org.jboss.fuse.mvnd.builder;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import javax.enterprise.inject.Default;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;

import com.google.common.base.Joiner;
import groovy.lang.Binding;
import groovy.lang.GroovyShell;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.lifecycle.internal.LifecycleModuleBuilder;
import org.apache.maven.lifecycle.internal.ProjectBuildList;
Expand All @@ -37,6 +45,8 @@ public class SmartBuilder implements Builder {
public static final String PROP_PROFILING = "smartbuilder.profiling";
public static final String MVND_BUILDER_RULES = "mvnd.builder.rules";
public static final String MVND_BUILDER_RULE = "mvnd.builder.rule";
public static final String MVND_BUILDER_RULES_PROVIDER_URL = "mvnd.builder.rules.provider.url";
public static final String MVND_BUILDER_RULES_PROVIDER_SCRIPT = "mvnd.builder.rules.provider.script";

private final Logger logger = LoggerFactory.getLogger(getClass());

Expand All @@ -54,6 +64,58 @@ public void build(final MavenSession session, final ReactorContext reactorContex
ReactorBuildStatus reactorBuildStatus) throws ExecutionException, InterruptedException {

List<String> list = new ArrayList<>();

String providerScript = null;
String providerUrl = session.getTopLevelProject().getProperties()
.getProperty(MVND_BUILDER_RULES_PROVIDER_URL);
if (providerUrl != null) {
URL url;
try {
url = new URL(providerUrl);
} catch (MalformedURLException e) {
try {
url = new File(providerUrl).toURI().toURL();
} catch (MalformedURLException ex) {
url = null;
}
}
if (url == null) {
throw new ExecutionException("Bad syntax for " + MVND_BUILDER_RULES_PROVIDER_URL, null);
}
try (BufferedReader r = new BufferedReader(new InputStreamReader(url.openStream()))) {
StringBuilder sb = new StringBuilder();
char[] buf = new char[8192];
int l;
while ((l = r.read(buf)) >= 0) {
sb.append(buf, 0, l);
}
providerScript = sb.toString();
} catch (IOException e) {
throw new ExecutionException("Unable to read provider url " + MVND_BUILDER_RULES_PROVIDER_URL, e);
}
}
if (providerScript == null) {
providerScript = session.getTopLevelProject().getProperties()
.getProperty(MVND_BUILDER_RULES_PROVIDER_SCRIPT);
}
if (providerScript != null) {
Binding binding = new Binding();
GroovyShell shell = new GroovyShell(binding);
binding.setProperty("session", session);
Object result = shell.evaluate(providerScript);
if (result instanceof Iterable) {
for (Object r : (Iterable) result) {
list.add(r.toString());
}
} else if (result != null) {
list.add(result.toString());
} else {
throw new ExecutionException("The provider script did not return a valid string or string collection", null);
}
list.add(result.toString());
}


String topRule = session.getTopLevelProject().getProperties()
.getProperty(MVND_BUILDER_RULES);
if (topRule != null) {
Expand All @@ -75,7 +137,7 @@ public void build(final MavenSession session, final ReactorContext reactorContex

// log overall build info
final int degreeOfConcurrency = session.getRequest().getDegreeOfConcurrency();
logger.info("Task segments : " + Joiner.on(" ").join(taskSegments));
logger.info("Task segments : " + taskSegments.stream().map(Object::toString).collect(Collectors.joining(" ")));
logger.info("Build maximum degree of concurrency is " + degreeOfConcurrency);
logger.info("Total number of projects is " + graph.getProjects().count());

Expand Down

0 comments on commit 4e684aa

Please sign in to comment.