Skip to content

Commit

Permalink
Fix issue that causes new utility tasks to fail task configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
jjohannes committed Feb 8, 2024
1 parent 431cbbe commit 88ed065
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 14 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Java Module Dependencies Gradle Plugin - Changelog

## Version 1.6.1
* Fix in setup of new utility tasks

## Version 1.6
* Add more utility tasks to migrate from/to module-info based dependencies
* Additional notation for module version DSL
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ private List<BuildFileDependenciesGenerate.DependencyDeclaration> collectDepende
}
}
return moduleInfo.get(directive).stream()
.map(moduleName -> new BuildFileDependenciesGenerate.DependencyDeclaration(scope, moduleName, javaModuleDependencies.ga(moduleName).get()))
.map(moduleName -> new BuildFileDependenciesGenerate.DependencyDeclaration(scope, moduleName, javaModuleDependencies.ga(moduleName)))
.collect(Collectors.toList());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,11 @@ private void registerCatalogTask(Project project) {
}

private List<CatalogGenerate.CatalogEntry> collectCatalogEntriesFromVersions(JavaModuleDependenciesExtension javaModuleDependencies, ModuleVersions moduleVersions) {
return moduleVersions.getDeclaredVersions().entrySet().stream().map(mv -> new CatalogGenerate.CatalogEntry(mv.getKey(), javaModuleDependencies.ga(mv.getKey()).get(), mv.getValue())).collect(Collectors.toList());
return moduleVersions.getDeclaredVersions().entrySet().stream().map(mv -> new CatalogGenerate.CatalogEntry(mv.getKey(), javaModuleDependencies.ga(mv.getKey()), mv.getValue())).collect(Collectors.toList());
}

