Skip to content

Commit

Permalink
Apply Java formatter.
Browse files Browse the repository at this point in the history
  • Loading branch information
petervdonovan committed May 26, 2023
1 parent 71d15c1 commit de058fb
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 39 deletions.
72 changes: 36 additions & 36 deletions buildSrc/src/main/java/lfformat/LfFormatStep.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
package lfformat;

import com.diffplug.spotless.FormatterFunc;
import com.diffplug.spotless.FormatterStep;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.Serializable;
import java.io.Writer;
import java.lang.ProcessBuilder.Redirect;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Locale;
Expand All @@ -41,6 +33,7 @@ private static class Step implements FormatterStep {
// The use of the static keyword here is a workaround for serialization difficulties.
/** The path to the lingua-franca repository. */
private static Path projectRoot;

private static Step instance;

private static Process formatter;
Expand All @@ -56,16 +49,19 @@ public static Step getInstance() throws IOException {

private Step() throws IOException {
initializeFormatter();
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
try {
writer.close();
formatter.waitFor();
reader.close();
error.close();
} catch (IOException | InterruptedException e) {
throw new RuntimeException(e);
}
}));
Runtime.getRuntime()
.addShutdownHook(
new Thread(
() -> {
try {
writer.close();
formatter.waitFor();
reader.close();
error.close();
} catch (IOException | InterruptedException e) {
throw new RuntimeException(e);
}
}));
}

@Override
Expand All @@ -81,7 +77,10 @@ public String format(
formatter.waitFor();
error.lines().forEach(System.out::println);
initializeFormatter();
throw new RuntimeException("Failed to format " + file + ".\nPlease ensure that this file passes validator checks.");
throw new RuntimeException(
"Failed to format "
+ file
+ ".\nPlease ensure that this file passes validator checks.");
}
String line = reader.readLine();
while (line != null && !line.endsWith("\0")) {
Expand All @@ -105,28 +104,29 @@ private void initializeFormatter() throws IOException {
"build",
"libs",
String.format("org.lflang-%s.jar", properties.getString("VERSION")));
formatter = new ProcessBuilder(
List.of(
"java",
"-cp",
lffPath.toString(),
"org.lflang.cli.Lff",
"--dry-run",
"--stdin"))
.start();
formatter =
new ProcessBuilder(
List.of(
"java",
"-cp",
lffPath.toString(),
"org.lflang.cli.Lff",
"--dry-run",
"--stdin"))
.start();
writer = new BufferedWriter(new OutputStreamWriter(formatter.getOutputStream()));
reader = new BufferedReader(new InputStreamReader(formatter.getInputStream()));
error = new BufferedReader(new InputStreamReader(formatter.getErrorStream()));
}

// /** Run the formatter on the given file and return the resulting process handle. */
// private Process runFormatter(File file) throws IOException {
// // It looks silly to invoke Java from Java, but it is necessary in
// // order to break the circularity of needing the program to be built
// // in order for it to be built.
// var formatter = getFormatter();
// return formatter;
// }
// /** Run the formatter on the given file and return the resulting process handle. */
// private Process runFormatter(File file) throws IOException {
// // It looks silly to invoke Java from Java, but it is necessary in
// // order to break the circularity of needing the program to be built
// // in order for it to be built.
// var formatter = getFormatter();
// return formatter;
// }

@SuppressWarnings("NullableProblems")
@Override
Expand Down
4 changes: 1 addition & 3 deletions org.lflang/src/org/lflang/cli/CliBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import com.google.inject.Inject;
import com.google.inject.Injector;
import com.google.inject.Provider;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
Expand Down Expand Up @@ -186,8 +185,7 @@ protected List<Path> getInputPaths() {
if (line == null) return List.of();
return List.of(Path.of(line));
} else {
paths =
topLevelArg.files.stream().map(io.getWd()::resolve).collect(Collectors.toList());
paths = topLevelArg.files.stream().map(io.getWd()::resolve).collect(Collectors.toList());
}

for (Path path : paths) {
Expand Down

0 comments on commit de058fb

Please sign in to comment.