Skip to content

Commit

Permalink
Merge branch 'master' into feat/objectionary#2437/primitives-with-dat…
Browse files Browse the repository at this point in the history
…a-bytes
  • Loading branch information
maxonfjvipon committed Nov 13, 2023
2 parents f4be324 + 37d6963 commit 2ebf9bb
Show file tree
Hide file tree
Showing 26 changed files with 348 additions and 80 deletions.
1 change: 1 addition & 0 deletions eo-maven-plugin/src/it/custom_goals/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ can check in the [pom.xml](pom.xml) in `eo-maven-plugin` description:
<goal>register</goal>
<goal>parse</goal>
<goal>optimize</goal>
<goal>shake</goal>
<goal>discover-foreign</goal>
<goal>probe</goal>
<goal>pull</goal>
Expand Down
1 change: 1 addition & 0 deletions eo-maven-plugin/src/it/custom_goals/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ SOFTWARE.
<goal>register</goal>
<goal>parse</goal>
<goal>optimize</goal>
<goal>shake</goal>
<goal>discover-foreign</goal>
<goal>probe</goal>
<goal>pull</goal>
Expand Down
9 changes: 5 additions & 4 deletions eo-maven-plugin/src/it/custom_goals/verify.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
'target/eo/foreign.csv',
'target/eo/1-parse',
'target/eo/2-optimize',
'target/eo/3-pull',
'target/eo/3-shake',
'target/eo/4-pull',
].each { assert new File(basedir, it).exists() }

/**
Expand All @@ -35,9 +36,9 @@
* and only on the next step it starts to resolve the dependencies.
*/
[
'target/eo/4-resolve',
'target/eo/5-pre',
'target/eo/6-transpile',
'target/eo/5-resolve',
'target/eo/6-pre',
'target/eo/7-transpile',
].each { assert !new File(basedir, it).exists() }

