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.
This commit is contained in:
parent
d3d1cb48b6
commit
d4e3da23fa
|
@ -15,7 +15,7 @@ repositories {
|
|||
}
|
||||
}
|
||||
dependencies {
|
||||
compile 'de.speiger:Simple-Code-Generator:1.0.5'
|
||||
compile 'de.speiger:Simple-Code-Generator:1.1.0'
|
||||
}
|
||||
```
|
||||
|
||||
|
|
|
@ -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'
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
test=e949777578333aa849d74d6a2c651236
|
|
@ -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
|
|
@ -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
|
|
@ -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<String> lines, int currentIndex, int startIndex, List<ConditionedSegment> segments) throws IllegalStateException
|
||||
public static int parse(String fileName, String currentLine, List<String> lines, int currentIndex, int startIndex, AtomicInteger ignoreCounter, List<ConditionedSegment> segments, List<PostSegment> 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<ConditionedSegment> 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<lines.size();i++)
|
||||
{
|
||||
String s = lines.get(i);
|
||||
|
@ -51,6 +57,7 @@ public class ConditionedSegment
|
|||
{
|
||||
if(trimmed.startsWith("#else if"))
|
||||
{
|
||||
if(currentLine == null) throw new IllegalStateException("#else if isn't working while in a ignore segment");
|
||||
segment.addSegment(new Segment(segmentText.toString(), condition, childSegments));
|
||||
condition = ICondition.parse(trimmed.substring(8).trim());
|
||||
childSegments = new ArrayList<>();
|
||||
|
@ -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");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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<ConditionedSegment> segments;
|
||||
List<PostSegment> postSegments;
|
||||
|
||||
public Template(String fileName, String textFile, List<ConditionedSegment> segments)
|
||||
public Template(String fileName, String textFile, List<ConditionedSegment> segments, List<PostSegment> 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<m;i++)
|
||||
{
|
||||
result = postSegments.get(i).build(result);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static Template parse(Path file) throws IOException
|
||||
{
|
||||
String fileName = FileUtils.getFileName(file.getFileName());
|
||||
List<ConditionedSegment> segments = new ArrayList<ConditionedSegment>();
|
||||
AtomicInteger segmentIds = new AtomicInteger();
|
||||
List<ConditionedSegment> segments = new ArrayList<>();
|
||||
List<PostSegment> postSegments = new ArrayList<>();
|
||||
StringJoiner joiner = new StringJoiner("\n");
|
||||
List<String> lines = Files.readAllLines(file);
|
||||
for(int i = 0;i<lines.size();i++)
|
||||
|
@ -70,9 +79,17 @@ public class Template
|
|||
{
|
||||
if(trimmed.startsWith("#if"))
|
||||
{
|
||||
i += ConditionedSegment.parse(fileName, s.trim().substring(3).trim(), lines, i, joiner.length(), segments);
|
||||
i += ConditionedSegment.parse(fileName, s.trim().substring(3).trim(), lines, i, joiner.length(), segmentIds, segments, postSegments);
|
||||
continue;
|
||||
}
|
||||
else if(trimmed.startsWith("#ignore"))
|
||||
{
|
||||
segmentIds.getAndIncrement();
|
||||
int prev = i;
|
||||
i += ConditionedSegment.parse(fileName, null, lines, i, joiner.length(), segmentIds, segments, postSegments);
|
||||
joiner.add(lines.get(prev));
|
||||
continue;
|
||||
}
|
||||
else if(trimmed.startsWith("#symlink"))
|
||||
{
|
||||
return Template.parse(file.getParent().resolve(trimmed.substring(8).trim()));
|
||||
|
@ -80,6 +97,6 @@ public class Template
|
|||
}
|
||||
joiner.add(s);
|
||||
}
|
||||
return new Template(fileName, joiner.toString(), segments);
|
||||
return new Template(fileName, joiner.toString(), segments, postSegments);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -73,6 +73,11 @@ public class ArgumentMapper implements IMapper
|
|||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
catch(Error error)
|
||||
{
|
||||
System.out.println("Error with ["+pattern.pattern()+"] pattern");
|
||||
throw error;
|
||||
}
|
||||
return t;
|
||||
}
|
||||
|
||||
|
|
|
@ -70,6 +70,11 @@ public class InjectMapper implements IMapper
|
|||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
catch(Error error)
|
||||
{
|
||||
System.out.println("Error with ["+pattern.pattern()+"] pattern");
|
||||
throw error;
|
||||
}
|
||||
return t;
|
||||
}
|
||||
|
||||
|
|
|
@ -31,22 +31,34 @@ public class LineMapper implements IMapper
|
|||
public String apply(String t)
|
||||
{
|
||||
RegexMatcher matcher = new RegexMatcher(pattern, t);
|
||||
if(matcher.find())
|
||||
try
|
||||
{
|
||||
StringBuffer buffer = new StringBuffer();
|
||||
do
|
||||
if(matcher.find())
|
||||
{
|
||||
int start = matcher.end() - 1;
|
||||
int[] result = RegexUtil.findFullLine(t, start);
|
||||
if(result != null)
|
||||
StringBuffer buffer = new StringBuffer();
|
||||
do
|
||||
{
|
||||
matcher.appendReplacement(buffer, pattern.pattern());
|
||||
buffer.setLength(buffer.length() - (start - result[0]));
|
||||
matcher.skip(result[1] - start);
|
||||
}
|
||||
} while (matcher.find());
|
||||
matcher.appendTail(buffer);
|
||||
return apply(buffer.toString());
|
||||
int start = matcher.end() - 1;
|
||||
int[] result = RegexUtil.findFullLine(t, start);
|
||||
if(result != null)
|
||||
{
|
||||
matcher.appendReplacement(buffer, pattern.pattern());
|
||||
buffer.setLength(buffer.length() - (start - result[0]));
|
||||
matcher.skip(result[1] - start);
|
||||
}
|
||||
} while (matcher.find());
|
||||
matcher.appendTail(buffer);
|
||||
return apply(buffer.toString());
|
||||
}
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
catch(Error error)
|
||||
{
|
||||
System.out.println("Error with ["+pattern.pattern()+"] pattern");
|
||||
throw error;
|
||||
}
|
||||
return t;
|
||||
}
|
||||
|
|
|
@ -29,6 +29,19 @@ public class SimpleMapper implements IMapper
|
|||
@Override
|
||||
public String apply(String t)
|
||||
{
|
||||
return pattern.matcher(t).replaceAll(replacement);
|
||||
try
|
||||
{
|
||||
return pattern.matcher(t).replaceAll(replacement);
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
catch(Error error)
|
||||
{
|
||||
System.out.println("Error with ["+pattern.pattern()+"] pattern");
|
||||
throw error;
|
||||
}
|
||||
return t;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,12 +14,14 @@ public class BuildTask implements Runnable
|
|||
Template template;
|
||||
TemplateProcess process;
|
||||
Set<IMapper>[] mappers;
|
||||
boolean silencedSuccess;
|
||||
|
||||
public BuildTask(Path basePath, Template template, TemplateProcess process, Set<IMapper>[] mappers)
|
||||
public BuildTask(Path basePath, Template template, TemplateProcess process, boolean silencedSuccess, Set<IMapper>[] 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)
|
||||
{
|
||||
|
|
|
@ -23,28 +23,33 @@ public class TemplateProcess
|
|||
this.fileName = fileName;
|
||||
}
|
||||
|
||||
public void setPathBuilder(UnaryOperator<Path> pathBuilder)
|
||||
public TemplateProcess setPathBuilder(UnaryOperator<Path> 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<String> flags)
|
||||
public TemplateProcess addFlags(Collection<String> 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<IMapper> mappers)
|
||||
public TemplateProcess addMappers(Collection<IMapper> mappers)
|
||||
{
|
||||
this.mappers.addAll(mappers);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
{
|
||||
|
|
|
@ -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<TemplateProcess> 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;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue