Skip to content

Commit c81de48

Browse files
feat: add autodocBom task to generate Autodoc manifests for BOMs (#282)
1 parent 9d042ea commit c81de48

File tree

7 files changed

+115
-13
lines changed

7 files changed

+115
-13
lines changed

plugins/autodoc/autodoc-plugin/src/main/java/org/eclipse/edc/plugins/autodoc/AutodocPlugin.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
package org.eclipse.edc.plugins.autodoc;
1616

17+
import org.eclipse.edc.plugins.autodoc.tasks.AutodocBomTask;
1718
import org.eclipse.edc.plugins.autodoc.tasks.DownloadManifestTask;
1819
import org.eclipse.edc.plugins.autodoc.tasks.MarkdownRendererTask.ToHtml;
1920
import org.eclipse.edc.plugins.autodoc.tasks.MarkdownRendererTask.ToMarkdown;
@@ -47,6 +48,15 @@ public void apply(Project project) {
4748
project.getTasks().register(ToHtml.NAME, ToHtml.class, t -> t.setGroup(GROUP_NAME));
4849
project.getTasks().register(DownloadManifestTask.NAME, DownloadManifestTask.class, t -> t.setGroup(GROUP_NAME));
4950
// resolving manifests requires the Autodoc manifests of all dependencies to exist already
50-
project.getTasks().register(ResolveManifestTask.NAME, ResolveManifestTask.class, t -> t.dependsOn(AUTODOC_TASK_NAME).setGroup(GROUP_NAME));
51+
project.getTasks().register(ResolveManifestTask.NAME, ResolveManifestTask.class, t -> {
52+
t.dependsOn(AUTODOC_TASK_NAME);
53+
t.setGroup(GROUP_NAME);
54+
t.setDescription(ResolveManifestTask.DESCRIPTION);
55+
});
56+
project.getTasks().register(AutodocBomTask.NAME, AutodocBomTask.class, t -> {
57+
t.dependsOn(ResolveManifestTask.NAME);
58+
t.setDescription(AutodocBomTask.DESCRIPTION);
59+
t.setGroup(GROUP_NAME);
60+
});
5161
}
5262
}

plugins/autodoc/autodoc-plugin/src/main/java/org/eclipse/edc/plugins/autodoc/tasks/AbstractManifestResolveTask.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.util.Set;
3232

3333
import static java.util.Objects.requireNonNull;
34+
import static org.eclipse.edc.plugins.autodoc.tasks.Constants.DEFAULT_AUTODOC_FOLDER;
3435

