Skip to content

Commit

Permalink
Fixed some Gradle introspection problems wit pre-6.1 Gradle Versions
Browse files Browse the repository at this point in the history
  • Loading branch information
lkishalmi committed Nov 27, 2022
1 parent 794802e commit 1eff472
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@
import org.gradle.api.plugins.ExtensionAware;
import org.gradle.api.plugins.ExtensionContainer;
import org.gradle.api.plugins.ExtensionsSchema.ExtensionSchema;
import org.gradle.api.plugins.JavaPlatformPlugin;
import org.gradle.api.provider.Provider;
import org.gradle.api.reflect.HasPublicType;
import org.gradle.api.reflect.TypeOf;
Expand All @@ -105,7 +104,7 @@
import org.gradle.language.base.artifact.SourcesArtifact;
import org.gradle.language.java.artifact.JavadocArtifact;
import org.gradle.plugin.use.PluginId;
import org.gradle.util.VersionNumber;
import org.gradle.util.GradleVersion;
import org.netbeans.modules.gradle.tooling.internal.NbProjectInfo;

/**
Expand Down Expand Up @@ -176,8 +175,9 @@ class NbProjectInfoBuilder {
"war"
}));

private static final GradleVersion GRADLE_VERSION = GradleVersion.current().getBaseVersion();

final Project project;
final VersionNumber gradleVersion;
final GradleInternalAdapter adapter;

public static final class ValueAndType {
Expand All @@ -197,9 +197,7 @@ public ValueAndType(Class type) {

NbProjectInfoBuilder(Project project) {
this.project = project;
this.gradleVersion = VersionNumber.parse(project.getGradle().getGradleVersion());
// checked that version 7.6.0 > 7.6.0-rc-1 in the VersionNumber order
this.adapter = sinceGradleOrDefault("7.6.0-rc-1", () -> new GradleInternalAdapter.Gradle76(project), () -> new GradleInternalAdapter(project));
this.adapter = sinceGradleOrDefault("7.6", () -> new GradleInternalAdapter.Gradle76(project), () -> new GradleInternalAdapter(project));
}

private NbProjectInfoModel model = new NbProjectInfoModel();
Expand Down Expand Up @@ -864,7 +862,7 @@ private void detectProjectMetadata(NbProjectInfoModel model) {
model.getInfo().put("project_subProjects", sp);

Map<String, File> ib = new HashMap<>();
LOG.lifecycle("Gradle Version: {}", gradleVersion);
LOG.lifecycle("Gradle Version: {}", GradleVersion.current());
sinceGradle("3.1", () -> {
for(IncludedBuild p: project.getGradle().getIncludedBuilds()) {
LOG.lifecycle("Include Build: {}", p.getName());
Expand Down Expand Up @@ -1016,36 +1014,44 @@ private void detectSources(NbProjectInfoModel model) {
}
if (Boolean.TRUE.equals(available.get(langId))) {
model.getInfo().put(propBase + lang, storeSet(getProperty(sourceSet, langId, "srcDirs")));
DirectoryProperty dirProp = (DirectoryProperty)getProperty(sourceSet, langId, "classesDirectory");
if (dirProp != null) {
File outDir;

if (dirProp.isPresent()) {
outDir = dirProp.get().getAsFile();
} else {
// kotlin plugin uses some weird late binding, so it has the output item, but it cannot be resolved to a
// concrete file path at this time. Let's make an approximation from
Path candidate = null;
if (base != null) {
Path prefix = base.resolve(langId);
// assume the language has just one output dir in the source set:
for (int i = 0; i < outPaths.size(); i++) {
Path p = outPaths.get(i);
if (p.startsWith(prefix)) {
if (candidate != null) {
candidate = null;
break;
} else {
candidate = p;
asGradle("4.0", "6.1", () -> {
File outDir = (File) getProperty(sourceSet, langId, "outputDir");
model.getInfo().put(propBase + lang + "_output_classes", outDir);
});
sinceGradle("6.1", () -> {
DirectoryProperty dirProp = (DirectoryProperty)getProperty(sourceSet, langId, "classesDirectory");
if (dirProp != null) {
File outDir;

if (dirProp.isPresent()) {
outDir = dirProp.get().getAsFile();
} else {
// kotlin plugin uses some weird late binding, so it has the output item, but it cannot be resolved to a
// concrete file path at this time. Let's make an approximation from
Path candidate = null;
if (base != null) {
Path prefix = base.resolve(langId);
// assume the language has just one output dir in the source set:
for (int i = 0; i < outPaths.size(); i++) {
Path p = outPaths.get(i);
if (p.startsWith(prefix)) {
if (candidate != null) {
candidate = null;
break;
} else {
candidate = p;
}
}
}
}
outDir = candidate != null ? candidate.toFile() : new File("");
}
outDir = candidate != null ? candidate.toFile() : new File("");

model.getInfo().put(propBase + lang + "_output_classes", outDir);
}
model.getInfo().put(propBase + lang + "_output_classes", outDir);
}
});


}
}

Expand Down Expand Up @@ -1295,7 +1301,7 @@ private void detectDependencies(NbProjectInfoModel model) {

// NETBEANS-5846: if this project uses javaPlatform plugin with dependencies enabled,
// do not report unresolved problems
boolean ignoreUnresolvable = (project.getPlugins().hasPlugin(JavaPlatformPlugin.class) &&
boolean ignoreUnresolvable = (project.getPlugins().hasPlugin("java-platform") &&
Boolean.TRUE.equals(getProperty(project, "javaPlatform", "allowDependencies")));

visibleConfigurations.forEach(it -> {
Expand Down Expand Up @@ -1606,7 +1612,7 @@ private static <T extends Throwable> void sneakyThrow(Throwable exception) throw
}

private <T, E extends Throwable> T sinceGradleOrDefault(String version, ExceptionCallable<T, E> c, Supplier<T> def) {
if (gradleVersion.compareTo(VersionNumber.parse(version)) >= 0) {
if (GRADLE_VERSION.compareTo(GradleVersion.version(version)) >= 0) {
try {
return c.call();
} catch (RuntimeException | Error e) {
Expand All @@ -1627,23 +1633,34 @@ private <T, E extends Throwable> T sinceGradle(String version, ExceptionCallable
}

private void sinceGradle(String version, Runnable r) {
if (gradleVersion.compareTo(VersionNumber.parse(version)) >= 0) {
if (GRADLE_VERSION.compareTo(GradleVersion.version(version)) >= 0) {
r.run();
}
}

private void beforeGradle(String version, Runnable r) {
if (gradleVersion.compareTo(VersionNumber.parse(version)) < 0) {
if (GRADLE_VERSION.compareTo(GradleVersion.version(version)) < 0) {
r.run();
}
}

private void asGradle(String fromVersion, String toVersion, Runnable r) {
if ((GRADLE_VERSION.compareTo(GradleVersion.version(fromVersion)) >= 0)
&& (GRADLE_VERSION.compareTo(GradleVersion.version(toVersion)) < 0)) {
r.run();
}
}

private static Object getProperty(Object obj, String... propPath) {
Object currentObject = obj;
for(String prop: propPath) {
currentObject = InvokerHelper.getPropertySafe(currentObject, prop);
try {
for(String prop: propPath) {
currentObject = InvokerHelper.getPropertySafe(currentObject, prop);
}
return currentObject;
} catch (MissingPropertyException ex) {
return null;
}
return currentObject;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ void processDependencies() {
if (sourceSetNames != null) {
for (String name : sourceSetNames) {
Set<File> dirs = (Set<File>) info.get("sourceset_" + name + "_output_classes");
sourceSetOutputs.addAll(dirs);
sourceSetOutputs.addAll(dirs != null ? dirs : Collections.emptySet());
sourceSetOutputs.add((File) info.get("sourceset_" + name + "_output_resources"));
}
}
Expand Down

0 comments on commit 1eff472

Please sign in to comment.