Skip to content

Commit

Permalink
Merge branch '__rultor'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed Sep 10, 2022
2 parents 1e94017 + 1c52c20 commit d26976a
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 12 deletions.
39 changes: 30 additions & 9 deletions eo-maven-plugin/src/main/java/org/eolang/maven/GmiMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
import java.nio.file.Paths;
import java.util.Collection;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
Expand Down Expand Up @@ -203,16 +205,12 @@ public final class GmiMojo extends SafeMojo {
* @implNote {@code property} attribute is omitted for collection
* properties since there is no way of passing it via command line.
* @checkstyle MemberNameCheck (15 lines)
* @todo #1146:30min At the moment we don't support pattern matching, but
* double-star means "everything". Let's implement proper matching,
* where "org.eolang.int" would be matched by "org.*.int" and by
* "org.**". The same is true about gmiExclude, let's fix it too.
*/
@Parameter
private Set<String> gmiIncludes = new SetOf<>("**");

/**
* List of object names to participate in GMI generation.
* List of object names which are excluded from GMI generation.
* @implNote {@code property} attribute is omitted for collection
* properties since there is no way of passing it via command line.
* @checkstyle MemberNameCheck (15 lines)
Expand All @@ -238,9 +236,15 @@ public void exec() throws IOException {
final Path home = this.targetDir.toPath().resolve(GmiMojo.DIR);
int total = 0;
int instructions = 0;
final Set<Pattern> includes = this.gmiIncludes.stream()
.map(i -> Pattern.compile(GmiMojo.createMatcher(i)))
.collect(Collectors.toSet());
final Set<Pattern> excludes = this.gmiExcludes.stream()
.map(i -> Pattern.compile(GmiMojo.createMatcher(i)))
.collect(Collectors.toSet());
for (final Tojo tojo : tojos) {
final String name = tojo.get(Tojos.KEY);
if (this.exclude(name)) {
if (this.exclude(name, includes, excludes)) {
continue;
}
final Path gmi = new Place(name).make(home, "gmi");
Expand Down Expand Up @@ -275,18 +279,35 @@ public void exec() throws IOException {
}
}

/**
* Creates a regular expression out of gmiInclude string.
* @param pattern String from gmiIncludes
* @return Created regular expression
*/
private static String createMatcher(final String pattern) {
return pattern
.replace("**", "[A-Za-z0-9.]+?")
.replace("*", "[A-Za-z0-9]+");
}

/**
* Exclude this EO program from processing?
* @param name The name
* @param includes Patterns for gmis to be included
* @param excludes Patterns for gmis to be excluded
* @return TRUE if to exclude
*/
private boolean exclude(final String name) {
private boolean exclude(
final String name,
final Set<Pattern> includes,
final Set<Pattern> excludes
) {
boolean exclude = false;
if (!this.gmiIncludes.contains(name) && !this.gmiIncludes.contains("**")) {
if (includes.stream().noneMatch(p -> p.matcher(name).matches())) {
Logger.debug(this, "Excluding %s due to gmiIncludes option", name);
exclude = true;
}
if (this.gmiExcludes.contains(name) || this.gmiExcludes.contains("**")) {
if (excludes.stream().anyMatch(p -> p.matcher(name).matches())) {
Logger.debug(this, "Excluding %s due to gmiExcludes option", name);
exclude = true;
}
Expand Down
15 changes: 12 additions & 3 deletions eo-maven-plugin/src/test/java/org/eolang/maven/GmiMojoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.util.List;
import java.util.Map;
import org.cactoos.io.ResourceOf;
import org.cactoos.set.SetOf;
import org.cactoos.text.TextOf;
import org.cactoos.text.UncheckedText;
import org.hamcrest.Description;
Expand Down Expand Up @@ -67,7 +68,7 @@ void bigSlowTest() throws Exception {
}
program.append("[x y z] > foo\n");
}
final XML graph = GmiMojoTest.toGraph(program.toString());
final XML graph = GmiMojoTest.toGraph(program.toString(), "**");
MatcherAssert.assertThat(
".foo .foo",
new GmiMojoTest.ExistsIn(graph)
Expand All @@ -89,7 +90,12 @@ void testPacks(final String pack) throws Exception {
map.get("skip") == null,
String.format("%s is skipped", pack)
);
final XML graph = GmiMojoTest.toGraph(map.get("eo").toString());
final Object value = map.get("inclusion");
String inclusion = "**";
if (value != null) {
inclusion = value.toString().substring(1, value.toString().length() - 1);
}
final XML graph = GmiMojoTest.toGraph(map.get("eo").toString(), inclusion);
final Collection<Executable> assertions = new LinkedList<>();
for (final String loc : (Iterable<String>) map.get("locators")) {
assertions.add(
Expand Down Expand Up @@ -130,11 +136,13 @@ private static Collection<String> yamls(final String path,

/**
* Convert EO source to Graph.
*
* @param code Code in EO
* @param inclusion Value of gmiIncludes property
* @return The graph
* @throws IOException If fails
*/
private static XML toGraph(final String code) throws IOException {
private static XML toGraph(final String code, final String inclusion) throws IOException {
final Path temp = Files.createTempDirectory("eo");
final Path src = temp.resolve("foo/main.eo");
new Save(code, src).save();
Expand Down Expand Up @@ -162,6 +170,7 @@ private static XML toGraph(final String code) throws IOException {
.with("targetDir", target.toFile())
.with("foreign", foreign.toFile())
.with("foreignFormat", "csv")
.with("gmiIncludes", new SetOf<>(inclusion))
.execute();
return new XMLDocument(
target.resolve(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
inclusion:
- foo.*
locators:
- .a .x .α0 Δ=bytes/00-00-00-00-00-00-00-2A
- .b .y .α0 Δ=bytes/00-00-00-00-00-00-01-00
eo: |
[f] > a
b 42 > x
[t] > b
a 256 > y

1 comment on commit d26976a

@0pdd
Copy link

@0pdd 0pdd commented on d26976a Sep 10, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 1146-f869e4b8 disappeared from eo-maven-plugin/src/main/java/org/eolang/maven/GmiMojo.java), that's why I closed #1150. Please, remember that the puzzle was not necessarily removed in this particular commit. Maybe it happened earlier, but we discovered this fact only now.

Please sign in to comment.