From 4639a79c006bc65a855f950860c0da10c679e844 Mon Sep 17 00:00:00 2001 From: Adrian Malacoda Date: Wed, 11 Nov 2020 21:50:14 -0600 Subject: [PATCH] java: automatically download and install jdt.ls if not given on command line --- lsp_proxy/java.py | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/lsp_proxy/java.py b/lsp_proxy/java.py index 1c56ae8..5ea4733 100644 --- a/lsp_proxy/java.py +++ b/lsp_proxy/java.py @@ -1,11 +1,21 @@ import os import sys +import tarfile +from urllib import request + from .lsp import LspServer, command, get_data_home FILE_URI = "file://" +VERSION = "0.64.0" +DOWNLOADS_URI = "http://download.eclipse.org/jdtls/milestones/{version}/{file}" +LATEST = "latest.txt" + def locate_product_directory (jdt_ls_path): + if os.path.isdir(os.path.join(jdt_ls_path, "plugins")): + return jdt_ls_path + return os.path.join(jdt_ls_path, "org.eclipse.jdt.ls.product/target/repository") def locate_launcher_jar (jdt_ls_path): @@ -15,8 +25,37 @@ def locate_launcher_jar (jdt_ls_path): def locate_launcher_configuration (jdt_ls_path): return os.path.join(jdt_ls_path, "config_linux") +def log (message, *args, **kwargs): + print(message.format(*args, **kwargs), file=sys.stderr) + def download_jdt_ls (destination): - raise Exception("download_jdt_ls not implemented") + if not os.path.isdir(destination): + os.makedirs(destination) + + current_version = None + current_version_file = os.path.join(destination, LATEST) + if os.path.isfile(current_version_file): + with open(current_version_file) as f: + current_version = f.read().strip() + log("Currently downloaded version is {version}:", version=current_version) + + latest_version = None + with request.urlopen(DOWNLOADS_URI.format(version=VERSION, file=LATEST)) as f: + latest_version = f.read().decode().strip() + log("Latest available version is {version}:", version=latest_version) + + if not latest_version == current_version: + latest_uri = DOWNLOADS_URI.format(version=VERSION, file=latest_version) + log("Downloading latest version from: {uri}:", uri=latest_uri) + with request.urlopen(latest_uri) as f: + with tarfile.open(fileobj=f, mode="r|gz") as tar: + tar.extractall(destination) + print(tar) + + with open(current_version_file, "w") as cvf: + cvf.write(latest_version) + + return destination # TODO def locate_lombok ():