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

Remove dependencies on indexer #250

Merged
merged 1 commit into from
Jan 19, 2022
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*******************************************************************************
* Copyright (c) 2022 Red Hat Inc. and others.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/
package org.eclipse.lemminx.extensions.maven.participants;
Copy link
Contributor

Choose a reason for hiding this comment

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

Missing license header.


import org.apache.maven.project.MavenProject;
import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.artifact.DefaultArtifact;

public class ArtifactWithDescription {

public final Artifact artifact;
public final String description;

public ArtifactWithDescription(MavenProject p) {
this.artifact = new DefaultArtifact(p.getGroupId(), p.getArtifactId(), null, p.getVersion());
this.description = p.getDescription();
}

public ArtifactWithDescription(Artifact a) {
this.artifact = a;
this.description = null;
}
}

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,20 @@
import static org.eclipse.lemminx.extensions.maven.DOMConstants.PLUGIN_ELT;
import static org.eclipse.lemminx.extensions.maven.DOMConstants.VERSION_ELT;

import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Optional;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.function.UnaryOperator;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;

import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
import org.apache.maven.index.ArtifactInfo;
import org.apache.maven.index.artifact.Gav;
import org.apache.maven.model.Dependency;
import org.apache.maven.model.building.ModelBuilder;
import org.apache.maven.model.building.ModelBuildingRequest;
Expand All @@ -51,6 +41,7 @@
import org.apache.maven.project.MavenProject;
import org.apache.maven.properties.internal.EnvironmentUtils;
import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.artifact.DefaultArtifact;
import org.eclipse.aether.impl.ArtifactResolver;
import org.eclipse.aether.resolution.ArtifactRequest;
Expand All @@ -61,7 +52,6 @@
import org.eclipse.lemminx.dom.DOMNode;
import org.eclipse.lemminx.extensions.maven.MavenLemminxExtension;
import org.eclipse.lemminx.extensions.maven.MojoParameter;
import org.eclipse.lemminx.extensions.maven.participants.completion.MavenCompletionParticipant;
import org.eclipse.lemminx.extensions.maven.utils.DOMUtils;
import org.eclipse.lemminx.extensions.maven.utils.MarkdownUtils;
import org.eclipse.lemminx.extensions.maven.utils.MavenParseUtils;
Expand All @@ -71,7 +61,6 @@
import org.eclipse.lemminx.services.extensions.IHoverRequest;
import org.eclipse.lemminx.services.extensions.IPositionRequest;
import org.eclipse.lemminx.utils.XMLPositionUtility;
import org.eclipse.lsp4j.CompletionItem;
import org.eclipse.lsp4j.Hover;
import org.eclipse.lsp4j.MarkupContent;
import org.eclipse.lsp4j.MarkupKind;
Expand Down Expand Up @@ -159,7 +148,6 @@ private Hover hoverForProject(boolean supportsMarkdown, MavenProject p) {

private Hover collectArtifactDescription(IHoverRequest request, boolean isPlugin) {
boolean supportsMarkdown = request.canSupportMarkupKind(MarkupKind.MARKDOWN);
Collection<String> possibleHovers = Collections.synchronizedSet(new LinkedHashSet<>());
DOMNode node = request.getNode();
DOMDocument doc = request.getXMLDocument();

Expand All @@ -175,7 +163,7 @@ private Hover collectArtifactDescription(IHoverRequest request, boolean isPlugin
|| artifactToSearch.getArtifactId().equals(gav.getArtifactId()))
&& (artifactToSearch.getVersion() == null
|| artifactToSearch.getVersion().equals(gav.getVersion())))
.sorted(Comparator.comparing((Gav gav) -> new DefaultArtifactVersion(gav.getVersion())).reversed())
.sorted(Comparator.comparing((Artifact artifact) -> new DefaultArtifactVersion(artifact.getVersion())).reversed())
.findFirst().map(plugin.getLocalRepositorySearcher()::findLocalFile).map(file -> builder
.buildRawModel(file, ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL, false).get())
.map(model -> {
Expand All @@ -199,38 +187,11 @@ private Hover collectArtifactDescription(IHoverRequest request, boolean isPlugin
localDescription.get()));
}
} catch (Exception e1) {
LOGGER.log(Level.SEVERE, e1.getCause().toString(), e1);
LOGGER.log(Level.SEVERE, e1.toString(), e1);
}

final String updatingItem = MavenCompletionParticipant.WAITING_LABEL;
possibleHovers.add(updatingItem);