private List<CatalogGenerate.CatalogEntry> collectCatalogEntriesFromModuleInfos(JavaModuleDependenciesExtension javaModuleDependencies, List<String> moduleNames) {
return moduleNames.stream().map(moduleName -> new CatalogGenerate.CatalogEntry(moduleName, javaModuleDependencies.ga(moduleName).get(), null)).collect(Collectors.toList());
return moduleNames.stream().map(moduleName -> new CatalogGenerate.CatalogEntry(moduleName, javaModuleDependencies.ga(moduleName), null)).collect(Collectors.toList());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.gradle.api.model.ObjectFactory;
import org.gradle.api.provider.ListProperty;
import org.gradle.api.provider.Property;
import org.gradle.api.provider.Provider;
import org.gradle.api.tasks.Internal;
import org.gradle.api.tasks.TaskAction;

Expand Down Expand Up @@ -65,9 +66,9 @@ && getCompileOnlyDependencies().get().isEmpty()
public static class DependencyDeclaration {
private final String scope;
private final String moduleName;
private final String fullId;
private final Provider<String> fullId;

public DependencyDeclaration(String scope, String moduleName, String fullId) {
public DependencyDeclaration(String scope, String moduleName, Provider<String> fullId) {
this.scope = scope;
this.moduleName = moduleName;
this.fullId = fullId;
Expand Down Expand Up @@ -113,7 +114,7 @@ public void generate() throws IOException {
content.add("");
}

if (!getDependencies().get().isEmpty()) {
if (!getDependencies().get().stream().allMatch(SourceSetDependencies::isEmpty)) {
content.add("dependencies {");
getDependencies().get().stream().sorted((a, b) -> ("main".equals(a.name)) ? -1 : a.name.compareTo(b.name)).forEach(sourceSetBlock -> {
content.addAll(toDeclarationString(sourceSetBlock.getApiDependencies()));
Expand All @@ -134,8 +135,8 @@ public void generate() throws IOException {

private List<String> toDeclarationString(ListProperty<DependencyDeclaration> dependencies) {
return dependencies.get().stream().map(d -> {
String group = d.fullId.split(":")[0];
String artifact = d.fullId.split(":")[1];
String group = d.fullId.get().split(":")[0];
String artifact = d.fullId.get().split(":")[1];
String feature = null;
if (artifact.contains("|")) {
feature = artifact.split("\\|")[1];
Expand All @@ -145,7 +146,7 @@ private List<String> toDeclarationString(ListProperty<DependencyDeclaration> dep
String identifier;
if (group.equals(getOwnProjectGroup().get())) {
if (getWithCatalog().get()) {
identifier = "projects." + artifact;
identifier = "projects." + toCamelCase(artifact);
} else {
identifier = "project(\":" + artifact + "\")";
}
Expand All @@ -164,4 +165,19 @@ private List<String> toDeclarationString(ListProperty<DependencyDeclaration> dep
}
}).collect(Collectors.toList());
}

private String toCamelCase(String s) {
String[] segments = s.split("[\\W_]+");
StringBuilder builder = new StringBuilder();
for (int i = 0; i < segments.length; i++) {
String word = segments[i];
if (i == 0) {
word = word.isEmpty() ? word : word.toLowerCase();
} else {
word = word.isEmpty() ? word : Character.toUpperCase(word.charAt(0)) + word.substring(1).toLowerCase();
}
builder.append(word);
}
return builder.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.gradle.api.file.RegularFileProperty;
import org.gradle.api.provider.ListProperty;
import org.gradle.api.provider.Property;
import org.gradle.api.provider.Provider;
import org.gradle.api.provider.SetProperty;
import org.gradle.api.tasks.Internal;
import org.gradle.api.tasks.TaskAction;
Expand All @@ -38,10 +39,10 @@ public abstract class CatalogGenerate extends DefaultTask {

public static class CatalogEntry implements Comparator<CatalogEntry> {
private final String moduleName;
private final String fullId;
private final Provider<String> fullId;
private final String version;

public CatalogEntry(String moduleName, String fullId, @Nullable String version) {
public CatalogEntry(String moduleName, Provider<String> fullId, @Nullable String version) {
this.moduleName = moduleName;
this.fullId = fullId;
this.version = version;
Expand Down Expand Up @@ -90,15 +91,15 @@ public void generate() throws IOException {

@Nullable
private String toDeclarationString(CatalogEntry entry) {
String group = entry.fullId.split(":")[0];
String group = entry.fullId.get().split(":")[0];
if (group.equals(getOwnProjectGroup().get())) {
return null;
}
String notation;
if (entry.version == null) {
notation = "{ module = \"" + entry.fullId + "\" }";
notation = "{ module = \"" + entry.fullId.get() + "\" }";
} else {
notation = "{ module = \"" + entry.fullId + "\", version = \"" + entry.version + "\" }";
notation = "{ module = \"" + entry.fullId.get() + "\", version = \"" + entry.version + "\" }";
}
return entry.moduleName.replace('.', '-') + " = " + notation;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.gradlex.javamodule.dependencies.test

import org.gradlex.javamodule.dependencies.test.fixture.GradleBuild
import spock.lang.Specification

class BasicFunctionalityTest extends Specification {

@Delegate
GradleBuild build = new GradleBuild()

def "can configure all tasks in a build without error"() {
given:
libModuleInfoFile << 'module abc.lib { }'
appModuleInfoFile << '''
module org.gradlex.test.app {
requires abc.lib;
}
'''

when:
def result = runner('tasks').build()

then:
result.output.contains('''
Java modules tasks
------------------
checkModuleInfo - Check order of directives in 'module-info.java' in 'main' source set
checkTestFixturesModuleInfo - Check order of directives in 'module-info.java' in 'testFixtures' source set
checkTestModuleInfo - Check order of directives in 'module-info.java' in 'test' source set
generateAllModuleInfoFiles - Generate 'module-info.java' files in all source sets
generateBuildFileDependencies - Generate 'dependencies' block in 'build.gradle.kts'
generateCatalog - Generate 'libs.versions.toml' file
generateModuleInfoFile - Generate 'module-info.java' in 'main' source set
generateTestFixturesModuleInfoFile - Generate 'module-info.java' in 'testFixtures' source set
generateTestModuleInfoFile - Generate 'module-info.java' in 'test' source set'''.stripIndent()
)
}

}

0 comments on commit 88ed065

Please sign in to comment.