Skip to content

Commit

Permalink
Add a new Maven plugin to simplify the creation of aggregated javadoc…
Browse files Browse the repository at this point in the history
  • Loading branch information
romain-grecourt committed Dec 20, 2023
1 parent fe2ce84 commit db58709
Show file tree
Hide file tree
Showing 43 changed files with 4,514 additions and 185 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, 2022 Oracle and/or its affiliates.
* Copyright (c) 2021, 2023 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -26,7 +26,7 @@
*/
final class ConsoleRecorder {

private static final String EOL = System.getProperty("line.separator");
private static final String EOL = System.lineSeparator();
private final StringBuilder capturedOutput = new StringBuilder();
private final StringBuilder capturedStdOut = new StringBuilder();
private final StringBuilder capturedStdErr = new StringBuilder();
Expand All @@ -37,27 +37,31 @@ final class ConsoleRecorder {
private final PrintStream stdErr;
private LineReader stdOutReader;
private LineReader stdErrReader;
private final boolean autoEol;

/**
* Create a new output forwarder.
*
* @param stdOut print stream for {@code stdout}
* @param stdErr print stream for {@code stderr}
* @param filter predicate to filter the lines to print
* @param transform function to transform the lines to print
* @param recording {@code true} if the output should be captured
* @param stdOut print stream for {@code stdout}
* @param stdErr print stream for {@code stderr}
* @param filter predicate to filter the lines to print
* @param transform function to transform the lines to print
* @param recording {@code true} if the output should be captured
* @param autoEol {@code true} if new line character should be added to captured lines
*/
ConsoleRecorder(PrintStream stdOut,
PrintStream stdErr,
Predicate<String> filter,
Function<String, String> transform,
boolean recording) {
boolean recording,
boolean autoEol) {

this.filter = filter;
this.transform = transform;
this.capturing = recording;
this.stdOut = PrintStreams.delegate(stdOut, (printer, str) -> print(printer, str, capturedStdOut));
this.stdErr = PrintStreams.delegate(stdErr, (printer, str) -> print(printer, str, capturedStdErr));
this.autoEol = autoEol;
}

/**
Expand Down Expand Up @@ -129,7 +133,10 @@ String capturedStdErr() {
}

private void print(PrintStream printer, String str, StringBuilder capture) {
String line = str + EOL;
String line = str;
if (autoEol) {
line += EOL;
}
if (filter.test(str)) {
printer.print(transform.apply(line));
}
Expand Down
187 changes: 123 additions & 64 deletions common/common/src/main/java/io/helidon/build/common/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import java.nio.file.attribute.FileTime;
import java.nio.file.attribute.PosixFilePermission;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Base64;
import java.util.Comparator;
Expand All @@ -54,6 +55,8 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import io.helidon.build.common.logging.Log;

import static java.nio.charset.StandardCharsets.UTF_8;
import static java.nio.file.FileSystems.getFileSystem;
import static java.nio.file.FileSystems.newFileSystem;
Expand All @@ -74,7 +77,6 @@ public final class FileUtils {
private static final Path TMPDIR = Path.of(System.getProperty("java.io.tmpdir"));
private static final Random RANDOM = new Random();


/**
* The working directory.
*/
Expand All @@ -100,7 +102,7 @@ public final class FileUtils {
*/
public static Path requiredDirectoryFromProperty(String systemPropertyName, boolean createIfRequired) {
final String path = Requirements.requireNonNull(System.getProperty(systemPropertyName),
"Required system property %s not set", systemPropertyName);
"Required system property %s not set", systemPropertyName);
return requiredDirectory(path, createIfRequired);
}

Expand Down Expand Up @@ -253,7 +255,7 @@ public static List<Path> list(Path directory) {
* @param maxDepth The maximum recursion depth.
* @return The normalized, absolute file paths.
*/
public static List<Path> list(Path directory, final int maxDepth) {
public static List<Path> list(Path directory, int maxDepth) {
try (Stream<Path> pathStream = Files.find(requireDirectory(directory), maxDepth, (path, attrs) -> true)) {
return pathStream
.collect(Collectors.toList());
Expand All @@ -262,6 +264,52 @@ public static List<Path> list(Path directory, final int maxDepth) {
}
}

/**
* Walk the directory and return the files that match the given predicate.
* If a directory is filtered out by the predicate its subtree is skipped.
*
* @param directory The directory
* @param predicate predicate used to filter files and directories
* @return The normalized, absolute file paths.
*/
public static List<Path> walk(Path directory, BiPredicate<Path, BasicFileAttributes> predicate) {
try {
List<Path> files = new ArrayList<>();
Files.walkFileTree(directory, new FileVisitor<>() {
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
if (predicate.test(dir, attrs)) {
return FileVisitResult.CONTINUE;
}
return FileVisitResult.SKIP_SUBTREE;
}

@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
if (predicate.test(file, attrs)) {
files.add(file);
return FileVisitResult.CONTINUE;
}
return FileVisitResult.SKIP_SUBTREE;
}

@Override
public FileVisitResult visitFileFailed(Path file, IOException ex) {
Log.warn(ex, ex.getMessage());
return FileVisitResult.SKIP_SIBLINGS;
}

@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) {
return FileVisitResult.CONTINUE;
}
});
return files;
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
}

/**
* Check that the given path exists and is a directory.
*
Expand Down Expand Up @@ -358,13 +406,13 @@ public static Path deleteDirectory(Path directory) {
if (Files.isDirectory(directory)) {
try (Stream<Path> stream = Files.walk(directory)) {
stream.sorted(Comparator.reverseOrder())
.forEach(file -> {
try {
Files.delete(file);
} catch (IOException ioe) {
throw new UncheckedIOException(ioe);
}
});
.forEach(file -> {
try {
Files.delete(file);
} catch (IOException ioe) {
throw new UncheckedIOException(ioe);
}
});
} catch (IOException ioe) {
throw new UncheckedIOException(ioe);
}
Expand All @@ -388,14 +436,14 @@ public static Path deleteDirectoryContent(Path directory) throws IOException {
//noinspection DuplicatedCode
try (Stream<Path> stream = Files.walk(directory)) {
stream.sorted(Comparator.reverseOrder())
.filter(file -> !file.equals(directory))
.forEach(file -> {
try {
Files.delete(file);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
});
.filter(file -> !file.equals(directory))
.forEach(file -> {
try {
Files.delete(file);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
});
}
} else {
throw new IllegalArgumentException(directory + " is not a directory");
Expand Down Expand Up @@ -559,10 +607,10 @@ public static String fileName(Path file) {
*/
public static Optional<Path> findExecutableInPath(String executableName) {
return Arrays.stream(requireNonNull(System.getenv(PATH_VAR)).split(File.pathSeparator))
.map(Paths::get)
.map(path -> path.resolve(executableName))
.filter(Files::isExecutable)
.findFirst();
.map(Paths::get)
.map(path -> path.resolve(executableName))
.filter(Files::isExecutable)
.findFirst();
}

/**
Expand All @@ -587,8 +635,8 @@ public static Optional<Path> javaExecutable() {
*/
public static Path requireJavaExecutable() {
return javaExecutable().orElseThrow(() -> new IllegalStateException(JAVA_BINARY_NAME
+ " not found. Please add it to"
+ " your PATH or set the JAVA_HOME or variable."));
+ " not found. Please add it to"
+ " your PATH or set the JAVA_HOME or variable."));
}

/**
Expand All @@ -601,24 +649,34 @@ public static Optional<Path> javaExecutableInPath() {
}

/**
* Returns the path to the java executable using the {@code JAVA_HOME} var if present and valid.
* Returns the path to the given executable using the {@code JAVA_HOME} var if present and valid.
*
* @param name executable name
* @return The path.
*/
public static Optional<Path> javaExecutableInJavaHome() {
public static Optional<Path> findExecutableInJavaHome(String name) {
final String javaHomePath = System.getenv(JAVA_HOME_VAR);
if (javaHomePath != null) {
final Path javaHome = Paths.get(javaHomePath);
final Path binary = javaHome.resolve(BIN_DIR_NAME).resolve(JAVA_BINARY_NAME);
final Path binary = javaHome.resolve(BIN_DIR_NAME).resolve(name);
if (Files.isExecutable(binary)) {
return Optional.of(binary);
} else {
throw new IllegalStateException(JAVA_BINARY_NAME + " not found in JAVA_HOME path: " + javaHomePath);
throw new IllegalStateException(name + " not found in JAVA_HOME path: " + javaHomePath);
}
}
return Optional.empty();
}

/**
* Returns the path to the java executable using the {@code JAVA_HOME} var if present and valid.
*
* @return The path.
*/
public static Optional<Path> javaExecutableInJavaHome() {
return findExecutableInJavaHome(JAVA_BINARY_NAME);
}

/**
* Creates the given file (with no content) if it does not already exist.
*
Expand Down Expand Up @@ -747,37 +805,38 @@ public static FileSystem newZipFileSystem(Path zip) {
* @return zip file
*/
public static Path zip(Path zip, Path directory) {
return zip(zip, directory, path -> {});
return zip(zip, directory, path -> {
});
}

/**
* Zip a directory.
*
* @param zip target file
* @param directory source directory
* @param fileConsumer zipped file consumer
* @param zip target file
* @param directory source directory
* @param fileConsumer zipped file consumer
* @return zip file
*/
public static Path zip(Path zip, Path directory, Consumer<Path> fileConsumer) {
ensureDirectory(zip.getParent());
try (FileSystem fs = newZipFileSystem(zip)) {
try (Stream<Path> entries = Files.walk(directory)) {
entries.sorted(Comparator.reverseOrder())
.filter(p -> Files.isRegularFile(p) && !p.equals(zip))
.map(p -> {
try {
Path target = fs.getPath(directory.relativize(p).toString());
Path parent = target.getParent();
if (parent != null) {
Files.createDirectories(parent);
}
Files.copy(p, target, REPLACE_EXISTING);
return target;
} catch (IOException ioe) {
throw new UncheckedIOException(ioe);
}
})
.forEach(fileConsumer);
.filter(p -> Files.isRegularFile(p) && !p.equals(zip))
.map(p -> {
try {
Path target = fs.getPath(directory.relativize(p).toString());
Path parent = target.getParent();
if (parent != null) {
Files.createDirectories(parent);
}
Files.copy(p, target, REPLACE_EXISTING);
return target;
} catch (IOException ioe) {
throw new UncheckedIOException(ioe);
}
})
.forEach(fileConsumer);
}
return zip;
} catch (IOException ioe) {
Expand Down Expand Up @@ -808,22 +867,22 @@ public static void unzip(Path zip, Path directory) {
Path root = fs.getRootDirectories().iterator().next();
try (Stream<Path> entries = Files.walk(root)) {
entries.filter(p -> !p.equals(root))
.forEach(file -> {
Path filePath = directory.resolve(Path.of(file.toString().substring(1)));
try {
if (Files.isDirectory(file)) {
Files.createDirectories(filePath);
} else {
Files.copy(file, filePath);
}
if (posix) {
Set<PosixFilePermission> perms = Files.getPosixFilePermissions(file);
Files.setPosixFilePermissions(filePath, perms);
}
} catch (IOException ioe) {
throw new UncheckedIOException(ioe);
}
});
.forEach(file -> {
Path filePath = directory.resolve(Path.of(file.toString().substring(1)));
try {
if (Files.isDirectory(file)) {
Files.createDirectories(filePath);
} else {
Files.copy(file, filePath);
}
if (posix) {
Set<PosixFilePermission> perms = Files.getPosixFilePermissions(file);
Files.setPosixFilePermissions(filePath, perms);
}
} catch (IOException ioe) {
throw new UncheckedIOException(ioe);
}
});
}
} catch (IOException e) {
throw new UncheckedIOException(e);
Expand Down Expand Up @@ -871,7 +930,7 @@ public static Path pathOf(URI uri, ClassLoader classLoader) {
/**
* Get the path for the given URL.
*
* @param url url
* @param url url
* @return Path
*/
public static Path pathOf(URL url) {
Expand Down
Loading

0 comments on commit db58709

Please sign in to comment.