plugin.getCentralSearcher().ifPresent(centralSearcher -> {
try {
CompletableFuture.runAsync(() -> {
if (isPlugin) {
// TODO: make a new function that gets only the exact artifact ID match, or just
// take the first thing given
centralSearcher.getPluginArtifacts(artifactToSearch).stream()
.map(ArtifactInfo::getDescription).filter(Objects::nonNull)
.forEach(possibleHovers::add);
} else {
centralSearcher.getArtifacts(artifactToSearch).stream()
.map(ArtifactInfo::getDescription).filter(Objects::nonNull)
.forEach(possibleHovers::add);
}
}).whenComplete((ok, error) -> possibleHovers.remove(updatingItem)).get(10, TimeUnit.SECONDS);
} catch (InterruptedException | ExecutionException exception) {
LOGGER.log(Level.SEVERE, exception.getCause() != null ? exception.getCause().toString() : exception.getMessage(), exception);
} catch (TimeoutException e) {
// nothing to log, some work still pending
}
});
if (possibleHovers.isEmpty()) {
return null;
}

return new Hover(new MarkupContent(MarkupKind.PLAINTEXT, possibleHovers.iterator().next()));
// we don't have description or other valuable information for non-local artifacts.
return null;
}

private Hover collectGoal(IPositionRequest request) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@

import org.apache.maven.artifact.versioning.ArtifactVersion;
import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
import org.apache.maven.index.artifact.Gav;
import org.apache.maven.model.Dependency;
import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.artifact.DefaultArtifact;

