Skip to content

Commit

Permalink
Fix review comments: Add ModuleDescriptorRecommendation task
Browse files Browse the repository at this point in the history
  • Loading branch information
iherasymenko committed Oct 30, 2023
1 parent fd2a0df commit d743c78
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,13 @@
import org.gradle.api.plugins.HelpTasksPlugin;
import org.gradle.api.plugins.JavaPlugin;
import org.gradle.api.provider.Provider;
import org.gradle.api.tasks.SourceSet;
import org.gradle.api.tasks.SourceSetContainer;
import org.gradle.util.GradleVersion;
import org.gradlex.javamodule.moduleinfo.tasks.ModuleDescriptorRecommendation;
import org.jetbrains.annotations.Nullable;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.io.File;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand All @@ -70,21 +69,53 @@ public void apply(Project project) {
// setup the transform and the tasks for all projects in the build
project.getPlugins().withType(JavaPlugin.class).configureEach(javaPlugin -> {
configureTransform(project, extension);
configureModuleDescriptorTask(project);
configureModuleDescriptorTasks(project);
});
}

private void configureModuleDescriptorTask(Project project) {
private void configureModuleDescriptorTasks(Project project) {
project.getExtensions().getByType(SourceSetContainer.class).all(sourceSet -> {
String name = SourceSet.MAIN_SOURCE_SET_NAME.equals(sourceSet.getName())
? "moduleDescriptorRecommendations"
: sourceSet.getName() + "ModuleDescriptorRecommendations";
String name = sourceSet.getTaskName("", "moduleDescriptorRecommendations");
project.getTasks().register(name, ModuleDescriptorRecommendation.class, task -> {
Transformer<@Nullable List<File>, Configuration> artifactsTransformer = configuration -> {
//noinspection CodeBlock2Expr
return configuration.getIncoming()
.getArtifacts()
.getArtifacts()
.stream()
.sorted(Comparator.comparing(artifact -> artifact.getId().getComponentIdentifier().toString()))
.map(ResolvedArtifactResult::getFile)
.collect(Collectors.toList());
};

Transformer<@Nullable List<ResolvedComponentResult>, Configuration> componentsTransformer = configuration -> {
Set<ComponentIdentifier> artifacts = configuration.getIncoming()
.getArtifacts()
.getArtifacts()
.stream()
.map(artifact -> artifact.getId().getComponentIdentifier())
.collect(Collectors.toSet());
return configuration.getIncoming()
.getResolutionResult()
.getAllComponents()
.stream()
.filter(component -> artifacts.contains(component.getId()))
.sorted(Comparator.comparing(artifact -> artifact.getId().toString()))
.collect(Collectors.toList());
};

Provider<Configuration> compileClasspath = project.getConfigurations().named(sourceSet.getCompileClasspathConfigurationName());
task.getCompileArtifacts().set(compileClasspath.map(artifactsTransformer));
task.getCompileResolvedComponentResults().set(compileClasspath.map(componentsTransformer));

Provider<Configuration> runtimeClasspath = project.getConfigurations().named(sourceSet.getRuntimeClasspathConfigurationName());
task.getRuntimeArtifacts().set(runtimeClasspath.map(artifactsTransformer));
task.getRuntimeResolvedComponentResults().set(runtimeClasspath.map(componentsTransformer));

task.getRelease().convention(21);
task.getCompileConfiguration().set(project.getConfigurations().getByName(sourceSet.getCompileClasspathConfigurationName()));
task.getRuntimeConfiguration().set(project.getConfigurations().getByName(sourceSet.getRuntimeClasspathConfigurationName()));

task.setGroup(HelpTasksPlugin.HELP_GROUP);
task.setDescription("Generates module descriptors for extraJavaModuleInfo plugin based on the dependency and class file analysis of automatic module and non-modular dependencies");
task.setDescription("Generates module descriptors for 'org.gradlex.extra-java-module-info' plugin based on the dependency and class file analysis of automatic modules and non-modular dependencies");
});
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,21 @@
* limitations under the License.
*/

package org.gradlex.javamodule.moduleinfo;
package org.gradlex.javamodule.moduleinfo.tasks;

import org.gradle.api.DefaultTask;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.ModuleIdentifier;
import org.gradle.api.artifacts.ModuleVersionIdentifier;
import org.gradle.api.artifacts.component.ComponentIdentifier;
import org.gradle.api.artifacts.component.ModuleComponentIdentifier;
import org.gradle.api.artifacts.result.DependencyResult;
import org.gradle.api.artifacts.result.ResolvedArtifactResult;
import org.gradle.api.artifacts.result.ResolvedComponentResult;
import org.gradle.api.artifacts.result.ResolvedDependencyResult;
import org.gradle.api.file.FileSystemOperations;
import org.gradle.api.provider.ListProperty;
import org.gradle.api.provider.Property;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.InputFiles;
import org.gradle.api.tasks.TaskAction;

import javax.inject.Inject;
Expand Down Expand Up @@ -138,11 +138,15 @@ static Java8SafeToolProvider findFirst(String name) {

}

@InputFiles
public abstract ListProperty<File> getRuntimeArtifacts();
@Input
public abstract Property<Configuration> getRuntimeConfiguration();
public abstract ListProperty<ResolvedComponentResult> getRuntimeResolvedComponentResults();

@InputFiles
public abstract ListProperty<File> getCompileArtifacts();
@Input
public abstract Property<Configuration> getCompileConfiguration();
public abstract ListProperty<ResolvedComponentResult> getCompileResolvedComponentResults();

@Input
public abstract Property<Integer> getRelease();
Expand All @@ -156,8 +160,8 @@ public void execute() throws IOException {
Java8SafeToolProvider jarTool = Java8SafeToolProvider.findFirst("jar");

Map<ModuleIdentifier, Artifact> artifacts = new HashMap<>();
extractArtifactsAndTheirDependencies(artifacts, getRuntimeConfiguration().get(), artifact -> artifact.runtimeDependencies);
extractArtifactsAndTheirDependencies(artifacts, getCompileConfiguration().get(), artifact -> artifact.compileDependencies);
extractArtifactsAndTheirDependencies(artifacts, getRuntimeArtifacts().get(), getRuntimeResolvedComponentResults().get(), artifact -> artifact.runtimeDependencies);
extractArtifactsAndTheirDependencies(artifacts, getCompileArtifacts().get(), getCompileResolvedComponentResults().get(), artifact -> artifact.compileDependencies);

Path temporaryFolder = Files.createTempDirectory("jdeps-task");
for (Map.Entry<ModuleIdentifier, Artifact> entry : artifacts.entrySet()) {
Expand Down Expand Up @@ -204,15 +208,19 @@ public void execute() throws IOException {
getFileSystemOperations().delete(spec -> spec.delete(temporaryFolder));
}

private static void extractArtifactsAndTheirDependencies(Map<ModuleIdentifier, Artifact> jarsToAnalyze, Configuration configuration, Function<Artifact, Set<ModuleIdentifier>> depsSink) {
for (ResolvedArtifactResult artifact : configuration.getIncoming().getArtifacts()) {
ComponentIdentifier identifier = artifact.getId().getComponentIdentifier();
private static void extractArtifactsAndTheirDependencies(Map<ModuleIdentifier, Artifact> jarsToAnalyze,
List<File> artifacts,
List<ResolvedComponentResult> resolvedComponentResults,
Function<Artifact, Set<ModuleIdentifier>> depsSink) {
for (ResolvedComponentResult artifact : resolvedComponentResults) {
ComponentIdentifier identifier = artifact.getId();
if (identifier instanceof ModuleComponentIdentifier) {
ModuleIdentifier moduleIdentifier = ((ModuleComponentIdentifier) identifier).getModuleIdentifier();
jarsToAnalyze.computeIfAbsent(moduleIdentifier, (ignore) -> new Artifact(moduleIdentifier, artifact.getFile()));
int index = resolvedComponentResults.indexOf(artifact);
jarsToAnalyze.computeIfAbsent(moduleIdentifier, (ignore) -> new Artifact(moduleIdentifier, artifacts.get(index)));
}
}
for (ResolvedComponentResult resolvedComponent : configuration.getIncoming().getResolutionResult().getAllComponents()) {
for (ResolvedComponentResult resolvedComponent : resolvedComponentResults) {
ModuleVersionIdentifier moduleVersion = resolvedComponent.getModuleVersion();
if (moduleVersion == null) {
continue;
Expand Down

0 comments on commit d743c78

Please sign in to comment.