Skip to content

Commit

Permalink
Added support for displaying extensions dependencies in the dependenc…
Browse files Browse the repository at this point in the history
…y-tree command.
  • Loading branch information
gbevin committed Jul 13, 2024
1 parent feed8a8 commit a222bec
Show file tree
Hide file tree
Showing 3 changed files with 338 additions and 44 deletions.
99 changes: 99 additions & 0 deletions src/main/java/rife/bld/operations/DependencyTreeOperation.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
package rife.bld.operations;

import rife.bld.BaseProject;
import rife.bld.BldVersion;
import rife.bld.dependencies.*;
import rife.bld.wrapper.Wrapper;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

Expand All @@ -22,6 +25,9 @@ public class DependencyTreeOperation extends AbstractOperation<DependencyTreeOpe
private ArtifactRetriever retriever_ = null;
private final List<Repository> repositories_ = new ArrayList<>();
private final DependencyScopes dependencies_ = new DependencyScopes();
private final List<Repository> extensionRepositories_ = new ArrayList<>();
private final DependencyScopes extensionDependencies_ = new DependencyScopes();

private final StringBuilder dependencyTree_ = new StringBuilder();

/**
Expand All @@ -30,11 +36,14 @@ public class DependencyTreeOperation extends AbstractOperation<DependencyTreeOpe
* @since 1.5.21
*/
public void execute() {
var extensions_tree = executeGenerateExtensionsDependencies();
var compile_tree = executeGenerateCompileDependencies();
var provided_tree = executeGenerateProvidedDependencies();
var runtime_tree = executeGenerateRuntimeDependencies();
var test_tree = executeGenerateTestDependencies();
dependencyTree_.setLength(0);
dependencyTree_.append(extensions_tree);
dependencyTree_.append(System.lineSeparator());
dependencyTree_.append(compile_tree);
dependencyTree_.append(System.lineSeparator());
dependencyTree_.append(provided_tree);
Expand All @@ -44,12 +53,26 @@ public void execute() {
dependencyTree_.append(test_tree);
dependencyTree_.append(System.lineSeparator());

System.out.println(extensions_tree);
System.out.println(compile_tree);
System.out.println(provided_tree);
System.out.println(runtime_tree);
System.out.println(test_tree);
}

/**
* Part of the {@link #execute} operation, generates the tree for the extensions.
*
* @since 2.0
*/
protected String executeGenerateExtensionsDependencies() {
var extensions_tree = extensionDependencies().scope(compile).generateTransitiveDependencyTree(artifactRetriever(), extensionRepositories(), compile, runtime);
if (extensions_tree.isEmpty()) {
extensions_tree = "no dependencies" + System.lineSeparator();
}
return "extensions:" + System.lineSeparator() + extensions_tree;
}

/**
* Part of the {@link #execute} operation, generates the tree for the compile scope.
*
Expand Down Expand Up @@ -110,6 +133,20 @@ protected String executeGenerateTestDependencies() {
* @since 1.5.21
*/
public DependencyTreeOperation fromProject(BaseProject project) {
// add the repositories and dependencies from the extensions
var wrapper = new Wrapper();
wrapper.currentDir(project.workDirectory());
try {
wrapper.initWrapperProperties(BldVersion.getVersion());
for (var repository : wrapper.repositories()) {
extensionRepositories().add(Repository.resolveRepository(project.properties(), repository));
}
extensionDependencies().scope(compile).addAll(wrapper.extensions().stream().map(Dependency::parse).toList());
} catch (IOException e) {
throw new RuntimeException(e);
}

// add the repositories and the dependencies from the project
return artifactRetriever(project.artifactRetriever())
.repositories(project.repositories())
.dependencies(project.dependencies());
Expand Down Expand Up @@ -153,6 +190,44 @@ public DependencyTreeOperation dependencies(DependencyScopes dependencies) {
return this;
}

/**
* Provides extension repositories to resolve the extension dependencies against.
*
* @param repositories extension repositories against which extension dependencies will be resolved
* @return this operation instance
* @since 2.0
*/
public DependencyTreeOperation extensionRepositories(Repository... repositories) {
extensionRepositories_.addAll(List.of(repositories));
return this;
}

/**
* Provides a list of extension repositories to resolve the extension dependencies against.
* <p>
* A copy will be created to allow this list to be independently modifiable.
*
* @param repositories a list of extension repositories against which extension dependencies will be resolved
* @return this operation instance
* @since 2.0
*/
public DependencyTreeOperation extensionRepositories(List<Repository> repositories) {
extensionRepositories_.addAll(repositories);
return this;
}

/**
* Provides scoped extension dependencies to generate a tree for.
*
* @param dependencies the extension dependencies that will be resolved for tree generation
* @return this operation instance
* @since 2.0
*/
public DependencyTreeOperation extensionDependencies(DependencyScopes dependencies) {
extensionDependencies_.include(dependencies);
return this;
}

/**
* Provides the artifact retriever to use.
*
Expand Down Expand Up @@ -189,6 +264,30 @@ public DependencyScopes dependencies() {
return dependencies_;
}

/**
* Retrieves the extension repositories in which the dependencies will be resolved.
* <p>
* This is a modifiable list that can be retrieved and changed.
*
* @return the extension repositories used for dependency resolution
* @since 2.0
*/
public List<Repository> extensionRepositories() {
return extensionRepositories_;
}

/**
* Retrieves the scoped extension dependencies that will be used for tree generation.
* <p>
* This is a modifiable structure that can be retrieved and changed.
*
* @return the scoped extension dependencies
* @since 2.0
*/
public DependencyScopes extensionDependencies() {
return extensionDependencies_;
}

/**
* Returns the artifact retriever that is used.
*
Expand Down
129 changes: 85 additions & 44 deletions src/main/java/rife/bld/wrapper/Wrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,91 @@ private void createWrapperJar(File destinationDirectory)
}
}

/**
* Sets the current directory for the wrapper.
*
* @param dir the directory to set as the current directory
* @since 2.0
*/
public void currentDir(File dir) {
currentDir_ = dir;
}

/**
* Initializes the properties set for the wrapper.
*
* @param version the bld version they should be using
* @throws IOException when an error occurred during the creation of the wrapper files
* @since 2.0
*/
public void initWrapperProperties(String version)
throws IOException {
// ensure required properties are available
wrapperProperties_.put(PROPERTY_REPOSITORIES, MAVEN_CENTRAL);
wrapperProperties_.put(BLD_PROPERTY_VERSION, version);

// retrieve properties from possible locations
var config = libBldDirectory(WRAPPER_PROPERTIES);
if (config.exists()) {
wrapperPropertiesFile_ = config;
wrapperProperties_.load(new FileReader(config));
} else {
config = libDirectory(WRAPPER_PROPERTIES);
if (config.exists()) {
wrapperPropertiesFile_ = config;
wrapperProperties_.load(new FileReader(config));
}
}

// extract repositories
if (wrapperProperties_.containsKey(PROPERTY_REPOSITORIES)) {
for (var repository : wrapperProperties_.getProperty(PROPERTY_REPOSITORIES).split(",")) {
repository = repository.trim();
if (!repository.isBlank()) {
repositories_.add(repository);
}
}
}
// extract wrapper extension specifications
for (var property : wrapperProperties_.entrySet()) {
if (property.getKey().toString().startsWith(PROPERTY_EXTENSION_PREFIX)) {
for (var extension : property.getValue().toString().split(",")) {
extension = extension.trim();
if (!extension.isBlank()) {
extensions_.add(extension);
}
}
}
}
// check whether extension sources or javadoc should be downloaded
downloadExtensionSources_ = Boolean.parseBoolean(wrapperProperties_.getProperty(PROPERTY_DOWNLOAD_EXTENSION_SOURCES, "false"));
downloadExtensionJavadoc_ = Boolean.parseBoolean(wrapperProperties_.getProperty(PROPERTY_DOWNLOAD_EXTENSION_JAVADOC, "false"));
}

/**
* Returns the set of extension repositories.
*
* @return the set of extension repositories
* @since 2.0
*/
public Set<String> repositories() {
return repositories_;
}

/**
* Returns the set of extensions.
*
* @return the set of extensions
* @since 2.0
*/
public Set<String> extensions() {
return extensions_;
}

public File wrapperPropertiesFile() {
return wrapperPropertiesFile_;
}

private void addClassToJar(JarOutputStream jar, Class klass)
throws IOException {
addFileToJar(jar, klass.getName().replace('.', '/') + ".class");
Expand Down Expand Up @@ -328,50 +413,6 @@ private File libBldDirectory(String path) {
return Path.of(currentDir_.getAbsolutePath(), "lib", "bld", path).toFile();
}

private void initWrapperProperties(String version)
throws IOException {
// ensure required properties are available
wrapperProperties_.put(PROPERTY_REPOSITORIES, MAVEN_CENTRAL);
wrapperProperties_.put(BLD_PROPERTY_VERSION, version);

// retrieve properties from possible locations
var config = libBldDirectory(WRAPPER_PROPERTIES);
if (config.exists()) {
wrapperPropertiesFile_ = config;
wrapperProperties_.load(new FileReader(config));
} else {
config = libDirectory(WRAPPER_PROPERTIES);
if (config.exists()) {
wrapperPropertiesFile_ = config;
wrapperProperties_.load(new FileReader(config));
}
}

// extract repositories
if (wrapperProperties_.containsKey(PROPERTY_REPOSITORIES)) {
for (var repository : wrapperProperties_.getProperty(PROPERTY_REPOSITORIES).split(",")) {
repository = repository.trim();
if (!repository.isBlank()) {
repositories_.add(repository);
}
}
}
// extract wrapper extension specifications
for (var property : wrapperProperties_.entrySet()) {
if (property.getKey().toString().startsWith(PROPERTY_EXTENSION_PREFIX)) {
for (var extension : property.getValue().toString().split(",")) {
extension = extension.trim();
if (!extension.isBlank()) {
extensions_.add(extension);
}
}
}
}
// check whether extension sources or javadoc should be downloaded
downloadExtensionSources_ = Boolean.parseBoolean(wrapperProperties_.getProperty(PROPERTY_DOWNLOAD_EXTENSION_SOURCES, "false"));
downloadExtensionJavadoc_ = Boolean.parseBoolean(wrapperProperties_.getProperty(PROPERTY_DOWNLOAD_EXTENSION_JAVADOC, "false"));
}

private String getWrapperVersion()
throws IOException {
return wrapperProperties_.getProperty(BLD_PROPERTY_VERSION, getVersion());
Expand Down
Loading

0 comments on commit a222bec

Please sign in to comment.