Skip to content

Commit

Permalink
Merge branch 'master' into 3800
Browse files Browse the repository at this point in the history
  • Loading branch information
h1alexbel committed Feb 12, 2025
2 parents c580c45 + e184458 commit 38dfa42
Show file tree
Hide file tree
Showing 165 changed files with 1,362 additions and 1,009 deletions.
38 changes: 38 additions & 0 deletions .github/workflows/infer.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# The MIT License (MIT)
#
# Copyright (c) 2016-2025 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.
---
name: infer
'on':
push:
branches:
- master
jobs:
infer:
timeout-minutes: 15
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v4
- uses: srz-zumix/setup-infer@v1
with:
infer_version: v1.0.0
- run: infer --version
- run: infer run --fail-on-issue --no-progress-bar -- mvn test
1 change: 1 addition & 0 deletions .rultor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ release:
script: |-
[[ "${tag}" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]] || exit -1
mvn flatten:flatten
mvn versions:set "-DnewVersion=${tag}" -Dstyle.color=never
git commit -am "${tag}"
mvn clean install -DskipTests -Dinvoker.skip
Expand Down
2 changes: 1 addition & 1 deletion .xcop
Original file line number Diff line number Diff line change
@@ -1 +1 @@
--exclude=paper/**.xml
--exclude=paper/**.xml,**/.flattened-pom.xml
1 change: 1 addition & 0 deletions eo-maven-plugin/.xcop
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--exclude=**/.flattened-pom.xml
4 changes: 2 additions & 2 deletions eo-maven-plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ create a file `pom.xml` with this content (it's just a sample):
<plugin>
<groupId>org.eolang</groupId>
<artifactId>eo-maven-plugin</artifactId>
<version>0.51.0</version>
<version>0.51.3</version>
<executions>
<execution>
<goals>
Expand Down Expand Up @@ -142,7 +142,7 @@ To transform your XMIR files, you need to add the following plugin configuration
<plugin>
<groupId>org.eolang</groupId>
<artifactId>eo-maven-plugin</artifactId>
<version>0.51.0</version>
<version>0.51.3</version>
<executions>
<execution>
<id>xmir-to-phi</id>
Expand Down
2 changes: 1 addition & 1 deletion eo-maven-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ SOFTWARE.
<dependency>
<groupId>com.tngtech.archunit</groupId>
<artifactId>archunit</artifactId>
<version>1.3.0</version>
<version>1.4.0</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
13 changes: 5 additions & 8 deletions eo-maven-plugin/src/main/java/org/eolang/maven/LatexMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@
*/
package org.eolang.maven;

import com.github.lombrozo.xnav.Xnav;
import com.jcabi.log.Logger;
import com.jcabi.xml.XMLDocument;
import java.io.IOException;
import java.nio.file.Path;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.eolang.maven.latex.LatexTemplate;
import org.eolang.maven.tojos.ForeignTojo;
import org.eolang.maven.util.HmBase;

Expand Down Expand Up @@ -83,16 +83,13 @@ static String last(final String input) {
void exec() throws IOException {
for (final ForeignTojo tojo : this.scopedTojos().withShaken()) {
final Path file = tojo.shaken();
final Place place = new Place(
LatexMojo.last(new XMLDocument(file).xpath("/program/@name").get(0))
);
final Path dir = this.targetDir.toPath();
final Path target = place.make(
dir.resolve(LatexMojo.DIR), LatexMojo.EXT
);
final Path target = new Place(
LatexMojo.last(new ProgramName(new XMLDocument(file)).get())
).make(dir.resolve(LatexMojo.DIR), LatexMojo.EXT);
new HmBase(dir).save(
new LatexTemplate(
new XMLDocument(file).nodes("/program/listing").get(0).toString()
new Xnav(file).element("program").element("listing").text().get()
).asString(),
dir.relativize(target)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.eolang.maven.latex;
package org.eolang.maven;

import java.util.regex.Pattern;
import org.cactoos.io.ResourceOf;
import org.cactoos.text.TextOf;
import org.cactoos.text.UncheckedText;
Expand All @@ -32,11 +33,12 @@
* in LaTex notation as a standalone compilable document.
*
* @since 0.30
* @todo #2067:30min We need to refactor LatexTemplate class.
* And to remove redundant parts in the code, like DOM variables and
* license header. E.g.: "&lt;listing&gt;# The MIT License (MIT)...&lt;/listing&gt;".
*/
public final class LatexTemplate {
/**
* Pattern to extract the content starting with `+`.
*/
private static final Pattern PATTERN = Pattern.compile("(?s)^\\s*\\+.*", Pattern.MULTILINE);

/**
* The code.
Expand All @@ -63,7 +65,23 @@ public String asString() {
new ResourceOf("org/eolang/maven/latex/latex-template.txt")
)
).asString(),
this.code
this.extractedContent()
);
}

/**
* Extracts the main content from the code.
* <p>
* This method removes unnecessary headers (e.g., license information)
* and retains the relevant code starting from the first line that begins with `+`.
* If no such line is found, the entire input is returned as is.
* @return The extracted content.
*/
private String extractedContent() {
return LatexTemplate.PATTERN.matcher(this.code)
.results()
.map(match -> match.group().trim())
.findFirst()
.orElse(this.code);
}
}
106 changes: 43 additions & 63 deletions eo-maven-plugin/src/main/java/org/eolang/maven/LintMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,8 @@
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
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;
import org.cactoos.list.ListOf;
import org.eolang.lints.Defect;
import org.eolang.lints.Program;
Expand Down Expand Up @@ -73,65 +71,62 @@ public final class LintMojo extends SafeMojo {
*/
static final String CACHE = "linted";

/**
* Whether we should fail on warning.
*
* @checkstyle MemberNameCheck (11 lines)
*/
@SuppressWarnings("PMD.ImmutableField")
@Parameter(
property = "eo.failOnWarning",
required = true,
defaultValue = "true"
)
private boolean failOnWarning;

@Override
void exec() throws IOException {
if (this.skipLinting) {
Logger.info(this, "Linting is skipped because eo:skipLinting is TRUE");
} else {
this.lint();
}
}

/**
* Lint.
* @throws IOException If fails
*/
private void lint() throws IOException {
final long start = System.currentTimeMillis();
final Collection<ForeignTojo> tojos = this.scopedTojos().withShaken();
final ConcurrentHashMap<Severity, List<Defect>> counts = new ConcurrentHashMap<>();
counts.putIfAbsent(Severity.CRITICAL, new ListOf<>());
counts.putIfAbsent(Severity.ERROR, new ListOf<>());
counts.putIfAbsent(Severity.WARNING, new ListOf<>());
final ConcurrentHashMap<Severity, Integer> counts = new ConcurrentHashMap<>();
counts.putIfAbsent(Severity.CRITICAL, 0);
counts.putIfAbsent(Severity.ERROR, 0);
counts.putIfAbsent(Severity.WARNING, 0);
final int passed = new Threaded<>(
tojos,
tojo -> this.lintOne(tojo, counts)
).total();
if (tojos.isEmpty()) {
Logger.info(this, "There are no XMIR programs, nothing to lint individually");
}
Logger.info(
this,
"Also, %d XMIR programs linted as a package",
this.lintAll(counts)
);
if (this.lintAsPackage) {
Logger.info(
this,
"XMIR programs linted as a package: %d",
this.lintAll(counts)
);
} else {
Logger.info(
this,
"Skipping linting as package (use -Deo.lintAsPackage=true to enable)"
);
}
final String sum = LintMojo.summary(counts);
Logger.info(
this,
"Linted %d out of %d XMIR program(s) that needed this (out of %d total programs) in %[ms]s: %s",
passed, tojos.size(), tojos.size(), System.currentTimeMillis() - start, sum
);
Logger.info(
this,
"Read more about lints: https://www.objectionary.com/lints/%s",
counts.values().stream()
.flatMap(List::stream)
.collect(Collectors.toList()).get(0).version()
);
if (!counts.get(Severity.WARNING).isEmpty() && this.failOnWarning) {
if (counts.get(Severity.ERROR) > 0 || counts.get(Severity.CRITICAL) > 0) {
throw new IllegalStateException(
String.format(
"In %d XMIR files, we found %s (use -Deo.failOnWarning=false to ignore)",
"In %d XMIR files, we found %s (must stop here)",
tojos.size(), sum
)
);
} else if (
!counts.get(Severity.ERROR).isEmpty() || !counts.get(Severity.CRITICAL).isEmpty()
) {
} else if (counts.get(Severity.WARNING) > 0 && this.failOnWarning) {
throw new IllegalStateException(
String.format(
"In %d XMIR files, we found %s (must stop here)",
"In %d XMIR files, we found %s (use -Deo.failOnWarning=false to ignore)",
tojos.size(), sum
)
);
Expand All @@ -146,15 +141,14 @@ void exec() throws IOException {
* @throws Exception If failed to lint
*/
private int lintOne(final ForeignTojo tojo,
final ConcurrentHashMap<Severity, List<Defect>> counts) throws Exception {
final ConcurrentHashMap<Severity, Integer> counts) throws Exception {
final Path source = tojo.shaken();
final XML xmir = new XMLDocument(source);
final String name = xmir.xpath("/program/@name").get(0);
final Path base = this.targetDir.toPath().resolve(LintMojo.DIR);
final Path target = new Place(name).make(base, AssembleMojo.XMIR);
final Path target = new Place(new ProgramName(xmir).get()).make(base, AssembleMojo.XMIR);
tojo.withLinted(
new FpDefault(
src -> LintMojo.lint(xmir, counts).toString(),
src -> LintMojo.linted(xmir, counts).toString(),
this.cache.toPath().resolve(LintMojo.CACHE),
this.plugin.getVersion(),
new TojoHash(tojo),
Expand All @@ -170,7 +164,7 @@ private int lintOne(final ForeignTojo tojo,
* @return Amount of seen XMIR files
* @throws IOException If failed to lint
*/
private int lintAll(final ConcurrentHashMap<Severity, List<Defect>> counts) throws IOException {
private int lintAll(final ConcurrentHashMap<Severity, Integer> counts) throws IOException {
final Map<String, Path> paths = new HashMap<>();
for (final ForeignTojo tojo : this.scopedTojos().withShaken()) {
paths.put(tojo.identifier(), tojo.shaken());
Expand All @@ -184,13 +178,7 @@ private int lintAll(final ConcurrentHashMap<Severity, List<Defect>> counts) thro
}
final Collection<Defect> defects = new Programs(pkg).defects();
for (final Defect defect : defects) {
counts.compute(
defect.severity(),
(sev, before) -> {
before.add(defect);
return before;
}
);
counts.compute(defect.severity(), (sev, before) -> before + 1);
LintMojo.embed(
pkg.get(defect.program()),
new ListOf<>(defect)
Expand Down Expand Up @@ -251,18 +239,17 @@ private static String plural(final int count, final String name) {
* @param counts Counts of errors, warnings, and critical
* @return Summary text
*/
private static String summary(
final ConcurrentHashMap<Severity, List<Defect>> counts) {
private static String summary(final ConcurrentHashMap<Severity, Integer> counts) {
final List<String> parts = new ArrayList<>(0);
final int criticals = counts.get(Severity.CRITICAL).size();
final int criticals = counts.get(Severity.CRITICAL);
if (criticals > 0) {
parts.add(LintMojo.plural(criticals, "critical error"));
}
final int errors = counts.get(Severity.ERROR).size();
final int errors = counts.get(Severity.ERROR);
if (errors > 0) {
parts.add(LintMojo.plural(errors, "error"));
}
final int warnings = counts.get(Severity.WARNING).size();
final int warnings = counts.get(Severity.WARNING);
if (warnings > 0) {
parts.add(LintMojo.plural(warnings, "warning"));
}
Expand All @@ -288,22 +275,15 @@ private static String summary(
* @param counts Counts of errors, warnings, and critical
* @return XML after linting
*/
private static XML lint(final XML xmir,
final ConcurrentHashMap<Severity, List<Defect>> counts) {
private static XML linted(final XML xmir, final ConcurrentHashMap<Severity, Integer> counts) {
final Directives dirs = new Directives();
final Collection<Defect> defects = new Program(xmir).defects();
if (!defects.isEmpty()) {
dirs.xpath("/program").addIf("errors").strict(1);
LintMojo.embed(xmir, defects);
}
for (final Defect defect : defects) {
counts.compute(
defect.severity(),
(sev, before) -> {
before.add(defect);
return before;
}
);
counts.compute(defect.severity(), (sev, before) -> before + 1);
LintMojo.logOne(defect);
}
final Node node = xmir.inner();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
package org.eolang.maven;

import com.github.lombrozo.xnav.Xnav;
import com.jcabi.log.Logger;
import com.jcabi.xml.XML;
import com.jcabi.xml.XMLDocument;
Expand Down Expand Up @@ -130,10 +131,13 @@ private int parsed(final ForeignTojo tojo) throws Exception {
Logger.debug(this, "Parsed %[file]s to %[file]s", source, target);
} else {
for (final XML error : errors) {
final Xnav xnav = new Xnav(error.inner());
Logger.error(
this,
"Failed to parse '%[file]s:%s': %s",
source, error.xpath("@line").get(0), error.xpath("text()").get(0)
source,
xnav.attribute("line").text().get(),
xnav.text().get()
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ private int translate(final Path xmir, final int position, final int total)
final XML xml = new XMLDocument(new TextOf(xmir).asString());
try {
new Saved(this.translated(xml), target).value();
Logger.info(
Logger.debug(
this,
"Translated to phi (#%d/%d): %[file]s (%[size]s) -> %[file]s (%[size]s) in %[ms]s",
position, total, xmir,
Expand Down
Loading

0 comments on commit 38dfa42

Please sign in to comment.