|
| 1 | +/* |
| 2 | + * Copyright 2012-2024 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 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. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot.build.antora; |
| 18 | + |
| 19 | +import org.gradle.api.Project; |
| 20 | +import org.gradle.api.Task; |
| 21 | +import org.gradle.api.artifacts.Configuration; |
| 22 | +import org.gradle.api.artifacts.dsl.DependencyHandler; |
| 23 | +import org.gradle.api.file.CopySpec; |
| 24 | +import org.gradle.api.file.Directory; |
| 25 | +import org.gradle.api.file.RegularFile; |
| 26 | +import org.gradle.api.provider.Provider; |
| 27 | +import org.gradle.api.publish.PublishingExtension; |
| 28 | +import org.gradle.api.publish.maven.MavenPublication; |
| 29 | +import org.gradle.api.tasks.TaskContainer; |
| 30 | +import org.gradle.api.tasks.TaskProvider; |
| 31 | + |
| 32 | +/** |
| 33 | + * A contribution of content to Antora that can be consumed by other projects. |
| 34 | + * |
| 35 | + * @author Andy Wilkinson |
| 36 | + */ |
| 37 | +class ConsumableContentContribution extends ContentContribution { |
| 38 | + |
| 39 | + protected ConsumableContentContribution(Project project, String type, String name) { |
| 40 | + super(project, name, type); |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + void produceFrom(CopySpec copySpec) { |
| 45 | + TaskProvider<? extends Task> producer = super.configureProduction(copySpec); |
| 46 | + Configuration configuration = createConfiguration(getName(), |
| 47 | + "Configuration for %s Antora %s content artifacts."); |
| 48 | + configuration.setCanBeConsumed(true); |
| 49 | + configuration.setCanBeResolved(false); |
| 50 | + getProject().getArtifacts().add(configuration.getName(), producer); |
| 51 | + } |
| 52 | + |
| 53 | + void consumeFrom(String path) { |
| 54 | + Configuration configuration = createConfiguration(getName(), "Configuration for %s Antora %s content."); |
| 55 | + configuration.setCanBeConsumed(false); |
| 56 | + configuration.setCanBeResolved(true); |
| 57 | + DependencyHandler dependencies = getProject().getDependencies(); |
| 58 | + dependencies.add(configuration.getName(), |
| 59 | + getProject().provider(() -> projectDependency(path, configuration.getName()))); |
| 60 | + Provider<Directory> outputDirectory = outputDirectory("content", getName()); |
| 61 | + TaskContainer tasks = getProject().getTasks(); |
| 62 | + TaskProvider<?> copyAntoraContent = tasks.register(taskName("copy", "%s", configuration.getName()), |
| 63 | + CopyAntoraContent.class, (task) -> configureCopyContent(task, path, configuration, outputDirectory)); |
| 64 | + configureAntora(addInputFrom(copyAntoraContent, configuration.getName())); |
| 65 | + configurePlaybookGeneration(this::addToZipContentsCollectorDependencies); |
| 66 | + getProject().getExtensions() |
| 67 | + .getByType(PublishingExtension.class) |
| 68 | + .getPublications() |
| 69 | + .withType(MavenPublication.class) |
| 70 | + .configureEach((mavenPublication) -> addPublishedMavenArtifact(mavenPublication, copyAntoraContent)); |
| 71 | + } |
| 72 | + |
| 73 | + private void configureCopyContent(CopyAntoraContent task, String path, Configuration configuration, |
| 74 | + Provider<Directory> outputDirectory) { |
| 75 | + task.setDescription( |
| 76 | + "Syncs the %s Antora %s content from %s.".formatted(getName(), toDescription(getType()), path)); |
| 77 | + task.setSource(configuration); |
| 78 | + task.getOutputFile().set(outputDirectory.map(this::getContentZipFile)); |
| 79 | + } |
| 80 | + |
| 81 | + private void addToZipContentsCollectorDependencies(GenerateAntoraPlaybook task) { |
| 82 | + task.getAntoraExtensions().getZipContentsCollector().getDependencies().add(getName()); |
| 83 | + } |
| 84 | + |
| 85 | + private void addPublishedMavenArtifact(MavenPublication mavenPublication, TaskProvider<?> copyAntoraContent) { |
| 86 | + if ("maven".equals(mavenPublication.getName())) { |
| 87 | + String classifier = "%s-%s-content".formatted(getName(), getType()); |
| 88 | + mavenPublication.artifact(copyAntoraContent, (mavenArtifact) -> mavenArtifact.setClassifier(classifier)); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + private RegularFile getContentZipFile(Directory dir) { |
| 93 | + Object version = getProject().getVersion(); |
| 94 | + return dir.file("spring-boot-docs-%s-%s-%s-content.zip".formatted(version, getName(), getType())); |
| 95 | + } |
| 96 | + |
| 97 | + private static String toDescription(String input) { |
| 98 | + return input.replace("-", " "); |
| 99 | + } |
| 100 | + |
| 101 | + private Configuration createConfiguration(String name, String description) { |
| 102 | + return getProject().getConfigurations() |
| 103 | + .create(configurationName(name, "Antora%sContent", getType()), |
| 104 | + (configuration) -> configuration.setDescription(description.formatted(getName(), getType()))); |
| 105 | + } |
| 106 | + |
| 107 | +} |
0 commit comments