|
1 | 1 | /*
|
2 |
| - * Copyright 2022-2023 the original author or authors. |
| 2 | + * Copyright 2022-2024 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.time.LocalDate; |
20 |
| -import java.util.Collections; |
21 |
| -import java.util.HashMap; |
22 |
| -import java.util.Map; |
23 | 19 |
|
24 |
| -import org.asciidoctor.gradle.base.AsciidoctorAttributeProvider; |
25 |
| -import org.asciidoctor.gradle.jvm.AbstractAsciidoctorTask; |
26 |
| -import org.asciidoctor.gradle.jvm.AsciidoctorJExtension; |
27 |
| -import org.asciidoctor.gradle.jvm.AsciidoctorJPlugin; |
28 |
| -import org.asciidoctor.gradle.jvm.AsciidoctorTask; |
29 |
| -import org.gradle.api.Action; |
30 | 20 | import org.gradle.api.Plugin;
|
31 | 21 | import org.gradle.api.Project;
|
32 |
| -import org.gradle.api.Task; |
33 | 22 | import org.gradle.api.plugins.JavaLibraryPlugin;
|
34 | 23 | import org.gradle.api.plugins.JavaPlugin;
|
| 24 | +import org.gradle.api.plugins.JavaPluginConvention; |
35 | 25 | import org.gradle.api.plugins.PluginManager;
|
36 | 26 | import org.gradle.api.publish.tasks.GenerateModuleMetadata;
|
37 |
| -import org.gradle.api.tasks.PathSensitivity; |
38 |
| -import org.gradle.api.tasks.Sync; |
39 |
| -import org.springframework.util.StringUtils; |
| 27 | +import org.gradle.api.tasks.SourceSet; |
| 28 | +import org.gradle.api.tasks.javadoc.Javadoc; |
| 29 | +import org.gradle.external.javadoc.CoreJavadocOptions; |
| 30 | +import org.gradle.external.javadoc.JavadocMemberLevel; |
| 31 | +import org.gradle.external.javadoc.JavadocOutputLevel; |
40 | 32 |
|
41 | 33 | /**
|
42 | 34 | * @author Janne Valkealahti
|
43 | 35 | */
|
44 | 36 | class DocsPlugin implements Plugin<Project> {
|
45 | 37 |
|
46 |
| - private static final String ASCIIDOCTORJ_VERSION = "2.4.3"; |
47 |
| - private static final String EXTENSIONS_CONFIGURATION_NAME = "asciidoctorExtensions"; |
48 |
| - |
49 | 38 | @Override
|
50 | 39 | public void apply(Project project) {
|
51 | 40 | PluginManager pluginManager = project.getPluginManager();
|
52 | 41 | pluginManager.apply(JavaPlugin.class);
|
53 | 42 | pluginManager.apply(JavaLibraryPlugin.class);
|
54 | 43 | pluginManager.apply(ManagementConfigurationPlugin.class);
|
55 | 44 | pluginManager.apply(SpringMavenPlugin.class);
|
56 |
| - pluginManager.apply(AsciidoctorJPlugin.class); |
57 | 45 |
|
58 |
| - ExtractVersionConstraints dependencyVersions = project.getTasks().create("dependencyVersions", |
59 |
| - ExtractVersionConstraints.class, task -> { |
60 |
| - task.enforcedPlatform(":spring-shell-management"); |
61 |
| - }); |
| 46 | + createApiTask(project); |
62 | 47 |
|
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 |
| - }); |
71 | 48 | project.getTasks().withType(GenerateModuleMetadata.class, metadata -> {
|
72 | 49 | metadata.setEnabled(false);
|
73 | 50 | });
|
74 | 51 | }
|
75 | 52 |
|
76 |
| - private void upgradeAsciidoctorJVersion(Project project) { |
77 |
| - project.getExtensions().getByType(AsciidoctorJExtension.class).setVersion(ASCIIDOCTORJ_VERSION); |
78 |
| - } |
79 |
| - |
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")); |
| 53 | + private Javadoc createApiTask(Project project) { |
| 54 | + Javadoc api = project.getTasks().create("aggregatedJavadoc", Javadoc.class, a -> { |
| 55 | + a.setGroup("Documentation"); |
| 56 | + a.setDescription("Generates aggregated Javadoc API documentation."); |
| 57 | + a.setDestinationDir(new File(project.getBuildDir(), "generated-antora-javadocs/modules/ROOT/assets/attachments/api/java")); |
| 58 | + a.setTitle(String.format("Spring Shell %s API", project.getVersion())); |
| 59 | + CoreJavadocOptions options = (CoreJavadocOptions) a.getOptions(); |
| 60 | + options.windowTitle(String.format("Spring Shell %s API", project.getVersion())); |
| 61 | + options.setMemberLevel(JavadocMemberLevel.PROTECTED); |
| 62 | + options.setOutputLevel(JavadocOutputLevel.QUIET); |
| 63 | + options.addStringOption("Xdoclint:none", "-quiet"); |
85 | 64 | });
|
86 |
| - } |
87 | 65 |
|
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 |
| - } |
97 |
| - } |
98 |
| - |
99 |
| - private void configureOptions(AbstractAsciidoctorTask asciidoctorTask) { |
100 |
| - asciidoctorTask.options(Collections.singletonMap("doctype", "book")); |
101 |
| - } |
| 66 | + project.getRootProject().getSubprojects().forEach(p -> { |
| 67 | + p.getPlugins().withType(ModulePlugin.class, m -> { |
| 68 | + JavaPluginConvention java = p.getConvention().getPlugin(JavaPluginConvention.class); |
| 69 | + SourceSet mainSourceSet = java.getSourceSets().getByName("main"); |
102 | 70 |
|
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 |
| - } |
| 71 | + api.setSource(api.getSource().plus(mainSourceSet.getAllJava())); |
123 | 72 |
|
124 |
| - private Sync createSnippetsResourcesTask(Project project) { |
125 |
| - Sync sync = project.getTasks().create("snippetResources", Sync.class, s -> { |
126 |
| - s.from(new File(project.getRootProject().getRootDir(), "spring-shell-docs/src/test/java/org/springframework/shell"), spec -> { |
127 |
| - spec.include("docs/*"); |
128 |
| - }); |
129 |
| - s.preserve(filter -> { |
130 |
| - filter.include("**/*"); |
131 |
| - }); |
132 |
| - File destination = new File(project.getBuildDir(), "docs/src/asciidoctor/asciidoc"); |
133 |
| - s.into(destination); |
134 |
| - }); |
135 |
| - return sync; |
136 |
| - } |
137 |
| - |
138 |
| - private void configureCommonAttributes(Project project, AbstractAsciidoctorTask asciidoctorTask, |
139 |
| - ExtractVersionConstraints dependencyVersions) { |
140 |
| - asciidoctorTask.doFirst(new Action<Task>() { |
141 |
| - |
142 |
| - @Override |
143 |
| - public void execute(Task arg0) { |
144 |
| - asciidoctorTask.getAttributeProviders().add(new AsciidoctorAttributeProvider() { |
145 |
| - @Override |
146 |
| - public Map<String, Object> getAttributes() { |
147 |
| - Map<String, String> versionConstraints = dependencyVersions.getVersionConstraints(); |
148 |
| - Map<String, Object> attrs = new HashMap<>(); |
149 |
| - attrs.put("spring-version", versionConstraints.get("org.springframework:spring-core")); |
150 |
| - attrs.put("spring-boot-version", versionConstraints.get("org.springframework.boot:spring-boot")); |
151 |
| - return attrs; |
152 |
| - } |
| 73 | + p.getTasks().withType(Javadoc.class, j -> { |
| 74 | + api.setClasspath(api.getClasspath().plus(j.getClasspath())); |
153 | 75 | });
|
154 |
| - } |
| 76 | + }); |
155 | 77 | });
|
156 | 78 |
|
157 |
| - Map<String, Object> attributes = new HashMap<>(); |
158 |
| - attributes.put("toc", "left"); |
159 |
| - attributes.put("icons", "font"); |
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()); |
166 |
| - attributes.put("snippets", "docs"); |
167 |
| - |
168 |
| - asciidoctorTask.getAttributeProviders().add(new AsciidoctorAttributeProvider() { |
169 |
| - @Override |
170 |
| - public Map<String, Object> getAttributes() { |
171 |
| - Object version = project.getVersion(); |
172 |
| - Map<String, Object> attrs = new HashMap<>(); |
173 |
| - if (version != null && version.toString() != Project.DEFAULT_VERSION) { |
174 |
| - attrs.put("project-version", version); |
175 |
| - } |
176 |
| - return attrs; |
177 |
| - } |
178 |
| - }); |
179 |
| - asciidoctorTask.attributes(attributes); |
| 79 | + return api; |
180 | 80 | }
|
| 81 | + |
181 | 82 | }
|
0 commit comments