Switch from spring-doc-resources to spring-asciidoctor-backends.

This commit is contained in:
John Blum
2023-06-09 15:23:47 -07:00
parent 401afbfe44
commit c80d3fbcca

View File

@@ -1,22 +1,21 @@
/*
* Copyright 2022-present the original author or authors.
* Copyright 2017-present the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package io.spring.gradle.convention;
import java.io.File;
import java.net.URI;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Collections;
@@ -29,17 +28,17 @@ import org.asciidoctor.gradle.jvm.AbstractAsciidoctorTask;
import org.asciidoctor.gradle.jvm.AsciidoctorJExtension;
import org.asciidoctor.gradle.jvm.AsciidoctorJPlugin;
import org.asciidoctor.gradle.jvm.AsciidoctorTask;
import org.gradle.api.JavaVersion;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.dsl.RepositoryHandler;
import org.gradle.api.file.DuplicatesStrategy;
import org.gradle.api.file.FileTree;
import org.gradle.api.tasks.Sync;
/**
* Conventions that are applied in the presence of the {@link AsciidoctorJPlugin}.
*
* <p/>
* When the plugin is applied:
*
* <ul>
@@ -63,16 +62,19 @@ import org.gradle.api.tasks.Sync;
*
* @author Andy Wilkinson
* @author Rob Winch
* @author John Blum
*/
public class AsciidoctorConventionPlugin implements Plugin<Project> {
private static final String SPRING_ASCIIDOCTOR_EXTENSIONS_BLOCK_SWITCH_VERSION = "0.4.2.RELEASE";
private static final String ASCIIDOCTORJ_VERSION = "2.4.3";
private static final String SPRING_ASCIIDOCTOR_BACKENDS_VERSION = "0.0.5";
private static final String SPRING_DOC_RESOURCES_VERSION = "0.2.5";
private static final String SPRING_ASCIIDOCTOR_EXTENSION_BLOCK_SWITCH_DEPENDENCY =
String.format("io.spring.asciidoctor:spring-asciidoctor-extensions-block-switch:%s",
SPRING_ASCIIDOCTOR_EXTENSIONS_BLOCK_SWITCH_VERSION);
private static final String SPRING_ASCIIDOCTOR_BACKENDS_DEPENDENCY =
String.format("io.spring.asciidoctor.backends:spring-asciidoctor-backends:%s",
SPRING_ASCIIDOCTOR_BACKENDS_VERSION);
@SuppressWarnings("unused")
private static final String SPRING_DOC_RESOURCES_DEPENDENCY =
String.format("io.spring.docresources:spring-doc-resources:%s", SPRING_DOC_RESOURCES_VERSION);
@@ -81,19 +83,16 @@ public class AsciidoctorConventionPlugin implements Plugin<Project> {
project.getPlugins().withType(AsciidoctorJPlugin.class, asciidoctorPlugin -> {
createDefaultAsciidoctorRepository(project);
setAsciidoctorJVersion(project);
makeAllWarningsFatal(project);
createAsciidoctorExtensionsConfiguration(project);
Sync unzipResources = createUnzipDocumentationResourcesTask(project);
project.getTasks().withType(AbstractAsciidoctorTask.class, asciidoctorTask -> {
asciidoctorTask.dependsOn(unzipResources);
configureAttributes(project, asciidoctorTask);
configureExtensions(project, asciidoctorTask);
configureOptions(asciidoctorTask);
asciidoctorTask.baseDirFollowsSourceDir();
asciidoctorTask.useIntermediateWorkDir();
configureAsciidoctorTask(project, asciidoctorTask);
asciidoctorTask.resources(resourcesSpec -> {
resourcesSpec.setDuplicatesStrategy(DuplicatesStrategy.INCLUDE);
@@ -105,45 +104,52 @@ public class AsciidoctorConventionPlugin implements Plugin<Project> {
// resourcesSrcDirSpec.include("images/**");
});
});
if (asciidoctorTask instanceof AsciidoctorTask) {
configureHtmlOnlyAttributes(project, asciidoctorTask);
}
});
});
}
private void createDefaultAsciidoctorRepository(Project project) {
private void setAsciidoctorJVersion(Project project) {
project.getExtensions().getByType(AsciidoctorJExtension.class).setVersion(ASCIIDOCTORJ_VERSION);
}
project.getGradle().afterProject(it -> {
private void makeAllWarningsFatal(Project project) {
project.getExtensions().getByType(AsciidoctorJExtension.class).fatalWarnings(".*");
}
RepositoryHandler repositories = it.getRepositories();
private void createAsciidoctorExtensionsConfiguration(Project project) {
if (repositories.isEmpty()) {
repositories.mavenCentral();
repositories.maven(repo -> repo.setUrl(URI.create("https://repo.spring.io/release")));
}
project.getConfigurations().create("asciidoctorExtensions", configuration -> {
project.getConfigurations()
.matching(it -> "dependencyManagement".equals(it.getName()))
.all(configuration::extendsFrom);
configuration.getDependencies()
.add(project.getDependencies().create(SPRING_ASCIIDOCTOR_BACKENDS_DEPENDENCY));
// TODO: Why is the asiidoctorj-pdf dependency needed?
configuration.getDependencies()
.add(project.getDependencies().create("org.asciidoctor:asciidoctorj-pdf:1.5.3"));
});
}
/**
* Requests the base Spring Documentation Resources from {@literal https://repo.spring.io/release} and uses it
* to format and render documentation.
* Requests the base Spring Documentation Resources from {@literal Maven Central} and uses it to format
* and render documentation.
*
* @param project {@literal this} Gradle {@link Project}.
* @return a {@link Sync} task used to copy the Spring Documentation Resources to a build directory
* @return a {@link Sync} task that copies Spring Documentation Resources to the build directory
* used to generate documentation.
* @see <a href="https://repo.spring.io/ui/native/release/io/spring/docresources/spring-doc-resources">spring-doc-resources</a>
* @see org.gradle.api.tasks.Sync
* @see org.gradle.api.Project
*/
@SuppressWarnings("all")
private Sync createUnzipDocumentationResourcesTask(Project project) {
Configuration documentationResources = project.getConfigurations().maybeCreate("documentationResources");
Configuration documentationResources = project.getConfigurations().create("documentationResources");
documentationResources.getDependencies()
.add(project.getDependencies().create(SPRING_DOC_RESOURCES_DEPENDENCY));
.add(project.getDependencies().create(SPRING_ASCIIDOCTOR_BACKENDS_DEPENDENCY));
Sync unzipResources = project.getTasks().create("unzipDocumentationResources", Sync.class, sync -> {
@@ -166,56 +172,66 @@ public class AsciidoctorConventionPlugin implements Plugin<Project> {
return unzipResources;
}
@SuppressWarnings("unused")
private void configureAsciidoctorTask(Project project, AbstractAsciidoctorTask asciidoctorTask) {
asciidoctorTask.baseDirFollowsSourceDir();
asciidoctorTask.configurations("asciidoctorExtensions");
//asciidoctorTask.useIntermediateWorkDir();
configureAttributes(project, asciidoctorTask);
configureForkOptions(asciidoctorTask);
configureOptions(asciidoctorTask);
if (asciidoctorTask instanceof AsciidoctorTask) {
boolean pdf = asciidoctorTask.getName().toLowerCase().contains("pdf");
String backend = pdf ? "spring-pdf" : "spring-html";
((AsciidoctorTask) asciidoctorTask).outputOptions((outputOptions) -> outputOptions.backends(backend));
configureHtmlOnlyAttributes(asciidoctorTask);
}
}
private void configureAttributes(Project project, AbstractAsciidoctorTask asciidoctorTask) {
Map<String, Object> attributes = new HashMap<>();
attributes.put("attribute-missing", "warn");
attributes.put("icons", "font");
attributes.put("docinfo", "shared");
attributes.put("idprefix", "");
attributes.put("idseparator", "-");
attributes.put("docinfo", "shared");
attributes.put("sectanchors", "");
attributes.put("sectnums", "");
attributes.put("today-year", LocalDate.now().getYear());
Object version = project.getVersion();
if (version != null && !Project.DEFAULT_VERSION.equals(version)) {
attributes.put("revnumber", version);
}
asciidoctorTask.attributes(attributes);
}
private void configureExtensions(Project project, AbstractAsciidoctorTask asciidoctorTask) {
Configuration extensionsConfiguration = project.getConfigurations().maybeCreate("asciidoctorExtensions");
extensionsConfiguration.defaultDependencies(dependencies -> dependencies.add(project.getDependencies()
.create(SPRING_ASCIIDOCTOR_EXTENSION_BLOCK_SWITCH_DEPENDENCY)));
asciidoctorTask.configurations(extensionsConfiguration);
private void configureForkOptions(AbstractAsciidoctorTask asciidoctorTask) {
if (JavaVersion.current().isCompatibleWith(JavaVersion.VERSION_16)) {
asciidoctorTask.forkOptions(options -> options.jvmArgs(
"--add-opens", "java.base/sun.nio.ch=ALL-UNNAMED",
"--add-opens", "java.base/java.io=ALL-UNNAMED")
);
}
}
private void configureHtmlOnlyAttributes(Project project, AbstractAsciidoctorTask asciidoctorTask) {
private void configureHtmlOnlyAttributes(AbstractAsciidoctorTask asciidoctorTask) {
Map<String, Object> attributes = new HashMap<>();
attributes.put("source-highlighter", "highlight.js");
attributes.put("highlightjsdir", "js/highlight");
attributes.put("highlightjs-theme", "github");
attributes.put("linkcss", true);
attributes.put("source-highlighter", "highlight.js");
attributes.put("icons", "font");
attributes.put("stylesheet", "css/spring.css");
asciidoctorTask.getAttributeProviders().add(() -> {
Object version = project.getVersion();
Map<String, Object> localAttributes = new HashMap<>();
if (version != null && !Project.DEFAULT_VERSION.equals(version)) {
localAttributes.put("revnumber", version);
}
return localAttributes;
});
attributes.put("imagesdir", "./images");
attributes.put("linkcss", true);
attributes.put("stylesdir", "css/");
//attributes.put("stylesheet", "spring.css");
asciidoctorTask.attributes(attributes);
}
@@ -223,8 +239,4 @@ public class AsciidoctorConventionPlugin implements Plugin<Project> {
private void configureOptions(AbstractAsciidoctorTask asciidoctorTask) {
asciidoctorTask.options(Collections.singletonMap("doctype", "book"));
}
private void makeAllWarningsFatal(Project project) {
project.getExtensions().getByType(AsciidoctorJExtension.class).fatalWarnings(".*");
}
}