Skip to content

Commit

Permalink
Merge branch 'master' into #1602-discovers-with-versions
Browse files Browse the repository at this point in the history
  • Loading branch information
maxonfjvipon committed Jul 11, 2023
2 parents c6cae5f + 6e20782 commit 01adde0
Show file tree
Hide file tree
Showing 15 changed files with 603 additions and 87 deletions.
5 changes: 5 additions & 0 deletions eo-maven-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,11 @@ SOFTWARE.
<version>3.12.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.13.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-reload4j</artifactId>
Expand Down
138 changes: 90 additions & 48 deletions eo-maven-plugin/src/main/java/org/eolang/maven/BinarizeMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,30 @@

import com.jcabi.log.Logger;
import com.jcabi.log.VerboseProcess;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.apache.commons.io.FileUtils;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.cactoos.io.InputOf;
import org.cactoos.io.OutputTo;
import org.cactoos.io.TeeInput;
import org.cactoos.scalar.LengthOf;
import org.cactoos.scalar.Unchecked;
import org.cactoos.experimental.Threads;
import org.cactoos.iterable.Filtered;
import org.cactoos.iterable.Mapped;
import org.cactoos.number.SumOf;
import org.eolang.maven.rust.BuildFailureException;

/**
* Compile binaries.
*
* @checkstyle ClassDataAbstractionCouplingCheck (500 lines)
* @since 0.1
* @todo #2195:90min Make cargo compilation in parallel. Now cargo
* projects are being built consistently which is too long.
* It is much better to build them in parallel to reduce time.
* @todo #2197:45min Update cached rust insert if it was changed.
* Now it copies cargo project to cache directory in the end of every
* compilation. It is better to copy the project only if it was changed
* with the last compilation.
*/
@Mojo(
name = "binarize",
Expand Down Expand Up @@ -78,45 +78,87 @@ public final class BinarizeMojo extends SafeMojo {
@Override
public void exec() throws IOException {
new Moja<>(BinarizeParseMojo.class).copy(this).execute();
for (final File file: targetDir.toPath().resolve("Lib").toFile().listFiles()) {
if (file.isDirectory() && file.toPath().resolve("Cargo.toml").toFile().exists()) {
Logger.info(this, String.format("Building rust project.."));
final Process building = new ProcessBuilder("cargo", "build")
.directory(file)
.start();
try {
building.waitFor();
} catch (final InterruptedException exception) {
Thread.currentThread().interrupt();
throw new BuildFailureException(
String.format(
"Interrupted while building %s",
file
),
exception
);
}
if (building.exitValue() != 0) {
Logger.error(this, "There was an error in compilation");
try (VerboseProcess process = new VerboseProcess(building)) {
new Unchecked<>(
new LengthOf(
new TeeInput(
new InputOf(process.stdoutQuietly()),
new OutputTo(new ByteArrayOutputStream())
)
)
).value();
}
throw new BuildFailureException(
String.format(
"Failed to build cargo project with dest = %s",
file
)
);
}
}
}
final int total = new SumOf(
new Threads<>(
Runtime.getRuntime().availableProcessors(),
new Mapped<>(
project -> () -> {
this.build(project);
return 1;
},
new Filtered<>(
project -> BinarizeMojo.valid(project),
targetDir.toPath().resolve("Lib").toFile().listFiles()
)
)
)
).intValue();
Logger.info(
this,
String.format("Built in total %d cargo projects", total)
);
}

/**
* Is the project valid?
* @param project File to check.
* @return True if valid. Otherwise false.
*/
private static boolean valid(final File project) {
return project.isDirectory()
&& project.toPath().resolve("Cargo.toml").toFile().exists();
}

/**
* Builds cargo project.
* @param project Path to the project.
* @throws IOException If any issues with IO.
*/
private void build(final File project) throws IOException {
final File target = project.toPath().resolve("target").toFile();
final File cached = this.cache
.resolve("Lib")
.resolve(project.getName())
.resolve("target").toFile();
if (cached.exists()) {
Logger.info(
this,
String.format(
"Copying %s to %s",
cached,
target
)
);
FileUtils.copyDirectory(
cached,
target
);
}
Logger.info(this, "Building rust project..");
try (
VerboseProcess proc = new VerboseProcess(
new ProcessBuilder("cargo", "build")
.directory(project)
)
) {
proc.stdout();
} catch (final IllegalArgumentException exc) {
throw new BuildFailureException(
String.format(
"Failed to build cargo project with dest = %s",
project
),
exc
);
}
Logger.info(
this,
String.format(
"Cargo building succeeded, update cached %s with %s",
cached,
target
)
);
FileUtils.copyDirectory(project, cached);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Map;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -72,4 +75,25 @@ void failsWithIncorrectInsert(@TempDir final Path temp) throws IOException {
() -> maven.execute(new FakeMaven.Binarize())
);
}

@Test
@Tag("slow")
void savesToCache(@TempDir final Path temp) throws IOException {
final FakeMaven maven;
synchronized (BinarizeMojoTest.class) {
maven = new FakeMaven(temp)
.with("cache", temp.resolve(".cache"))
.withProgram(Paths.get("src/test/resources/org/eolang/maven/simple-rust.eo"));
}
final Map<String, Path> res = maven
.execute(new FakeMaven.Binarize())
.result();
MatcherAssert.assertThat(
res,
Matchers.hasValue(temp.resolve(".cache").resolve("Lib"))
);
Assertions.assertDoesNotThrow(
() -> maven.execute(new FakeMaven.Binarize())
);
}
}
62 changes: 37 additions & 25 deletions eo-parser/src/main/antlr4/org/eolang/parser/Program.g4
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,38 @@ program

license
:
(COMMENT SINGLE_EOL)*
COMMENT DOUBLE_EOL
(COMMENT EOL)*
COMMENT EOP
;

metas
:
(META SINGLE_EOL)*
META DOUBLE_EOL
(META EOL)*
META EOP
;

objects
:
(
(COMMENT SINGLE_EOL)*
(COMMENT EOL)*
object
(SINGLE_EOL | DOUBLE_EOL)
(EOL | EOP)
)+
;

object
:
(
abstraction
(
abstraction
type?
)
|
application
)
tail?
(
SINGLE_EOL
EOL
method
htail?
suffix?
Expand All @@ -50,12 +53,16 @@ object

abstraction
:
(COMMENT SINGLE_EOL)*
(COMMENT EOL)*
attributes
(
(suffix (SPACE SLASH (NAME | QUESTION))?)
| htail
)?
;

type
:
suffix
SPACE
SLASH
(NAME | QUESTION)?
;

attributes
Expand Down Expand Up @@ -89,9 +96,9 @@ label

tail
:
SINGLE_EOL
EOL
TAB
(object (SINGLE_EOL | DOUBLE_EOL))+
(object (EOL | EOP))+
UNTAB
;

Expand Down Expand Up @@ -133,6 +140,9 @@ application
head
version
|
abstraction
htail?
|
head
htail?
|
Expand All @@ -154,7 +164,10 @@ application
|
application
suffix
htail?
(
SPACE
scope
)?
;

htail
Expand All @@ -180,6 +193,7 @@ htail
|
SPACE
abstraction
suffix?
)+
;

Expand Down Expand Up @@ -215,15 +229,13 @@ head
DOT
|
data
|
abstraction
)
;