public class LocalRepositorySearcher {

Expand All @@ -43,24 +44,24 @@ public LocalRepositorySearcher(File localRepository) {
this.localRepository = localRepository;
}

private Map<File, Collection<Gav>> cache = new HashMap<>();
private Map<File, Collection<Artifact>> cache = new HashMap<>();
private WatchKey watchKey;
private WatchService watchService;

public Set<String> searchGroupIds() throws IOException {
return getLocalArtifactsLastVersion().stream().map(Gav::getGroupId).distinct().collect(Collectors.toSet());
return getLocalArtifactsLastVersion().stream().map(Artifact::getGroupId).distinct().collect(Collectors.toSet());
}

public Set<String> searchPluginGroupIds() throws IOException {
return getLocalPluginArtifacts().stream().map(Gav::getGroupId).distinct().collect(Collectors.toSet());
return getLocalPluginArtifacts().stream().map(Artifact::getGroupId).distinct().collect(Collectors.toSet());
}

public Collection<Gav> getLocalPluginArtifacts() throws IOException {
public Collection<Artifact> getLocalPluginArtifacts() throws IOException {
return getLocalArtifactsLastVersion().stream().filter(gav -> gav.getArtifactId().contains("-plugin")).collect(Collectors.toSet());
}

public Collection<Gav> getLocalArtifactsLastVersion() throws IOException {
Collection<Gav> res = cache.get(localRepository);
public Collection<Artifact> getLocalArtifactsLastVersion() throws IOException {
Collection<Artifact> res = cache.get(localRepository);
if (res == null) {
res = computeLocalArtifacts();
Path localRepoPath = localRepository.toPath();
Expand All @@ -86,9 +87,9 @@ public Collection<Gav> getLocalArtifactsLastVersion() throws IOException {
return res;
}

public Collection<Gav> computeLocalArtifacts() throws IOException {
public Collection<Artifact> computeLocalArtifacts() throws IOException {
final Path repoPath = localRepository.toPath();
Map<String, Gav> groupIdArtifactIdToVersion = new HashMap<>();
Map<String, Artifact> groupIdArtifactIdToVersion = new HashMap<>();
Files.walkFileTree(repoPath, Collections.emptySet(), 10, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(Path file, BasicFileAttributes attrs) throws IOException {
Expand All @@ -110,15 +111,15 @@ public FileVisitResult preVisitDirectory(Path file, BasicFileAttributes attrs) t
return FileVisitResult.SKIP_SUBTREE;
}
String groupIdArtifactId = groupId + ':' + artifactId;
Gav existingGav = groupIdArtifactIdToVersion.get(groupIdArtifactId);
Artifact existingGav = groupIdArtifactIdToVersion.get(groupIdArtifactId);
boolean replace = existingGav == null;
if (existingGav != null) {
ArtifactVersion existingVersion = new DefaultArtifactVersion(existingGav.getVersion());
replace |= existingVersion.compareTo(version) < 0;
replace |= (existingVersion.toString().endsWith("-SNAPSHOT") && !version.toString().endsWith("-SNAPSHOT"));
}
if (replace) {
groupIdArtifactIdToVersion.put(groupIdArtifactId, new Gav(groupId, artifactId, version.toString()));
groupIdArtifactIdToVersion.put(groupIdArtifactId, new DefaultArtifact(groupId, artifactId, null, version.toString()));
}
return FileVisitResult.SKIP_SUBTREE;
}
Expand All @@ -131,7 +132,7 @@ public FileVisitResult preVisitDirectory(Path file, BasicFileAttributes attrs) t
public File findLocalFile(Dependency dependency) {
return new File(localRepository, dependency.getGroupId().replace('.', File.separatorChar) + File.separatorChar + dependency.getArtifactId() + File.separatorChar + dependency.getVersion() + File.separatorChar + dependency.getArtifactId() + '-' + dependency.getVersion() + ".pom");
}
public File findLocalFile(Gav gav) {
public File findLocalFile(Artifact gav) {
return new File(localRepository, gav.getGroupId().replace('.', File.separatorChar) + File.separatorChar + gav.getArtifactId() + File.separatorChar + gav.getVersion() + File.separatorChar + gav.getArtifactId() + '-' + gav.getVersion() + ".pom");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@

import org.apache.maven.artifact.versioning.ArtifactVersion;
import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
import org.apache.maven.index.ArtifactInfo;
import org.apache.maven.model.Dependency;
import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.artifact.DefaultArtifact;
import org.eclipse.aether.repository.RemoteRepository;
import org.eclipse.lemminx.extensions.maven.MavenLemminxExtension;

Expand All @@ -46,7 +47,6 @@ public class RemoteCentralRepositorySearcher {
private static final String VERSION = "v";
private static final String LATEST_VERSION = "latestVersion";
private static final String PACKAGING = "p";
private static final String REPO_ID = "repositoryId";
private static final String SEPARATOR = ":";
private static final String PARAM_SEPARATOR = " AND ";
private static final String ASTERISK = "*";
Expand Down Expand Up @@ -85,7 +85,7 @@ public void closeContext() {
* {@code Map<String artifactId, String artifactDescription>}
* @return
*/
public Collection<ArtifactInfo> getArtifacts(Dependency artifactToSearch) {
public Collection<Artifact> getArtifacts(Dependency artifactToSearch) {
return disableCentralSearch ? Collections.emptySet() : internalGetArtifacts(artifactToSearch, PACKAGING_TYPE_JAR);
}

Expand All @@ -97,7 +97,7 @@ public Set<String> getGroupIds(Dependency artifactToSearch) {
return disableCentralSearch ? Collections.emptySet() : internalGetGroupIds(artifactToSearch, PACKAGING_TYPE_JAR);
}

public Collection<ArtifactInfo> getPluginArtifacts(Dependency artifactToSearch) {
public Collection<Artifact> getPluginArtifacts(Dependency artifactToSearch) {
return disableCentralSearch ? Collections.emptySet() : internalGetArtifacts(artifactToSearch, PACKAGING_TYPE_MAVEN_PLUGIN);
}

Expand All @@ -110,7 +110,7 @@ public Set<String> getPluginGroupIds(Dependency artifactToSearch) {
}


private Collection<ArtifactInfo> internalGetArtifacts(Dependency artifactToSearch, String packaging) {
private Collection<Artifact> internalGetArtifacts(Dependency artifactToSearch, String packaging) {
GetRequest request = createDefaultRequest();

Map<String, String> queryPatams = new HashMap<>();
Expand All @@ -125,10 +125,10 @@ private Collection<ArtifactInfo> internalGetArtifacts(Dependency artifactToSearc
}

final int[] count = {0};
List<ArtifactInfo> artifactInfos = new ArrayList<>();
List<Artifact> artifactInfos = new ArrayList<>();
responseBody.getJSONArray("docs").forEach(d -> {
count[0]++;
ArtifactInfo artifactInfo = toArtifactInfo((JSONObject)d);
Artifact artifactInfo = toArtifact((JSONObject)d);
artifactInfos.add(artifactInfo);
});

Expand Down Expand Up @@ -237,15 +237,12 @@ private JSONObject getResponseBody(GetRequest request, Dependency artifactToSear
return null;
}

private static final ArtifactInfo toArtifactInfo(JSONObject object) {
private static final Artifact toArtifact(JSONObject object) {
String g = object.has(GROUP_ID) ? object.getString(GROUP_ID) : null;
String a = object.has(ARTIFACT_ID) ? object.getString(ARTIFACT_ID) : null;
String v = object.has(LATEST_VERSION) ? object.getString(LATEST_VERSION) : null;
String p = object.has(PACKAGING) ? object.getString(PACKAGING) : null;
String r = object.has(REPO_ID) ? object.getString(REPO_ID) : null;
ArtifactInfo artifactInfo = new ArtifactInfo(r, g, a, v, null, null);
artifactInfo.setPackaging(p);
artifactInfo.setDescription(artifactInfo.toString()); // TODO: Currently description is not provided
Artifact artifactInfo = new DefaultArtifact(g, a, null, v);
return artifactInfo;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@
import java.util.logging.Logger;
import java.util.stream.Collectors;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
import org.apache.maven.index.artifact.Gav;
import org.apache.maven.model.Plugin;
import org.apache.maven.model.Repository;
import org.apache.maven.plugin.InvalidPluginDescriptorException;
Expand All @@ -40,6 +38,7 @@
import org.apache.maven.plugin.descriptor.Parameter;
import org.apache.maven.plugin.descriptor.PluginDescriptor;
import org.apache.maven.project.MavenProject;
import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.repository.RemoteRepository;
import org.eclipse.aether.repository.RemoteRepository.Builder;
import org.eclipse.lemminx.dom.DOMElement;
Expand Down Expand Up @@ -204,7 +203,7 @@ public static PluginDescriptor getContainingPluginDescriptor(IPositionRequest re
version = lemminxMavenPlugin.getLocalRepositorySearcher().getLocalArtifactsLastVersion().stream()
.filter(gav -> thePlugin.getArtifactId().equals(gav.getArtifactId()))
.filter(gav -> thePlugin.getGroupId().equals(gav.getGroupId()))
.map(Gav::getVersion) //
.map(Artifact::getVersion) //
.map(DefaultArtifactVersion::new) //
.collect(Collectors.maxBy(Comparator.naturalOrder()));
if (version.isPresent()) {
Expand All @@ -226,7 +225,7 @@ private static Plugin findPluginInProject(MavenProject project, String pluginKey
plugin = project.getPluginManagement().getPluginsAsMap().get(pluginKey);
}
if (plugin == null && artifactId.isPresent()) {
for (Entry<String, Artifact> entry : project.getPluginArtifactMap().entrySet()) {
for (Entry<String, org.apache.maven.artifact.Artifact> entry : project.getPluginArtifactMap().entrySet()) {
if (entry.getValue().getArtifactId().equals(artifactId.get())) {
plugin = project.getPlugin(entry.getKey());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
import java.util.Collection;
import java.util.Set;

import org.eclipse.lemminx.extensions.maven.searcher.RemoteCentralRepositorySearcher;
import org.apache.maven.artifact.versioning.ArtifactVersion;
import org.apache.maven.index.ArtifactInfo;
import org.apache.maven.model.Dependency;
import org.eclipse.aether.artifact.Artifact;
import org.eclipse.lemminx.extensions.maven.searcher.RemoteCentralRepositorySearcher;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

Expand All @@ -42,7 +42,7 @@ public void testGetArtifacts() {
dep.setVersion(V);

System.out.println("\nDep: " + dep.toString());
Collection<ArtifactInfo> artifactInfos = repoSearcher.getArtifacts(dep);
Collection<Artifact> artifactInfos = repoSearcher.getArtifacts(dep);
assertNotNull(artifactInfos);
assertFalse(artifactInfos.isEmpty());

Expand Down Expand Up @@ -127,7 +127,7 @@ public void testGetPlugiArtifacts() {
dep.setVersion(V);

System.out.println("\nDep: " + dep.toString());
Collection<ArtifactInfo> artifactInfos = repoSearcher.getPluginArtifacts(dep);
Collection<Artifact> artifactInfos = repoSearcher.getPluginArtifacts(dep);
assertNotNull(artifactInfos);
assertFalse(artifactInfos.isEmpty());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,24 +102,6 @@ public void testRemoteCentralVersionCompletion()
// if we get out of the loop, then it's OK; otherwise we get a timeout
}

@Test
// @Timeout(15000)
@Timeout(value = 15000, unit = TimeUnit.MILLISECONDS)
public void testRemoteCentralArtifactHover()
throws IOException, InterruptedException, ExecutionException, URISyntaxException {
// TODO: Currently Central REST API doesn't provide the description field,
// so, the hover is generated from the artifact info for testing purposes
String description = "org.fujion.webjars|webjar-angular";
final DOMDocument document = createDOMDocument("/pom-remote-central-artifact-hover.xml");
final Position position = new Position(14, 25);
Hover hover;
do {
hover = languageService.doHover(document, position, new SharedSettings());
Thread.sleep(500);
} while (!(((MarkupContent) hover.getContents().getRight()).getValue().contains(description)));
// if got out of the loop without timeout, then test is PASSED
}

@Test
@Timeout(value = 15000, unit = TimeUnit.MILLISECONDS)
public void testRemoteCentralPluginGroupIdCompletion()
Expand Down