Skip to content

Commit 416805a

Browse files
feat: download transitive autodoc manifests (eclipse-edc#167)
* feat: download transitive autodoc manifests * add download * documentation, cleanup * fix test
1 parent fe44324 commit 416805a

File tree

6 files changed

+244
-8
lines changed

6 files changed

+244
-8
lines changed

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import java.io.File;
2020

2121
public abstract class AutodocExtension {
22+
private boolean includeTransitive = true;
23+
2224
/**
2325
* Overrides the default output directory relative to the current project dir
2426
*/
@@ -29,4 +31,37 @@ public abstract class AutodocExtension {
2931
*/
3032
public abstract Property<String> getProcessorVersion();
3133

34+
/**
35+
* Optional input to specify, where additional autodoc manifests that are to be merged, are located on the filesystem.
36+
* Use this, if you have a directory that contains multiple autodoc manifests, e.g. of third-party or transitive deps.
37+
* <p>
38+
* If this is set, the merge task will take all manifests found in this directory and append them to the {@code manifest.json} file.
39+
* Usually, this points to wherever the downloaded manifests are store.
40+
*
41+
* @see AutodocExtension#getDownloadDirectory()
42+
*/
43+
public abstract Property<File> getAdditionalInputDirectory();
44+
45+
/**
46+
* Retrieves the directory where downloaded manifests are to be stored. Defaults to {@code <rootProject>/build/manifests}
47+
*
48+
* @return The property representing the download directory, or null if not specified.
49+
*/
50+
public abstract Property<File> getDownloadDirectory();
51+
52+
53+
/**
54+
* Determines whether to include transitive dependencies in the merge process.
55+
* If set to {@code true}, the merge task will download the manifests of transitive (EDC) dependencies and include them in the merged manifest.
56+
* If set to {@code false}, only the direct dependencies will be merged.
57+
*
58+
* @return {@code true} if transitive dependencies should be included, {@code false} otherwise.
59+
*/
60+
public boolean isIncludeTransitive() {
61+
return includeTransitive;
62+
}
63+
64+
public void setIncludeTransitive(boolean includeTransitive) {
65+
this.includeTransitive = includeTransitive;
66+
}
3267
}

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

Lines changed: 7 additions & 5 deletions
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.ManifestDownloadTask;
1718
import org.eclipse.edc.plugins.autodoc.tasks.MarkdownRendererTask;
1819
import org.eclipse.edc.plugins.autodoc.tasks.MergeManifestsTask;
1920
import org.gradle.api.Plugin;
@@ -26,6 +27,8 @@
2627
*/
2728
public class AutodocPlugin implements Plugin<Project> {
2829

30+
public static final String GROUP_NAME = "autodoc";
31+
public static final String AUTODOC_TASK_NAME = "autodoc";
2932
private final List<String> exclusions = List.of("version-catalog", "edc-build", "module-names", "openapi-merger", "test-summary", "autodoc-plugin", "autodoc-processor");
3033

3134
@Override
@@ -37,10 +40,9 @@ public void apply(Project project) {
3740
}
3841

3942
// registers a "named" task, that does nothing, except depend on the compileTask, which then runs the annotation processor
40-
project.getTasks().register("autodoc", t -> t.dependsOn("compileJava"));
41-
project.getTasks().register("mergeManifest", MergeManifestsTask.class, t -> t.dependsOn("autodoc").finalizedBy("doc2md"));
42-
project.getTasks().register("doc2md", MarkdownRendererTask.class, t -> t.dependsOn("autodoc"));
43-
43+
project.getTasks().register(AUTODOC_TASK_NAME, t -> t.dependsOn("compileJava").setGroup(GROUP_NAME));
44+
project.getTasks().register(MergeManifestsTask.NAME, MergeManifestsTask.class, t -> t.dependsOn(AUTODOC_TASK_NAME).setGroup(GROUP_NAME));
45+
project.getTasks().register(MarkdownRendererTask.NAME, MarkdownRendererTask.class, t -> t.dependsOn(AUTODOC_TASK_NAME).setGroup(GROUP_NAME));
46+
project.getTasks().register(ManifestDownloadTask.NAME, ManifestDownloadTask.class, t -> t.setGroup(GROUP_NAME));
4447
}
45-
4648
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
/*
2+
* Copyright (c) 2022 Microsoft Corporation
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+
* Microsoft Corporation - initial API and implementation
12+
*
13+
*/
14+
15+
package org.eclipse.edc.plugins.autodoc.tasks;
16+
17+
import org.eclipse.edc.plugins.autodoc.AutodocExtension;
18+
import org.gradle.api.DefaultTask;
19+
import org.gradle.api.artifacts.Dependency;
20+
import org.gradle.api.artifacts.repositories.MavenArtifactRepository;
21+
import org.gradle.api.tasks.TaskAction;
22+
23+
import java.io.FileOutputStream;
24+
import java.io.IOException;
25+
import java.net.URI;
26+
import java.net.http.HttpClient;
27+
import java.net.http.HttpRequest;
28+
import java.net.http.HttpResponse;
29+
import java.nio.file.Files;
30+
import java.nio.file.Path;
31+
import java.time.Duration;
32+
import java.time.Instant;
33+
import java.util.Objects;
34+
import java.util.Optional;
35+
import java.util.Set;
36+
37+
import static java.lang.String.format;
38+
import static java.util.Objects.requireNonNull;
39+
40+
public class ManifestDownloadTask extends DefaultTask {
41+
42+
public static final String NAME = "downloadManifests";
43+
private static final String EDC_GROUP = "org.eclipse.edc";
44+
private static final Duration MAX_MANIFEST_AGE = Duration.ofHours(24);
45+
private static final String MANIFEST_CLASSIFIER = "manifest";
46+
private static final String MANIFEST_TYPE = "json";
47+
private final HttpClient httpClient;
48+
private Path downloadDirectory;
49+
50+
public ManifestDownloadTask() {
51+
httpClient = HttpClient.newHttpClient();
52+
downloadDirectory = getProject().getRootProject().getBuildDir().toPath().resolve("manifests");
53+
}
54+
55+
@TaskAction
56+
public void downloadManifests() {
57+
var autodocExt = getProject().getExtensions().findByType(AutodocExtension.class);
58+
requireNonNull(autodocExt, "AutodocExtension cannot be null");
59+
60+
if (autodocExt.getDownloadDirectory().isPresent()) {
61+
downloadDirectory = autodocExt.getDownloadDirectory().get().toPath();
62+
}
63+
64+
getProject().getConfigurations()
65+
.stream().flatMap(config -> config.getDependencies().stream())
66+
.filter(dep -> EDC_GROUP.equals(dep.getGroup()))
67+
.filter(dep -> !getExclusions().contains(dep.getName()))
68+
.map(this::createDownloadRequest)
69+
.filter(Optional::isPresent)
70+
.forEach(dt -> downloadDependency(dt.get(), downloadDirectory));
71+
}
72+
73+
private String createArtifactUrl(Dependency dep, MavenArtifactRepository repo) {
74+
return format("%s%s/%s/%s/%s-%s-%s.%s", repo.getUrl(), dep.getGroup().replace(".", "/"), dep.getName(), dep.getVersion(),
75+
dep.getName(), dep.getVersion(), MANIFEST_CLASSIFIER, MANIFEST_TYPE);
76+
}
77+
78+
private void downloadDependency(DependencyDownload dt, Path outputDirectory) {
79+
80+
var p = outputDirectory.resolve(dt.filename());
81+
var request = HttpRequest.newBuilder().uri(dt.uri()).GET().build();
82+
try {
83+
var response = httpClient.send(request, HttpResponse.BodyHandlers.ofInputStream());
84+
if (response.statusCode() != 200) {
85+
getLogger().warn("Could not download {}, HTTP response: {}", dt.dependency, response);
86+
return;
87+
}
88+
outputDirectory.toFile().mkdirs();
89+
getLogger().debug("Downloading {} into {}", dt, outputDirectory);
90+
try (var is = response.body(); var fos = new FileOutputStream(p.toFile())) {
91+
is.transferTo(fos);
92+
}
93+
} catch (IOException | InterruptedException e) {
94+
throw new RuntimeException(e);
95+
}
96+
}
97+
98+
/**
99+
* Creates a download request for a given dependency, classifier, and type. A download request is successfully created if:
100+
* <ul>
101+
* <li>the output directory does not exists</li>
102+
* <li>the file does not exist locally</li>
103+
* <li>the file exists locally, but is too old (<24hrs) </li>
104+
* <li>the file exists locally, but is not readable</li>
105+
* <li>the file is found in at least one Maven repository. MavenLocal is ignored.</li>
106+
* </ul>
107+
*
108+
* @param dep the dependency to download
109+
* @return an optional DownloadRequest if the artifact should be downloaded, otherwise an empty optional
110+
*/
111+
private Optional<DependencyDownload> createDownloadRequest(Dependency dep) {
112+
if (isLocalFileValid(dep)) {
113+
getLogger().debug("Local file {} was deemed to be viable, will not download", new DependencyDownload(dep, null, MANIFEST_CLASSIFIER, MANIFEST_TYPE).filename());
114+
return Optional.empty();
115+
}
116+
var repos = getProject().getRepositories().stream().toList();
117+
return repos.stream()
118+
.filter(repo -> repo instanceof MavenArtifactRepository)
119+
.map(repo -> (MavenArtifactRepository) repo)
120+
.map(repo -> {
121+
var repoUrl = createArtifactUrl(dep, repo);
122+
try {
123+
// we use a HEAD request, because we only want to see whether that module has a `-manifest.json`
124+
var uri = URI.create(repoUrl);
125+
var headRequest = HttpRequest.newBuilder()
126+
.uri(uri)
127+
.method("HEAD", HttpRequest.BodyPublishers.noBody())
128+
.build();
129+
var response = httpClient.send(headRequest, HttpResponse.BodyHandlers.discarding());
130+
if (response.statusCode() == 200) {
131+
return new DependencyDownload(dep, uri, MANIFEST_CLASSIFIER, MANIFEST_TYPE);
132+
}
133+
return null;
134+
} catch (IOException | InterruptedException | IllegalArgumentException e) {
135+
return null;
136+
}
137+
})
138+
.filter(Objects::nonNull)
139+
.findFirst();
140+
}
141+
142+
/**
143+
* Checks if the manifest for a dependency exists locally. A local file is considered valid if:
144+
* <ul>
145+
* <li>The output directory exists</li>
146+
* <li>The file exists locally and is readable</li>
147+
* <li>The file is not older than 24 hours</li>
148+
* </ul>
149+
*
150+
* @param dep the dependency to check
151+
* @return true if the local file is valid, false otherwise
152+
*/
153+
private boolean isLocalFileValid(Dependency dep) {
154+
if (!downloadDirectory.toFile().exists()) return false;
155+
var filePath = downloadDirectory.resolve(new DependencyDownload(dep, null, MANIFEST_CLASSIFIER, MANIFEST_TYPE).filename());
156+
var file = filePath.toFile();
157+
if (!file.exists() || !file.canRead()) return false;
158+
159+
try {
160+
var date = Files.getLastModifiedTime(filePath).toInstant();
161+
return Duration.between(date, Instant.now()).compareTo(MAX_MANIFEST_AGE) <= 0;
162+
} catch (IOException e) {
163+
throw new RuntimeException(e);
164+
}
165+
}
166+
167+
private Set<String> getExclusions() {
168+
return Set.of();
169+
}
170+
171+
private record DependencyDownload(Dependency dependency, URI uri, String classifier, String type) {
172+
@Override
173+
public String toString() {
174+
return "{" +
175+
"dependency=" + dependency +
176+
", uri=" + uri +
177+
'}';
178+
}
179+
180+
String filename() {
181+
return format("%s-%s-%s.%s", dependency.getName(), dependency.getVersion(), classifier, type);
182+
}
183+
}
184+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
public class MarkdownRendererTask extends DefaultTask {
3434

35+
public static final String NAME = "doc2md";
3536
private final JsonManifestReader reader = new JsonManifestReader(new ObjectMapper());
3637

3738
@TaskAction

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.gradle.api.GradleException;
2020
import org.gradle.api.tasks.OutputFile;
2121
import org.gradle.api.tasks.TaskAction;
22+
import org.gradle.util.internal.GFileUtils;
2223

2324
import java.io.File;
2425
import java.nio.file.Path;
@@ -29,6 +30,7 @@
2930
*/
3031
public class MergeManifestsTask extends DefaultTask {
3132

33+
public static final String NAME = "mergeManifests";
3234
private static final String MERGED_MANIFEST_FILENAME = "manifest.json";
3335
private final JsonFileAppender appender;
3436
private File destinationFile;
@@ -52,13 +54,23 @@ public void mergeManifests() {
5254
throw new GradleException("destinationFile must be configured but was null!");
5355
}
5456

55-
5657
if (sourceFile.exists()) {
5758
appender.append(destination, sourceFile);
5859
} else {
5960
getProject().getLogger().lifecycle("Skip project [{}] - no manifest file found", sourceFile);
6061
}
6162

63+
// if an additional input directory was specified, lets include the files in it.
64+
if (autodocExt.getAdditionalInputDirectory().isPresent() &&
65+
autodocExt.getAdditionalInputDirectory().get().exists() &&
66+
getProject().equals(getProject().getRootProject()) &&
67+
autodocExt.isIncludeTransitive()) {
68+
var dir = autodocExt.getAdditionalInputDirectory().get();
69+
var files = GFileUtils.listFiles(dir, new String[]{ "json" }, false);
70+
getLogger().lifecycle("Appending [{}] additional JSON files to the merged manifest", files.size());
71+
files.forEach(f -> appender.append(destination, f));
72+
}
73+
6274
}
6375

6476
/**

plugins/autodoc/autodoc-plugin/src/test/java/org/eclipse/edc/plugins/autodoc/AutodocPluginTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@
1414

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

17+
import org.eclipse.edc.plugins.autodoc.tasks.MergeManifestsTask;
1718
import org.gradle.api.Project;
1819
import org.gradle.testfixtures.ProjectBuilder;
1920
import org.junit.jupiter.api.Test;
2021

2122
import static org.assertj.core.api.Assertions.assertThat;
23+
import static org.eclipse.edc.plugins.autodoc.AutodocPlugin.AUTODOC_TASK_NAME;
2224

2325

2426
public class AutodocPluginTest {
@@ -30,7 +32,7 @@ public void pluginRegistersAutodocTask() {
3032

3133
// Verify the result
3234
var tasks = project.getTasks();
33-
assertThat(tasks.findByName("autodoc")).isNotNull();
34-
assertThat(tasks.findByName("mergeManifest")).isNotNull();
35+
assertThat(tasks.findByName(AUTODOC_TASK_NAME)).isNotNull();
36+
assertThat(tasks.findByName(MergeManifestsTask.NAME)).isNotNull();
3537
}
3638
}

0 commit comments

Comments
 (0)