From aad658b0c53f226e10acf23725c23dffc30e9a8c Mon Sep 17 00:00:00 2001 From: Yegor Bugayenko Date: Mon, 16 Dec 2024 13:20:43 +0300 Subject: [PATCH 1/4] #3678: fixed --- .../src/main/java/org/eolang/maven/TranspileMojo.java | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/eo-maven-plugin/src/main/java/org/eolang/maven/TranspileMojo.java b/eo-maven-plugin/src/main/java/org/eolang/maven/TranspileMojo.java index 65260d1d19..c4ef3da161 100644 --- a/eo-maven-plugin/src/main/java/org/eolang/maven/TranspileMojo.java +++ b/eo-maven-plugin/src/main/java/org/eolang/maven/TranspileMojo.java @@ -38,6 +38,7 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.util.Arrays; import java.util.Collection; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; @@ -253,7 +254,7 @@ private int javaGenerated(final boolean rewrite, final Path target, final String final Path tgt = new Place(jname).make( this.generatedDir.toPath(), TranspileMojo.JAVA ); - this.pinfo(tgt, jname, java.xpath("@package").get(0)); + this.pinfo(tgt, jname, java.xpath("@package").stream().findFirst().orElse("")); final Supplier che = new CachePath( this.cache.toPath().resolve(TranspileMojo.CACHE), this.plugin.getVersion(), @@ -335,7 +336,13 @@ private void pinfo(final Path java, final String oname, final String pname) " * don't modify it, all changes will be lost anyway.", " */", String.format("// @org.eolang.XmirPackage(\"%s\")", pname), - String.format("package %s;", oname.substring(0, oname.lastIndexOf('.'))) + String.format( + "package %s;", + Arrays.stream(oname.split("\\.")) + .reduce((f, s) -> s) + .stream() + .findFirst() + ) ).getBytes(StandardCharsets.UTF_8) ); Logger.debug(this, "Saved %[file]s (%[size]s)", pinfo, pinfo.toFile().length()); From f862ceeeaee917ba060b481f430a1a3c13492ea2 Mon Sep 17 00:00:00 2001 From: Yegor Bugayenko Date: Mon, 16 Dec 2024 14:42:43 +0300 Subject: [PATCH 2/4] #3678: typos fixed --- .../java/org/eolang/maven/TranspileMojo.java | 15 ++-- .../org/eolang/maven/TranspileMojoTest.java | 86 +++++++++++++++++++ 2 files changed, 92 insertions(+), 9 deletions(-) diff --git a/eo-maven-plugin/src/main/java/org/eolang/maven/TranspileMojo.java b/eo-maven-plugin/src/main/java/org/eolang/maven/TranspileMojo.java index c4ef3da161..c67aff0323 100644 --- a/eo-maven-plugin/src/main/java/org/eolang/maven/TranspileMojo.java +++ b/eo-maven-plugin/src/main/java/org/eolang/maven/TranspileMojo.java @@ -38,7 +38,6 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; -import java.util.Arrays; import java.util.Collection; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; @@ -323,10 +322,14 @@ private int javaGenerated(final boolean rewrite, final Path target, final String private void pinfo(final Path java, final String oname, final String pname) throws IOException { final Path pinfo = java.getParent().resolve("package-info.java"); - if (!pinfo.toFile().exists()) { + if (!pinfo.toFile().exists() && !pname.isEmpty()) { if (pinfo.getParent().toFile().mkdirs()) { Logger.debug(this, "Directory created for %[file]s", pinfo); } + String pkg = oname; + if (oname.contains(".")) { + pkg = pkg.substring(0, pkg.lastIndexOf('.')); + } Files.write( pinfo, String.join( @@ -336,13 +339,7 @@ private void pinfo(final Path java, final String oname, final String pname) " * don't modify it, all changes will be lost anyway.", " */", String.format("// @org.eolang.XmirPackage(\"%s\")", pname), - String.format( - "package %s;", - Arrays.stream(oname.split("\\.")) - .reduce((f, s) -> s) - .stream() - .findFirst() - ) + String.format("package %s;", pkg) ).getBytes(StandardCharsets.UTF_8) ); Logger.debug(this, "Saved %[file]s (%[size]s)", pinfo, pinfo.toFile().length()); diff --git a/eo-maven-plugin/src/test/java/org/eolang/maven/TranspileMojoTest.java b/eo-maven-plugin/src/test/java/org/eolang/maven/TranspileMojoTest.java index 0f581ece71..4564b772ed 100755 --- a/eo-maven-plugin/src/test/java/org/eolang/maven/TranspileMojoTest.java +++ b/eo-maven-plugin/src/test/java/org/eolang/maven/TranspileMojoTest.java @@ -25,7 +25,10 @@ import com.yegor256.Mktmp; import com.yegor256.MktmpResolver; +import com.yegor256.farea.Farea; +import com.yegor256.farea.RequisiteMatcher; import java.io.IOException; +import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; @@ -54,6 +57,7 @@ */ @SuppressWarnings({"PMD.AvoidDuplicateLiterals", "PMD.TooManyMethods"}) @ExtendWith(MktmpResolver.class) +@ExtendWith(RandomProgramResolver.class) final class TranspileMojoTest { /** @@ -82,6 +86,88 @@ void createsPreStylesheets(final String yaml) { ); } + @Test + void transpilesWithPackage(@Mktmp final Path temp) + throws Exception { + new Farea(temp).together( + f -> { + f.clean(); + f.files().file("src/main/eo/one/foo.eo").write( + String.join( + "\n", + "+package one", + "", + "# no comments.", + "[] > foo", + " QQ.io.stdout > @", + " \"Hello, world!\\n\"", + "" + ).getBytes(StandardCharsets.UTF_8) + ); + f.build() + .plugins() + .appendItself() + .execution() + .goals("register", "parse", "optimize", "shake", "transpile") + .configuration() + .set("failOnWarning", "false"); + f.exec("process-sources"); + MatcherAssert.assertThat( + "the build was clean", + f.log(), + RequisiteMatcher.SUCCESS + ); + } + ); + MatcherAssert.assertThat( + "the .java file is generated", + temp.resolve("target/generated-sources/EOone/EOfoo.java").toFile().exists(), + Matchers.is(true) + ); + MatcherAssert.assertThat( + "the package-info.java file contains the right package name", + Files.readString( + temp.resolve("target/generated-sources/EOone/package-info.java"), + StandardCharsets.UTF_8 + ), + Matchers.containsString("package EOone;") + ); + } + + @Test + void transpilesSimpleApp(@Mktmp final Path temp, @RandomProgram final String program) + throws Exception { + new Farea(temp).together( + f -> { + f.clean(); + f.files().file("src/main/eo/foo.eo").write(program.getBytes()); + f.build() + .plugins() + .appendItself() + .execution() + .goals("register", "parse", "optimize", "shake", "transpile") + .configuration() + .set("failOnWarning", "false"); + f.exec("process-sources"); + MatcherAssert.assertThat( + "the build was clean", + f.log(), + RequisiteMatcher.SUCCESS + ); + } + ); + MatcherAssert.assertThat( + "the .java file is re-generated", + temp.resolve("target/generated-sources/EOfoo.java").toFile().exists(), + Matchers.is(true) + ); + MatcherAssert.assertThat( + "the package-info.java file contains the right package name", + temp.resolve("target/generated-sources/package-info.java").toFile().exists(), + Matchers.is(false) + ); + } + @Test void recompilesIfModified(@Mktmp final Path temp) throws IOException { final FakeMaven maven = new FakeMaven(temp); From 804f50fca8cf22e0ab9cbfd4d0a53ad635c7be8c Mon Sep 17 00:00:00 2001 From: Yegor Bugayenko Date: Mon, 16 Dec 2024 14:44:18 +0300 Subject: [PATCH 3/4] #3678: simplified --- .../org/eolang/maven/TranspileMojoTest.java | 19 ++----------------- 1 file changed, 2 insertions(+), 17 deletions(-) diff --git a/eo-maven-plugin/src/test/java/org/eolang/maven/TranspileMojoTest.java b/eo-maven-plugin/src/test/java/org/eolang/maven/TranspileMojoTest.java index 4564b772ed..bdc8b15591 100755 --- a/eo-maven-plugin/src/test/java/org/eolang/maven/TranspileMojoTest.java +++ b/eo-maven-plugin/src/test/java/org/eolang/maven/TranspileMojoTest.java @@ -26,7 +26,6 @@ import com.yegor256.Mktmp; import com.yegor256.MktmpResolver; import com.yegor256.farea.Farea; -import com.yegor256.farea.RequisiteMatcher; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; @@ -108,15 +107,8 @@ void transpilesWithPackage(@Mktmp final Path temp) .plugins() .appendItself() .execution() - .goals("register", "parse", "optimize", "shake", "transpile") - .configuration() - .set("failOnWarning", "false"); + .goals("register", "parse", "optimize", "shake", "transpile"); f.exec("process-sources"); - MatcherAssert.assertThat( - "the build was clean", - f.log(), - RequisiteMatcher.SUCCESS - ); } ); MatcherAssert.assertThat( @@ -145,15 +137,8 @@ void transpilesSimpleApp(@Mktmp final Path temp, @RandomProgram final String pro .plugins() .appendItself() .execution() - .goals("register", "parse", "optimize", "shake", "transpile") - .configuration() - .set("failOnWarning", "false"); + .goals("register", "parse", "optimize", "shake", "transpile"); f.exec("process-sources"); - MatcherAssert.assertThat( - "the build was clean", - f.log(), - RequisiteMatcher.SUCCESS - ); } ); MatcherAssert.assertThat( From c562dbd47f5efdc2acca456c067b47860ddc406a Mon Sep 17 00:00:00 2001 From: Yegor Bugayenko Date: Mon, 16 Dec 2024 14:50:55 +0300 Subject: [PATCH 4/4] #3678: rename --- .../src/test/java/org/eolang/maven/TranspileMojoTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/eo-maven-plugin/src/test/java/org/eolang/maven/TranspileMojoTest.java b/eo-maven-plugin/src/test/java/org/eolang/maven/TranspileMojoTest.java index bdc8b15591..fe76cb1efc 100755 --- a/eo-maven-plugin/src/test/java/org/eolang/maven/TranspileMojoTest.java +++ b/eo-maven-plugin/src/test/java/org/eolang/maven/TranspileMojoTest.java @@ -127,12 +127,12 @@ void transpilesWithPackage(@Mktmp final Path temp) } @Test - void transpilesSimpleApp(@Mktmp final Path temp, @RandomProgram final String program) + void transpilesSimpleApp(@Mktmp final Path temp, @RandomProgram final String prog) throws Exception { new Farea(temp).together( f -> { f.clean(); - f.files().file("src/main/eo/foo.eo").write(program.getBytes()); + f.files().file("src/main/eo/foo.eo").write(prog.getBytes()); f.build() .plugins() .appendItself()