Variouse BugFixes and additions.

-Fixed: Bugs in Mappers causing problems when parsing library.
-Fixed: IConditon should throw a error with 0 arguments.
-Added: CacheFile is now sorted so you can remove caches easier.
-Removed: Unnessesary code.
-Added: Counters.
-Added: Gitignore
This commit is contained in:
Speiger 2021-06-21 16:55:39 +02:00
parent 0f00ad1771
commit 54e66d7f53
8 changed files with 69 additions and 28 deletions

1
.gitignore vendored
View File

@ -1,5 +1,6 @@
# ---> Gradle # ---> Gradle
.gradle .gradle
settings.gradle
**/build/ **/build/
!src/**/build/ !src/**/build/

View File

@ -42,7 +42,7 @@ public class ConditionedSegment
ConditionedSegment segment = new ConditionedSegment(startIndex); ConditionedSegment segment = new ConditionedSegment(startIndex);
ICondition condition = ICondition.parse(currentLine); ICondition condition = ICondition.parse(currentLine);
List<ConditionedSegment> childSegments = new ArrayList<>(); List<ConditionedSegment> childSegments = new ArrayList<>();
StringJoiner segmentText = new StringJoiner("\n", "\n", ""); StringJoiner segmentText = new StringJoiner("\n", (currentIndex <= 0 || includeStartNewLine(lines.get(currentIndex-1).trim())) ? "\n" : "", "");
for(int i = currentIndex+1;i<lines.size();i++) for(int i = currentIndex+1;i<lines.size();i++)
{ {
String s = lines.get(i); String s = lines.get(i);
@ -80,4 +80,8 @@ public class ConditionedSegment
throw new IllegalStateException("Unclosed #If found in ["+fileName+"] at line ["+startIndex+"]"); throw new IllegalStateException("Unclosed #If found in ["+fileName+"] at line ["+startIndex+"]");
} }
static boolean includeStartNewLine(String s)
{
return !s.startsWith("#") || s.equalsIgnoreCase("#endif");
}
} }

View File

@ -48,7 +48,7 @@ public interface ICondition
} }
switch(conditions.size()) switch(conditions.size())
{ {
case 0: return ALWAYS_TRUE; case 0: throw new IllegalStateException("Empty Conditions are not allowed");
case 1: return conditions.get(0); case 1: return conditions.get(0);
default: return new OrCondition(conditions); default: return new OrCondition(conditions);
} }

View File

@ -50,11 +50,14 @@ public class ArgumentMapper implements IMapper
public String apply(String t) public String apply(String t)
{ {
Matcher matcher = pattern.matcher(t); Matcher matcher = pattern.matcher(t);
try
{
if(matcher.find()) if(matcher.find())
{ {
StringBuffer buffer = new StringBuffer(); StringBuffer buffer = new StringBuffer();
do do
{ {
if(matcher.end() < RegexUtil.getLastAppendPosition(matcher)) return apply(matcher.appendTail(buffer).toString());
String text = RegexUtil.searchUntil(t, matcher.end()-1, braces.charAt(0), braces.charAt(1)); String text = RegexUtil.searchUntil(t, matcher.end()-1, braces.charAt(0), braces.charAt(1));
if(!text.isEmpty()) if(!text.isEmpty())
{ {
@ -66,6 +69,11 @@ public class ArgumentMapper implements IMapper
matcher.appendTail(buffer); matcher.appendTail(buffer);
return buffer.toString(); return buffer.toString();
} }
}
catch(Exception e)
{
e.printStackTrace();
}
return t; return t;
} }

View File

@ -48,11 +48,14 @@ public class InjectMapper implements IMapper
public String apply(String t) public String apply(String t)
{ {
Matcher matcher = pattern.matcher(t); Matcher matcher = pattern.matcher(t);
try
{
if(matcher.find()) if(matcher.find())
{ {
StringBuffer buffer = new StringBuffer(); StringBuffer buffer = new StringBuffer();
do do
{ {
if(matcher.end() < RegexUtil.getLastAppendPosition(matcher)) return apply(matcher.appendTail(buffer).toString());
String text = RegexUtil.searchUntil(t, matcher.end()-1, braces.charAt(0), braces.charAt(1)); String text = RegexUtil.searchUntil(t, matcher.end()-1, braces.charAt(0), braces.charAt(1));
if(!text.isEmpty()) if(!text.isEmpty())
{ {
@ -63,6 +66,11 @@ public class InjectMapper implements IMapper
matcher.appendTail(buffer); matcher.appendTail(buffer);
return buffer.toString(); return buffer.toString();
} }
}
catch(Exception e)
{
e.printStackTrace();
}
return t; return t;
} }

View File

@ -6,9 +6,10 @@ import java.io.InputStream;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry;
public class FileUtils public class FileUtils
{ {
@ -40,9 +41,11 @@ public class FileUtils
{ {
try(BufferedWriter writer = Files.newBufferedWriter(dataFolder.resolve("cache.bin"))) try(BufferedWriter writer = Files.newBufferedWriter(dataFolder.resolve("cache.bin")))
{ {
for(Entry<String, String> entry : mappings.entrySet()) List<String> keys = new ArrayList<String>(mappings.keySet());
keys.sort(String.CASE_INSENSITIVE_ORDER);
for(String s : keys)
{ {
writer.write(entry.getKey()+"="+entry.getValue()); writer.write(s+"="+mappings.get(s));
writer.newLine(); writer.newLine();
} }
writer.flush(); writer.flush();

View File

@ -20,6 +20,18 @@ public class RegexUtil
return matcher; return matcher;
} }
public static int getLastAppendPosition(Matcher matcher)
{
try
{
return LAST_POS.getInt(matcher);
}
catch(Exception e)
{
throw new RuntimeException(e);
}
}
public static String searchUntil(String text, int startIndex, char increase, char decrease) public static String searchUntil(String text, int startIndex, char increase, char decrease)
{ {
if(text.charAt(startIndex + 1) != increase) if(text.charAt(startIndex + 1) != increase)

View File

@ -11,6 +11,7 @@ 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;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -56,6 +57,7 @@ public abstract class TemplateProcessor
System.out.println("Nothing has changed"); System.out.println("Nothing has changed");
return false; return false;
} }
AtomicLong[] counters = new AtomicLong[]{new AtomicLong(), new AtomicLong(), new AtomicLong()};
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; 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());
@ -67,8 +69,11 @@ public abstract class TemplateProcessor
Path path = pathsLeft.get(i); Path path = pathsLeft.get(i);
try try
{ {
long startTime = System.currentTimeMillis();
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, mappers))); 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);});
} }
catch(Exception e) catch(Exception e)
{ {
@ -95,7 +100,7 @@ public abstract class TemplateProcessor
System.out.println("Mapper ["+mapper.getSearchValue()+"] is not used in the Entire Build Process"); 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 Building ["+counters[0].get()+"] Files from ["+counters[1].get()+"] in "+(System.currentTimeMillis() - start)+"ms (Template Parsing: "+counters[2].get()+"ms (avg: "+((counters[2].get() / Math.max(1D, counters[1].get())))+"ms)");
FileUtils.saveMappings(existing, dataFolder); FileUtils.saveMappings(existing, dataFolder);
System.out.print("Saved Changes"); System.out.print("Saved Changes");
return true; return true;