From d4e3da23faf67afb185dcc58f024ebf8f06b5baf Mon Sep 17 00:00:00 2001 From: Speiger Date: Sat, 21 May 2022 14:32:02 +0200 Subject: [PATCH] Added New Feature that was really nessesary. -Added: #ignore which allows the template process to ignore segments. They can be used with #if but in a ignored segment you are not allowed to use #if/#else/#else if/#endif since it is outside of the context of the template process. But this allows to address issues with marking areas. --- README.md | 2 +- build.gradle | 3 +- example/cache.bin | 1 + example/input/test.template | 23 +++++++ example/output/test.txt | 8 +++ .../src/builder/base/ConditionedSegment.java | 32 +++++++-- .../speiger/src/builder/base/PostSegment.java | 18 +++++ .../speiger/src/builder/base/Template.java | 25 +++++-- .../src/builder/mappers/ArgumentMapper.java | 5 ++ .../src/builder/mappers/InjectMapper.java | 5 ++ .../src/builder/mappers/LineMapper.java | 38 +++++++---- .../src/builder/mappers/SimpleMapper.java | 15 +++- .../src/builder/processor/BuildTask.java | 6 +- .../builder/processor/TemplateProcess.java | 15 ++-- .../builder/processor/TemplateProcessor.java | 10 ++- src/test/java/example/ExampleBuilder.java | 68 +++++++++++++++++++ 16 files changed, 241 insertions(+), 33 deletions(-) create mode 100644 example/cache.bin create mode 100644 example/input/test.template create mode 100644 example/output/test.txt create mode 100644 src/main/java/speiger/src/builder/base/PostSegment.java create mode 100644 src/test/java/example/ExampleBuilder.java diff --git a/README.md b/README.md index 260622f..bb31bbb 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ repositories { } } dependencies { - compile 'de.speiger:Simple-Code-Generator:1.0.5' + compile 'de.speiger:Simple-Code-Generator:1.1.0' } ``` diff --git a/build.gradle b/build.gradle index 51d5c06..96a03bd 100644 --- a/build.gradle +++ b/build.gradle @@ -5,7 +5,7 @@ repositories { } archivesBaseName = 'Simple Code Generator' -version = '1.0.5' +version = '1.1.0' apply plugin: 'maven' tasks.withType(JavaCompile) { @@ -47,7 +47,6 @@ uploadArchives { developer { id = 'speiger' name = 'Speiger' - email = 'speiger@gmx.net' } } } diff --git a/example/cache.bin b/example/cache.bin new file mode 100644 index 0000000..c2588e6 --- /dev/null +++ b/example/cache.bin @@ -0,0 +1 @@ +test=e949777578333aa849d74d6a2c651236 diff --git a/example/input/test.template b/example/input/test.template new file mode 100644 index 0000000..da17d64 --- /dev/null +++ b/example/input/test.template @@ -0,0 +1,23 @@ +Hello this is my test + +#ignore +TESTING should not be overriden +#endignore + +TESTING should be overriden + +#if TEST0 +#ignore +TESTING should not be overriden too +#endignore + +TESTING should be overriden too +#endif +#if TEST1 + +#ignore +TESTING should not be overriden +#endignore + +TESTING should not be overriden +#endif \ No newline at end of file diff --git a/example/output/test.txt b/example/output/test.txt new file mode 100644 index 0000000..accf736 --- /dev/null +++ b/example/output/test.txt @@ -0,0 +1,8 @@ +Hello this is my test + +TESTING should not be overriden + +Test2 should be overriden +TESTING should not be overriden too + +Test2 should be overriden too \ No newline at end of file diff --git a/src/main/java/speiger/src/builder/base/ConditionedSegment.java b/src/main/java/speiger/src/builder/base/ConditionedSegment.java index 8ebe26c..6a6c5f3 100644 --- a/src/main/java/speiger/src/builder/base/ConditionedSegment.java +++ b/src/main/java/speiger/src/builder/base/ConditionedSegment.java @@ -4,6 +4,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Set; import java.util.StringJoiner; +import java.util.concurrent.atomic.AtomicInteger; import java.util.regex.Pattern; import speiger.src.builder.conditions.ICondition; @@ -37,12 +38,17 @@ public class ConditionedSegment return builder.length() - length; } - public static int parse(String fileName, String currentLine, List lines, int currentIndex, int startIndex, List segments) throws IllegalStateException + public static int parse(String fileName, String currentLine, List lines, int currentIndex, int startIndex, AtomicInteger ignoreCounter, List segments, List postSegments) throws IllegalStateException { + int ignoreSegmentId = ignoreCounter.get(); ConditionedSegment segment = new ConditionedSegment(startIndex); - ICondition condition = ICondition.parse(currentLine); + ICondition condition = currentLine == null ? null : ICondition.parse(currentLine); List childSegments = new ArrayList<>(); StringJoiner segmentText = new StringJoiner("\n", (currentIndex <= 0 || includeStartNewLine(lines.get(currentIndex-1).trim())) ? "\n" : "", ""); + if(currentLine == null) { + lines.set(currentIndex, "#ignoreLine"+ignoreSegmentId); + } + for(int i = currentIndex+1;i(); @@ -58,6 +65,7 @@ public class ConditionedSegment } else if(trimmed.startsWith("#else")) { + if(currentLine == null) throw new IllegalStateException("#else isn't working while in a ignore segment"); segment.addSegment(new Segment(segmentText.toString(), condition, childSegments)); condition = ICondition.ALWAYS_TRUE; childSegments = new ArrayList<>(); @@ -65,13 +73,29 @@ public class ConditionedSegment } else if(trimmed.startsWith("#endif")) { + if(currentLine == null) throw new IllegalStateException("#endif isn't working while in a ignore segment"); segment.addSegment(new Segment(segmentText.toString(), condition, childSegments)); segments.add(segment); return i - currentIndex; } + else if(trimmed.startsWith("#endignore")) + { + postSegments.add(new PostSegment(segmentText.toString(), "#ignoreLine"+ignoreSegmentId)); + segments.add(segment); + return i - currentIndex; + } else if(trimmed.startsWith("#if")) { - i += parse(fileName, trimmed.substring(3).trim(), lines, i, segmentText.length(), childSegments); + if(currentLine == null) throw new IllegalStateException("#if isn't working while in a ignore segment"); + i += parse(fileName, trimmed.substring(3).trim(), lines, i, segmentText.length(), ignoreCounter, childSegments, postSegments); + } + else if(trimmed.startsWith("#ignore")) + { + if(currentLine == null) throw new IllegalStateException("#ignore lines can't be applied Recursively"); + ignoreCounter.incrementAndGet(); + int prev = i; + i += parse(fileName, null, lines, i, segmentText.length(), ignoreCounter, childSegments, postSegments); + segmentText.add(lines.get(prev)); } continue; } @@ -82,6 +106,6 @@ public class ConditionedSegment static boolean includeStartNewLine(String s) { - return !s.startsWith("#") || s.equalsIgnoreCase("#endif"); + return !s.startsWith("#") || s.equalsIgnoreCase("#endif") || s.equalsIgnoreCase("#endignore"); } } diff --git a/src/main/java/speiger/src/builder/base/PostSegment.java b/src/main/java/speiger/src/builder/base/PostSegment.java new file mode 100644 index 0000000..73cf300 --- /dev/null +++ b/src/main/java/speiger/src/builder/base/PostSegment.java @@ -0,0 +1,18 @@ +package speiger.src.builder.base; + +public class PostSegment +{ + String text; + String identifier; + + public PostSegment(String text, String identifier) + { + this.text = text; + this.identifier = identifier; + } + + public String build(String input) + { + return input.replaceAll("\n"+identifier, text); + } +} diff --git a/src/main/java/speiger/src/builder/base/Template.java b/src/main/java/speiger/src/builder/base/Template.java index b5222e3..5fbb500 100644 --- a/src/main/java/speiger/src/builder/base/Template.java +++ b/src/main/java/speiger/src/builder/base/Template.java @@ -7,6 +7,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Set; import java.util.StringJoiner; +import java.util.concurrent.atomic.AtomicInteger; import speiger.src.builder.mappers.IMapper; import speiger.src.builder.misc.FileUtils; @@ -16,12 +17,14 @@ public class Template String fileName; String textFile; List segments; + List postSegments; - public Template(String fileName, String textFile, List segments) + public Template(String fileName, String textFile, List segments, List postSegments) { this.fileName = fileName; this.textFile = textFile; this.segments = segments; + this.postSegments = postSegments; } public String getFileName() @@ -53,13 +56,19 @@ public class Template result = mappers.get(i).apply(result); } } + for(int i = 0,m=postSegments.size();i segments = new ArrayList(); + AtomicInteger segmentIds = new AtomicInteger(); + List segments = new ArrayList<>(); + List postSegments = new ArrayList<>(); StringJoiner joiner = new StringJoiner("\n"); List lines = Files.readAllLines(file); for(int i = 0;i[] mappers; + boolean silencedSuccess; - public BuildTask(Path basePath, Template template, TemplateProcess process, Set[] mappers) + public BuildTask(Path basePath, Template template, TemplateProcess process, boolean silencedSuccess, Set[] mappers) { this.basePath = basePath; this.template = template; this.process = process; + this.silencedSuccess = silencedSuccess; this.mappers = mappers; } @@ -44,7 +46,7 @@ public class BuildTask implements Runnable { writer.write(s); writer.flush(); - System.out.println("Created: "+process.fileName); + if(!silencedSuccess) System.out.println("Created: "+process.fileName); } catch(Exception e) { diff --git a/src/main/java/speiger/src/builder/processor/TemplateProcess.java b/src/main/java/speiger/src/builder/processor/TemplateProcess.java index cdca589..ec2bc4c 100644 --- a/src/main/java/speiger/src/builder/processor/TemplateProcess.java +++ b/src/main/java/speiger/src/builder/processor/TemplateProcess.java @@ -23,28 +23,33 @@ public class TemplateProcess this.fileName = fileName; } - public void setPathBuilder(UnaryOperator pathBuilder) + public TemplateProcess setPathBuilder(UnaryOperator pathBuilder) { this.pathBuilder = pathBuilder; + return this; } - public void addFlags(String...flags) + public TemplateProcess addFlags(String...flags) { parsePool.addAll(Arrays.asList(flags)); + return this; } - public void addFlags(Collection flags) + public TemplateProcess addFlags(Collection flags) { parsePool.addAll(flags); + return this; } - public void addMapper(IMapper mapper) + public TemplateProcess addMapper(IMapper mapper) { mappers.add(mapper); + return this; } - public void addMappers(Collection mappers) + public TemplateProcess addMappers(Collection mappers) { this.mappers.addAll(mappers); + return this; } } diff --git a/src/main/java/speiger/src/builder/processor/TemplateProcessor.java b/src/main/java/speiger/src/builder/processor/TemplateProcessor.java index 25a7d8b..235323c 100644 --- a/src/main/java/speiger/src/builder/processor/TemplateProcessor.java +++ b/src/main/java/speiger/src/builder/processor/TemplateProcessor.java @@ -24,10 +24,17 @@ public abstract class TemplateProcessor Path sourceFolder; Path outputFolder; Path dataFolder; + boolean silencedSuccess; boolean init = false; public TemplateProcessor(Path sourceFolder, Path outputFolder, Path dataFolder) { + this(false, sourceFolder, outputFolder, dataFolder); + } + + public TemplateProcessor(boolean silencedSuccess, Path sourceFolder, Path outputFolder, Path dataFolder) + { + this.silencedSuccess = silencedSuccess; this.sourceFolder = sourceFolder; this.outputFolder = outputFolder; this.dataFolder = dataFolder; @@ -87,6 +94,7 @@ public abstract class TemplateProcessor protected Path getSourceFolder() { return sourceFolder; } protected Path getOutputFolder() { return outputFolder; } protected Path getDataFolder() { return dataFolder; } + protected boolean isSilencedSuccess() { return silencedSuccess; } public final boolean process(boolean force) throws IOException, InterruptedException { @@ -119,7 +127,7 @@ public abstract class TemplateProcessor Template template = Template.parse(path); counters[2].addAndGet(System.currentTimeMillis() - startTime); counters[1].addAndGet(1); - createProcesses(FileUtils.getFileName(path), T -> {service.execute(new BuildTask(relative ? outputFolder.resolve(sourceFolder.relativize(path).getParent()) : outputFolder, template, T, mappers));counters[0].addAndGet(1);}); + createProcesses(FileUtils.getFileName(path), T -> {service.execute(new BuildTask(relative ? outputFolder.resolve(sourceFolder.relativize(path).getParent()) : outputFolder, template, T, silencedSuccess, mappers));counters[0].addAndGet(1);}); } catch(Exception e) { diff --git a/src/test/java/example/ExampleBuilder.java b/src/test/java/example/ExampleBuilder.java new file mode 100644 index 0000000..f987466 --- /dev/null +++ b/src/test/java/example/ExampleBuilder.java @@ -0,0 +1,68 @@ +package example; + +import java.io.IOException; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.function.Consumer; + +import speiger.src.builder.mappers.SimpleMapper; +import speiger.src.builder.processor.TemplateProcess; +import speiger.src.builder.processor.TemplateProcessor; + +public class ExampleBuilder extends TemplateProcessor +{ + + public static void main(String...args) + { + try + { + new ExampleBuilder().process(true); + } + catch(IOException e) + { + // TODO Auto-generated catch block + e.printStackTrace(); + } + catch(InterruptedException e) + { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + public ExampleBuilder() + { + super(Paths.get("example/input"), Paths.get("example/output"), Paths.get("example")); + } + + @Override + protected void init() + { + + } + + @Override + protected boolean isFileValid(Path fileName) + { + return true; + } + + @Override + public void createProcesses(String fileName, Consumer process) + { + process.accept(new TemplateProcess(fileName+".txt").addFlags("TEST0").addMapper(new SimpleMapper("TESTING", "Test2"))); + } + + @Override + protected boolean relativePackages() + { + return false; + } + + @Override + protected boolean debugUnusedMappers() + { + return false; + } + +}