3536
/**
3637
* Abstract gradle task, that "resolves" an already-existing autodoc manifest from a URI and transfers (=copies, downloads,...)
@@ -45,7 +46,7 @@ public abstract class AbstractManifestResolveTask extends DefaultTask {
4546
private File outputDirectoryOverride;
4647

4748
public AbstractManifestResolveTask() {
48-
downloadDirectory = getProject().getLayout().getBuildDirectory().getAsFile().get().toPath().resolve("autodoc");
49+
downloadDirectory = getProject().getLayout().getBuildDirectory().getAsFile().get().toPath().resolve(DEFAULT_AUTODOC_FOLDER);
4950
}
5051

5152
@TaskAction
@@ -92,10 +93,12 @@ protected Set<String> getExclusions() {
9293
private void transferDependencyFile(DependencySource dependencySource, Path downloadDirectory) {
9394
var targetFilePath = downloadDirectory.resolve(dependencySource.filename());
9495
try (var inputStream = resolveManifest(dependencySource)) {
95-
downloadDirectory.toFile().mkdirs();
96-
getLogger().debug("Downloading {} into {}", dependencySource, downloadDirectory);
97-
try (var fos = new FileOutputStream(targetFilePath.toFile())) {
98-
inputStream.transferTo(fos);
96+
if (inputStream != null) {
97+
downloadDirectory.toFile().mkdirs();
98+
getLogger().debug("Downloading {} into {}", dependencySource, downloadDirectory);
99+
try (var fos = new FileOutputStream(targetFilePath.toFile())) {
100+
inputStream.transferTo(fos);
101+
}
99102
}
100103
} catch (IOException e) {
101104
throw new RuntimeException(e);
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Apache License, Version 2.0 which is available at
6+
* https://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* SPDX-License-Identifier: Apache-2.0
9+
*
10+
* Contributors:
11+
* Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation
12+
*
13+
*/
14+
15+
package org.eclipse.edc.plugins.autodoc.tasks;
16+
17+
import org.gradle.api.DefaultTask;
18+
import org.gradle.api.tasks.OutputFile;
19+
import org.gradle.api.tasks.TaskAction;
20+
import org.gradle.util.internal.GFileUtils;
21+
22+
import java.io.File;
23+
24+
public class AutodocBomTask extends DefaultTask {
25+
26+
public static final String NAME = "autodocBom";
27+
public static final String DESCRIPTION = """
28+
This task is intended for BOM modules. It resolves all autodoc manifests of modules that the BOM depends on
29+
and generates a merged manifest file. By default, this merged file is stored at {project}/build/edc.json.
30+
""";
31+
private final JsonFileAppender appender;
32+
private File outputFile;
33+
34+
public AutodocBomTask() {
35+
appender = new JsonFileAppender(getLogger());
36+
outputFile = getProject().getLayout().getBuildDirectory().file(outputFileName()).get().getAsFile();
37+
}
38+
39+
@TaskAction
40+
public void mergeManifests() {
41+
if (!getProject().getName().endsWith("-bom")) {
42+
getLogger().warn("Project name does not end with '-bom'. Is this really a BOM module?");
43+
}
44+
45+
var inputDirectory = getProject().getLayout().getBuildDirectory().dir(Constants.DEFAULT_AUTODOC_FOLDER).get();
46+
if (!inputDirectory.getAsFile().exists()) {
47+
getLogger().info("Input directory does not exist: {}, Skipping", inputDirectory);
48+
return;
49+
}
50+
51+
var destinationFile = outputFile;
52+
53+
var files = GFileUtils.listFiles(inputDirectory.getAsFile(), new String[]{ "json" }, false);
54+
getLogger().debug("Appending [{}] additional JSON files to the merged manifest", files.size());
55+
files.forEach(f -> appender.append(destinationFile, f));
56+
57+
}
58+
59+
@OutputFile
60+
public File getOutputFile() {
61+
return outputFile;
62+
}
63+
64+
public void setOutputFile(File outputFile) {
65+
this.outputFile = outputFile;
66+
}
67+
68+
private String outputFileName() {
69+
return "edc.json";
70+
}
71+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Apache License, Version 2.0 which is available at
6+
* https://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* SPDX-License-Identifier: Apache-2.0
9+
*
10+
* Contributors:
11+
* Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation
12+
*
13+
*/
14+
15+
package org.eclipse.edc.plugins.autodoc.tasks;
16+
17+
public interface Constants {
18+
String DEFAULT_AUTODOC_FOLDER = "autodoc";
19+
}

plugins/autodoc/autodoc-plugin/src/main/java/org/eclipse/edc/plugins/autodoc/tasks/DownloadManifestTask.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ protected InputStream resolveManifest(DependencySource autodocManifest) {
5353
}
5454
if (response.statusCode() != 200) {
5555
getLogger().warn("Could not download {}, HTTP response: {}", autodocManifest.dependency(), response);
56-
return InputStream.nullInputStream();
56+
return null;
5757
}
5858
return response.body();
5959
}

plugins/autodoc/autodoc-plugin/src/main/java/org/eclipse/edc/plugins/autodoc/tasks/MergeManifestsTask.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
import java.nio.file.Path;
2727
import java.util.Objects;
2828

29+
import static org.eclipse.edc.plugins.autodoc.tasks.Constants.DEFAULT_AUTODOC_FOLDER;
30+
2931
/**
3032
* Task that takes an input file (JSON) and appends its contents to a destination file. This task is intended to be called per-project.
3133
*/
@@ -43,7 +45,7 @@ public MergeManifestsTask() {
4345
appender = new JsonFileAppender(getProject().getLogger());
4446
projectBuildDirectory = getProject().getLayout().getBuildDirectory().getAsFile().get();
4547
destinationFile = getProject().getRootProject().getLayout().getBuildDirectory().get().getAsFile().toPath().resolve(MERGED_MANIFEST_FILENAME).toFile();
46-
inputDirectory = projectBuildDirectory.toPath().resolve("autodoc").toFile();
48+
inputDirectory = projectBuildDirectory.toPath().resolve(DEFAULT_AUTODOC_FOLDER).toFile();
4749
}
4850

4951
/**

plugins/autodoc/autodoc-plugin/src/main/java/org/eclipse/edc/plugins/autodoc/tasks/ResolveManifestTask.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,10 @@
2323
import java.io.InputStream;
2424
import java.util.Optional;
2525

26-
import static java.io.InputStream.nullInputStream;
27-
2826
public class ResolveManifestTask extends AbstractManifestResolveTask {
2927

3028
public static final String NAME = "resolveManifests";
31-
29+
public static final String DESCRIPTION = "This task is intended for BOM modules and resolves the autodoc manifests of all modules that the project depends on. By default, all manifests are stored in {project}/build/autodoc.";
3230

3331
@Override
3432
protected InputStream resolveManifest(DependencySource autodocManifest) {
@@ -40,7 +38,7 @@ protected InputStream resolveManifest(DependencySource autodocManifest) {
4038
}
4139

4240
getLogger().info("File {} does not exist", file);
43-
return nullInputStream();
41+
return null;
4442
} catch (FileNotFoundException e) {
4543
throw new RuntimeException(e);
4644
}
@@ -62,5 +60,4 @@ protected Optional<DependencySource> createSource(Dependency dependency) {
6260

6361
return Optional.empty();
6462
}
65-
6663
}

0 commit comments

Comments
 (0)