Skip to content

Commit

Permalink
[NETBEANS-3600] Added Gradle wrapper distribution change detection.
Browse files Browse the repository at this point in the history
  • Loading branch information
lkishalmi committed Sep 20, 2020
1 parent c62fb59 commit 5a2ad9e
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ private enum GoOnline { NEVER, ON_DEMAND, ALWAYS }
private static final Map<File, Set<File>> SUB_PROJECT_DIR_CACHE = new ConcurrentHashMap<>();

// Increase this number if new info is gathered from the projects.
private static final int COMPATIBLE_CACHE_VERSION = 13;
private static final int COMPATIBLE_CACHE_VERSION = 14;

private GradleProjectCache() {
}
Expand Down Expand Up @@ -446,8 +446,9 @@ private static void saveCachedProjectInfo(NbProjectInfo data, GradleProject gp)
assert gp.getQuality().betterThan(FALLBACK) : "Never attempt to cache FALLBACK projects."; //NOi18N
//TODO: Make it possible to handle external file set as cache.
GradleFiles gf = new GradleFiles(gp.getBaseProject().getProjectDir(), true);

ProjectCacheEntry entry = new ProjectCacheEntry(new StoredProjectInfo(data), gp, gf.getProjectFiles());
Set<File> cacheInvalidators = new HashSet<>(gf.getProjectFiles());
if (gf.hasWrapper()) cacheInvalidators.add(gf.getWrapperProperties());
ProjectCacheEntry entry = new ProjectCacheEntry(new StoredProjectInfo(data), gp, cacheInvalidators);
File cacheFile = new File(getCacheDir(gp), INFO_CACHE_FILE_NAME);
if (!cacheFile.exists()) {
cacheFile.getParentFile().mkdirs();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
import org.netbeans.api.progress.ProgressHandle;
import org.netbeans.api.progress.ProgressHandleFactory;
import org.netbeans.modules.gradle.api.NbGradleProject;
import org.netbeans.modules.gradle.spi.GradleFiles;
import org.openide.awt.Notification;
import org.openide.awt.NotificationDisplayer;
import org.openide.util.Exceptions;
Expand Down Expand Up @@ -201,7 +202,31 @@ public GradleDistribution distributionFromVersion(String version) {
* the Gradle distribution cannot be determined form it.
*/
public GradleDistribution distributionFromWrapper(File gradleProjectRoot) throws IOException, URISyntaxException {
File wrapperProps = new File(gradleProjectRoot, "gradle/wrapper/gradle-wrapper.properties"); //NOI18N
URI uri = getWrapperDistributionURI(gradleProjectRoot);
Matcher m = DIST_VERSION_PATTERN.matcher(uri.getPath());
if (m.matches()) {
String version = m.group(1);
return new GradleDistribution(distributionBaseDir(uri, version), uri, version);
} else {
throw new URISyntaxException(uri.getPath(), "Cannot get the Gradle distribution version from the URI"); //NOI18N
}
}

/**
* Retrieves a normalized URI for the Gradle Wrapper distribution for the
* given root project directory.
*
* @param rootDir the root project directory
* @return the normalized URI of the Gradle wrapper distribution.
* @throws IOException if there is no <code>gradle-wrapper.properties</code>
* or it cannot be read.
* @throws URISyntaxException if the <code>distributionUrl</code> is missing
* or cannot be resolved to a valid URI.
*/
public static URI getWrapperDistributionURI(File rootDir) throws IOException, URISyntaxException {
URI ret;

File wrapperProps = new File(rootDir, GradleFiles.WRAPPER_PROPERTIES);
if (wrapperProps.isFile() && wrapperProps.canRead()) {
Properties wrapper = new Properties();
try (FileInputStream is = new FileInputStream(wrapperProps)) {
Expand All @@ -211,20 +236,17 @@ public GradleDistribution distributionFromWrapper(File gradleProjectRoot) throws
}
String distUrlProp = wrapper.getProperty("distributionUrl"); //NOI18N
if (distUrlProp != null) {
URI uri = new URI(distUrlProp);
Matcher m = DIST_VERSION_PATTERN.matcher(distUrlProp);
if (m.matches()) {
String version = m.group(1);
return new GradleDistribution(distributionBaseDir(uri, version), uri, version);
} else {
throw new URISyntaxException(distUrlProp, "Cannot get the Gradle distribution version from the URI"); //NOI18N
ret = new URI(distUrlProp);
if (ret.getScheme() == null) {
ret = wrapperProps.getParentFile().toPath().resolve(distUrlProp).normalize().toUri();
}
} else {
throw new URISyntaxException("", "No distributionUrl property found in: " + wrapperProps.getAbsolutePath()); //NOI18N
}
} else {
throw new FileNotFoundException("Gradle Wrapper properties not found at: " + wrapperProps.getAbsolutePath()); //NOI18N
}
return ret;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,42 @@
*/
package org.netbeans.modules.gradle.execute;

import java.beans.PropertyChangeListener;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.prefs.PreferenceChangeEvent;
import java.util.prefs.PreferenceChangeListener;
import javax.swing.event.ChangeListener;
import org.gradle.internal.impldep.com.google.common.base.Objects;
import org.netbeans.api.project.Project;
import org.netbeans.modules.gradle.api.GradleBaseProject;
import org.netbeans.modules.gradle.api.NbGradleProject;
import org.netbeans.modules.gradle.api.execute.GradleDistributionManager;
import org.netbeans.modules.gradle.api.execute.GradleDistributionManager.GradleDistribution;
import org.netbeans.modules.gradle.spi.GradleFiles;
import org.netbeans.modules.gradle.spi.GradleSettings;
import org.netbeans.modules.gradle.spi.execute.GradleDistributionProvider;
import org.netbeans.spi.project.ProjectServiceProvider;
import org.openide.util.ChangeSupport;

import static org.netbeans.modules.gradle.spi.GradleSettings.*;
import org.netbeans.modules.gradle.spi.WatchedResourceProvider;
import org.openide.util.WeakListeners;

/**
*
* @author lkishalmi
*/
@ProjectServiceProvider(service = GradleDistributionProvider.class, projectType = NbGradleProject.GRADLE_PROJECT_TYPE)
public class GradleDistributionProviderImpl implements GradleDistributionProvider {
@ProjectServiceProvider(service = {GradleDistributionProvider.class, WatchedResourceProvider.class}, projectType = NbGradleProject.GRADLE_PROJECT_TYPE)
public class GradleDistributionProviderImpl implements GradleDistributionProvider, WatchedResourceProvider {

private final static Logger LOGGER = Logger.getLogger(GradleDistributionProviderImpl.class.getName());

Expand All @@ -59,16 +68,31 @@ public class GradleDistributionProviderImpl implements GradleDistributionProvide
private final ChangeSupport support = new ChangeSupport(this);
private final PreferenceChangeListener listener = (PreferenceChangeEvent evt) -> {
if (AFFECTING_PROPS.contains(evt.getKey())) {
dist = null;
support.fireChange();
distributionChanged();
}
};

final Project project;
private GradleDistribution dist;
private PropertyChangeListener pcl;
private URI distributionURI;

public GradleDistributionProviderImpl(Project project) {
this.project = project;
pcl = (evt) -> {
if (NbGradleProject.PROP_RESOURCES.endsWith(evt.getPropertyName())) {
URI uri = (URI) evt.getNewValue();
if ((uri != null) && (uri.getPath() != null) && uri.getPath().endsWith(GradleFiles.WRAPPER_PROPERTIES)) {
URI newDistURI = getWrapperDistributionURI();
if (GradleSettings.getDefault().isWrapperPreferred() && ! Objects.equal(distributionURI, newDistURI)) {
distributionURI = newDistURI;
distributionChanged();
}
}
}
};
distributionURI = getWrapperDistributionURI();
NbGradleProject.addPropertyChangeListener(project, WeakListeners.propertyChange(pcl, project));
}

@Override
Expand Down Expand Up @@ -103,6 +127,23 @@ public GradleDistribution getGradleDistribution() {
return dist;
}

private void distributionChanged() {
dist = null;
support.fireChange();
NbGradleProject.fireGradleProjectReload(project);
}

private URI getWrapperDistributionURI() {
URI ret = null;
try {
GradleBaseProject gbp = GradleBaseProject.get(project);
if (gbp != null) {
ret = GradleDistributionManager.getWrapperDistributionURI(gbp.getRootDir());
}
} catch (IOException | URISyntaxException ex) {}
return ret;
}

@Override
public void addChangeListener(ChangeListener l) {
if (!support.hasListeners()) {
Expand All @@ -119,4 +160,10 @@ public void removeChangeListener(ChangeListener l) {
}
}

@Override
public Set<File> getWatchedResources() {
GradleBaseProject gbp = GradleBaseProject.get(project);
return gbp != null ? Collections.singleton(new File(gbp.getRootDir(), GradleFiles.WRAPPER_PROPERTIES)) : Collections.emptySet();
}

}

0 comments on commit 5a2ad9e

Please sign in to comment.