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..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 @@ -253,7 +253,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(), @@ -322,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( @@ -335,7 +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;", oname.substring(0, oname.lastIndexOf('.'))) + 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..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 @@ -25,7 +25,9 @@ import com.yegor256.Mktmp; import com.yegor256.MktmpResolver; +import com.yegor256.farea.Farea; 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 +56,7 @@ */ @SuppressWarnings({"PMD.AvoidDuplicateLiterals", "PMD.TooManyMethods"}) @ExtendWith(MktmpResolver.class) +@ExtendWith(RandomProgramResolver.class) final class TranspileMojoTest { /** @@ -82,6 +85,74 @@ 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"); + f.exec("process-sources"); + } + ); + 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 prog) + throws Exception { + new Farea(temp).together( + f -> { + f.clean(); + f.files().file("src/main/eo/foo.eo").write(prog.getBytes()); + f.build() + .plugins() + .appendItself() + .execution() + .goals("register", "parse", "optimize", "shake", "transpile"); + f.exec("process-sources"); + } + ); + 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);