version
:
BAR
VERSION
VER
;

has
Expand Down Expand Up @@ -253,7 +265,7 @@ data
HEX
;

COMMENT: HASH | (HASH ~[\r\n]* ~[\r\n\p{Space}]);
COMMENT: HASH | (HASH ~[\r\n]* ~[\r\n\t ]);
META: PLUS NAME (SPACE ~[\r\n]+)?;

ROOT: 'Q';
Expand Down Expand Up @@ -289,13 +301,13 @@ fragment LINEBREAK:
('\n' | '\r\n')
;

SINGLE_EOL
EOL
:
LINEBREAK
INDENT*
;

DOUBLE_EOL
EOP
:
LINEBREAK
LINEBREAK
Expand All @@ -309,7 +321,7 @@ fragment LINE_BYTES : BYTE (MINUS BYTE)+;
BYTES:
EMPTY_BYTES
| BYTE MINUS
| LINE_BYTES (MINUS SINGLE_EOL LINE_BYTES)*;
| LINE_BYTES (MINUS EOL LINE_BYTES)*;

BOOL: 'TRUE' | 'FALSE';
STRING: '"' (~["\\\r\n] | ESCAPE_SEQUENCE)* '"';
Expand All @@ -329,8 +341,8 @@ fragment EXPONENT: ('e'|'E') (PLUS | MINUS)? ('0'..'9')+;
FLOAT: (PLUS | MINUS)? [0-9]+ DOT [0-9]+ EXPONENT?;
HEX: '0x' [0-9a-fA-F]+;

NAME: [a-z][\p{Letter}\p{General_Category=Decimal_Number}_-]*;
VERSION: [0-9]+ DOT [0-9]+ DOT [0-9]+;
NAME: [a-z] ~[ \r\n\t,.|':;!?\][}{)(]*;
VER: [0-9]+ DOT [0-9]+ DOT [0-9]+;
fragment TEXT_MARK: '"""';
TEXT:
Expand Down
Loading

0 comments on commit 01adde0

Please sign in to comment.