PT 173730396 - Further support for version validation
Added further support to generate warning messages for a java project from spring project metadata.
This commit is contained in:
@@ -1,66 +0,0 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2020 Pivotal, Inc.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
* https://www.eclipse.org/legal/epl-v10.html
|
||||
*
|
||||
* Contributors:
|
||||
* Pivotal, Inc. - initial API and implementation
|
||||
*******************************************************************************/
|
||||
package org.springframework.ide.vscode.boot.validation.generations;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.ide.vscode.boot.validation.generations.json.Generations;
|
||||
import org.springframework.ide.vscode.boot.validation.generations.json.Link;
|
||||
import org.springframework.ide.vscode.boot.validation.generations.json.Links;
|
||||
import org.springframework.ide.vscode.boot.validation.generations.json.SpringProject;
|
||||
import org.springframework.ide.vscode.boot.validation.generations.json.SpringProjects;
|
||||
|
||||
public class SpringProjectIndex {
|
||||
|
||||
private final SpringProjectsClient client;
|
||||
|
||||
public SpringProjectIndex(SpringProjectsClient client) {
|
||||
this.client = client;
|
||||
}
|
||||
|
||||
public SpringProjects getProjects() throws Exception {
|
||||
return client.getSpringProjects();
|
||||
}
|
||||
|
||||
public Generations getGenerations(SpringProject project) throws Exception {
|
||||
if (project != null) {
|
||||
Links _links = project.get_links();
|
||||
if (_links != null) {
|
||||
Link genLink = _links.getGenerations();
|
||||
if (genLink != null) {
|
||||
return client.getGenerations(genLink.getHref());
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param projectSlug slug used by Spring projects metadata, for example
|
||||
* "spring-boot" for Spring Boot projects
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public SpringProject getProject(String projectSlug) throws Exception {
|
||||
SpringProjects springProjects = getProjects();
|
||||
List<SpringProject> projects = springProjects.getProjects();
|
||||
SpringProject prj = null;
|
||||
for (SpringProject project : projects) {
|
||||
if (project.getSlug().equals(projectSlug)) {
|
||||
prj = project;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return prj;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2020 Pivotal, Inc.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
* https://www.eclipse.org/legal/epl-v10.html
|
||||
*
|
||||
* Contributors:
|
||||
* Pivotal, Inc. - initial API and implementation
|
||||
*******************************************************************************/
|
||||
package org.springframework.ide.vscode.boot.validation.generations;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.ide.vscode.boot.validation.generations.json.Generations;
|
||||
import org.springframework.ide.vscode.boot.validation.generations.json.Link;
|
||||
import org.springframework.ide.vscode.boot.validation.generations.json.Links;
|
||||
import org.springframework.ide.vscode.boot.validation.generations.json.SpringProject;
|
||||
import org.springframework.ide.vscode.boot.validation.generations.json.SpringProjects;
|
||||
import org.springframework.ide.vscode.commons.languageserver.util.SimpleLanguageServer;
|
||||
import org.springframework.ide.vscode.commons.util.AsyncRunner;
|
||||
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.google.common.collect.ImmutableMap.Builder;
|
||||
|
||||
public class SpringProjectsCache {
|
||||
|
||||
private static final long TIMEOUT_SECS = 30;
|
||||
private final SpringProjectsClient client;
|
||||
private final SimpleLanguageServer server;
|
||||
private Map<String, SpringProject> cache;
|
||||
|
||||
public SpringProjectsCache(SpringProjectsClient client, SimpleLanguageServer server) {
|
||||
this.client = client;
|
||||
this.server = server;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param Project slug. E.g. "spring-boot"
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public SpringProject getProject(String slug) throws Exception {
|
||||
return cache().get(slug);
|
||||
}
|
||||
|
||||
public Generations getGenerations(SpringProject project) throws Exception {
|
||||
if (project != null) {
|
||||
Links _links = project.get_links();
|
||||
if (_links != null) {
|
||||
Link genLink = _links.getGenerations();
|
||||
if (genLink != null) {
|
||||
return client.getGenerations(genLink.getHref());
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private Map<String, SpringProject> cache() throws Exception {
|
||||
if (cache == null) {
|
||||
loadCache();
|
||||
}
|
||||
return cache != null ? cache : ImmutableMap.of();
|
||||
}
|
||||
|
||||
private void loadCache() throws Exception {
|
||||
AsyncRunner async = this.server.getAsync();
|
||||
if (async != null) {
|
||||
async.invoke(Duration.ofSeconds(TIMEOUT_SECS), () -> {
|
||||
SpringProjects springProjects = client.getSpringProjects();
|
||||
return asMap(springProjects);
|
||||
}).thenAccept((map) -> cache = map).get();
|
||||
}
|
||||
}
|
||||
|
||||
private Map<String, SpringProject> asMap(SpringProjects springProjects) {
|
||||
Builder<String, SpringProject> builder = ImmutableMap.builder();
|
||||
|
||||
if (springProjects != null) {
|
||||
List<SpringProject> projects = springProjects.getProjects();
|
||||
if (projects != null) {
|
||||
for (SpringProject project : projects) {
|
||||
builder.put(project.getSlug(), project);
|
||||
}
|
||||
}
|
||||
}
|
||||
return builder.build();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2020 Pivotal, Inc.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
* https://www.eclipse.org/legal/epl-v10.html
|
||||
*
|
||||
* Contributors:
|
||||
* Pivotal, Inc. - initial API and implementation
|
||||
*******************************************************************************/
|
||||
package org.springframework.ide.vscode.boot.validation.generations;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.ide.vscode.boot.validation.generations.json.Generation;
|
||||
import org.springframework.ide.vscode.boot.validation.generations.json.Generations;
|
||||
import org.springframework.ide.vscode.boot.validation.generations.json.SpringProject;
|
||||
import org.springframework.ide.vscode.commons.java.IJavaProject;
|
||||
import org.springframework.ide.vscode.commons.java.SpringProjectUtil;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
|
||||
public class SpringProjectsValidations {
|
||||
|
||||
private final SpringProjectsCache cache;
|
||||
|
||||
public SpringProjectsValidations(SpringProjectsCache cache) {
|
||||
this.cache = cache;
|
||||
}
|
||||
|
||||
public List<String> getVersionWarnings(IJavaProject jp) throws Exception {
|
||||
ImmutableList.Builder<String> messages = ImmutableList.builder();
|
||||
|
||||
if (jp != null) {
|
||||
List<File> librariesOnClasspath = SpringProjectUtil.getLibrariesOnClasspath(jp, "spring");
|
||||
if (librariesOnClasspath != null) {
|
||||
for (File file : librariesOnClasspath) {
|
||||
String fileName = file.getName();
|
||||
String slug = SpringProjectUtil.getProjectSlug(fileName);
|
||||
String majMin = SpringProjectUtil.getMajMinVersion(fileName);
|
||||
String fullVersion = SpringProjectUtil.getVersion(fileName);
|
||||
SpringProject springProject = cache.getProject(slug);
|
||||
if (springProject != null && majMin != null) {
|
||||
Generations generations = cache.getGenerations(springProject);
|
||||
if (generations != null) {
|
||||
List<Generation> gens = generations.getGenerations();
|
||||
if (gens != null) {
|
||||
Generation gen = null;
|
||||
for (Generation g : gens) {
|
||||
if (isInGeneration(majMin, g)) {
|
||||
gen = g;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (gen != null) {
|
||||
messages.add(getWarning(slug, fullVersion, gen));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return messages.build();
|
||||
}
|
||||
|
||||
private String getWarning(String slug, String version, Generation gen) {
|
||||
StringBuilder msg = new StringBuilder();
|
||||
msg.append("Using ");
|
||||
msg.append(slug);
|
||||
msg.append(" version: ");
|
||||
msg.append(version);
|
||||
msg.append(" - OSS support end date: ");
|
||||
msg.append(gen.getOssSupportEndDate());
|
||||
return msg.toString();
|
||||
}
|
||||
|
||||
private boolean isInGeneration(String version, Generation g) {
|
||||
return g.getName().contains(version);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user