Skip to content

Commit

Permalink
Schema cache directory should be configurable
Browse files Browse the repository at this point in the history
Fixes eclipse-lemminx#222

Signed-off-by: Nikolas Komonen <[email protected]>
  • Loading branch information
NikolasKomonen committed Apr 5, 2019
1 parent 9f2d414 commit 7a484a1
Show file tree
Hide file tree
Showing 7 changed files with 246 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
import static org.eclipse.lsp4j.jsonrpc.CompletableFutures.computeAsync;
import static org.eclipse.lsp4xml.utils.VersionHelper.getVersion;

import java.io.File;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
Expand Down Expand Up @@ -44,11 +50,13 @@
import org.eclipse.lsp4xml.settings.AllXMLSettings;
import org.eclipse.lsp4xml.settings.InitializationOptionsSettings;
import org.eclipse.lsp4xml.settings.LogsSettings;
import org.eclipse.lsp4xml.settings.ServerSettings;
import org.eclipse.lsp4xml.settings.XMLGeneralClientSettings;
import org.eclipse.lsp4xml.settings.XMLExperimentalCapabilities;
import org.eclipse.lsp4xml.settings.XMLFormattingOptions;
import org.eclipse.lsp4xml.settings.capabilities.ServerCapabilitiesInitializer;
import org.eclipse.lsp4xml.settings.capabilities.XMLCapabilityManager;
import org.eclipse.lsp4xml.utils.FilesUtils;

/**
* XML language server.
Expand Down Expand Up @@ -135,6 +143,12 @@ public void updateSettings(Object initializationOptionsSettings) {
if (newCompletions != null) {
xmlTextDocumentService.updateCompletionSettings(newCompletions);
}

ServerSettings serverSettings = xmlClientSettings.getServer();
if(serverSettings != null) {
String workDir = serverSettings.getNormalizedWorkDir();
FilesUtils.setCachePathSetting(workDir);
}

// Experimental capabilities
XMLExperimentalCapabilities experimental = xmlClientSettings.getExperimental();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*******************************************************************************
* Copyright (c) 2019 Red Hat Inc. and others.
* All rights reserved. This program and the accompanying materials
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* Red Hat Inc. - initial API and implementation
*******************************************************************************/

package org.eclipse.lsp4xml.settings;

import org.eclipse.lsp4xml.utils.FilesUtils;

