1
1
/*
2
- * Copyright 2022 the original author or authors.
2
+ * Copyright 2022-2023 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.
16
16
package org .springframework .shell .gradle ;
17
17
18
18
import java .io .File ;
19
- import java .net .URI ;
20
19
import java .time .LocalDate ;
21
- import java .util .ArrayList ;
22
20
import java .util .Collections ;
23
21
import java .util .HashMap ;
24
- import java .util .List ;
25
22
import java .util .Map ;
26
- import java .util .concurrent .Callable ;
27
- import java .util .function .Consumer ;
28
23
29
24
import org .asciidoctor .gradle .base .AsciidoctorAttributeProvider ;
30
25
import org .asciidoctor .gradle .jvm .AbstractAsciidoctorTask ;
26
+ import org .asciidoctor .gradle .jvm .AsciidoctorJExtension ;
31
27
import org .asciidoctor .gradle .jvm .AsciidoctorJPlugin ;
32
28
import org .asciidoctor .gradle .jvm .AsciidoctorTask ;
33
29
import org .gradle .api .Action ;
38
34
import org .gradle .api .plugins .JavaPlugin ;
39
35
import org .gradle .api .plugins .PluginManager ;
40
36
import org .gradle .api .publish .tasks .GenerateModuleMetadata ;
41
- import org .gradle .api .artifacts .Configuration ;
42
- import org .gradle .api .artifacts .dsl .RepositoryHandler ;
43
- import org .gradle .api .file .CopySpec ;
44
- import org .gradle .api .file .FileTree ;
37
+ import org .gradle .api .tasks .PathSensitivity ;
45
38
import org .gradle .api .tasks .Sync ;
39
+ import org .springframework .util .StringUtils ;
46
40
47
41
/**
48
42
* @author Janne Valkealahti
49
43
*/
50
44
class DocsPlugin implements Plugin <Project > {
51
45
46
+ private static final String ASCIIDOCTORJ_VERSION = "2.4.3" ;
47
+ private static final String EXTENSIONS_CONFIGURATION_NAME = "asciidoctorExtensions" ;
48
+
52
49
@ Override
53
50
public void apply (Project project ) {
54
51
PluginManager pluginManager = project .getPluginManager ();
@@ -63,133 +60,83 @@ public void apply(Project project) {
63
60
task .enforcedPlatform (":spring-shell-management" );
64
61
});
65
62
66
- configureAdocPlugins (project , dependencyVersions );
67
-
63
+ project .getPlugins ().withType (AsciidoctorJPlugin .class , (asciidoctorPlugin ) -> {
64
+ // makeAllWarningsFatal(project);
65
+ upgradeAsciidoctorJVersion (project );
66
+ createAsciidoctorExtensionsConfiguration (project );
67
+ project .getTasks ()
68
+ .withType (AbstractAsciidoctorTask .class ,
69
+ (asciidoctorTask ) -> configureAsciidoctorTask (project , asciidoctorTask , dependencyVersions ));
70
+ });
68
71
project .getTasks ().withType (GenerateModuleMetadata .class , metadata -> {
69
72
metadata .setEnabled (false );
70
73
});
71
74
}
72
75
73
- private void configureAdocPlugins (Project project , ExtractVersionConstraints dependencyVersions ) {
74
- project .getPlugins ().withType (AsciidoctorJPlugin .class , (asciidoctorPlugin ) -> {
75
- createDefaultAsciidoctorRepository (project );
76
- makeAllWarningsFatal (project );
77
- Sync unzipResources = createUnzipDocumentationResourcesTask (project );
78
- Sync snippetsResources = createSnippetsResourcesTask (project );
79
- project .getTasks ().withType (AbstractAsciidoctorTask .class , (asciidoctorTask ) -> {
80
- asciidoctorTask .dependsOn (dependencyVersions );
81
- asciidoctorTask .dependsOn (unzipResources );
82
- asciidoctorTask .dependsOn (snippetsResources );
83
- // configureExtensions(project, asciidoctorTask);
84
- configureCommonAttributes (project , asciidoctorTask );
85
- configureOptions (asciidoctorTask );
86
- asciidoctorTask .sourceDir ("src/main/asciidoc" );
87
- asciidoctorTask .baseDirFollowsSourceDir ();
88
- asciidoctorTask .useIntermediateWorkDir ();
89
- asciidoctorTask .resources (new Action <CopySpec >() {
90
- @ Override
91
- public void execute (CopySpec resourcesSpec ) {
92
- resourcesSpec .from (unzipResources );
93
- // resourcesSpec.from(snippetsResources);
94
- resourcesSpec .from (snippetsResources , copySpec -> {
95
- copySpec .include ("docs/*" );
96
- });
97
- resourcesSpec .from (asciidoctorTask .getSourceDir (), new Action <CopySpec >() {
98
- @ Override
99
- public void execute (CopySpec resourcesSrcDirSpec ) {
100
- // https://github.com/asciidoctor/asciidoctor-gradle-plugin/issues/523
101
- // For now copy the entire sourceDir over so that include files are
102
- // available in the intermediateWorkDir
103
- resourcesSrcDirSpec .include ("images/*" );
104
- resourcesSrcDirSpec .include ("code/*" );
105
- }
106
- });
107
- }
108
- });
109
- if (asciidoctorTask instanceof AsciidoctorTask ) {
110
- configureHtmlOnlyAttributes (project , asciidoctorTask , dependencyVersions );
111
- }
112
- });
113
- });
76
+ private void upgradeAsciidoctorJVersion (Project project ) {
77
+ project .getExtensions ().getByType (AsciidoctorJExtension .class ).setVersion (ASCIIDOCTORJ_VERSION );
114
78
}
115
79
116
- private void createDefaultAsciidoctorRepository (Project project ) {
117
- project .getGradle ().afterProject (new Action <Project >() {
118
- @ Override
119
- public void execute (Project project ) {
120
- RepositoryHandler repositories = project .getRepositories ();
121
- if (repositories .isEmpty ()) {
122
- repositories .mavenCentral ();
123
- repositories .maven (repo -> {
124
- repo .setUrl (URI .create ("https://repo.spring.io/release" ));
125
- });
126
- }
127
- }
80
+ private void createAsciidoctorExtensionsConfiguration (Project project ) {
81
+ project .getConfigurations ().create (EXTENSIONS_CONFIGURATION_NAME , (configuration ) -> {
82
+ configuration .getDependencies ()
83
+ .add (project .getDependencies ()
84
+ .create ("io.spring.asciidoctor.backends:spring-asciidoctor-backends:0.0.5" ));
128
85
});
129
86
}
130
87
131
- private void makeAllWarningsFatal (Project project ) {
132
- // project.getExtensions().getByType(AsciidoctorJExtension.class).fatalWarnings(".*");
88
+ private void configureAsciidoctorTask (Project project , AbstractAsciidoctorTask asciidoctorTask , ExtractVersionConstraints dependencyVersions ) {
89
+ asciidoctorTask .configurations (EXTENSIONS_CONFIGURATION_NAME );
90
+ configureCommonAttributes (project , asciidoctorTask , dependencyVersions );
91
+ configureOptions (asciidoctorTask );
92
+ asciidoctorTask .baseDirFollowsSourceDir ();
93
+ createSyncDocumentationSourceTask (project , asciidoctorTask , dependencyVersions );
94
+ if (asciidoctorTask instanceof AsciidoctorTask task ) {
95
+ task .outputOptions ((outputOptions ) -> outputOptions .backends ("spring-html" ));
96
+ }
133
97
}
134
98
135
- // private void configureExtensions(Project project, AbstractAsciidoctorTask asciidoctorTask) {
136
- // Configuration extensionsConfiguration = project.getConfigurations().maybeCreate("asciidoctorExtensions");
137
- // extensionsConfiguration.defaultDependencies(new Action<DependencySet>() {
138
- // @Override
139
- // public void execute(DependencySet dependencies) {
140
- // dependencies.add(project.getDependencies().create("io.spring.asciidoctor:spring-asciidoctor-extensions-block-switch:0.4.2.RELEASE"));
141
- // }
142
- // });
143
- // asciidoctorTask.configurations(extensionsConfiguration);
144
- // }
99
+ private void configureOptions (AbstractAsciidoctorTask asciidoctorTask ) {
100
+ asciidoctorTask .options (Collections .singletonMap ("doctype" , "book" ));
101
+ }
102
+
103
+ private Sync createSyncDocumentationSourceTask (Project project , AbstractAsciidoctorTask asciidoctorTask , ExtractVersionConstraints dependencyVersions ) {
104
+ Sync syncDocumentationSource = project .getTasks ()
105
+ .create ("syncDocumentationSourceFor" + StringUtils .capitalize (asciidoctorTask .getName ()), Sync .class );
106
+ syncDocumentationSource .preserve (filter -> {
107
+ filter .include ("**/*" );
108
+ });
109
+ File syncedSource = new File (project .getBuildDir (), "docs/src/" + asciidoctorTask .getName ());
110
+ syncDocumentationSource .setDestinationDir (syncedSource );
111
+ syncDocumentationSource .from ("src/main/" );
112
+ asciidoctorTask .dependsOn (syncDocumentationSource );
113
+ asciidoctorTask .dependsOn (dependencyVersions );
114
+ Sync snippetsResources = createSnippetsResourcesTask (project );
115
+ asciidoctorTask .dependsOn (snippetsResources );
116
+ asciidoctorTask .getInputs ()
117
+ .dir (syncedSource )
118
+ .withPathSensitivity (PathSensitivity .RELATIVE )
119
+ .withPropertyName ("synced source" );
120
+ asciidoctorTask .setSourceDir (project .relativePath (new File (syncedSource , "asciidoc/" )));
121
+ return syncDocumentationSource ;
122
+ }
145
123
146
124
private Sync createSnippetsResourcesTask (Project project ) {
147
125
Sync sync = project .getTasks ().create ("snippetResources" , Sync .class , s -> {
148
126
s .from (new File (project .getRootProject ().getRootDir (), "spring-shell-docs/src/test/java/org/springframework/shell" ), spec -> {
149
127
spec .include ("docs/*" );
150
128
});
151
- File destination = new File (project .getBuildDir (), "docs/snippets" );
129
+ s .preserve (filter -> {
130
+ filter .include ("**/*" );
131
+ });
132
+ File destination = new File (project .getBuildDir (), "docs/src/asciidoctor/asciidoc" );
152
133
s .into (destination );
153
134
});
154
135
return sync ;
155
136
}
156
137
157
- private Sync createUnzipDocumentationResourcesTask (Project project ) {
158
- Configuration documentationResources = project .getConfigurations ().maybeCreate ("documentationResources" );
159
- documentationResources .getDependencies ()
160
- .add (project .getDependencies ().create ("io.spring.docresources:spring-doc-resources:0.2.5" ));
161
- Sync unzipResources = project .getTasks ().create ("unzipDocumentationResources" ,
162
- Sync .class , new Action <Sync >() {
163
- @ Override
164
- public void execute (Sync sync ) {
165
- sync .dependsOn (documentationResources );
166
- sync .from (new Callable <List <FileTree >>() {
167
- @ Override
168
- public List <FileTree > call () throws Exception {
169
- List <FileTree > result = new ArrayList <>();
170
- documentationResources .getAsFileTree ().forEach (new Consumer <File >() {
171
- @ Override
172
- public void accept (File file ) {
173
- result .add (project .zipTree (file ));
174
- }
175
- });
176
- return result ;
177
- }
178
- });
179
- File destination = new File (project .getBuildDir (), "docs/resources" );
180
- sync .into (project .relativePath (destination ));
181
- }
182
- });
183
- return unzipResources ;
184
- }
185
-
186
- private void configureOptions (AbstractAsciidoctorTask asciidoctorTask ) {
187
- asciidoctorTask .options (Collections .singletonMap ("doctype" , "book" ));
188
- }
189
-
190
- private void configureHtmlOnlyAttributes (Project project , AbstractAsciidoctorTask asciidoctorTask ,
138
+ private void configureCommonAttributes (Project project , AbstractAsciidoctorTask asciidoctorTask ,
191
139
ExtractVersionConstraints dependencyVersions ) {
192
-
193
140
asciidoctorTask .doFirst (new Action <Task >() {
194
141
195
142
@ Override
@@ -209,13 +156,15 @@ public Map<String, Object> getAttributes() {
209
156
210
157
Map <String , Object > attributes = new HashMap <>();
211
158
attributes .put ("toc" , "left" );
212
- attributes .put ("source-highlighter" , "highlight.js" );
213
- attributes .put ("highlightjsdir" , "js/highlight" );
214
- attributes .put ("highlightjs-theme" , "github" );
215
- attributes .put ("linkcss" , true );
216
159
attributes .put ("icons" , "font" );
217
- attributes .put ("stylesheet" , "css/spring.css" );
160
+ attributes .put ("idprefix" , "" );
161
+ attributes .put ("idseparator" , "-" );
162
+ attributes .put ("docinfo" , "shared" );
163
+ attributes .put ("sectanchors" , "" );
164
+ attributes .put ("sectnums" , "" );
165
+ attributes .put ("today-year" , LocalDate .now ().getYear ());
218
166
attributes .put ("snippets" , "docs" );
167
+
219
168
asciidoctorTask .getAttributeProviders ().add (new AsciidoctorAttributeProvider () {
220
169
@ Override
221
170
public Map <String , Object > getAttributes () {
@@ -229,18 +178,4 @@ public Map<String, Object> getAttributes() {
229
178
});
230
179
asciidoctorTask .attributes (attributes );
231
180
}
232
-
233
- private void configureCommonAttributes (Project project , AbstractAsciidoctorTask asciidoctorTask ) {
234
- Map <String , Object > attributes = new HashMap <>();
235
- // attributes.put("attribute-missing", "warn");
236
- attributes .put ("icons" , "font" );
237
- attributes .put ("idprefix" , "" );
238
- attributes .put ("idseparator" , "-" );
239
- attributes .put ("docinfo" , "shared" );
240
- attributes .put ("sectanchors" , "" );
241
- attributes .put ("sectnums" , "" );
242
- attributes .put ("today-year" , LocalDate .now ().getYear ());
243
- asciidoctorTask .attributes (attributes );
244
- }
245
-
246
181
}
0 commit comments