Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for multiple NetBeans Gradle Tooling #6829

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions extide/gradle/manifest.mf
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ AutoUpdate-Show-In-Client: true
OpenIDE-Module: org.netbeans.modules.gradle/2
OpenIDE-Module-Layer: org/netbeans/modules/gradle/layer.xml
OpenIDE-Module-Localizing-Bundle: org/netbeans/modules/gradle/Bundle.properties
OpenIDE-Module-Java-Dependencies: Java > 11
OpenIDE-Module-Specification-Version: 2.38
3 changes: 2 additions & 1 deletion extide/gradle/nbproject/project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@

file.reference.netbeans-gradle-tooling.jar=release/modules/gradle/netbeans-gradle-tooling.jar
is.autoload=true
javac.source=1.8
javac.source=11
javac.target=11
javac.compilerargs=-Xlint -Xlint:-serial
javadoc.arch=${basedir}/arch.xml
javadoc.apichanges=${basedir}/apichanges.xml
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
import org.netbeans.modules.gradle.spi.GradleFiles;
import org.netbeans.modules.gradle.spi.execute.GradleDistributionProvider;
import org.netbeans.modules.gradle.spi.execute.GradleJavaPlatformProvider;
import org.netbeans.modules.gradle.spi.loaders.GradlePluginProvider;
import org.netbeans.spi.project.ui.support.BuildExecutionSupport;
import org.openide.awt.StatusDisplayer;
import org.openide.execution.ExecutorTask;
Expand Down Expand Up @@ -205,7 +206,8 @@ public void statusChanged(org.gradle.tooling.events.ProgressEvent event) {

if (RunUtils.isAugmentedBuildEnabled(config.getProject())) {
augmented = new GradleCommandLine(cmd);
augmented.addParameter(GradleCommandLine.Parameter.INIT_SCRIPT, GradleDaemon.initScript());
augmented.addParameter(GradleCommandLine.Parameter.INIT_SCRIPT,
GradleDaemon.initScript(GradlePluginProvider.GradleRuntime.fromProject(config.getProject())));
}
GradleBaseProject gbp = GradleBaseProject.get(config.getProject());
augmented.configure(buildLauncher, gbp != null ? gbp.getRootDir() : null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import org.netbeans.modules.gradle.spi.GradleFiles;
import org.netbeans.modules.gradle.spi.GradleSettings;
import org.netbeans.modules.gradle.spi.ProjectInfoExtractor;
import org.netbeans.modules.gradle.spi.loaders.GradlePluginProvider.GradleRuntime;
import org.openide.util.Lookup;

/**
Expand Down Expand Up @@ -86,10 +87,10 @@ public NbGradleProject.Quality getAim() {
}
}

static GradleCommandLine injectNetBeansTooling(GradleCommandLine cmd) {
static GradleCommandLine injectNetBeansTooling(GradleCommandLine cmd, GradleRuntime rt) {
GradleCommandLine ret = new GradleCommandLine(cmd);
ret.setFlag(GradleCommandLine.Flag.CONFIGURE_ON_DEMAND, GradleSettings.getDefault().isConfigureOnDemand());
ret.addParameter(GradleCommandLine.Parameter.INIT_SCRIPT, GradleDaemon.initScript());
ret.addParameter(GradleCommandLine.Parameter.INIT_SCRIPT, GradleDaemon.initScript(rt));
ret.setStackTrace(GradleCommandLine.StackTrace.SHORT);
ret.addProjectProperty("nbSerializeCheck", "true");
return ret;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@
import org.netbeans.modules.gradle.api.NbGradleProject;
import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.nio.file.Files;
import java.util.List;
import java.nio.file.Path;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.netbeans.modules.gradle.spi.loaders.GradlePluginProvider;
import org.netbeans.modules.gradle.spi.loaders.GradlePluginProvider.GradleRuntime;

import org.openide.modules.InstalledFileLocator;
import org.openide.modules.Places;
import org.openide.util.Lookup;
import org.openide.util.RequestProcessor;

/**
Expand All @@ -52,22 +52,45 @@ public final class GradleDaemon {

private GradleDaemon() {}

public static String initScript() {
File initScript = Places.getCacheSubfile("gradle/nb-tooling.gradle"); //NOI18N
synchronized (initScriptReady) {
if (!initScriptReady.get()) {
File initTemplate = InstalledFileLocator.getDefault().locate(INIT_SCRIPT_NAME, NbGradleProject.CODENAME_BASE, false);
try (Stream<String> lines = Files.lines(initTemplate.toPath())) {
List<String> script = lines.map(line -> line.replace(PROP_TOOLING_JAR, TOOLING_JAR)).collect(Collectors.toList());
Files.write(initScript.toPath(), script);
initScriptReady.set(true);
} catch(IOException ex) {
// This one is unlikely
LOG.log(Level.WARNING, "Can't create NetBeans Gradle init script", ex); //NOI18N
// Let it pass trough. Gradle call will display some errors as well
public static String initScript(GradleRuntime rt) {
Path initScript = rt.rootDir.toPath().resolve(".gradle/nb-tooling.gradle"); //NOI18N
String onDisk = "";
try {
onDisk = Files.readString(initScript);
} catch (IOException ex) {}
String init = generateInitScript(rt);
if (!onDisk.equals(init)) {
try {
Files.createDirectories(initScript.getParent());
Files.writeString(initScript, init);
} catch (IOException ex) {}
}
return initScript.toString();
}

private static String generateInitScript(GradleRuntime rt) {
var providers = Lookup.getDefault().lookupAll(GradlePluginProvider.class);
try (var wr = new StringWriter()) {
wr.append("initscript {\n");
wr.append(" dependencies {\n");
for (GradlePluginProvider pvd : providers) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A suggestion: if a GradlePluginProvider had a method boolean accepts(GradleRuntime r), then the providers can be filtered centrally. In the current state, each of the methods (classpath, plugins) need to accept GradleRuntime and test for applicable version.
Alternatively (future-compatible), suggest to have just

@CheckForNull GradlePluginDescriptor getPlugins(GradleRuntime r);

public final class GradlePluginDescriptor {
    Collection<File> getClasspath(GradleRuntime runtime) {...}
    Collection<String> getPlugins(GradleRuntime runtime) {...}
}

which could eventually allow to compatibly add more information in the future, if needed.

for (File f : pvd.classpath(rt)) {
String fname = f.getAbsolutePath();
wr.append(" classpath files('").append(fname).append("')\n");
}
}
}
return initScript.getAbsolutePath();
wr.append(" }\n}\n\n");

wr.append("allprojects {\n");
for (GradlePluginProvider pvd : providers) {
for (var plugin : pvd.plugins(rt)) {
wr.append(" apply plugin: ").append(plugin).append("\n");
}
}
wr.append("}\n");

return wr.toString();
} catch (IOException ex) {}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.netbeans.modules.gradle.loaders;

import java.io.File;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import org.netbeans.modules.gradle.api.NbGradleProject;
import org.netbeans.modules.gradle.spi.loaders.GradlePluginProvider;
import org.openide.modules.InstalledFileLocator;
import org.openide.util.lookup.ServiceProvider;

/**
*
* @author lkishalmi
*/
@ServiceProvider(service = GradlePluginProvider.class)
public final class LegacyGradlePluginProvider implements GradlePluginProvider {

static final String TOOLING_JAR_NAME = "modules/gradle/netbeans-gradle-tooling.jar"; //NOI18N

private static final File TOOLING_JAR = InstalledFileLocator.getDefault().locate(TOOLING_JAR_NAME, NbGradleProject.CODENAME_BASE, false);

@Override
public Collection<File> classpath(GradleRuntime runtime) {
return Collections.singleton(TOOLING_JAR);
}

@Override
public Collection<String> plugins(GradleRuntime runtime) {
return List.of(
"org.netbeans.modules.gradle.tooling.NetBeansToolingPlugin",
"org.netbeans.modules.gradle.tooling.NetBeansRunSinglePlugin",
"org.netbeans.modules.gradle.tooling.NetBeansExplodedWarPlugin"
);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
import org.openide.util.NbBundle;

import static org.netbeans.modules.gradle.loaders.Bundle.*;
import org.netbeans.modules.gradle.spi.loaders.GradlePluginProvider;
import org.openide.filesystems.FileUtil;
import org.openide.util.Exceptions;
import org.openide.util.RequestProcessor;
Expand Down Expand Up @@ -150,7 +151,7 @@ private static GradleProject loadGradleProject(ReloadContext ctx, CancellationTo
GradleCommandLine cmd = new GradleCommandLine(RunUtils.getCompatibleGradleDistribution(ctx.project), ctx.cmd);
cmd.setFlag(GradleCommandLine.Flag.CONFIGURE_ON_DEMAND, GradleSettings.getDefault().isConfigureOnDemand());
cmd.setFlag(GradleCommandLine.Flag.CONFIGURATION_CACHE, GradleSettings.getDefault().getUseConfigCache());
cmd.addParameter(GradleCommandLine.Parameter.INIT_SCRIPT, GradleDaemon.initScript());
cmd.addParameter(GradleCommandLine.Parameter.INIT_SCRIPT, GradleDaemon.initScript(GradlePluginProvider.GradleRuntime.fromProject(ctx.project)));
cmd.setStackTrace(GradleCommandLine.StackTrace.SHORT);
cmd.addProjectProperty("nbSerializeCheck", "true");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.netbeans.modules.gradle.tooling.internal.ModelFetcher;
import static org.netbeans.modules.gradle.loaders.ModelCache.State.*;
import org.netbeans.modules.gradle.spi.GradleSettings;
import org.netbeans.modules.gradle.spi.loaders.GradlePluginProvider;
import org.openide.util.RequestProcessor;

/**
Expand Down Expand Up @@ -77,7 +78,7 @@ private void load() {
try {
List<String> filteredTargets = descriptor.getTargets().stream().filter((String target) -> descriptor.needsRefresh(target)).collect(Collectors.toList());
if (!filteredTargets.isEmpty()) {
final GradleCommandLine cmd = new GradleCommandLine(RunUtils.getCompatibleGradleDistribution(project), descriptor.gradleCommandLine());
final GradleCommandLine cmd = new GradleCommandLine(RunUtils.getCompatibleGradleDistribution(project), descriptor.gradleCommandLine(GradlePluginProvider.GradleRuntime.fromProject(project)));

cmd.setFlag(GradleCommandLine.Flag.CONFIGURE_ON_DEMAND, GradleSettings.getDefault().isConfigureOnDemand());
cmd.setFlag(GradleCommandLine.Flag.CONFIGURATION_CACHE, GradleSettings.getDefault().getUseConfigCache());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Set;
import org.gradle.tooling.model.Model;
import org.netbeans.modules.gradle.api.execute.GradleCommandLine;
import org.netbeans.modules.gradle.spi.loaders.GradlePluginProvider.GradleRuntime;

/**
*
Expand All @@ -32,7 +33,7 @@ public interface ModelCachingDescriptor <T extends Model> {

Set<String> getTargets();

GradleCommandLine gradleCommandLine();
GradleCommandLine gradleCommandLine(GradleRuntime rt);

void onLoad(String target, T model);
void onError(String target, Exception ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.netbeans.modules.gradle.spi.GradleFiles;

import static org.netbeans.modules.gradle.api.NbGradleProject.Quality.*;
import org.netbeans.modules.gradle.spi.loaders.GradlePluginProvider.GradleRuntime;

/**
*
Expand All @@ -52,8 +53,8 @@ public Set<String> getTargets() {
}

@Override
public GradleCommandLine gradleCommandLine() {
return AbstractProjectLoader.injectNetBeansTooling(new GradleCommandLine());
public GradleCommandLine gradleCommandLine(GradleRuntime rt) {
return AbstractProjectLoader.injectNetBeansTooling(new GradleCommandLine(), rt);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.netbeans.modules.gradle.api.execute.GradleCommandLine;
import org.netbeans.modules.gradle.cache.SubProjectDiskCache;
import org.netbeans.modules.gradle.cache.SubProjectDiskCache.SubProjectInfo;
import org.netbeans.modules.gradle.spi.loaders.GradlePluginProvider.GradleRuntime;

/**
*
Expand All @@ -50,7 +51,7 @@ public Set<String> getTargets() {
}

@Override
public GradleCommandLine gradleCommandLine() {
public GradleCommandLine gradleCommandLine(GradleRuntime rt) {
return new GradleCommandLine();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.netbeans.modules.gradle.spi.loaders;

import java.io.File;
import java.util.Collection;
import org.netbeans.api.project.Project;
import org.netbeans.api.project.ProjectUtils;
import org.netbeans.modules.gradle.api.execute.GradleDistributionManager.GradleDistribution;
import org.netbeans.modules.gradle.spi.execute.GradleDistributionProvider;
import org.openide.filesystems.FileUtil;

/**
*
* @author lkishalmi
*/
public interface GradlePluginProvider {

Collection<File> classpath(GradleRuntime runtime);
Collection<String> plugins(GradleRuntime runtime);

public final static class GradleRuntime {
public final File rootDir;
public final GradleDistribution gradleDistribution;

private GradleRuntime(File rootDir, GradleDistribution dist) {
this.rootDir = rootDir;
this.gradleDistribution = dist;
}

public File rootDir() {
return rootDir;
}

public GradleDistribution gradleDistribution() {
return gradleDistribution;
}

public static GradleRuntime fromProject(Project p) {
Project root = ProjectUtils.rootOf(p);
File rootDir = FileUtil.toFile(root.getProjectDirectory());
GradleDistributionProvider pvd = root.getLookup().lookup(GradleDistributionProvider.class);
return new GradleRuntime(rootDir, pvd.getGradleDistribution());
}
}
}
Loading