Skip to content

Commit f0ee6d5

Browse files
authored
Merge pull request objectionary#3989 from maxonfjvipon/bug/objectionary#3199/move-resolve-out-of-assemble
bug(objectionary#3199): move `ResolveMojo` out of `assemble` pipeline
2 parents fdb5c53 + cb1cff6 commit f0ee6d5

24 files changed

+202
-214
lines changed

eo-maven-plugin/src/main/java/org/eolang/maven/AssembleMojo.java

-3
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,6 @@ public final class AssembleMojo extends SafeMojo {
3737
new Moja<>(ShakeMojo.class),
3838
new Moja<>(ProbeMojo.class),
3939
new Moja<>(PullMojo.class),
40-
new Moja<>(ResolveMojo.class),
41-
new Moja<>(MarkMojo.class),
42-
new Moja<>(PlaceMojo.class),
4340
};
4441

4542
@Override

eo-maven-plugin/src/main/java/org/eolang/maven/CompileMojo.java

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ public final class CompileMojo extends SafeMojo {
2626
new Moja<>(DownloadDepsMojo.class),
2727
new Moja<>(AssembleMojo.class),
2828
new Moja<>(LintMojo.class),
29+
new Moja<>(ResolveMojo.class),
30+
new Moja<>(PlaceMojo.class),
2931
};
3032

3133
@Override

eo-maven-plugin/src/main/java/org/eolang/maven/FpDefault.java

+10-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.nio.file.Path;
88
import java.util.function.Supplier;
99
import org.cactoos.Func;
10+
import org.cactoos.io.InputOf;
1011

1112
/**
1213
* Default footprint that covers all the scenarios of updating target
@@ -63,7 +64,15 @@ final class FpDefault extends FpEnvelope {
6364
final Supplier<String> hash,
6465
final Path tail
6566
) {
66-
this(new FpGenerated(content), base, semver, hash, tail);
67+
this(
68+
new FpGenerated(
69+
src -> new InputOf(content.apply(src))
70+
),
71+
base,
72+
semver,
73+
hash,
74+
tail
75+
);
6776
}
6877

6978
/**

eo-maven-plugin/src/main/java/org/eolang/maven/FpFork.java

+21-30
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
*/
55
package org.eolang.maven;
66

7-
import java.io.IOException;
87
import java.nio.file.Path;
98
import org.cactoos.BiFunc;
109
import org.cactoos.func.UncheckedBiFunc;
@@ -14,44 +13,36 @@
1413
* condition.
1514
* @since 0.41
1615
*/
17-
final class FpFork implements Footprint {
16+
final class FpFork extends FpEnvelope {
1817
/**
19-
* Lazy condition.
20-
*/
21-
private final UncheckedBiFunc<Path, Path, Boolean> condition;
22-
23-
/**
24-
* First wrapped footprint.
25-
*/
26-
private final Footprint first;
27-
28-
/**
29-
* Second wrapped footprint.
18+
* Ctor.
19+
* @param condition Condition as boolean
20+
* @param first First wrapped footprint
21+
* @param second Second wrapped footprint
3022
*/
31-
private final Footprint second;
23+
FpFork(final boolean condition, final Footprint first, final Footprint second) {
24+
this((src, tgt) -> condition, first, second);
25+
}
3226

3327
/**
3428
* Ctor.
3529
* @param condition Lazy condition
36-
* @param first First wrapped condition
37-
* @param second Second wrapped condition
30+
* @param first First wrapped footprint
31+
* @param second Second wrapped footprint
3832
*/
3933
FpFork(
4034
final BiFunc<Path, Path, Boolean> condition, final Footprint first, final Footprint second
4135
) {
42-
this.condition = new UncheckedBiFunc<>(condition);
43-
this.first = first;
44-
this.second = second;
45-
}
46-
47-
@Override
48-
public Path apply(final Path source, final Path target) throws IOException {
49-
final Footprint footprint;
50-
if (this.condition.apply(source, target)) {
51-
footprint = this.first;
52-
} else {
53-
footprint = this.second;
54-
}
55-
return footprint.apply(source, target);
36+
super(
37+
(src, tgt) -> {
38+
final Footprint footprint;
39+
if (new UncheckedBiFunc<>(condition).apply(src, tgt)) {
40+
footprint = first;
41+
} else {
42+
footprint = second;
43+
}
44+
return footprint.apply(src, tgt);
45+
}
46+
);
5647
}
5748
}

eo-maven-plugin/src/main/java/org/eolang/maven/FpGenerated.java

+7-6
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
import java.io.IOException;
88
import java.nio.file.Path;
99
import org.cactoos.Func;
10-
import org.cactoos.scalar.ScalarOf;
10+
import org.cactoos.Input;
11+
import org.cactoos.func.UncheckedFunc;
1112

1213
/**
1314
* Footprint that saves content generated from lambda to the target file.
@@ -17,18 +18,18 @@ final class FpGenerated implements Footprint {
1718
/**
1819
* Content function.
1920
*/
20-
private final Func<Path, String> content;
21+
private final UncheckedFunc<Path, Input> content;
2122

2223
/**
2324
* Ctor.
24-
* @param content Content function
25+
* @param content Content as bytes
2526
*/
26-
FpGenerated(final Func<Path, String> content) {
27-
this.content = content;
27+
FpGenerated(final Func<Path, Input> content) {
28+
this.content = new UncheckedFunc<>(content);
2829
}
2930

3031
@Override
3132
public Path apply(final Path source, final Path target) throws IOException {
32-
return new Saved(new ScalarOf<>(this.content, source), target).value();
33+
return new Saved(this.content.apply(source), target).value();
3334
}
3435
}

eo-maven-plugin/src/main/java/org/eolang/maven/LintMojo.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public final class LintMojo extends SafeMojo {
4343
/**
4444
* The directory where to transpile to.
4545
*/
46-
static final String DIR = "6-lint";
46+
static final String DIR = "5-lint";
4747

4848
/**
4949
* Subdirectory for optimized cache.

eo-maven-plugin/src/main/java/org/eolang/maven/ParseMojo.java

+69-22
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,21 @@
77
import com.github.lombrozo.xnav.Filter;
88
import com.github.lombrozo.xnav.Xnav;
99
import com.jcabi.log.Logger;
10+
import com.jcabi.xml.XML;
1011
import com.jcabi.xml.XMLDocument;
12+
import java.io.FileNotFoundException;
1113
import java.io.IOException;
1214
import java.nio.file.Path;
15+
import java.util.ArrayList;
1316
import java.util.List;
1417
import java.util.stream.Collectors;
1518
import org.apache.maven.plugins.annotations.LifecyclePhase;
1619
import org.apache.maven.plugins.annotations.Mojo;
1720
import org.apache.maven.plugins.annotations.ResolutionScope;
18-
import org.cactoos.Func;
1921
import org.cactoos.io.InputOf;
2022
import org.cactoos.iterable.Filtered;
2123
import org.eolang.parser.EoSyntax;
24+
import org.w3c.dom.Node;
2225
import org.xembly.Directives;
2326
import org.xembly.Xembler;
2427

@@ -95,15 +98,20 @@ private int parsed(final TjForeign tojo) throws Exception {
9598
final String name = tojo.identifier();
9699
final Path base = this.targetDir.toPath().resolve(ParseMojo.DIR);
97100
final Path target = new Place(name).make(base, AssembleMojo.XMIR);
101+
final List<Node> refs = new ArrayList<>(1);
98102
tojo.withXmir(
99103
new FpDefault(
100-
this.parse(name),
104+
src -> {
105+
final Node node = this.parsed(src, name);
106+
refs.add(node);
107+
return new XMLDocument(node).toString();
108+
},
101109
this.cache.toPath().resolve(ParseMojo.CACHE),
102110
this.plugin.getVersion(),
103111
new TojoHash(tojo),
104112
base.relativize(target)
105113
).apply(source, target)
106-
);
114+
).withVersion(ParseMojo.version(target, refs));
107115
final List<Xnav> errors = new Xnav(target)
108116
.element("program")
109117
.element("errors")
@@ -126,26 +134,65 @@ private int parsed(final TjForeign tojo) throws Exception {
126134
}
127135

128136
/**
129-
* Function that parses EO source.
137+
* Source parsed to {@link Node}.
138+
* @param source Relative source path
130139
* @param name Name of the EO object
131-
* @return Function that parses EO source
140+
* @return Parsed EO object as {@link Node}
141+
* @throws IOException If fails to parse
132142
*/
133-
private Func<Path, String> parse(final String name) {
134-
return source -> {
135-
final String parsed = new XMLDocument(
136-
new Xembler(
137-
new Directives().xpath("/program").attr(
138-
"source", this.sourcesDir.toPath().relativize(source.toAbsolutePath())
139-
)
140-
).applyQuietly(new EoSyntax(name, new InputOf(source)).parsed().inner())
141-
).toString();
142-
Logger.debug(
143-
ParseMojo.class,
144-
"Parsed program %s:\n %s",
145-
name,
146-
parsed
147-
);
148-
return parsed;
149-
};
143+
private Node parsed(final Path source, final String name) throws IOException {
144+
final XML xmir = new EoSyntax(name, new InputOf(source)).parsed();
145+
final Path src = this.sourcesDir.toPath().relativize(source.toAbsolutePath());
146+
final Node node = new Xembler(
147+
new Directives().xpath("/program").attr("source", src)
148+
).applyQuietly(xmir.inner());
149+
Logger.debug(
150+
ParseMojo.class,
151+
"Parsed program '%s' from %[file]s:\n %s",
152+
name, src, xmir
153+
);
154+
return node;
155+
}
156+
157+
/**
158+
* Tojo version.
159+
* The version can be extracted from:
160+
* 1. Parsed {@link Node} if EO object was parsed for the first time
161+
* 2. XML document that was already parsed before
162+
* @param target Path to result XML document
163+
* @param parsed List with either one parsed {@link Node} or empty
164+
* @return Tojo version
165+
* @throws FileNotFoundException If XML document file does not exist
166+
*/
167+
private static String version(
168+
final Path target,
169+
final List<Node> parsed
170+
) throws FileNotFoundException {
171+
final Node node;
172+
if (parsed.isEmpty()) {
173+
node = new XMLDocument(target).inner();
174+
} else {
175+
node = parsed.get(0);
176+
}
177+
return new Xnav(node)
178+
.element("program")
179+
.element("metas")
180+
.elements(
181+
Filter.all(
182+
Filter.withName("meta"),
183+
meta -> new Xnav(meta)
184+
.elements(
185+
Filter.all(
186+
Filter.withName("head"),
187+
head -> head.text().map("version"::equals).orElse(false)
188+
)
189+
)
190+
.findAny()
191+
.isPresent()
192+
)
193+
)
194+
.findFirst()
195+
.map(meta -> meta.element("tail").text().get())
196+
.orElse(ParseMojo.ZERO);
150197
}
151198
}

0 commit comments

Comments
 (0)