Skip to content

Commit c46ea44

Browse files
committed
Refactor dependency handling for multi-project support
1 parent 3c279f0 commit c46ea44

2 files changed

Lines changed: 64 additions & 13 deletions

File tree

src/main/java/io/github/intisy/gradle/github/Main.java

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,19 @@
2020
import java.io.IOException;
2121
import java.util.HashSet;
2222
import java.util.Set;
23+
import java.util.stream.Collectors;
2324

2425
/**
25-
* Main class.
26+
* Main class that implements a Gradle Plugin for managing GitHub-related functionalities in projects.
27+
* This plugin integrates with the GitHub API and provides tasks for managing resources and dependencies.
2628
*/
2729
class Main implements Plugin<Project> {
2830
/**
29-
* Applies all the project stuff.
31+
* Applies the GitHub plugin to the given Gradle project. This method is responsible for configuring
32+
* project-level tasks, extensions, and dependencies related to GitHub integration.
33+
*
34+
* @param project The Gradle project on which the plugin is being applied. This object represents the
35+
* project instance that acts as the target for configurations and tasks.
3036
*/
3137
public void apply(Project project) {
3238
GithubExtension githubExtension = project.getExtensions().create("github", GithubExtension.class);
@@ -92,7 +98,7 @@ public void apply(Project project) {
9298
task.setGroup("github");
9399
task.setDescription("Implement an github dependency");
94100
task.doLast(t -> {
95-
for (Dependency dependency : getDependencies(project)) {
101+
for (Dependency dependency : getAllDependencies(project)) {
96102
String group = dependency.getGroup();
97103
String name = dependency.getName();
98104
String version = dependency.getVersion();
@@ -107,7 +113,7 @@ public void apply(Project project) {
107113
task.setDescription("Updates all GitHub dependencies");
108114
task.doLast(t -> {
109115
boolean refresh = false;
110-
Set<Dependency> dependencyList = getDependencies(project);
116+
Set<Dependency> dependencyList = getAllDependencies(project);
111117
logger.debug("Updating GitHub dependencies: " + dependencyList);
112118
for (Dependency dependency : dependencyList) {
113119
String group = dependency.getGroup();
@@ -118,7 +124,9 @@ public void apply(Project project) {
118124
String newVersion = gitHub.getLatestVersion(group, name);
119125
if (version != null && !version.equals(newVersion)) {
120126
logger.log("Updating GitHub dependency " + group + "/" + name + " (" + version + " -> " + newVersion + ")");
121-
Gradle.modifyBuildFile(project, group + ":" + name + ":" + version, group + ":" + name + ":" + newVersion);
127+
for (Project p : GradleUtils.getAllProjectsRecursive(project)) {
128+
Gradle.modifyBuildFile(p, group + ":" + name + ":" + version, group + ":" + name + ":" + newVersion);
129+
}
122130
refresh = true;
123131
} else {
124132
logger.log("Dependency " + group + "/" + name + " is already up to date");
@@ -130,14 +138,27 @@ public void apply(Project project) {
130138
});
131139
}
132140

141+
/**
142+
* Aggregates all dependencies from the given project and its subprojects.
143+
*
144+
* @param project The Gradle project for which dependencies are to be retrieved. This includes the
145+
* specified project as well as all its subprojects.
146+
* @return A set containing all {@code Dependency} objects from the given project and its subprojects.
147+
*/
148+
public Set<Dependency> getAllDependencies(Project project) {
149+
return project.getAllprojects().stream().flatMap(p -> getDependencies(p).stream()).collect(Collectors.toSet());
150+
}
151+
152+
/**
153+
* Retrieves the set of dependencies associated with the "githubImplementation" configuration
154+
* of the specified Gradle project.
155+
*
156+
* @param project The Gradle project from which dependencies are to be retrieved. This project
157+
* should have a "githubImplementation" configuration defined.
158+
* @return A set of {@code Dependency} objects representing all dependencies declared
159+
* under the "githubImplementation" configuration of the given project.
160+
*/
133161
public Set<Dependency> getDependencies(Project project) {
134-
Set<Dependency> dependencyList = new HashSet<>();
135-
project.getAllprojects().forEach(p -> {
136-
if (!p.equals(project.getRootProject())) {
137-
dependencyList.addAll(p.getConfigurations().getByName("githubImplementation").getAllDependencies());
138-
}
139-
});
140-
dependencyList.addAll(project.getConfigurations().getByName("githubImplementation").getAllDependencies());
141-
return dependencyList;
162+
return new HashSet<>(project.getConfigurations().getByName("githubImplementation").getAllDependencies());
142163
}
143164
}

src/main/java/io/github/intisy/gradle/github/utils/GradleUtils.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package io.github.intisy.gradle.github.utils;
22

3+
import org.gradle.api.Project;
4+
35
import java.nio.file.Path;
46
import java.nio.file.Paths;
7+
import java.util.HashSet;
8+
import java.util.Set;
59

610
/**
711
* This utility class provides methods for interacting with Gradle.
@@ -18,4 +22,30 @@ public static Path getGradleHome() {
1822
String userHome = System.getProperty("user.home");
1923
return Paths.get(userHome, ".gradle", "caches");
2024
}
25+
26+
/**
27+
* Retrieves all projects recursively, starting from the specified project and including
28+
* its subprojects, if any.
29+
*
30+
* @param project the root project from which to begin collecting projects
31+
* @return a set containing the root project and all its subprojects
32+
*/
33+
public static Set<Project> getAllProjectsRecursive(Project project) {
34+
Set<Project> projects = new HashSet<>();
35+
collectProjectsRecursive(project, projects);
36+
return projects;
37+
}
38+
39+
/**
40+
* Recursively collects all subprojects of the specified project and adds them to the provided set.
41+
* This ensures that all hierarchical project relationships are traversed and included.
42+
*
43+
* @param project the project from which to start collecting subprojects
44+
* @param projects the set to which collected projects will be added
45+
*/
46+
private static void collectProjectsRecursive(Project project, Set<Project> projects) {
47+
if (!projects.add(project))
48+
return;
49+
project.getSubprojects().forEach(p -> collectProjectsRecursive(p, projects));
50+
}
2151
}

0 commit comments

Comments
 (0)