/**
* ServerSettings
*/
public class ServerSettings {

private String workDir;


/**
* @return the workDir
*/
public String getWorkDir() {
return workDir;
}

/**
* @param workDir the workDir to set
*/
public void setWorkDir(String workDir) {
this.workDir = workDir;
}

public String getNormalizedWorkDir() {
return FilesUtils.normalizePath(workDir);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public class XMLGeneralClientSettings {

private CompletionSettings completion;

private ServerSettings server;

public void setLogs(LogsSettings logs) {
this.logs = logs;
}
Expand Down Expand Up @@ -78,6 +80,20 @@ public CompletionSettings getCompletion() {
return completion;
}

/**
* @return the server
*/
public ServerSettings getServer() {
return server;
}

/**
* @param server the server to set
*/
public void setServer(ServerSettings server) {
this.server = server;
}

public static XMLGeneralClientSettings getGeneralXMLSettings(Object initializationOptionsSettings) {
return JSONUtility.toModel(initializationOptionsSettings, XMLGeneralClientSettings.class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Scanner;
import java.util.regex.Pattern;

import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;
Expand All @@ -29,6 +30,21 @@
public class FilesUtils {

public static final String LSP4XML_WORKDIR_KEY = "lsp4xml.workdir";
private static String cachePathSetting = null;

public static String getCachePathSetting() {
return cachePathSetting;
}

public static void setCachePathSetting(String cachePathSetting) {
if(StringUtils.isEmpty(cachePathSetting)) {
FilesUtils.cachePathSetting = null;
}
else {
FilesUtils.cachePathSetting = cachePathSetting;
}
resetDeployPath();
}

private FilesUtils() {
}
Expand All @@ -44,11 +60,28 @@ public static void resetDeployPath() {
DEPLOYED_BASE_PATH = Suppliers.memoize(() -> getDeployedBasePath());
}

/**
* Given a file path as a string, will normalize it
* and return the normalized string if valid, or null if not.
*/
public static String normalizePath(String pathString) {
if(pathString != null && !pathString.isEmpty()) {
Path p = Paths.get(pathString);
pathString = p.normalize().toString();
pathString = pathString.replaceFirst("^~", System.getProperty("user.home"));
return pathString;
}
return null;
}

private static Path getDeployedBasePath() {
String dir = System.getProperty(LSP4XML_WORKDIR_KEY);
if (dir != null) {
return Paths.get(dir);
}
if(cachePathSetting != null && !cachePathSetting.isEmpty()) {
return Paths.get(cachePathSetting);
}
dir = System.getProperty("user.home");
if (dir == null) {
dir = System.getProperty("user.dir");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,18 @@

import org.eclipse.lsp4j.FormattingOptions;
import org.eclipse.lsp4j.InitializeParams;
import org.eclipse.lsp4xml.XMLLanguageServer;
import org.eclipse.lsp4xml.extensions.contentmodel.settings.ContentModelSettings;
import org.eclipse.lsp4xml.utils.FilesUtils;
import org.junit.After;
import org.junit.Assert;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import java.io.File;

import com.google.gson.Gson;
import com.google.gson.JsonObject;

Expand All @@ -24,43 +32,56 @@
*/
public class SettingsTest {

private static String testFolder = "TestXMLCacheFolder";
private static String targetTestFolder = "target/generated-test-sources";


@After
public void cleanup() {
String path = System.getProperty("user.dir") + "/" + targetTestFolder + "/" + testFolder;

File f = new File(path);
if (f.exists()) {
f.delete();
}
}

private final String json = "{\r\n" + //
" \"settings\": {\r\n" + //
// Content model settings
" \"xml\": {\r\n" + " \"fileAssociations\": [\r\n" + //
" {\r\n" + //
" \"systemId\": \"src\\\\test\\\\resources\\\\xsd\\\\spring-beans-3.0.xsd\",\r\n" + //
" \"pattern\": \"**/test*.xml\"\r\n" + //
" },\r\n" + //
" {\r\n" + //
" \"systemId\": \"src\\\\test\\\\resources\\\\xsd\\\\projectDescription.xsd\",\r\n" + //
" \"pattern\": \"projectDescription.xml\"\r\n" + //
" }\r\n" + //
" ],\r\n" + //
" \"catalogs\": [\r\n" + //
" \"src\\\\test\\\\resources\\\\catalogs\\\\catalog.xml\"\r\n" + //
" ],\r\n" + //
" \"validation\": {\r\n" + //
" \"enabled\": true,\r\n" + //
" \"schema\": false\r\n" + //
" },\r\n" + //
// Client (commons) settings
" \"format\": {\r\n" + //
" \"tabSize\": 10,\r\n" + //
" \"insertSpaces\": false,\r\n" + //
" \"splitAttributes\": true,\r\n" + //
" \"joinCDATALines\": true,\r\n" + //
" \"formatComments\": true,\r\n" + //
" \"joinCommentLines\": true,\r\n" + //
" \"quotations\": " + XMLFormattingOptions.DOUBLE_QUOTES_VALUE + "\r\n" + //
" },\r\n" + " \"server\": {\r\n" + //
" \"workDir\": \"~/" + testFolder + "/Nested\"\r\n" + //
" }\r\n" + " }\r\n" + " }\r\n" + "}";

@Test
public void initializationOptionsSettings() {
String json =
"{\r\n" + //
" \"settings\": {\r\n" + //
// Content model settings
" \"xml\": {\r\n" +
" \"fileAssociations\": [\r\n" + //
" {\r\n" + //
" \"systemId\": \"src\\\\test\\\\resources\\\\xsd\\\\spring-beans-3.0.xsd\",\r\n" + //
" \"pattern\": \"**/test*.xml\"\r\n" + //
" },\r\n" + //
" {\r\n" + //
" \"systemId\": \"src\\\\test\\\\resources\\\\xsd\\\\projectDescription.xsd\",\r\n" + //
" \"pattern\": \"projectDescription.xml\"\r\n" + //
" }\r\n" + //
" ],\r\n" + //
" \"catalogs\": [\r\n" + //
" \"src\\\\test\\\\resources\\\\catalogs\\\\catalog.xml\"\r\n" + //
" ],\r\n" + //
" \"validation\": {\r\n" + //
" \"enabled\": true,\r\n" + //
" \"schema\": false\r\n" + //
" },\r\n" + //
// Client (commons) settings
" \"format\": {\r\n" + //
" \"tabSize\": 10,\r\n" + //
" \"insertSpaces\": false,\r\n" + //
" \"splitAttributes\": true,\r\n" + //
" \"joinCDATALines\": true,\r\n" + //
" \"formatComments\": true,\r\n" + //
" \"joinCommentLines\": true,\r\n" + //
" \"quotations\": " + XMLFormattingOptions.DOUBLE_QUOTES_VALUE + "\r\n" + //
" }\r\n" +
" }\r\n" +
" }\r\n" +
"}";

// Emulate InitializeParams#getInitializationOptions() object created as
// JSONObject when XMLLanguageServer#initialize(InitializeParams params) is
// called
Expand All @@ -71,6 +92,8 @@ public void initializationOptionsSettings() {
initializationOptionsSettings = AllXMLSettings.getAllXMLSettings(initializationOptionsSettings);
XMLGeneralClientSettings settings = XMLGeneralClientSettings.getGeneralXMLSettings(initializationOptionsSettings);
Assert.assertNotNull(settings);
// Server
Assert.assertEquals("~/" + testFolder + "/Nested", settings.getServer().getWorkDir());

// Test content model extension settings
ContentModelSettings cmSettings = ContentModelSettings.getContentModelXMLSettings(initializationOptionsSettings);
Expand All @@ -85,7 +108,7 @@ public void initializationOptionsSettings() {
Assert.assertEquals("src\\test\\resources\\xsd\\spring-beans-3.0.xsd",
cmSettings.getFileAssociations()[0].getSystemId());
Assert.assertEquals("**/test*.xml", cmSettings.getFileAssociations()[0].getPattern());
//Diagnostics
// Diagnostics
Assert.assertNotNull(cmSettings.getValidation());
Assert.assertEquals(true, cmSettings.getValidation().isEnabled());
Assert.assertEquals(false, cmSettings.getValidation().isSchema());
Expand All @@ -110,18 +133,16 @@ public void formatSettings() {
FormattingOptions formattingOptions = new FormattingOptions();
formattingOptions.setTabSize(5);
formattingOptions.setInsertSpaces(false);



XMLFormattingOptions xmlFormattingOptions = new XMLFormattingOptions(formattingOptions);

xmlFormattingOptions.setQuotations("InvalidValue"); // set a value that is not recognized

Assert.assertEquals(5, xmlFormattingOptions.getTabSize()); // value coming from the request formattingOptions
Assert.assertFalse(xmlFormattingOptions.isInsertSpaces()); // formattingOptions doesn't defines insert spaces
// flag
// flag

Assert.assertFalse(xmlFormattingOptions.isJoinCommentLines());//Since default for JoinCommentLines is False
Assert.assertFalse(xmlFormattingOptions.isJoinCommentLines());// Since default for JoinCommentLines is False

// Assumes the "InvalidValue" will be overridden
Assert.assertTrue(xmlFormattingOptions.getQuotations().equals(XMLFormattingOptions.DOUBLE_QUOTES_VALUE));
Expand All @@ -130,12 +151,44 @@ public void formatSettings() {
// the InitializeParams
xmlFormattingOptions.merge(sharedXMLFormattingOptions);
Assert.assertEquals(5, xmlFormattingOptions.getTabSize()); // tab size is kept as 5 (and not updated with
// shared value 10), because only the request's
// formattingOptions object is allowed to define it.
Assert.assertFalse(xmlFormattingOptions.isInsertSpaces()); // insert spaces is kept as false because only the request's
// formattingOptions object is allowed to define it.
// shared value 10), because only the request's
// formattingOptions object is allowed to define it.
Assert.assertFalse(xmlFormattingOptions.isInsertSpaces()); // insert spaces is kept as false because only the
// request's
// formattingOptions object is allowed to define it.
Assert.assertTrue(xmlFormattingOptions.isJoinCommentLines());

Assert.assertTrue(xmlFormattingOptions.getQuotations().equals(XMLFormattingOptions.SINGLE_QUOTES_VALUE));
}

@Test
public void cachePathSettings() {
// Emulate InitializeParams#getInitializationOptions() object created as
// JSONObject when XMLLanguageServer#initialize(InitializeParams params) is
// called

InitializeParams params = createInitializeParams(json);
Object initializationOptionsSettings = InitializationOptionsSettings.getSettings(params);
XMLLanguageServer languageServer = new XMLLanguageServer();

String originalUserHome = System.getProperty("user.home");
String userDir = System.getProperty("user.dir");
try {
System.setProperty("user.home", userDir + "/" + targetTestFolder); // .../org.eclipse.lsp4xml/target/generated-test-sources/

languageServer.updateSettings(initializationOptionsSettings);

//Ensure the expanded absolute path is being used.
Assert.assertEquals(System.getProperty("user.home") + "/" + testFolder + "/Nested", FilesUtils.getCachePathSetting());
} catch (Exception e) {
fail();
} finally {
//Reset static cache path
FilesUtils.setCachePathSetting(null);
System.setProperty("user.home", originalUserHome);
}



}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ public void testAvailableCache() throws Exception {
} catch (CacheResourceDownloadingException ignored) {
}
TimeUnit.MILLISECONDS.sleep(200);

assertNotNull(cacheResourcesManager.getResource(uri));

server.stop();
Expand All @@ -108,5 +107,4 @@ public void testAvailableCache() throws Exception {
private Cache<String, Boolean> testingCache() {
return CacheBuilder.newBuilder().expireAfterWrite(1, TimeUnit.SECONDS).maximumSize(1).build();
}

}
Loading

0 comments on commit 7a484a1

Please sign in to comment.