4 Commits
Author SHA1 Message Date
Speiger 0f00ad1771 Logging improvements.
-Added: Improved Logging to parsing & Building
2021-01-20 04:30:18 +01:00
Speiger e752679d78 Added Debug Feature
-Added: Feature where a Mapper is no longer used at all.
2021-01-18 15:51:59 +01:00
Speiger 152dfea955 Update License 2021-01-11 17:49:54 +01:00
Speiger 7a3b6230a8 Documentation new Line fix. 2021-01-11 13:51:58 +01:00
13 changed files with 150 additions and 55 deletions
+1 -1
View File
@@ -187,7 +187,7 @@ a file or class name and description of purpose be included on the same "printed
page" as the copyright notice for easier identification within third-party page" as the copyright notice for easier identification within third-party
archives. archives.
Copyright [yyyy] [name of copyright owner] Copyright 2021 Speiger
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
+6 -5
View File
@@ -9,11 +9,12 @@ It is as bare bones as it can get but that also makes it flexible.
# How to create a Template Processor # How to create a Template Processor
Create a class that extends TemplateProcessor. Create a class that extends TemplateProcessor.
And run the process method And run the process method
SourceFolder: Is the folder that is traversed through and Files are given back. SourceFolder: Is the folder that is traversed through and Files are given back.
OutputFolder: Is the folder where the Processed files get put into. If the "relativePackages" are set to true then the source folder structure is transferred. OutputFolder: Is the folder where the Processed files get put into. If the "relativePackages" are set to true then the source folder structure is transferred.
DataFolder: Is the folder where the input cache is stored. It uses a MD5 generator to compare inputs. Right now only FileNames without extensions are stored in there. So no Duplicated FileName support for now. DataFolder: Is the folder where the input cache is stored. It uses a MD5 generator to compare inputs. Right now only FileNames without extensions are stored in there.
So no Duplicated FileName support for now.
##### Methods: ##### Methods:
init: Is called when the Processes was started for the first time. init: Is called when the Processes was started for the first time.
+23 -23
View File
@@ -1,24 +1,24 @@
apply plugin: 'java-library' apply plugin: 'java-library'
repositories { repositories {
jcenter() jcenter()
} }
archivesBaseName = 'Simple Code Generator' archivesBaseName = 'Simple Code Generator'
version = '1.0' version = '1.0.1'
tasks.withType(JavaCompile) { tasks.withType(JavaCompile) {
options.encoding = 'UTF-8' options.encoding = 'UTF-8'
} }
dependencies { dependencies {
} }
task srcJar(type: Jar) { task srcJar(type: Jar) {
from sourceSets.main.allSource from sourceSets.main.allSource
classifier = 'sources' classifier = 'sources'
} }
artifacts { artifacts {
archives srcJar archives srcJar
} }
@@ -37,7 +37,7 @@ public class ConditionedSegment
return builder.length() - length; return builder.length() - length;
} }
public static int parse(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, List<ConditionedSegment> segments) throws IllegalStateException
{ {
ConditionedSegment segment = new ConditionedSegment(startIndex); ConditionedSegment segment = new ConditionedSegment(startIndex);
ICondition condition = ICondition.parse(currentLine); ICondition condition = ICondition.parse(currentLine);
@@ -71,13 +71,13 @@ public class ConditionedSegment
} }
else if(trimmed.startsWith("#if")) else if(trimmed.startsWith("#if"))
{ {
i += parse(trimmed.substring(3).trim(), lines, i, segmentText.length(), childSegments); i += parse(fileName, trimmed.substring(3).trim(), lines, i, segmentText.length(), childSegments);
} }
continue; continue;
} }
segmentText.add(s); segmentText.add(s);
} }
throw new IllegalStateException("Unclosed #If found!"); throw new IllegalStateException("Unclosed #If found in ["+fileName+"] at line ["+startIndex+"]");
} }
} }
@@ -7,8 +7,8 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.StringJoiner; import java.util.StringJoiner;
import java.util.function.UnaryOperator;
import speiger.src.builder.mappers.IMapper;
import speiger.src.builder.misc.FileUtils; import speiger.src.builder.misc.FileUtils;
public class Template public class Template
@@ -29,7 +29,7 @@ public class Template
return fileName; return fileName;
} }
public String build(Set<String> parsePool, List<UnaryOperator<String>> mappers) public String build(Set<String> parsePool, List<IMapper> mappers, boolean printNoWork, Set<IMapper> done)
{ {
StringBuilder builder = new StringBuilder(textFile); StringBuilder builder = new StringBuilder(textFile);
for(int i = 0,offset=0,m=segments.size();i<m;i++) for(int i = 0,offset=0,m=segments.size();i<m;i++)
@@ -39,13 +39,26 @@ public class Template
String result = builder.toString(); String result = builder.toString();
for(int i = 0,m=mappers.size();i<m;i++) for(int i = 0,m=mappers.size();i<m;i++)
{ {
result = mappers.get(i).apply(result); if(printNoWork)
{
String previous = result;
result = mappers.get(i).apply(result);
if(previous.equals(result))
{
done.add(mappers.get(i));
}
}
else
{
result = mappers.get(i).apply(result);
}
} }
return result; return result;
} }
public static Template parse(Path file) throws IOException public static Template parse(Path file) throws IOException
{ {
String fileName = FileUtils.getFileName(file.getFileName());
List<ConditionedSegment> segments = new ArrayList<ConditionedSegment>(); List<ConditionedSegment> segments = new ArrayList<ConditionedSegment>();
StringJoiner joiner = new StringJoiner("\n"); StringJoiner joiner = new StringJoiner("\n");
List<String> lines = Files.readAllLines(file); List<String> lines = Files.readAllLines(file);
@@ -57,7 +70,7 @@ public class Template
{ {
if(trimmed.startsWith("#if")) if(trimmed.startsWith("#if"))
{ {
i += ConditionedSegment.parse(s.trim().substring(3).trim(), lines, i, joiner.length(), segments); i += ConditionedSegment.parse(fileName, s.trim().substring(3).trim(), lines, i, joiner.length(), segments);
continue; continue;
} }
else if(trimmed.startsWith("#symlink")) else if(trimmed.startsWith("#symlink"))
@@ -67,6 +80,6 @@ public class Template
} }
joiner.add(s); joiner.add(s);
} }
return new Template(FileUtils.getFileName(file.getFileName()), joiner.toString(), segments); return new Template(fileName, joiner.toString(), segments);
} }
} }
@@ -1,13 +1,13 @@
package speiger.src.builder.mappers; package speiger.src.builder.mappers;
import java.util.function.UnaryOperator;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import speiger.src.builder.misc.RegexUtil; import speiger.src.builder.misc.RegexUtil;
public class ArgumentMapper implements UnaryOperator<String> public class ArgumentMapper implements IMapper
{ {
String searchValue;
Pattern pattern; Pattern pattern;
String replacement; String replacement;
String argumentBreaker; String argumentBreaker;
@@ -16,6 +16,12 @@ public class ArgumentMapper implements UnaryOperator<String>
public ArgumentMapper(String pattern, String replacement, String argumentBreaker) public ArgumentMapper(String pattern, String replacement, String argumentBreaker)
{ {
this(pattern, pattern, replacement, argumentBreaker);
}
public ArgumentMapper(String searchValue, String pattern, String replacement, String argumentBreaker)
{
this.searchValue = searchValue;
this.pattern = Pattern.compile(pattern); this.pattern = Pattern.compile(pattern);
this.replacement = replacement; this.replacement = replacement;
this.argumentBreaker = argumentBreaker; this.argumentBreaker = argumentBreaker;
@@ -34,6 +40,12 @@ public class ArgumentMapper implements UnaryOperator<String>
return this; return this;
} }
@Override
public String getSearchValue()
{
return searchValue;
}
@Override @Override
public String apply(String t) public String apply(String t)
{ {
@@ -0,0 +1,8 @@
package speiger.src.builder.mappers;
import java.util.function.UnaryOperator;
public interface IMapper extends UnaryOperator<String>
{
public String getSearchValue();
}
@@ -1,13 +1,13 @@
package speiger.src.builder.mappers; package speiger.src.builder.mappers;
import java.util.function.UnaryOperator;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import speiger.src.builder.misc.RegexUtil; import speiger.src.builder.misc.RegexUtil;
public class InjectMapper implements UnaryOperator<String> public class InjectMapper implements IMapper
{ {
String searchValue;
Pattern pattern; Pattern pattern;
String replacement; String replacement;
String braces = "()"; String braces = "()";
@@ -15,6 +15,12 @@ public class InjectMapper implements UnaryOperator<String>
public InjectMapper(String pattern, String replacement) public InjectMapper(String pattern, String replacement)
{ {
this(pattern, pattern, replacement);
}
public InjectMapper(String searchValue, String pattern, String replacement)
{
this.searchValue = searchValue;
this.pattern = Pattern.compile(pattern); this.pattern = Pattern.compile(pattern);
this.replacement = replacement; this.replacement = replacement;
} }
@@ -32,6 +38,12 @@ public class InjectMapper implements UnaryOperator<String>
return this; return this;
} }
@Override
public String getSearchValue()
{
return searchValue;
}
@Override @Override
public String apply(String t) public String apply(String t)
{ {
@@ -1,20 +1,32 @@
package speiger.src.builder.mappers; package speiger.src.builder.mappers;
import java.util.function.UnaryOperator;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import speiger.src.builder.misc.RegexUtil; import speiger.src.builder.misc.RegexUtil;
public class LineMapper implements UnaryOperator<String> public class LineMapper implements IMapper
{ {
String searchValue;
Pattern pattern; Pattern pattern;
public LineMapper(String pattern) public LineMapper(String pattern)
{ {
this(pattern, pattern);
}
public LineMapper(String searchValue, String pattern)
{
this.searchValue = searchValue;
this.pattern = Pattern.compile(pattern, Pattern.LITERAL); this.pattern = Pattern.compile(pattern, Pattern.LITERAL);
} }
@Override
public String getSearchValue()
{
return searchValue;
}
@Override @Override
public String apply(String t) public String apply(String t)
{ {
@@ -1,19 +1,31 @@
package speiger.src.builder.mappers; package speiger.src.builder.mappers;
import java.util.function.UnaryOperator;
import java.util.regex.Pattern; import java.util.regex.Pattern;
public class SimpleMapper implements UnaryOperator<String> public class SimpleMapper implements IMapper
{ {
String searchValue;
Pattern pattern; Pattern pattern;
String replacement; String replacement;
public SimpleMapper(String pattern, String replacement) public SimpleMapper(String pattern, String replacement)
{ {
this(pattern, pattern, replacement);
}
public SimpleMapper(String searchValue, String pattern, String replacement)
{
this.searchValue = searchValue;
this.pattern = Pattern.compile(pattern, Pattern.LITERAL); this.pattern = Pattern.compile(pattern, Pattern.LITERAL);
this.replacement = replacement; this.replacement = replacement;
} }
@Override
public String getSearchValue()
{
return searchValue;
}
@Override @Override
public String apply(String t) public String apply(String t)
{ {
@@ -3,26 +3,34 @@ package speiger.src.builder.processor;
import java.io.BufferedWriter; import java.io.BufferedWriter;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.Set;
import speiger.src.builder.base.Template; import speiger.src.builder.base.Template;
import speiger.src.builder.mappers.IMapper;
public class BuildTask implements Runnable public class BuildTask implements Runnable
{ {
Path basePath; Path basePath;
Template template; Template template;
TemplateProcess process; TemplateProcess process;
Set<IMapper>[] mappers;
public BuildTask(Path basePath, Template template, TemplateProcess process) public BuildTask(Path basePath, Template template, TemplateProcess process, Set<IMapper>[] mappers)
{ {
this.basePath = basePath; this.basePath = basePath;
this.template = template; this.template = template;
this.process = process; this.process = process;
this.mappers = mappers;
} }
@Override @Override
public void run() public void run()
{ {
String s = template.build(process.parsePool, process.mappers); String s = template.build(process.parsePool, process.mappers, mappers != null, mappers != null ? mappers[1] : null);
if(mappers != null)
{
mappers[0].addAll(process.mappers);
}
Path path = (process.pathBuilder != null ? process.pathBuilder.apply(basePath) : basePath).resolve(process.fileName); Path path = (process.pathBuilder != null ? process.pathBuilder.apply(basePath) : basePath).resolve(process.fileName);
try try
{ {
@@ -30,6 +38,7 @@ public class BuildTask implements Runnable
} }
catch(Exception e) catch(Exception e)
{ {
e.printStackTrace();
} }
try(BufferedWriter writer = Files.newBufferedWriter(path)) try(BufferedWriter writer = Files.newBufferedWriter(path))
{ {
@@ -42,5 +51,4 @@ public class BuildTask implements Runnable
e.printStackTrace(); e.printStackTrace();
} }
} }
} }
@@ -9,12 +9,14 @@ import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.function.UnaryOperator; import java.util.function.UnaryOperator;
import speiger.src.builder.mappers.IMapper;
public class TemplateProcess public class TemplateProcess
{ {
UnaryOperator<Path> pathBuilder; UnaryOperator<Path> pathBuilder;
String fileName; String fileName;
Set<String> parsePool = new HashSet<>(); Set<String> parsePool = new HashSet<>();
List<UnaryOperator<String>> mappers = new ArrayList<>(); List<IMapper> mappers = new ArrayList<>();
public TemplateProcess(String fileName) public TemplateProcess(String fileName)
{ {
@@ -36,12 +38,12 @@ public class TemplateProcess
parsePool.addAll(flags); parsePool.addAll(flags);
} }
public void addMapper(UnaryOperator<String> mapper) public void addMapper(IMapper mapper)
{ {
mappers.add(mapper); mappers.add(mapper);
} }
public void addMappers(Collection<UnaryOperator<String>> mappers) public void addMappers(Collection<IMapper> mappers)
{ {
this.mappers.addAll(mappers); this.mappers.addAll(mappers);
} }
@@ -3,8 +3,11 @@ package speiger.src.builder.processor;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.Collections;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@@ -12,6 +15,7 @@ import java.util.function.Consumer;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import speiger.src.builder.base.Template; import speiger.src.builder.base.Template;
import speiger.src.builder.mappers.IMapper;
import speiger.src.builder.misc.FileUtils; import speiger.src.builder.misc.FileUtils;
public abstract class TemplateProcessor public abstract class TemplateProcessor
@@ -36,6 +40,8 @@ public abstract class TemplateProcessor
protected abstract boolean relativePackages(); protected abstract boolean relativePackages();
protected abstract boolean debugUnusedMappers();
public final boolean process(boolean force) throws IOException, InterruptedException public final boolean process(boolean force) throws IOException, InterruptedException
{ {
if(!init) if(!init)
@@ -51,6 +57,7 @@ public abstract class TemplateProcessor
return false; return false;
} }
final boolean relative = relativePackages(); final boolean relative = relativePackages();
Set<IMapper>[] mappers = debugUnusedMappers() ? new Set[]{Collections.synchronizedSet(new HashSet<IMapper>()), Collections.synchronizedSet(new HashSet<IMapper>())} : null;
ThreadPoolExecutor service = (ThreadPoolExecutor)Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()); ThreadPoolExecutor service = (ThreadPoolExecutor)Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
service.setKeepAliveTime(10, TimeUnit.MILLISECONDS); service.setKeepAliveTime(10, TimeUnit.MILLISECONDS);
service.allowCoreThreadTimeOut(true); service.allowCoreThreadTimeOut(true);
@@ -61,7 +68,7 @@ public abstract class TemplateProcessor
try try
{ {
Template template = Template.parse(path); Template template = Template.parse(path);
createProcesses(FileUtils.getFileName(path), T -> service.execute(new BuildTask(relative ? outputFolder.resolve(sourceFolder.relativize(path).getParent()) : outputFolder, template, T))); createProcesses(FileUtils.getFileName(path), T -> service.execute(new BuildTask(relative ? outputFolder.resolve(sourceFolder.relativize(path).getParent()) : outputFolder, template, T, mappers)));
} }
catch(Exception e) catch(Exception e)
{ {
@@ -80,6 +87,14 @@ public abstract class TemplateProcessor
{ {
Thread.sleep(10); Thread.sleep(10);
} }
if(mappers != null && mappers[0].size() != mappers[1].size())
{
mappers[0].removeAll(mappers[1]);
for(IMapper mapper : mappers[0])
{
System.out.println("Mapper ["+mapper.getSearchValue()+"] is not used in the Entire Build Process");
}
}
System.out.println("Finished Tasks: "+(System.currentTimeMillis() - start)+"ms"); System.out.println("Finished Tasks: "+(System.currentTimeMillis() - start)+"ms");
FileUtils.saveMappings(existing, dataFolder); FileUtils.saveMappings(existing, dataFolder);
System.out.print("Saved Changes"); System.out.print("Saved Changes");