Added Iteration support.

Said support allows to use the #iterate & #argument #enditerate
parameters.
Which will duplicate the text block inside of it x amount of times with
arguments being replaced.

The idea being that if code can be replicated the iterate option would
simplify the process.

Multiple arguments can be defined (they will be not part of the output)
and it will simply insert them.
Note that all argument parameters have to be equal size.
Otherwise the iterator processor will fail.

Iterators can be stacked too to create layers upon layers.

On top of that the iterator is part of the Template parser and not of
the template processor meaning its a Pre processor step.
And all mappers still apply themselves on to the output of the
iterators!
This commit is contained in:
2023-06-27 13:29:31 +02:00
parent d90a9ad71c
commit d5a47ce568
11 changed files with 206 additions and 59 deletions
@@ -97,6 +97,10 @@ public class ConditionedSegment
i += parse(fileName, null, lines, i, segmentText.length(), ignoreCounter, childSegments, postSegments);
segmentText.add(lines.get(prev));
}
else if(trimmed.startsWith("#iterate")) {
SegmentIterator.iterateSegment(fileName, trimmed, lines, i, segmentText.length());
i--;
}
continue;
}
segmentText.add(s);
@@ -0,0 +1,61 @@
package speiger.src.builder.base;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.StringJoiner;
import java.util.stream.Collectors;
public class SegmentIterator {
public static void iterateSegment(String fileName, String currentLine, List<String> lines, int currentIndex, int startIndex) {
List<String[]> arguments = new ArrayList<>();
StringJoiner toIterate = new StringJoiner("\n");
int skip = 0;
int end = 1;
for(int i = currentIndex+1;i<lines.size();i++, end++)
{
String s = lines.get(i);
String trimmed = s.trim();
if(trimmed.startsWith("#"))
{
if(trimmed.startsWith("#enditerate"))
{
skip--;
if(skip < 0)
{
end++;
break;
}
}
if(trimmed.startsWith("#iterate")) skip++;
if(skip > 0)
{
toIterate.add(s);
continue;
}
if(trimmed.startsWith("#argument"))
{
arguments.add(trimmed.substring(9).trim().split(" "));
continue;
}
}
toIterate.add(s);
}
int size = arguments.get(0).length;
for(int i = 1,m=arguments.size();i<m;i++) {
if(arguments.get(i).length != size) throw new RuntimeException("Iteration arguments in file ["+fileName+"] are not equal size. Arugments="+arguments.stream().flatMap(Arrays::stream).collect(Collectors.toList()));
}
for(int i = 0;i<end;i++) {
lines.remove(currentIndex);
}
String toInsert = toIterate.toString();
for(int i = size-1;i>=1;i--) {
String temp = toInsert;
for(int j = 0;j<arguments.size();j++) {
String[] argument = arguments.get(j);
temp = temp.replace(argument[0], argument[i]);
}
lines.addAll(currentIndex, Arrays.asList(temp.split("\n", -1)));
}
}
}
@@ -92,6 +92,11 @@ public class Template
joiner.add(lines.get(prev));
continue;
}
else if(trimmed.startsWith("#iterate")) {
SegmentIterator.iterateSegment(fileName, trimmed, lines, i, joiner.length());
i--;
continue;
}
else if(trimmed.startsWith("#symlink"))
{
return Template.parse(file.getParent().resolve(trimmed.substring(8).trim()));
@@ -33,7 +33,7 @@ public interface ICondition
if(activeAnd != null) continue;
if(i != 0) {
ICondition prev = conditions.get(conditions.size()-1);
if(prev instanceof AndCondition) activeAnd = (AndCondition)prev;
if(prev instanceof AndCondition) activeAnd = (AndCondition)prev;
else {
activeAnd = new AndCondition(prev);
conditions.set(conditions.size()-1, activeAnd);
@@ -96,6 +96,7 @@ public abstract class TemplateProcessor
protected Path getDataFolder() { return dataFolder; }
protected boolean isSilencedSuccess() { return silencedSuccess; }
@SuppressWarnings("unchecked")
public final boolean process(boolean force) throws IOException, InterruptedException
{
if(!init)
@@ -112,7 +113,7 @@ public abstract class TemplateProcessor
}
AtomicLong[] counters = new AtomicLong[]{new AtomicLong(), new AtomicLong(), new AtomicLong()};
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<>()), Collections.synchronizedSet(new HashSet<>())} : null;
ThreadPoolExecutor service = (ThreadPoolExecutor)Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
service.setKeepAliveTime(10, TimeUnit.MILLISECONDS);
service.allowCoreThreadTimeOut(true);