1
1
package io .whelk .asciidoc ;
2
2
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
+
3
13
import java .nio .file .Files ;
4
14
import java .nio .file .Paths ;
5
15
import java .util .ArrayList ;
11
21
import java .util .regex .Pattern ;
12
22
import java .util .stream .Collectors ;
13
23
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 ;
25
25
26
26
@ Mojo (name = "build" , defaultPhase = LifecyclePhase .PACKAGE )
27
27
public class TemplateMojo extends AbstractMojo {
@@ -45,11 +45,15 @@ public class TemplateMojo extends AbstractMojo {
45
45
@ Parameter (defaultValue = "${project}" , required = true , readonly = true )
46
46
MavenProject project ;
47
47
48
+ // @VisibleForTesting
49
+ Map <String , String > vars = Map .of ();
50
+
48
51
@ SneakyThrows
49
52
public void execute () throws MojoExecutionException , MojoFailureException {
50
53
setDefaultConfiguration ();
51
54
52
55
final var lines = this .readLines (templateDirectory , templateFile );
56
+ this .vars = loadVars (lines );
53
57
final var updatedLines = this .updateLines (lines );
54
58
55
59
Files .write (Paths .get (outputDirectory , outputFile ), updatedLines );
@@ -61,6 +65,27 @@ private List<String> readLines(String first, String... more) {
61
65
.readAllLines (Paths .get (first , more )));
62
66
}
63
67
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
+
64
89
private List <String > updateLines (List <String > lines ) {
65
90
return lines
66
91
.stream ()
@@ -81,7 +106,7 @@ private boolean matchesIncludeLine(final String line) {
81
106
line .endsWith ("]" );
82
107
}
83
108
84
- @ VisibleForTesting
109
+ // @VisibleForTesting
85
110
List <String > updateIncludeLine (final String line ) {
86
111
var pathAndOptions = extractPathAndOptions (line );
87
112
if (pathAndOptions .optionMap .containsKey (TAG )) {
@@ -98,12 +123,14 @@ private List<String> readTaggedLines(String templateDirectory, PathAndOptions pa
98
123
AtomicReference <Boolean > startHasBeenReached = new AtomicReference <>(false );
99
124
AtomicReference <Boolean > endHasBeenReached = new AtomicReference <>(false );
100
125
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 ;
103
128
if (!startHasBeenReached .get ()) {
129
+ foundStart = x .contains (TAG + "::" + tag );
104
130
startHasBeenReached .set (foundStart );
105
131
}
106
132
if (startHasBeenReached .get () && !endHasBeenReached .get ()) {
133
+ foundEnd = x .contains (TAG_END + "::" + tag );
107
134
endHasBeenReached .set (foundEnd );
108
135
}
109
136
boolean thisIsATagLine = foundStart || foundEnd ;
@@ -119,7 +146,7 @@ class PathAndOptions {
119
146
Map <String , String > optionMap ;
120
147
}
121
148
122
- @ VisibleForTesting
149
+ // @VisibleForTesting
123
150
PathAndOptions extractPathAndOptions (String line ) {
124
151
int pathStart = 9 ;
125
152
Pattern pattern = Pattern .compile ("\\ [.*\\ ]$" );
@@ -129,9 +156,13 @@ PathAndOptions extractPathAndOptions(String line) {
129
156
Map <String , String > optionMap = Arrays .asList (allOptions ).stream ()
130
157
.filter (x -> x .trim ().length () > 0 )
131
158
.map (x -> x .split ("=" ))
132
- .collect (Collectors . toMap (x -> x [0 ], x -> x [1 ]));
159
+ .collect (toMap (x -> x [0 ], x -> x [1 ]));
133
160
int pathEnd = matcher .start ();
134
161
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
+ }
135
166
return new PathAndOptions (path , optionMap );
136
167
}
137
168
0 commit comments