Skip to content

Commit

Permalink
Merge branch '__rultor'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed Jul 14, 2023
2 parents ec7ef06 + 5dc8497 commit 9a7619f
Show file tree
Hide file tree
Showing 13 changed files with 241 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ public void exec() throws IOException {
if (this.central == null) {
this.central = new Central(this.project, this.session, this.manager);
}
String before = this.tojos.status();
String before = this.scopedTojos().status();
int cycle = 0;
final Moja<?>[] mojas = {
new Moja<>(ParseMojo.class),
Expand All @@ -211,7 +211,7 @@ public void exec() throws IOException {
for (final Moja<?> moja : mojas) {
moja.copy(this).execute();
}
final String after = this.tojos.status();
final String after = this.scopedTojos().status();
++cycle;
if (Logger.isInfoEnabled(this)) {
Logger.info(
Expand Down
88 changes: 68 additions & 20 deletions eo-maven-plugin/src/main/java/org/eolang/maven/DiscoverMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@
import java.nio.file.Path;
import java.util.Collection;
import java.util.HashSet;
import java.util.TreeSet;
import java.util.List;
import java.util.Set;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.cactoos.iterable.Filtered;
import org.cactoos.list.ListOf;
import org.cactoos.set.SetOf;
import org.eolang.maven.tojos.ForeignTojo;
import org.eolang.maven.util.Rel;

Expand Down Expand Up @@ -94,26 +96,14 @@ public void exec() throws IOException {
private Collection<String> discover(final Path file)
throws FileNotFoundException {
final XML xml = new XMLDocument(file);
final Collection<String> names = new TreeSet<>(
new ListOf<>(
new Filtered<>(
obj -> !obj.isEmpty(),
xml.xpath(
String.join(
" ",
"//o[",
"not(starts-with(@base,'.'))",
" and @base != 'Q'",
" and @base != '^'",
" and @base != '$'",
" and @base != '&'",
" and not(@ref)",
"]/@base"
)
)
)
)
final Collection<String> names = DiscoverMojo.names(
xml, this.xpath(false)
);
if (this.withVersions) {
names.addAll(
DiscoverMojo.names(xml, this.xpath(true))
);
}
if (!xml.nodes("//o[@vararg]").isEmpty()) {
names.add("org.eolang.tuple");
}
Expand All @@ -131,4 +121,62 @@ private Collection<String> discover(final Path file)
return names;
}

/**
* Xpath for selecting objects from given xml.
* @param versioned Select with versions or not.
* @return Xpath as list of strings
* @todo #1602:30min Simplify xpath. Current implementation for building
* xpath with and without versions is quite ugly. For some reason
* if we try to take `/concat(@base,'|',@ver)` and there are object without
* attribute `ver` - xpath returns nothing. So we need to take `/@base`
* from objects where attribute `ver` is not present in both cases and
* then if flag `withVersions` is `true` - take `concat(@base,'|',@ver)`
* from objects attribute `ver` is present.
*/
private List<String> xpath(final boolean versioned) {
final List<String> xpath = new ListOf<>(
"//o[",
"not(starts-with(@base,'.'))",
"and @base != 'Q'",
"and @base != '^'",
"and @base != '$'",
"and @base != '&'",
"and not(@ref)"
);
final List<String> tail;
if (versioned) {
tail = new ListOf<>(
"and @ver",
"]/concat(@base,'|',@ver)"
);
} else {
tail = new ListOf<>();
if (this.withVersions) {
tail.add("and not(@ver)");
}
tail.add("]/@base");
}
xpath.addAll(tail);
return xpath;
}

/**
* Get unique list of object names from given XML by provided xpath.
* @param xml XML.
* @param xpath Xpath.
* @return List of object names.
*/
private static Set<String> names(final XML xml, final List<String> xpath) {
return new SetOf<>(
new Filtered<>(
obj -> !obj.isEmpty(),
xml.xpath(
String.join(
" ",
xpath
)
)
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,6 @@ public void exec() throws IOException {
.withVersion(ParseMojo.ZERO)
.withVer(ParseMojo.ZERO);
Logger.debug(this, "EO source %s registered", name);
if (this.external != null) {
this.externalTojos
.add(name)
.withSource(file.toAbsolutePath())
.withVersion(ParseMojo.ZERO);
Logger.debug(this, "EO source %s registered (external)", name);
}
}
Logger.info(
this, "Registered %d EO sources from %s to %s, included %s, excluded %s",
Expand Down
25 changes: 23 additions & 2 deletions eo-maven-plugin/src/main/java/org/eolang/maven/SafeMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,26 @@ abstract class SafeMojo extends AbstractMojo {
@Parameter(property = "eo.cache")
protected Path cache = Paths.get(System.getProperty("user.home")).resolve(".eo");

/**
* Used for object versioning implementation.
* If set to TRUE, external tojos are used instead of foreign ones and all
* inherited Mojos behave a bit differently.
* @todo #1602:30min Remove the flag when objection versioned is
* implemented. The variable is used for implementation of object
* versioning. It allows to use external tojos instead of foreign in Mojos.
* for the test purposes. When object versioning is implemented there
* will be no need for that variable
* @checkstyle VisibilityModifierCheck (10 lines)
* @checkstyle MemberNameCheck (10 lines)
*/
@Parameter(property = "eo.withVersions", defaultValue = "false")
protected boolean withVersions;

/**
* Cached tojos.
* @checkstyle VisibilityModifierCheck (5 lines)
*/
protected final ForeignTojos tojos = new ForeignTojos(
private final ForeignTojos tojos = new ForeignTojos(
() -> Catalogs.INSTANCE.make(this.foreign.toPath(), this.foreignFormat),
() -> this.scope
);
Expand Down Expand Up @@ -318,7 +333,13 @@ public final void execute() throws MojoFailureException, MojoExecutionException
* @checkstyle AnonInnerLengthCheck (100 lines)
*/
protected final ForeignTojos scopedTojos() {
return this.tojos;
final ForeignTojos tjs;
if (this.external != null && this.withVersions) {
tjs = this.externalTojos;
} else {
tjs = this.tojos;
}
return tjs;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.util.stream.Collectors;
import org.cactoos.io.ResourceOf;
import org.eolang.maven.tojos.ForeignTojo;
import org.eolang.maven.tojos.ForeignTojos;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -89,4 +90,83 @@ void discoversForDifferentScopes(@TempDir final Path tmp) throws IOException {
Matchers.is(true)
);
}

@Test
void discoversWithVersions(@TempDir final Path tmp) throws IOException {
final FakeMaven maven = new FakeMaven(tmp)
.with("withVersions", true)
.withProgram(
"+alias org.eolang.txt.sprintf\n",
"[] > main",
" seq > @",
" QQ.io.stdout|0.29.1",
" sprintf|0.28.5",
" \"Hello world\"",
" TRUE",
" nop"
)
.execute(new FakeMaven.Discover());
final String sprintf = "org.eolang.txt.sprintf|0.28.5";
final String stdout = "org.eolang.io.stdout|0.29.1";
final String nop = "org.eolang.nop";
final ForeignTojos tojos = maven.externalTojos();
MatcherAssert.assertThat(
String.format(
"External tojos should have contained %s object after discovering, but they didn't",
sprintf
),
tojos.contains(sprintf),
Matchers.is(true)
);
MatcherAssert.assertThat(
String.format(
"External tojos should have not contained %s object after discovering, but they did",
stdout
),
tojos.contains(stdout),
Matchers.is(false)
);
MatcherAssert.assertThat(
String.format(
"External tojos should have contained %s object after discovering, but they didn't",
nop
),
tojos.contains(nop),
Matchers.is(true)
);
}

@Test
void doesNotDiscoverWithVersions(@TempDir final Path tmp) throws IOException {
final FakeMaven maven = new FakeMaven(tmp)
.with("withVersions", false)
.withProgram(
"+alias org.eolang.txt.sprintf\n",
"[] > main",
" seq > @",
" QQ.io.stdout|0.29.1",
" sprintf|0.28.5",
" \"Hello world\"",
" TRUE"
)
.execute(new FakeMaven.Discover());
final String sprintf = "org.eolang.txt.sprintf|0.28.5";
final String stdout = "org.eolang.io.stdout|0.29.1";
MatcherAssert.assertThat(
String.format(
"External tojos should not have contained %s object after discovering, but they did",
sprintf
),
maven.externalTojos().contains(sprintf),
Matchers.is(false)
);
MatcherAssert.assertThat(
String.format(
"External tojos should not have contained %s object after discovering, but they did",
stdout
),
maven.externalTojos().contains(stdout),
Matchers.is(false)
);
}
}
5 changes: 3 additions & 2 deletions eo-maven-plugin/src/test/java/org/eolang/maven/FakeMaven.java
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ public <T extends AbstractMojo> FakeMaven execute(final Class<T> mojo) throws IO
this.params.putIfAbsent("transpiledFormat", "csv");
this.params.putIfAbsent("skipZeroVersions", true);
this.params.putIfAbsent("discoverSelf", false);
this.params.putIfAbsent("versioned", false);
this.params.putIfAbsent("withVersions", false);
this.params.putIfAbsent("ignoreVersionConflict", false);
this.params.putIfAbsent("ignoreTransitive", true);
this.params.putIfAbsent("central", new DummyCentral());
Expand Down Expand Up @@ -339,12 +339,13 @@ FakeMaven withProgram(final Path path) throws IOException {
}

/**
* Specify hash for all foreign tojos.
* Specify hash for all foreign and external tojos.
* @param hash Commit hash
* @return The same maven instance.
*/
FakeMaven allTojosWithHash(final CommitHash hash) {
this.foreignTojos().all().forEach(tojo -> tojo.withHash(hash));
this.externalTojos().all().forEach(tojo -> tojo.withHash(hash));
return this;
}

Expand Down
Loading

2 comments on commit 9a7619f

@0pdd
Copy link

@0pdd 0pdd commented on 9a7619f Jul 14, 2023

Choose a reason for hiding this comment

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

Puzzle 1602-46b0e5d9 discovered in eo-maven-plugin/src/main/java/org/eolang/maven/SafeMojo.java) and submitted as #2258. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

@0pdd
Copy link

@0pdd 0pdd commented on 9a7619f Jul 14, 2023

Choose a reason for hiding this comment

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

Puzzle 1602-59e4ff34 discovered in eo-maven-plugin/src/main/java/org/eolang/maven/DiscoverMojo.java) and submitted as #2259. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

Please sign in to comment.