11/*
2- * Copyright 2022 the original author or authors.
2+ * Copyright 2022-2023 the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
1616package org .springframework .shell .gradle ;
1717
1818import java .io .File ;
19- import java .net .URI ;
2019import java .time .LocalDate ;
21- import java .util .ArrayList ;
2220import java .util .Collections ;
2321import java .util .HashMap ;
24- import java .util .List ;
2522import java .util .Map ;
26- import java .util .concurrent .Callable ;
27- import java .util .function .Consumer ;
2823
2924import org .asciidoctor .gradle .base .AsciidoctorAttributeProvider ;
3025import org .asciidoctor .gradle .jvm .AbstractAsciidoctorTask ;
26+ import org .asciidoctor .gradle .jvm .AsciidoctorJExtension ;
3127import org .asciidoctor .gradle .jvm .AsciidoctorJPlugin ;
3228import org .asciidoctor .gradle .jvm .AsciidoctorTask ;
3329import org .gradle .api .Action ;
3834import org .gradle .api .plugins .JavaPlugin ;
3935import org .gradle .api .plugins .PluginManager ;
4036import 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 ;
4538import org .gradle .api .tasks .Sync ;
39+ import org .springframework .util .StringUtils ;
4640
4741/**
4842 * @author Janne Valkealahti
4943 */
5044class DocsPlugin implements Plugin <Project > {
5145
46+ private static final String ASCIIDOCTORJ_VERSION = "2.4.3" ;
47+ private static final String EXTENSIONS_CONFIGURATION_NAME = "asciidoctorExtensions" ;
48+
5249 @ Override
5350 public void apply (Project project ) {
5451 PluginManager pluginManager = project .getPluginManager ();
@@ -63,133 +60,83 @@ public void apply(Project project) {
6360 task .enforcedPlatform (":spring-shell-management" );
6461 });
6562
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+ });
6871 project .getTasks ().withType (GenerateModuleMetadata .class , metadata -> {
6972 metadata .setEnabled (false );
7073 });
7174 }
7275
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 );
11478 }
11579
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" ));
12885 });
12986 }
13087
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+ }
13397 }
13498
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+ }
145123
146124 private Sync createSnippetsResourcesTask (Project project ) {
147125 Sync sync = project .getTasks ().create ("snippetResources" , Sync .class , s -> {
148126 s .from (new File (project .getRootProject ().getRootDir (), "spring-shell-docs/src/test/java/org/springframework/shell" ), spec -> {
149127 spec .include ("docs/*" );
150128 });
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" );
152133 s .into (destination );
153134 });
154135 return sync ;
155136 }
156137
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 ,
191139 ExtractVersionConstraints dependencyVersions ) {
192-
193140 asciidoctorTask .doFirst (new Action <Task >() {
194141
195142 @ Override
@@ -209,13 +156,15 @@ public Map<String, Object> getAttributes() {
209156
210157 Map <String , Object > attributes = new HashMap <>();
211158 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 );
216159 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 ());
218166 attributes .put ("snippets" , "docs" );
167+
219168 asciidoctorTask .getAttributeProviders ().add (new AsciidoctorAttributeProvider () {
220169 @ Override
221170 public Map <String , Object > getAttributes () {
@@ -229,18 +178,4 @@ public Map<String, Object> getAttributes() {
229178 });
230179 asciidoctorTask .attributes (attributes );
231180 }
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-
246181}
0 commit comments