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

@ -1,24 +1,24 @@
apply plugin: 'java-library'
repositories {
jcenter()
}
archivesBaseName = 'Simple Code Generator'
version = '1.0'
tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}
dependencies {
}
task srcJar(type: Jar) {
from sourceSets.main.allSource
classifier = 'sources'
}
artifacts {
archives srcJar
apply plugin: 'java-library'
repositories {
jcenter()
}
archivesBaseName = 'Simple Code Generator'
version = '1.0.1'
tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}
dependencies {
}
task srcJar(type: Jar) {
from sourceSets.main.allSource
classifier = 'sources'
}
artifacts {
archives srcJar
}

View File

@ -7,8 +7,8 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.StringJoiner;
import java.util.function.UnaryOperator;
import speiger.src.builder.mappers.IMapper;
import speiger.src.builder.misc.FileUtils;
public class Template
@ -29,7 +29,7 @@ public class Template
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);
for(int i = 0,offset=0,m=segments.size();i<m;i++)
@ -39,7 +39,19 @@ public class Template
String result = builder.toString();
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;
}

View File

@ -1,13 +1,13 @@
package speiger.src.builder.mappers;
import java.util.function.UnaryOperator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import speiger.src.builder.misc.RegexUtil;
public class ArgumentMapper implements UnaryOperator<String>
public class ArgumentMapper implements IMapper
{
String searchValue;
Pattern pattern;
String replacement;
String argumentBreaker;
@ -16,6 +16,12 @@ public class ArgumentMapper implements UnaryOperator<String>
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.replacement = replacement;
this.argumentBreaker = argumentBreaker;
@ -34,6 +40,12 @@ public class ArgumentMapper implements UnaryOperator<String>
return this;
}
@Override
public String getSearchValue()
{
return searchValue;
}
@Override
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;
import java.util.function.UnaryOperator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import speiger.src.builder.misc.RegexUtil;
public class InjectMapper implements UnaryOperator<String>
public class InjectMapper implements IMapper
{
String searchValue;
Pattern pattern;
String replacement;
String braces = "()";
@ -15,6 +15,12 @@ public class InjectMapper implements UnaryOperator<String>
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.replacement = replacement;
}
@ -32,6 +38,12 @@ public class InjectMapper implements UnaryOperator<String>
return this;
}
@Override
public String getSearchValue()
{
return searchValue;
}
@Override
public String apply(String t)
{

View File

@ -1,20 +1,32 @@
package speiger.src.builder.mappers;
import java.util.function.UnaryOperator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import speiger.src.builder.misc.RegexUtil;
public class LineMapper implements UnaryOperator<String>
public class LineMapper implements IMapper
{
String searchValue;
Pattern pattern;
public LineMapper(String pattern)
{
this(pattern, pattern);
}
public LineMapper(String searchValue, String pattern)
{
this.searchValue = searchValue;
this.pattern = Pattern.compile(pattern, Pattern.LITERAL);
}
@Override
public String getSearchValue()
{
return searchValue;
}
@Override
public String apply(String t)
{

View File

@ -1,19 +1,31 @@
package speiger.src.builder.mappers;
import java.util.function.UnaryOperator;
import java.util.regex.Pattern;
public class SimpleMapper implements UnaryOperator<String>
public class SimpleMapper implements IMapper
{
String searchValue;
Pattern 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.replacement = replacement;
}
@Override
public String getSearchValue()
{
return searchValue;
}
@Override
public String apply(String t)
{

View File

@ -3,26 +3,34 @@ package speiger.src.builder.processor;
import java.io.BufferedWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Set;
import speiger.src.builder.base.Template;
import speiger.src.builder.mappers.IMapper;
public class BuildTask implements Runnable
{
Path basePath;
Template template;
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.template = template;
this.process = process;
this.mappers = mappers;
}
@Override
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);
try
{
@ -42,5 +50,4 @@ public class BuildTask implements Runnable
e.printStackTrace();
}
}
}

View File

@ -9,12 +9,14 @@ import java.util.List;
import java.util.Set;
import java.util.function.UnaryOperator;
import speiger.src.builder.mappers.IMapper;
public class TemplateProcess
{
UnaryOperator<Path> pathBuilder;
String fileName;
Set<String> parsePool = new HashSet<>();
List<UnaryOperator<String>> mappers = new ArrayList<>();
List<IMapper> mappers = new ArrayList<>();
public TemplateProcess(String fileName)
{
@ -36,12 +38,12 @@ public class TemplateProcess
parsePool.addAll(flags);
}
public void addMapper(UnaryOperator<String> mapper)
public void addMapper(IMapper mapper)
{
mappers.add(mapper);
}
public void addMappers(Collection<UnaryOperator<String>> mappers)
public void addMappers(Collection<IMapper> mappers)
{
this.mappers.addAll(mappers);
}

View File

@ -3,8 +3,11 @@ package speiger.src.builder.processor;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
@ -12,6 +15,7 @@ import java.util.function.Consumer;
import java.util.stream.Collectors;
import speiger.src.builder.base.Template;
import speiger.src.builder.mappers.IMapper;
import speiger.src.builder.misc.FileUtils;
public abstract class TemplateProcessor
@ -36,6 +40,8 @@ public abstract class TemplateProcessor
protected abstract boolean relativePackages();
protected abstract boolean debugUnusedMappers();
public final boolean process(boolean force) throws IOException, InterruptedException
{
if(!init)
@ -51,6 +57,7 @@ public abstract class TemplateProcessor
return false;
}
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());
service.setKeepAliveTime(10, TimeUnit.MILLISECONDS);
service.allowCoreThreadTimeOut(true);
@ -61,7 +68,7 @@ public abstract class TemplateProcessor
try
{
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)
{
@ -80,6 +87,14 @@ public abstract class TemplateProcessor
{
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");
FileUtils.saveMappings(existing, dataFolder);
System.out.print("Saved Changes");