2
2
3
3
import java .nio .file .Files ;
4
4
import java .nio .file .Paths ;
5
+ import java .util .ArrayList ;
6
+ import java .util .Arrays ;
5
7
import java .util .List ;
8
+ import java .util .Map ;
9
+ import java .util .concurrent .atomic .AtomicReference ;
10
+ import java .util .regex .Matcher ;
11
+ import java .util .regex .Pattern ;
6
12
import java .util .stream .Collectors ;
7
13
14
+ import lombok .Value ;
8
15
import org .apache .maven .plugin .AbstractMojo ;
9
16
import org .apache .maven .plugin .MojoExecutionException ;
10
17
import org .apache .maven .plugin .MojoFailureException ;
14
21
import org .apache .maven .project .MavenProject ;
15
22
16
23
import lombok .SneakyThrows ;
24
+ import org .assertj .core .util .VisibleForTesting ;
17
25
18
26
@ Mojo (name = "build" , defaultPhase = LifecyclePhase .PACKAGE )
19
27
public class TemplateMojo extends AbstractMojo {
20
28
29
+ public static final String TAG = "tag" ;
30
+
31
+ public static final String TAG_END = "end" ;
32
+
21
33
@ Parameter (property = "templateDirectory" )
22
34
String templateDirectory ;
23
35
@@ -45,10 +57,8 @@ public void execute() throws MojoExecutionException, MojoFailureException {
45
57
46
58
@ SneakyThrows
47
59
private List <String > readLines (String first , String ... more ) {
48
- return Files
49
- .readAllLines (Paths .get (first , more ))
50
- .stream ()
51
- .collect (Collectors .toList ());
60
+ return new ArrayList <>(Files
61
+ .readAllLines (Paths .get (first , more )));
52
62
}
53
63
54
64
private List <String > updateLines (List <String > lines ) {
@@ -67,13 +77,62 @@ private List<String> updateLine(final String line) {
67
77
}
68
78
69
79
private boolean matchesIncludeLine (final String line ) {
70
- return line .startsWith ("include::" ) &&
71
- line .endsWith (".adoc[]" );
80
+ return line .startsWith ("include::" ) &&
81
+ line .endsWith ("]" );
82
+ }
83
+
84
+ @ VisibleForTesting
85
+ List <String > updateIncludeLine (final String line ) {
86
+ var pathAndOptions = extractPathAndOptions (line );
87
+ if (pathAndOptions .optionMap .containsKey (TAG )) {
88
+ return readTaggedLines (templateDirectory , pathAndOptions );
89
+ }
90
+ return this .readLines (templateDirectory , pathAndOptions .path );
91
+ }
92
+
93
+ @ SneakyThrows
94
+ private List <String > readTaggedLines (String templateDirectory , PathAndOptions path ) {
95
+ ArrayList <String > lines = new ArrayList <>(Files
96
+ .readAllLines (Paths .get (templateDirectory , path .path )));
97
+ String tag = path .optionMap .get (TAG );
98
+ AtomicReference <Boolean > startHasBeenReached = new AtomicReference <>(false );
99
+ AtomicReference <Boolean > endHasBeenReached = new AtomicReference <>(false );
100
+ List <String > taggedLines = lines .stream ().filter (x -> {
101
+ boolean foundStart = x .contains (TAG + "::" + tag );
102
+ boolean foundEnd = x .contains (TAG_END + "::" + tag );
103
+ if (!startHasBeenReached .get ()) {
104
+ startHasBeenReached .set (foundStart );
105
+ }
106
+ if (startHasBeenReached .get () && !endHasBeenReached .get ()) {
107
+ endHasBeenReached .set (foundEnd );
108
+ }
109
+ boolean thisIsATagLine = foundStart || foundEnd ;
110
+ return !thisIsATagLine && startHasBeenReached .get () && !endHasBeenReached .get ();
111
+ }).collect (Collectors .toList ());
112
+ return taggedLines ;
113
+ }
114
+
115
+ @ Value
116
+ static
117
+ class PathAndOptions {
118
+ String path ;
119
+ Map <String , String > optionMap ;
72
120
}
73
121
74
- private List <String > updateIncludeLine (final String line ) {
75
- var path = line .substring (9 , line .length () - 2 );
76
- return this .readLines (templateDirectory , path );
122
+ @ VisibleForTesting
123
+ PathAndOptions extractPathAndOptions (String line ) {
124
+ int pathStart = 9 ;
125
+ Pattern pattern = Pattern .compile ("\\ [.*\\ ]$" );
126
+ Matcher matcher = pattern .matcher (line );
127
+ boolean found = matcher .find ();
128
+ String [] allOptions = matcher .group ().replaceAll ("[\\ [\\ ]]" , "" ).split ("," );
129
+ Map <String , String > optionMap = Arrays .asList (allOptions ).stream ()
130
+ .filter (x -> x .trim ().length () > 0 )
131
+ .map (x -> x .split ("=" ))
132
+ .collect (Collectors .toMap (x -> x [0 ], x -> x [1 ]));
133
+ int pathEnd = matcher .start ();
134
+ String path = line .substring (pathStart , pathEnd );
135
+ return new PathAndOptions (path , optionMap );
77
136
}
78
137
79
138
private void setDefaultConfiguration () {
0 commit comments