1
1
/*
2
- * Copyright 2022 -present the original author or authors.
2
+ * Copyright 2017 -present the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
6
6
* You may obtain a copy of the License at
7
7
*
8
- * https://www.apache.org/licenses/LICENSE-2.0
8
+ * https://www.apache.org/licenses/LICENSE-2.0
9
9
*
10
10
* Unless required by applicable law or agreed to in writing, software
11
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- * See the License for the specific language governing permissions and
14
- * limitations under the License.
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
13
+ * or implied. See the License for the specific language governing
14
+ * permissions and limitations under the License.
15
15
*/
16
16
package io .spring .gradle .convention ;
17
17
18
18
import java .io .File ;
19
- import java .net .URI ;
20
19
import java .time .LocalDate ;
21
20
import java .util .ArrayList ;
22
21
import java .util .Collections ;
29
28
import org .asciidoctor .gradle .jvm .AsciidoctorJExtension ;
30
29
import org .asciidoctor .gradle .jvm .AsciidoctorJPlugin ;
31
30
import org .asciidoctor .gradle .jvm .AsciidoctorTask ;
31
+ import org .gradle .api .JavaVersion ;
32
32
import org .gradle .api .Plugin ;
33
33
import org .gradle .api .Project ;
34
34
import org .gradle .api .artifacts .Configuration ;
35
- import org .gradle .api .artifacts .dsl .RepositoryHandler ;
36
35
import org .gradle .api .file .DuplicatesStrategy ;
37
36
import org .gradle .api .file .FileTree ;
38
37
import org .gradle .api .tasks .Sync ;
39
38
40
39
/**
41
40
* Conventions that are applied in the presence of the {@link AsciidoctorJPlugin}.
42
- *
41
+ * <p/>
43
42
* When the plugin is applied:
44
43
*
45
44
* <ul>
63
62
*
64
63
* @author Andy Wilkinson
65
64
* @author Rob Winch
65
+ * @author John Blum
66
66
*/
67
67
public class AsciidoctorConventionPlugin implements Plugin <Project > {
68
68
69
- private static final String SPRING_ASCIIDOCTOR_EXTENSIONS_BLOCK_SWITCH_VERSION = "0.4.2.RELEASE" ;
69
+ private static final String ASCIIDOCTORJ_VERSION = "2.4.3" ;
70
+ private static final String SPRING_ASCIIDOCTOR_BACKENDS_VERSION = "0.0.5" ;
70
71
private static final String SPRING_DOC_RESOURCES_VERSION = "0.2.5" ;
71
72
72
- private static final String SPRING_ASCIIDOCTOR_EXTENSION_BLOCK_SWITCH_DEPENDENCY =
73
- String .format ("io.spring.asciidoctor:spring-asciidoctor-extensions-block-switch :%s" ,
74
- SPRING_ASCIIDOCTOR_EXTENSIONS_BLOCK_SWITCH_VERSION );
73
+ private static final String SPRING_ASCIIDOCTOR_BACKENDS_DEPENDENCY =
74
+ String .format ("io.spring.asciidoctor.backends :spring-asciidoctor-backends :%s" ,
75
+ SPRING_ASCIIDOCTOR_BACKENDS_VERSION );
75
76
77
+ @ SuppressWarnings ("unused" )
76
78
private static final String SPRING_DOC_RESOURCES_DEPENDENCY =
77
79
String .format ("io.spring.docresources:spring-doc-resources:%s" , SPRING_DOC_RESOURCES_VERSION );
78
80
@@ -81,19 +83,16 @@ public void apply(Project project) {
81
83
82
84
project .getPlugins ().withType (AsciidoctorJPlugin .class , asciidoctorPlugin -> {
83
85
84
- createDefaultAsciidoctorRepository (project );
86
+ setAsciidoctorJVersion (project );
85
87
makeAllWarningsFatal (project );
88
+ createAsciidoctorExtensionsConfiguration (project );
86
89
87
90
Sync unzipResources = createUnzipDocumentationResourcesTask (project );
88
91
89
92
project .getTasks ().withType (AbstractAsciidoctorTask .class , asciidoctorTask -> {
90
93
91
94
asciidoctorTask .dependsOn (unzipResources );
92
- configureAttributes (project , asciidoctorTask );
93
- configureExtensions (project , asciidoctorTask );
94
- configureOptions (asciidoctorTask );
95
- asciidoctorTask .baseDirFollowsSourceDir ();
96
- asciidoctorTask .useIntermediateWorkDir ();
95
+ configureAsciidoctorTask (project , asciidoctorTask );
97
96
98
97
asciidoctorTask .resources (resourcesSpec -> {
99
98
resourcesSpec .setDuplicatesStrategy (DuplicatesStrategy .INCLUDE );
@@ -105,45 +104,52 @@ public void apply(Project project) {
105
104
// resourcesSrcDirSpec.include("images/**");
106
105
});
107
106
});
108
-
109
- if (asciidoctorTask instanceof AsciidoctorTask ) {
110
- configureHtmlOnlyAttributes (project , asciidoctorTask );
111
- }
112
107
});
113
108
});
114
109
}
115
110
116
- private void createDefaultAsciidoctorRepository (Project project ) {
111
+ private void setAsciidoctorJVersion (Project project ) {
112
+ project .getExtensions ().getByType (AsciidoctorJExtension .class ).setVersion (ASCIIDOCTORJ_VERSION );
113
+ }
114
+
115
+ private void makeAllWarningsFatal (Project project ) {
116
+ project .getExtensions ().getByType (AsciidoctorJExtension .class ).fatalWarnings (".*" );
117
+ }
118
+
119
+ private void createAsciidoctorExtensionsConfiguration (Project project ) {
120
+
121
+ project .getConfigurations ().create ("asciidoctorExtensions" , configuration -> {
117
122
118
- project .getGradle ().afterProject (it -> {
123
+ project .getConfigurations ()
124
+ .matching (it -> "dependencyManagement" .equals (it .getName ()))
125
+ .all (configuration ::extendsFrom );
119
126
120
- RepositoryHandler repositories = it .getRepositories ();
127
+ configuration .getDependencies ()
128
+ .add (project .getDependencies ().create (SPRING_ASCIIDOCTOR_BACKENDS_DEPENDENCY ));
121
129
122
- if (repositories .isEmpty ()) {
123
- repositories .mavenCentral ();
124
- repositories .maven (repo -> repo .setUrl (URI .create ("https://repo.spring.io/release" )));
125
- }
130
+ // TODO: Why is the asiidoctorj-pdf dependency needed?
131
+ configuration .getDependencies ()
132
+ .add (project .getDependencies ().create ("org.asciidoctor:asciidoctorj-pdf:1.5.3" ));
126
133
});
127
134
}
128
135
129
136
/**
130
- * Requests the base Spring Documentation Resources from {@literal https://repo.spring.io/release } and uses it
131
- * to format and render documentation.
137
+ * Requests the base Spring Documentation Resources from {@literal Maven Central } and uses it to format
138
+ * and render documentation.
132
139
*
133
140
* @param project {@literal this} Gradle {@link Project}.
134
- * @return a {@link Sync} task used to copy the Spring Documentation Resources to a build directory
141
+ * @return a {@link Sync} task that copies Spring Documentation Resources to the build directory
135
142
* used to generate documentation.
136
- * @see <a href="https://repo.spring.io/ui/native/release/io/spring/docresources/spring-doc-resources">spring-doc-resources</a>
137
143
* @see org.gradle.api.tasks.Sync
138
144
* @see org.gradle.api.Project
139
145
*/
140
146
@ SuppressWarnings ("all" )
141
147
private Sync createUnzipDocumentationResourcesTask (Project project ) {
142
148
143
- Configuration documentationResources = project .getConfigurations ().maybeCreate ("documentationResources" );
149
+ Configuration documentationResources = project .getConfigurations ().create ("documentationResources" );
144
150
145
151
documentationResources .getDependencies ()
146
- .add (project .getDependencies ().create (SPRING_DOC_RESOURCES_DEPENDENCY ));
152
+ .add (project .getDependencies ().create (SPRING_ASCIIDOCTOR_BACKENDS_DEPENDENCY ));
147
153
148
154
Sync unzipResources = project .getTasks ().create ("unzipDocumentationResources" , Sync .class , sync -> {
149
155
@@ -166,65 +172,71 @@ private Sync createUnzipDocumentationResourcesTask(Project project) {
166
172
return unzipResources ;
167
173
}
168
174
169
- @ SuppressWarnings ("unused" )
175
+ private void configureAsciidoctorTask (Project project , AbstractAsciidoctorTask asciidoctorTask ) {
176
+
177
+ asciidoctorTask .baseDirFollowsSourceDir ();
178
+ asciidoctorTask .configurations ("asciidoctorExtensions" );
179
+ //asciidoctorTask.useIntermediateWorkDir();
180
+
181
+ configureAttributes (project , asciidoctorTask );
182
+ configureForkOptions (asciidoctorTask );
183
+ configureOptions (asciidoctorTask );
184
+
185
+ if (asciidoctorTask instanceof AsciidoctorTask ) {
186
+ boolean pdf = asciidoctorTask .getName ().toLowerCase ().contains ("pdf" );
187
+ String backend = pdf ? "spring-pdf" : "spring-html" ;
188
+ ((AsciidoctorTask ) asciidoctorTask ).outputOptions ((outputOptions ) -> outputOptions .backends (backend ));
189
+ configureHtmlOnlyAttributes (asciidoctorTask );
190
+ }
191
+ }
192
+
170
193
private void configureAttributes (Project project , AbstractAsciidoctorTask asciidoctorTask ) {
171
194
172
195
Map <String , Object > attributes = new HashMap <>();
173
196
174
197
attributes .put ("attribute-missing" , "warn" );
175
- attributes .put ("icons " , "font " );
198
+ attributes .put ("docinfo " , "shared " );
176
199
attributes .put ("idprefix" , "" );
177
200
attributes .put ("idseparator" , "-" );
178
- attributes .put ("docinfo" , "shared" );
179
201
attributes .put ("sectanchors" , "" );
180
202
attributes .put ("sectnums" , "" );
181
203
attributes .put ("today-year" , LocalDate .now ().getYear ());
182
204
183
- asciidoctorTask .attributes (attributes );
184
- }
185
-
186
- private void configureExtensions (Project project , AbstractAsciidoctorTask asciidoctorTask ) {
205
+ Object version = project .getVersion ();
187
206
188
- Configuration extensionsConfiguration = project .getConfigurations ().maybeCreate ("asciidoctorExtensions" );
207
+ if (version != null && !Project .DEFAULT_VERSION .equals (version )) {
208
+ attributes .put ("revnumber" , version );
209
+ }
189
210
190
- extensionsConfiguration . defaultDependencies ( dependencies -> dependencies . add ( project . getDependencies ()
191
- . create ( SPRING_ASCIIDOCTOR_EXTENSION_BLOCK_SWITCH_DEPENDENCY )));
211
+ asciidoctorTask . attributes ( attributes );
212
+ }
192
213
193
- asciidoctorTask .configurations (extensionsConfiguration );
214
+ private void configureForkOptions (AbstractAsciidoctorTask asciidoctorTask ) {
215
+ if (JavaVersion .current ().isCompatibleWith (JavaVersion .VERSION_16 )) {
216
+ asciidoctorTask .forkOptions (options -> options .jvmArgs (
217
+ "--add-opens" , "java.base/sun.nio.ch=ALL-UNNAMED" ,
218
+ "--add-opens" , "java.base/java.io=ALL-UNNAMED" )
219
+ );
220
+ }
194
221
}
195
222
196
- private void configureHtmlOnlyAttributes (Project project , AbstractAsciidoctorTask asciidoctorTask ) {
223
+ private void configureHtmlOnlyAttributes (AbstractAsciidoctorTask asciidoctorTask ) {
197
224
198
225
Map <String , Object > attributes = new HashMap <>();
199
226
200
- attributes .put ("source-highlighter" , "highlight.js" );
201
227
attributes .put ("highlightjsdir" , "js/highlight" );
202
228
attributes .put ("highlightjs-theme" , "github" );
203
- attributes .put ("linkcss " , true );
229
+ attributes .put ("source-highlighter " , "highlight.js" );
204
230
attributes .put ("icons" , "font" );
205
- attributes .put ("stylesheet" , "css/spring.css" );
206
-
207
- asciidoctorTask .getAttributeProviders ().add (() -> {
208
-
209
- Object version = project .getVersion ();
210
-
211
- Map <String , Object > localAttributes = new HashMap <>();
212
-
213
- if (version != null && !Project .DEFAULT_VERSION .equals (version )) {
214
- localAttributes .put ("revnumber" , version );
215
- }
216
-
217
- return localAttributes ;
218
- });
231
+ attributes .put ("imagesdir" , "./images" );
232
+ attributes .put ("linkcss" , true );
233
+ attributes .put ("stylesdir" , "css/" );
234
+ //attributes.put("stylesheet", "spring.css");
219
235
220
236
asciidoctorTask .attributes (attributes );
221
237
}
222
238
223
239
private void configureOptions (AbstractAsciidoctorTask asciidoctorTask ) {
224
240
asciidoctorTask .options (Collections .singletonMap ("doctype" , "book" ));
225
241
}
226
-
227
- private void makeAllWarningsFatal (Project project ) {
228
- project .getExtensions ().getByType (AsciidoctorJExtension .class ).fatalWarnings (".*" );
229
- }
230
242
}
0 commit comments