Added Debug Feature

-Added: Feature where a Mapper is no longer used at all.
This commit is contained in:
Speiger 2021-01-18 15:51:59 +01:00
parent 152dfea955
commit e752679d78
10 changed files with 136 additions and 44 deletions

View File

@ -5,7 +5,7 @@ repositories {
} }
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'

View File

@ -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,7 +39,19 @@ 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;
} }

View File

@ -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)
{ {

View File

@ -0,0 +1,8 @@
package speiger.src.builder.mappers;
import java.util.function.UnaryOperator;
public interface IMapper extends UnaryOperator<String>
{
public String getSearchValue();
}

View File

@ -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)
{ {

View File

@ -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)
{ {

View File

@ -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)
{ {

View File

@ -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[1]);
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
{ {
@ -42,5 +50,4 @@ public class BuildTask implements Runnable
e.printStackTrace(); e.printStackTrace();
} }
} }
} }

View File

@ -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);
} }

View File

@ -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");