Last active
October 22, 2020 13:12
-
-
Save SupunS/a08dc09b8fdd0d257a7abca708269efe to your computer and use it in GitHub Desktop.
BalFormatter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.FileWriter; | |
import java.io.IOException; | |
import java.nio.file.Path; | |
import java.nio.file.Paths; | |
import java.nio.file.Files; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.List; | |
import java.util.stream.Stream; | |
import io.ballerina.compiler.internal.parser.BallerinaParser; | |
import io.ballerina.compiler.internal.parser.ParserFactory; | |
import io.ballerina.compiler.internal.parser.tree.STNode; | |
import org.ballerinalang.formatter.core.Formatter; | |
public class BalFormatter { | |
private static List<String> skipList = new ArrayList<>(); | |
public static void main(String[] args) { | |
Path rootDir = Paths.get(args[0]); | |
updateSkipList(); | |
try { | |
try (Stream<Path> paths = Files.find(rootDir, | |
Integer.MAX_VALUE, | |
(path, file) -> file.isRegularFile())) { | |
paths.forEach(path -> format(path)); | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
private static void format(Path path) { | |
try { | |
if (!path.toString().endsWith(".bal")) { | |
return; | |
} | |
if (skipList.contains(path.getFileName().toString())) { | |
return; | |
} | |
String content = new String(Files.readAllBytes(path)); | |
BallerinaParser parser = ParserFactory.getParser(content); | |
STNode rootNode = parser.parse(); | |
if (rootNode.hasDiagnostics()) { | |
return; | |
} | |
String formattedTree = Formatter.format(content); | |
FileWriter writer = new FileWriter(path.toString()); | |
writer.write(formattedTree); | |
writer.close(); | |
System.out.println(path); | |
} catch (Exception e) { | |
System.out.println("********** ERROR: " + path); | |
} | |
} | |
private static void updateSkipList() { | |
String[] s = new String[] { | |
"typed_binding_patterns_source_02.bal" | |
}; | |
skipList = Arrays.asList(s); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment