diff --git a/eo-maven-plugin/src/main/java/org/eolang/maven/MjTranspile.java b/eo-maven-plugin/src/main/java/org/eolang/maven/MjTranspile.java index f53e094e59f..b2311d59614 100644 --- a/eo-maven-plugin/src/main/java/org/eolang/maven/MjTranspile.java +++ b/eo-maven-plugin/src/main/java/org/eolang/maven/MjTranspile.java @@ -16,17 +16,14 @@ import com.yegor256.xsline.TrJoined; import com.yegor256.xsline.Train; import com.yegor256.xsline.Xsline; -import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.util.Collection; -import java.util.List; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; import java.util.function.Function; import java.util.function.Supplier; -import java.util.regex.Pattern; import java.util.stream.Collectors; import org.apache.maven.plugins.annotations.LifecyclePhase; import org.apache.maven.plugins.annotations.Mojo; @@ -70,11 +67,6 @@ public final class MjTranspile extends MjSafe { */ private static final String JAVA = "java"; - /** - * Pattern for replacing EO in package. - */ - private static final Pattern PACKAGE = Pattern.compile("EO"); - /** * Parsing train with XSLs. */ @@ -123,7 +115,7 @@ public void exec() throws IOException { final int saved = new Threaded<>( sources, this::transpiled - ).total() + MjTranspile.pinfos(this.generatedDir.toPath()); + ).total() + new PackageInfos(this.generatedDir.toPath()).create(); Logger.info( this, "Transpiled %d XMIRs, created %d Java files in %[file]s", sources.size(), saved, this.generatedDir @@ -253,53 +245,6 @@ clazz, new FileGenerationReport(saved, tgt, target) return saved.get(); } - /** - * Create {@code package-info.java} files in all the directories - * in {@link MjTranspile#generatedDir}. - * @param generated Path to generated sources - * @return Amount of created files - * @throws IOException If fails to create a file - * @todo #4717:90min Move {@link #pinfos(Path)} method to a separate class. - * Currently, this method violates Single Responsibility Principle of - * MjTranspile class. After moving, make sure to cover it with unit tests. - */ - private static int pinfos(final Path generated) throws IOException { - final int size; - if (Files.exists(generated)) { - final List dirs = Files.walk(generated) - .filter(file -> Files.isDirectory(file) && !file.equals(generated)) - .collect(Collectors.toList()); - for (final Path dir : dirs) { - final String pkg = generated.relativize(dir).toString() - .replace(File.separator, "."); - final Path saved = new Saved( - String.join( - "\n", - "/**", - " * This file was auto-generated by eo-maven-plugin,", - " * don't modify it, all changes will be lost anyway.", - " */", - String.format( - "// @org.eolang.XmirPackage(\"%s\")", - MjTranspile.PACKAGE.matcher(pkg).replaceAll("") - ), - String.format("package %s;", pkg) - ), - dir.resolve("package-info.java") - ).value(); - Logger.debug(MjTranspile.class, "Created %s", saved); - } - size = dirs.size(); - } else { - Logger.info( - MjTranspile.class, - "No generated sources found, skipping package-info.java creation" - ); - size = 0; - } - return size; - } - /** * Cached path. * @param hsh Hash diff --git a/eo-maven-plugin/src/main/java/org/eolang/maven/PackageInfos.java b/eo-maven-plugin/src/main/java/org/eolang/maven/PackageInfos.java new file mode 100644 index 00000000000..996edbaf464 --- /dev/null +++ b/eo-maven-plugin/src/main/java/org/eolang/maven/PackageInfos.java @@ -0,0 +1,84 @@ +/* + * SPDX-FileCopyrightText: Copyright (c) 2016-2026 Objectionary.com + * SPDX-License-Identifier: MIT + */ +package org.eolang.maven; + +import com.jcabi.log.Logger; +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.List; +import java.util.regex.Pattern; +import java.util.stream.Collectors; + +/** + * Package info classes. + * @since 0.60 + */ +final class PackageInfos { + + /** + * Pattern for replacing EO in package. + */ + private static final Pattern PACKAGE = Pattern.compile("EO"); + + /** + * Directory where create package info files. + */ + private final Path root; + + /** + * Constructor. + * @param root In which directory create files. + */ + PackageInfos(final Path root) { + this.root = root; + } + + /** + * Create {@code package-info.java} files in all the directories under the {@link #root}. + * @return Amount of created files + * @throws IOException If fails to create a file + */ + int create() throws IOException { + final int size; + if (Files.exists(this.root)) { + final List dirs = Files.walk(this.root) + .filter(file -> Files.isDirectory(file) && !file.equals(this.root)) + .collect(Collectors.toList()); + for (final Path dir : dirs) { + final String pkg = this.root.relativize(dir).toString() + .replace(File.separator, "."); + final Path saved = new Saved( + PackageInfos.content(pkg), dir.resolve("package-info.java") + ).value(); + Logger.debug(this, "Created %s", saved); + } + size = dirs.size(); + } else { + Logger.info( + this, + "No generated sources found, skipping package-info.java creation" + ); + size = 0; + } + return size; + } + + private static String content(final String pkg) { + return String.join( + "\n", + "/**", + " * This file was auto-generated by eo-maven-plugin,", + " * don't modify it, all changes will be lost anyway.", + " */", + String.format( + "// @org.eolang.XmirPackage(\"%s\")", + PackageInfos.PACKAGE.matcher(pkg).replaceAll("") + ), + String.format("package %s;", pkg) + ); + } +} diff --git a/eo-maven-plugin/src/test/java/org/eolang/maven/MjTranspileTest.java b/eo-maven-plugin/src/test/java/org/eolang/maven/MjTranspileTest.java index 026a7b4294d..323ea9ae3c2 100644 --- a/eo-maven-plugin/src/test/java/org/eolang/maven/MjTranspileTest.java +++ b/eo-maven-plugin/src/test/java/org/eolang/maven/MjTranspileTest.java @@ -168,7 +168,6 @@ void createsPackageInfoFilesForAllPackages(@Mktmp final Path temp) throws IOExce ); } - @Disabled @Test void savesValidContentToPackageInfoFile(@Mktmp final Path temp) throws Exception { MatcherAssert.assertThat( diff --git a/eo-maven-plugin/src/test/java/org/eolang/maven/PackageInfosTest.java b/eo-maven-plugin/src/test/java/org/eolang/maven/PackageInfosTest.java new file mode 100644 index 00000000000..468f398015a --- /dev/null +++ b/eo-maven-plugin/src/test/java/org/eolang/maven/PackageInfosTest.java @@ -0,0 +1,56 @@ +/* + * SPDX-FileCopyrightText: Copyright (c) 2016-2026 Objectionary.com + * SPDX-License-Identifier: MIT + */ +package org.eolang.maven; + +import com.yegor256.Mktmp; +import com.yegor256.MktmpResolver; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; + +/** + * Test cases for {@link PackageInfos}. + * @since 0.60 + */ +@ExtendWith(MktmpResolver.class) +final class PackageInfosTest { + + @Test + void createsPackageInfosInSubDirectories(@Mktmp final Path tmp) throws IOException { + final Path subdir = tmp.resolve("subdir"); + final Path subsubdir = subdir.resolve("subsubdir"); + Files.createDirectory(subdir); + Files.createDirectories(subsubdir); + MatcherAssert.assertThat( + "We should create exactly two package-info.java files for two subdirectories", + new PackageInfos(tmp).create(), + Matchers.equalTo(2) + ); + MatcherAssert.assertThat( + "package-info.java should be created in the both subdirectories", + Files.exists(subdir.resolve("package-info.java")) + && Files.exists(subsubdir.resolve("package-info.java")), + Matchers.is(true) + ); + } + + @Test + void ignoresTheRootDirectoryItself(@Mktmp final Path tmp) throws IOException { + MatcherAssert.assertThat( + "No package-info.java files should be created in the root directory", + new PackageInfos(tmp).create(), + Matchers.equalTo(0) + ); + MatcherAssert.assertThat( + "package-info.java should not be created in the root directory", + Files.exists(tmp.resolve("package-info.java")), + Matchers.is(false) + ); + } +}