-
Notifications
You must be signed in to change notification settings - Fork 16
/
fmt.java
61 lines (54 loc) · 2.48 KB
/
fmt.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
///usr/bin/env jbang "$0" "$@" ; exit $?
//DEPS info.picocli:picocli:4.6.3
//DEPS org.jboss.forge.roaster:roaster-jdt:2.28.0.Final
//DEPS io.quarkus:quarkus-ide-config:2.16.3.Final
//DESCRIPTION This command will format your sources using the same formatting rules used in the Quarkus Core project
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Properties;
import java.util.concurrent.Callable;
import org.jboss.forge.roaster.Roaster;
import org.jboss.forge.roaster.model.util.FormatterProfileReader;
import picocli.CommandLine;
import static java.lang.System.out;
@CommandLine.Command(name = "format", aliases = "fmt", header = "Format your source code", description = "%n"
+ "This command will format your sources using the same formatting rules used in the Quarkus Core project", footer = {
"%n"
+ "For example, the following command will format the current directory:" + "%n"
+ "quarkus fmt" + "%n" })
public class fmt implements Callable<Integer> {
public static void main(String... args) {
int exitCode = new CommandLine(new fmt()).execute(args);
System.exit(exitCode);
}
@Override
public Integer call() throws Exception {
Properties formatterProperties;
try (InputStream eclipseFormat = getClass().getClassLoader().getResourceAsStream("eclipse-format.xml")) {
FormatterProfileReader reader = FormatterProfileReader.fromEclipseXml(eclipseFormat);
formatterProperties = reader.getPropertiesFor("Quarkus");
}
Path root = Paths.get(".");
Files.walkFileTree(root, new SimpleFileVisitor<>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
if (file.toString().endsWith(".java")) {
String source = Files.readString(file);
String formattedSource = Roaster.format(formatterProperties, source);
if (!formattedSource.equals(source)) {
out.println(root.relativize(file));
Files.writeString(file, formattedSource);
}
}
return FileVisitResult.CONTINUE;
}
});
return 0;
}
}