11package io .whelk .asciidoc ;
22
3+ import lombok .SneakyThrows ;
4+ import lombok .Value ;
5+ import org .apache .maven .plugin .AbstractMojo ;
6+ import org .apache .maven .plugin .MojoExecutionException ;
7+ import org .apache .maven .plugin .MojoFailureException ;
8+ import org .apache .maven .plugins .annotations .LifecyclePhase ;
9+ import org .apache .maven .plugins .annotations .Mojo ;
10+ import org .apache .maven .plugins .annotations .Parameter ;
11+ import org .apache .maven .project .MavenProject ;
12+
313import java .nio .file .Files ;
414import java .nio .file .Paths ;
515import java .util .ArrayList ;
1121import java .util .regex .Pattern ;
1222import java .util .stream .Collectors ;
1323
14- import lombok .Value ;
15- import org .apache .maven .plugin .AbstractMojo ;
16- import org .apache .maven .plugin .MojoExecutionException ;
17- import org .apache .maven .plugin .MojoFailureException ;
18- import org .apache .maven .plugins .annotations .LifecyclePhase ;
19- import org .apache .maven .plugins .annotations .Mojo ;
20- import org .apache .maven .plugins .annotations .Parameter ;
21- import org .apache .maven .project .MavenProject ;
22-
23- import lombok .SneakyThrows ;
24- import org .assertj .core .util .VisibleForTesting ;
24+ import static java .util .stream .Collectors .toMap ;
2525
2626@ Mojo (name = "build" , defaultPhase = LifecyclePhase .PACKAGE )
2727public class TemplateMojo extends AbstractMojo {
@@ -45,11 +45,15 @@ public class TemplateMojo extends AbstractMojo {
4545 @ Parameter (defaultValue = "${project}" , required = true , readonly = true )
4646 MavenProject project ;
4747
48+ // @VisibleForTesting
49+ Map <String , String > vars = Map .of ();
50+
4851 @ SneakyThrows
4952 public void execute () throws MojoExecutionException , MojoFailureException {
5053 setDefaultConfiguration ();
5154
5255 final var lines = this .readLines (templateDirectory , templateFile );
56+ this .vars = loadVars (lines );
5357 final var updatedLines = this .updateLines (lines );
5458
5559 Files .write (Paths .get (outputDirectory , outputFile ), updatedLines );
@@ -61,6 +65,27 @@ private List<String> readLines(String first, String... more) {
6165 .readAllLines (Paths .get (first , more )));
6266 }
6367
68+ // @VisibleForTesting
69+ Map <String , String > loadVars (List <String > strings ) {
70+ String varRegex = "^:[\\ w\\ -]+:" ; //includes dashes
71+ Pattern compile = Pattern .compile (varRegex );
72+ return strings .stream ()
73+ .map (x -> {
74+ Matcher matcher = compile .matcher (x );
75+ if (matcher .find ()) {
76+ int end = matcher .end ();
77+ String varName = matcher .group ().trim ();
78+ String trimMarkers = varName .substring (1 , varName .length () - 1 ).trim ();
79+ String value = x .substring (end ).trim ();
80+ return List .of (trimMarkers , value );
81+ } else {
82+ return List .<String >of ();
83+ }
84+ })
85+ .filter (x -> x .size () == 2 )
86+ .collect (toMap (x -> x .get (0 ), x -> x .get (1 )));
87+ }
88+
6489 private List <String > updateLines (List <String > lines ) {
6590 return lines
6691 .stream ()
@@ -81,7 +106,7 @@ private boolean matchesIncludeLine(final String line) {
81106 line .endsWith ("]" );
82107 }
83108
84- @ VisibleForTesting
109+ // @VisibleForTesting
85110 List <String > updateIncludeLine (final String line ) {
86111 var pathAndOptions = extractPathAndOptions (line );
87112 if (pathAndOptions .optionMap .containsKey (TAG )) {
@@ -98,12 +123,14 @@ private List<String> readTaggedLines(String templateDirectory, PathAndOptions pa
98123 AtomicReference <Boolean > startHasBeenReached = new AtomicReference <>(false );
99124 AtomicReference <Boolean > endHasBeenReached = new AtomicReference <>(false );
100125 List <String > taggedLines = lines .stream ().filter (x -> {
101- boolean foundStart = x . contains ( TAG + "::" + tag ) ;
102- boolean foundEnd = x . contains ( TAG_END + "::" + tag ) ;
126+ boolean foundStart = false ;
127+ boolean foundEnd = false ;
103128 if (!startHasBeenReached .get ()) {
129+ foundStart = x .contains (TAG + "::" + tag );
104130 startHasBeenReached .set (foundStart );
105131 }
106132 if (startHasBeenReached .get () && !endHasBeenReached .get ()) {
133+ foundEnd = x .contains (TAG_END + "::" + tag );
107134 endHasBeenReached .set (foundEnd );
108135 }
109136 boolean thisIsATagLine = foundStart || foundEnd ;
@@ -119,7 +146,7 @@ class PathAndOptions {
119146 Map <String , String > optionMap ;
120147 }
121148
122- @ VisibleForTesting
149+ // @VisibleForTesting
123150 PathAndOptions extractPathAndOptions (String line ) {
124151 int pathStart = 9 ;
125152 Pattern pattern = Pattern .compile ("\\ [.*\\ ]$" );
@@ -129,9 +156,13 @@ PathAndOptions extractPathAndOptions(String line) {
129156 Map <String , String > optionMap = Arrays .asList (allOptions ).stream ()
130157 .filter (x -> x .trim ().length () > 0 )
131158 .map (x -> x .split ("=" ))
132- .collect (Collectors . toMap (x -> x [0 ], x -> x [1 ]));
159+ .collect (toMap (x -> x [0 ], x -> x [1 ]));
133160 int pathEnd = matcher .start ();
134161 String path = line .substring (pathStart , pathEnd );
162+ for (var variable : this .vars .entrySet ()) {
163+ String needle = "\\ {" + variable .getKey () + "\\ }" ;
164+ path = path .replaceAll (needle , variable .getValue ());
165+ }
135166 return new PathAndOptions (path , optionMap );
136167 }
137168
0 commit comments