diff options
| author | Michael Williamson <mike@zwobble.org> | 2026-10-24 22:31:47 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2026-10-24 22:31:47 +0100 |
| commit | 55bf1c82276e401ea02488dcd621b7c0dbeda071 (patch) | |
| tree | a291f485654a76292f6e02e6c30c694184aa8256 /src | |
| parent | 76dba2d183609094788afafe0cebd2045b212882 (diff) | |
Extract CodeWriterConfig
Diffstat (limited to 'src')
6 files changed, 71 insertions, 41 deletions
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/CodeWriter.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/CodeWriter.java index 81fd091..b9c5d85 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/output/CodeWriter.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/CodeWriter.java @@ -10,6 +10,30 @@ import java.nio.file.Path; import java.util.*; public class CodeWriter implements AutoCloseable { + public static class CodeWriterConfig { + private final String header; + private final String customAreaStart; + private final String customAreaEnd; + + public CodeWriterConfig(String lineCommentStart) { + this.header = lineCommentStart + " Generated by hobgoblin.\n\n"; + this.customAreaStart = lineCommentStart + " Custom area start: "; + this.customAreaEnd = lineCommentStart + " Custom area end: "; + } + + public String header() { + return this.header; + } + + public String customAreaStart() { + return customAreaStart; + } + + public String customAreaEnd() { + return customAreaEnd; + } + } + public interface CodeWriterFile { String readString() throws IOException; Writer openWriter() throws IOException; @@ -51,13 +75,11 @@ public class CodeWriter implements AutoCloseable { public record CustomAreas( Map<String, List<String>> existing, - String customAreaStart, - String customAreaEnd + CodeWriterConfig config ) { public static CustomAreas read( String existingString, - String customAreaStart, - String customAreaEnd + CodeWriterConfig config ) { var existing = new HashMap<String, List<String>>(); @@ -66,9 +88,9 @@ public class CodeWriter implements AutoCloseable { var customAreaLines = new ArrayList<String>(); for (var line : lines) { var trimmedLine = line.trim(); - if (trimmedLine.startsWith(customAreaStart)) { - name = Optional.of(trimmedLine.substring(customAreaStart.length())); - } else if (name.isPresent() && trimmedLine.startsWith(customAreaEnd + name.get())) { + if (trimmedLine.startsWith(config.customAreaStart)) { + name = Optional.of(trimmedLine.substring(config.customAreaStart.length())); + } else if (name.isPresent() && trimmedLine.startsWith(config.customAreaEnd + name.get())) { existing.put(name.get(), customAreaLines); customAreaLines = new ArrayList<>(); name = Optional.empty(); @@ -77,14 +99,19 @@ public class CodeWriter implements AutoCloseable { } } - return new CustomAreas(existing, customAreaStart, customAreaEnd); + return new CustomAreas(existing, config); } - public static CustomAreas empty( - String customAreaStart, - String customAreaEnd - ) { - return new CustomAreas(Map.of(), customAreaStart, customAreaEnd); + public static CustomAreas empty(CodeWriterConfig config) { + return new CustomAreas(Map.of(), config); + } + + public String customAreaStart() { + return this.config.customAreaStart; + } + + public String customAreaEnd() { + return this.config.customAreaEnd; } public Iterable<String> lines(String name) { @@ -103,7 +130,7 @@ public class CodeWriter implements AutoCloseable { public static void writeTo( Path path, - String lineCommentStart, + CodeWriterConfig config, Write write ) throws IOException { writeTo( @@ -119,43 +146,38 @@ public class CodeWriter implements AutoCloseable { return new FileWriter(path.toFile()); } }, - lineCommentStart, + config, write ); } public static void writeTo( CodeWriterFile file, - String lineCommentStart, + CodeWriterConfig config, Write write ) throws IOException { var originalContents = readFile(file); - var header = lineCommentStart + " Generated by hobgoblin.\n\n"; - if (originalContents.isPresent() && !originalContents.get().startsWith(header)) { + if (originalContents.isPresent() && !originalContents.get().startsWith(config.header)) { return; } - var customAreaStart = lineCommentStart + " Custom area start: "; - var customAreaEnd = lineCommentStart + " Custom area end: "; - - var customAreas = readCustomAreas(file, customAreaStart, customAreaEnd); + var customAreas = readCustomAreas(file, config); try (var writer = file.openWriter()) { // TODO: warn if custom areas are unused? - write.write(new CodeWriter(writer, header, customAreas)); + write.write(new CodeWriter(writer, config.header, customAreas)); } } private static CustomAreas readCustomAreas( CodeWriterFile file, - String customAreaStart, - String customAreaEnd + CodeWriterConfig config ) throws IOException { try { var contents = file.readString(); - return CustomAreas.read(contents, customAreaStart, customAreaEnd); + return CustomAreas.read(contents, config); } catch (NoSuchFileException exception) { - return CustomAreas.empty(customAreaStart, customAreaEnd); + return CustomAreas.empty(config); } } @@ -210,7 +232,7 @@ public class CodeWriter implements AutoCloseable { } public void writeCustomArea(String name) throws IOException { - this.write(this.customAreas.customAreaStart); + this.write(this.customAreas.customAreaStart()); this.write(name); for (var line : this.customAreas.lines(name)) { @@ -219,7 +241,7 @@ public class CodeWriter implements AutoCloseable { } this.newLine(); - this.write(this.customAreas.customAreaEnd); + this.write(this.customAreas.customAreaEnd()); this.write(name); this.newLine(); } diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/JavaWriter.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/JavaWriter.java index d2f46ad..2c659a7 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/JavaWriter.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/JavaWriter.java @@ -14,6 +14,8 @@ import static org.zwobble.hobgoblin.compiler.output.CodeWriter.writeWithSeparato public class JavaWriter implements AutoCloseable { private static final String LINE_COMMENT_START = "//"; + private static final CodeWriter.CodeWriterConfig CONFIG = + new CodeWriter.CodeWriterConfig(LINE_COMMENT_START); public interface Write { void write(JavaWriter writer) throws IOException; @@ -22,7 +24,7 @@ public class JavaWriter implements AutoCloseable { public static void writeTo(Path path, Write write) throws IOException { CodeWriter.writeTo( path, - LINE_COMMENT_START, + CONFIG, writer -> write.write(new JavaWriter(writer)) ); } @@ -30,7 +32,7 @@ public class JavaWriter implements AutoCloseable { public static void writeTo(CodeWriter.CodeWriterFile file, Write write) throws IOException { CodeWriter.writeTo( file, - LINE_COMMENT_START, + CONFIG, writer -> write.write(new JavaWriter(writer)) ); } diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/RustWriter.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/RustWriter.java index df8aa8c..8ecf2bc 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/RustWriter.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/RustWriter.java @@ -14,6 +14,8 @@ import static org.zwobble.hobgoblin.compiler.output.CodeWriter.writeWithSeparato public class RustWriter implements AutoCloseable { private static final String LINE_COMMENT_START = "//"; + private static final CodeWriter.CodeWriterConfig CONFIG = + new CodeWriter.CodeWriterConfig(LINE_COMMENT_START); public interface Write { void write(RustWriter writer) throws IOException; @@ -22,7 +24,7 @@ public class RustWriter implements AutoCloseable { public static void writeTo(Path path, Write write) throws IOException { CodeWriter.writeTo( path, - LINE_COMMENT_START, + CONFIG, writer -> write.write(new RustWriter(writer)) ); } @@ -30,7 +32,7 @@ public class RustWriter implements AutoCloseable { public static void writeTo(CodeWriter.CodeWriterFile file, Write write) throws IOException { CodeWriter.writeTo( file, - LINE_COMMENT_START, + CONFIG, writer -> write.write(new RustWriter(writer)) ); } diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/SqlWriter.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/SqlWriter.java index b6edc1b..4c85a5a 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/SqlWriter.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/SqlWriter.java @@ -10,6 +10,8 @@ import static org.zwobble.hobgoblin.compiler.output.CodeWriter.writeWithSeparato public class SqlWriter { private static final String LINE_COMMENT_START = "--"; + private static final CodeWriter.CodeWriterConfig CONFIG = + new CodeWriter.CodeWriterConfig(LINE_COMMENT_START); public interface Write { void write(SqlWriter writer) throws IOException; @@ -18,7 +20,7 @@ public class SqlWriter { public static void writeTo(Path path, Write write) throws IOException { CodeWriter.writeTo( path, - LINE_COMMENT_START, + CONFIG, writer -> write.write(new SqlWriter(writer)) ); } @@ -26,7 +28,7 @@ public class SqlWriter { public static void writeTo(CodeWriter.CodeWriterFile file, Write write) throws IOException { CodeWriter.writeTo( file, - LINE_COMMENT_START, + CONFIG, writer -> write.write(new SqlWriter(writer)) ); } diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/TypescriptWriter.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/TypescriptWriter.java index 3d5d99f..ab17902 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/TypescriptWriter.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/TypescriptWriter.java @@ -11,6 +11,8 @@ import static org.zwobble.hobgoblin.compiler.output.CodeWriter.writeWithSeparato public class TypescriptWriter implements AutoCloseable { private static final String LINE_COMMENT_START = "//"; + private static final CodeWriter.CodeWriterConfig CONFIG = + new CodeWriter.CodeWriterConfig(LINE_COMMENT_START); public interface Write { void write(TypescriptWriter writer) throws IOException; @@ -19,7 +21,7 @@ public class TypescriptWriter implements AutoCloseable { public static void writeTo(Path path, Write write) throws IOException { CodeWriter.writeTo( path, - LINE_COMMENT_START, + CONFIG, writer -> write.write(new TypescriptWriter(writer)) ); } @@ -27,7 +29,7 @@ public class TypescriptWriter implements AutoCloseable { public static void writeTo(CodeWriter.CodeWriterFile file, Write write) throws IOException { CodeWriter.writeTo( file, - LINE_COMMENT_START, + CONFIG, writer -> write.write(new TypescriptWriter(writer)) ); } diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/CodeWriterTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/CodeWriterTests.java index 2b3ad6d..ae61cb8 100644 --- a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/CodeWriterTests.java +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/CodeWriterTests.java @@ -18,7 +18,7 @@ public class CodeWriterTests { CodeWriter.writeTo( file, - "//", + new CodeWriter.CodeWriterConfig("//"), codeWriter -> { codeWriter.writeHeader(); codeWriter.write("Before"); @@ -51,7 +51,7 @@ public class CodeWriterTests { CodeWriter.writeTo( file, - "//", + new CodeWriter.CodeWriterConfig("//"), codeWriter -> { codeWriter.writeHeader(); codeWriter.write("Before"); @@ -89,7 +89,7 @@ public class CodeWriterTests { CodeWriter.writeTo( file, - "//", + new CodeWriter.CodeWriterConfig("//"), codeWriter -> { codeWriter.writeHeader(); codeWriter.writeCustomArea("head"); @@ -124,7 +124,7 @@ public class CodeWriterTests { CodeWriter.writeTo( file, - "//", + new CodeWriter.CodeWriterConfig("//"), codeWriter -> { codeWriter.writeHeader(); codeWriter.writeCustomArea("head"); |
