-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add manifest resolve task (#276)
* feat: add manifest resolve task * checkstyle
- Loading branch information
1 parent
b56fd39
commit 56ef1b0
Showing
6 changed files
with
280 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
...ugin/src/main/java/org/eclipse/edc/plugins/autodoc/tasks/AbstractManifestResolveTask.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* | ||
* Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Apache License, Version 2.0 which is available at | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Contributors: | ||
* Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation | ||
* | ||
*/ | ||
|
||
package org.eclipse.edc.plugins.autodoc.tasks; | ||
|
||
import org.eclipse.edc.plugins.autodoc.AutodocExtension; | ||
import org.gradle.api.DefaultTask; | ||
import org.gradle.api.artifacts.Dependency; | ||
import org.gradle.api.internal.artifacts.dependencies.DefaultProjectDependency; | ||
import org.gradle.api.tasks.Internal; | ||
import org.gradle.api.tasks.TaskAction; | ||
import org.gradle.api.tasks.options.Option; | ||
|
||
import java.io.File; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.nio.file.Path; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
/** | ||
* Abstract gradle task, that "resolves" an already-existing autodoc manifest from a URI and transfers (=copies, downloads,...) | ||
* the file to a directory on the local file system. | ||
* <p> | ||
* Implementations must provide a reference to that autodoc manifest file in the form of a {@link DependencySource}. | ||
*/ | ||
public abstract class AbstractManifestResolveTask extends DefaultTask { | ||
public static final String MANIFEST_CLASSIFIER = "manifest"; | ||
public static final String MANIFEST_TYPE = "json"; | ||
protected Path downloadDirectory; | ||
private File outputDirectoryOverride; | ||
|
||
public AbstractManifestResolveTask() { | ||
downloadDirectory = getProject().getLayout().getBuildDirectory().getAsFile().get().toPath().resolve("autodoc"); | ||
} | ||
|
||
@TaskAction | ||
public void resolveAutodocManifest() { | ||
var autodocExt = getProject().getExtensions().findByType(AutodocExtension.class); | ||
requireNonNull(autodocExt, "AutodocExtension cannot be null"); | ||
|
||
if (autodocExt.getDownloadDirectory().isPresent()) { | ||
downloadDirectory = autodocExt.getDownloadDirectory().get().toPath(); | ||
} | ||
|
||
if (outputDirectoryOverride != null) { | ||
downloadDirectory = outputDirectoryOverride.toPath(); | ||
} | ||
|
||
getProject().getConfigurations() | ||
.stream().flatMap(config -> config.getDependencies().stream()) | ||
.filter(dep -> dep instanceof DefaultProjectDependency) | ||
.filter(dep -> !getExclusions().contains(dep.getName())) | ||
.map(this::createSource) | ||
.filter(Optional::isPresent) | ||
.forEach(dt -> transferDependencyFile(dt.get(), downloadDirectory)); | ||
} | ||
|
||
@Option(option = "output", description = "CLI option to override the output directory") | ||
public void setOutput(String output) { | ||
this.outputDirectoryOverride = new File(output); | ||
} | ||
|
||
/** | ||
* Returns an {@link InputStream} that points to the physical location of the autodoc manifest file. | ||
*/ | ||
@Internal //otherwise it would get interpreted as task input :/ | ||
protected abstract InputStream resolveManifest(DependencySource autodocManifest); | ||
|
||
@Internal //otherwise it would get interpreted as task input :/ | ||
protected Set<String> getExclusions() { | ||
return Set.of(); | ||
} | ||
|
||
@Internal //otherwise it would get interpreted as task input :/ | ||
protected abstract Optional<DependencySource> createSource(Dependency dependency); | ||
|
||
private void transferDependencyFile(DependencySource dependencySource, Path downloadDirectory) { | ||
var targetFilePath = downloadDirectory.resolve(dependencySource.filename()); | ||
try (var inputStream = resolveManifest(dependencySource)) { | ||
downloadDirectory.toFile().mkdirs(); | ||
getLogger().debug("Downloading {} into {}", dependencySource, downloadDirectory); | ||
try (var fos = new FileOutputStream(targetFilePath.toFile())) { | ||
inputStream.transferTo(fos); | ||
} | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
.../autodoc-plugin/src/main/java/org/eclipse/edc/plugins/autodoc/tasks/DependencySource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Apache License, Version 2.0 which is available at | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Contributors: | ||
* Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation | ||
* | ||
*/ | ||
|
||
package org.eclipse.edc.plugins.autodoc.tasks; | ||
|
||
import org.gradle.api.artifacts.Dependency; | ||
|
||
import java.net.URI; | ||
|
||
import static java.lang.String.format; | ||
|
||
/** | ||
* Represents the combination of a dependency and a pointer (URL) to its physical location. | ||
* | ||
* @param dependency the dependency in question | ||
* @param uri the location where the physical file exists | ||
* @param classifier what type of dependency we have, e.g. sources, sources, manifest etc | ||
* @param type file extension | ||
*/ | ||
public record DependencySource(Dependency dependency, URI uri, String classifier, String type) { | ||
@Override | ||
public String toString() { | ||
return "{" + | ||
"dependency=" + dependency + | ||
", uri=" + uri + | ||
'}'; | ||
} | ||
|
||
String filename() { | ||
return format("%s-%s-%s.%s", dependency.getName(), dependency.getVersion(), classifier, type); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.