true
6 changes: 3 additions & 3 deletions eo-maven-plugin/src/it/fibonacci/verify.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ private static boolean online() {
'target/eo/1-parse/org/eolang/examples/app.xmir',
'target/eo/2-optimization-steps/org/eolang/examples/app/00-not-empty-atoms.xml',
'target/eo/2-optimize/org/eolang/examples/app.xmir',
'target/eo/5-pre/org/eolang/examples/app/01-classes.xml',
'target/eo/6-transpile/org/eolang/examples/app.xmir',
'target/eo/6-pre/org/eolang/examples/app/01-classes.xml',
'target/eo/7-transpile/org/eolang/examples/app.xmir',
'target/eo/sodg/org/eolang/error.sodg',
'target/eo/sodg/org/eolang/error.sodg.xe',
'target/eo/sodg/org/eolang/error.sodg.graph.xml',
Expand All @@ -57,7 +57,7 @@ private static boolean online() {
[
'target/classes/EOorg/EOeolang/EOexamples/EOapp.class',
'target/eo/placed.json',
'target/eo/3-pull/org/eolang/tuple.eo',
'target/eo/4-pull/org/eolang/tuple.eo',
].each { assert new File(basedir, it).exists() || !online() }

String log = new File(basedir, 'build.log').text
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ public void exec() throws IOException {
final Moja<?>[] mojas = {
new Moja<>(ParseMojo.class),
new Moja<>(OptimizeMojo.class),
new Moja<>(ShakeMojo.class),
new Moja<>(DiscoverMojo.class),
new Moja<>(ProbeMojo.class),
new Moja<>(PullMojo.class),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public void exec() throws IOException {
final Names names = new Names(targetDir.toPath().getParent());
new File(this.targetDir.toPath().resolve("Lib/").toString()).mkdirs();
for (final ForeignTojo tojo : this.scopedTojos().withOptimized()) {
final Path file = tojo.optimized();
final Path file = tojo.shaken();
final XML input = new XMLDocument(file);
final List<XML> nodes = this.addRust(input).nodes("/program/rusts/rust");
for (final XML node: nodes) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void exec() throws FileNotFoundException {
final Collection<ForeignTojo> tojos = this.scopedTojos().notDiscovered();
final Collection<String> discovered = new HashSet<>();
for (final ForeignTojo tojo : tojos) {
final Path src = tojo.optimized();
final Path src = tojo.shaken();
final Collection<String> names = this.discover(src, tojo.identifier());
discovered.addAll(names);
for (final String name : names) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ static String last(final String input) {
@Override
void exec() throws IOException {
for (final ForeignTojo tojo : this.scopedTojos().withOptimized()) {
final Path file = tojo.optimized();
final Path file = tojo.shaken();
final Place place = new Place(
LatexMojo.last(new XMLDocument(file).xpath("/program/@name").get(0))
);
Expand Down
10 changes: 7 additions & 3 deletions eo-maven-plugin/src/main/java/org/eolang/maven/OptimizeMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.jcabi.log.Logger;
import com.jcabi.xml.XML;
import com.jcabi.xml.XMLDocument;
import com.yegor256.xsline.TrDefault;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Collection;
Expand Down Expand Up @@ -170,9 +171,12 @@ private Scalar<Integer> task(
private Optimization optimization() {
Optimization opt;
if (this.trackOptimizationSteps) {
opt = new OptSpy(this.targetDir.toPath().resolve(OptimizeMojo.STEPS));
opt = new OptSpy(
new ParsingTrain(),
this.targetDir.toPath().resolve(OptimizeMojo.STEPS)
);
} else {
opt = new OptTrain();
opt = new OptTrain(new ParsingTrain());
}
if (this.failOnError) {
opt = new OptTrain(opt, "/org/eolang/parser/fail-on-errors.xsl");
Expand All @@ -183,7 +187,7 @@ private Optimization optimization() {
if (this.failOnError) {
opt = new OptTrain(opt, "/org/eolang/parser/fail-on-critical.xsl");
} else {
opt = new OptTrain(opt, new ParsingTrain().empty());
opt = new OptTrain(opt, new TrDefault<>());
}
return opt;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public void exec() throws IOException {
final Collection<ObjectName> probed = new HashSet<>(1);
final Collection<ForeignTojo> tojos = this.scopedTojos().unprobed();
for (final ForeignTojo tojo : tojos) {
final Path src = tojo.optimized();
final Path src = tojo.shaken();
final Collection<ObjectName> objects = this.probes(src);
if (!objects.isEmpty()) {
Logger.info(this, "Probing object(s): %s", objects);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public final class PullMojo extends SafeMojo {
/**
* The directory where to process to.
*/
public static final String DIR = "3-pull";
public static final String DIR = "4-pull";

/**
* The Git tag to pull objects from, in objectionary.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public final class ResolveMojo extends SafeMojo {
/**
* The directory where to resolve to.
*/
public static final String DIR = "4-resolve";
public static final String DIR = "5-resolve";

/**
* Skip artifact with the version 0.0.0.
Expand Down
204 changes: 204 additions & 0 deletions eo-maven-plugin/src/main/java/org/eolang/maven/ShakeMojo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016-2023 Objectionary.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.eolang.maven;

import com.jcabi.log.Logger;
import com.jcabi.xml.XML;
import com.jcabi.xml.XMLDocument;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Collection;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.cactoos.Scalar;
import org.cactoos.experimental.Threads;
import org.cactoos.iterable.Filtered;
import org.cactoos.iterable.Mapped;
import org.cactoos.number.SumOf;
import org.eolang.maven.optimization.OptCached;
import org.eolang.maven.optimization.OptSpy;
import org.eolang.maven.optimization.OptTrain;
import org.eolang.maven.optimization.Optimization;
import org.eolang.maven.tojos.ForeignTojo;
import org.eolang.maven.util.HmBase;
import org.eolang.maven.util.Rel;

/**
* Shake (prepare) XML files after optimizations for translation to java.
*
* @since 0.33.0
* @todo #2577:30min Resolve code duplication between {@link OptimizeMojo} and {@link ShakeMojo}.
* ShakeMojo was created in order to split two optimization stages: optimizations themselves and
* preparations for translation to java. Now these two mojo look almost the same and there's a lot
* of code duplication. So it's needed to resolve that. Don't forget to remove the puzzle
* @todo #2577:30min Add tests for ShakeMojo. ShakeMojo was created in order to split two
* optimization stages: optimizations themselves and preparations for translation to java
* Need to create tests for {@link ShakeMojo} in order to be sure that it does only it's job.
*/
@Mojo(
name = "shake",
defaultPhase = LifecyclePhase.PROCESS_SOURCES,
threadSafe = true
)
public final class ShakeMojo extends SafeMojo {
/**
* The directory where to shake to.
*/
public static final String DIR = "3-shake";

/**
* Subdirectory for shaken cache.
*/
static final String SHAKEN = "shaken";

/**
* The directory where to place intermediary files.
*/
static final String STEPS = "3-shake-steps";

/**
* Track optimization steps into intermediate XML files?
*
* @checkstyle MemberNameCheck (7 lines)
* @since 0.24.0
*/
@SuppressWarnings("PMD.LongVariable")
@Parameter(property = "eo.trackOptimizationSteps", required = true, defaultValue = "false")
private boolean trackOptimizationSteps;

@Override
void exec() throws IOException {
final Collection<ForeignTojo> tojos = this.scopedTojos().withOptimized();
final Optimization opt = this.optimization();
final int total = new SumOf(
new Threads<>(
Runtime.getRuntime().availableProcessors(),
new Mapped<>(
tojo -> this.task(tojo, opt),
new Filtered<>(
ForeignTojo::notShaken,
tojos
)
)
)
).intValue();
if (total > 0) {
Logger.info(
this,
"Shaken %d out of %d XMIR program(s)", total,
tojos.size()
);
} else {
Logger.debug(this, "No XMIR programs out of %d shaken", tojos.size());
}
}

/**
* Converts tojo to optimization task.
*
* @param tojo Tojo that should be optimized.
* @param common Optimization.
* @return Optimization task.
*/
private Scalar<Integer> task(
final ForeignTojo tojo,
final Optimization common
) {
final Path src = tojo.optimized();
Logger.debug(
this, "Adding optimization task for %s",
src
);
return () -> {
tojo.withShaken(
this.make(
this.optimization(tojo, common).apply(new XMLDocument(src)),
src
).toAbsolutePath()
);
return 1;
};
}

/**
* Shake optimizations for tojos.
* @return Shake optimizations
*/
private Optimization optimization() {
final Optimization opt;
if (this.trackOptimizationSteps) {
opt = new OptSpy(this.targetDir.toPath().resolve(ShakeMojo.STEPS));
} else {
opt = new OptTrain();
}
return opt;
}

/**
* Optimization for specific tojo.
*
* @param tojo Tojo
* @param common Optimization
* @return Optimization for specific Tojo
*/
private Optimization optimization(final ForeignTojo tojo, final Optimization common) {
final Optimization res;
if (tojo.hasHash()) {
res = new OptCached(
common,
this.cache.resolve(ShakeMojo.SHAKEN).resolve(tojo.hash())
);
} else {
res = common;
}
return res;
}

/**
* Make a path with optimized XML file after parsing.
*
* @param xml Optimized xml
* @param file EO file
* @return The file with optimized XMIR
* @throws IOException If fails
*/
private Path make(final XML xml, final Path file) throws IOException {
final String name = new XMLDocument(file).xpath("/program/@name").get(0);
final Place place = new Place(name);
final Path dir = this.targetDir.toPath();
final Path target = place.make(
dir.resolve(ShakeMojo.DIR), TranspileMojo.EXT
);
new HmBase(dir).save(
xml.toString(),
dir.relativize(target)
);
Logger.debug(
this, "Optimized %s (program:%s) to %s",
new Rel(file), name, new Rel(target)
);
return target;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ public void exec() throws IOException {
continue;
}
final Path sodg = new Place(name).make(home, "sodg");
final Path xmir = tojo.optimized();
final Path xmir = tojo.shaken();
if (sodg.toFile().lastModified() >= xmir.toFile().lastModified()) {
Logger.debug(
this, "Already converted %s to %s (it's newer than the source)",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public final class TranspileMojo extends SafeMojo {
/**
* The directory where to transpile to.
*/
public static final String DIR = "6-transpile";
public static final String DIR = "7-transpile";

/**
* Extension for compiled sources in XMIR format (XML).
Expand Down Expand Up @@ -107,7 +107,7 @@ public final class TranspileMojo extends SafeMojo {
/**
* The directory where to put pre-transpile files.
*/
private static final String PRE = "5-pre";
private static final String PRE = "6-pre";

/**
* Target directory.
Expand Down Expand Up @@ -185,7 +185,7 @@ public void exec() throws IOException {
*/
private int transpile(final ForeignTojo tojo) throws IOException {
final int saved;
final Path file = tojo.optimized();
final Path file = tojo.shaken();
final XML input = new XMLDocument(file);
final String name = input.xpath("/program/@name").get(0);
final Place place = new Place(name);
Expand Down
Loading

0 comments on commit 2ebf9bb

Please sign in to comment.