-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathversion_manager.py
More file actions
93 lines (78 loc) · 3.11 KB
/
version_manager.py
File metadata and controls
93 lines (78 loc) · 3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
from __future__ import annotations
from .constants import PACKAGE_NAME
from .constants import PLATFORM_ARCH
from .log import log_info
from .utils import decompress_buffer
from .utils import rmtree_ex
from .utils import simple_urlopen
from LSP.plugin import AbstractPlugin
import io
import pathlib
class VersionManager:
# https://github.com/astral-sh/ty
DOWNLOAD_URL_TEMPLATE = "https://github.com/astral-sh/ty/releases/download/{version}/{tarball_name}"
PLATFORM_TARBALLS: dict[str, tuple[str, str]] = {
# platform_arch: (tarball_name, relative_bin_path_in_tarball)
"linux_arm64": (
"ty-aarch64-unknown-linux-gnu.tar.gz",
"ty-aarch64-unknown-linux-gnu/ty",
),
"linux_x64": (
"ty-x86_64-unknown-linux-gnu.tar.gz",
"ty-x86_64-unknown-linux-gnu/ty",
),
"osx_arm64": (
"ty-aarch64-apple-darwin.tar.gz",
"ty-aarch64-apple-darwin/ty",
),
"osx_x64": (
"ty-x86_64-apple-darwin.tar.gz",
"ty-x86_64-apple-darwin/ty",
),
"windows_x64": (
"ty-x86_64-pc-windows-msvc.zip",
"ty.exe",
),
"windows_x86": (
"ty-i686-pc-windows-msvc.zip",
"ty.exe",
),
}
THIS_TARBALL_NAME, THIS_TARBALL_BIN_PATH = PLATFORM_TARBALLS[PLATFORM_ARCH]
def __init__(self) -> None:
self.client_cls: type[AbstractPlugin] | None = None
self.server_version = ""
@property
def server_download_url(self) -> str:
"""The URL for downloading the server tarball."""
return self.DOWNLOAD_URL_TEMPLATE.format(tarball_name=self.THIS_TARBALL_NAME, version=self.server_version)
@property
def plugin_storage_dir(self) -> pathlib.Path:
"""The storage directory for this plugin."""
assert self.client_cls, "VersionManager.client_cls must be set to a subclass of Abstract"
return pathlib.Path(self.client_cls.storage_path()) / PACKAGE_NAME
@property
def versioned_server_dir(self) -> pathlib.Path:
"""The directory specific to the current server version."""
return self.plugin_storage_dir / f"v{self.server_version}"
@property
def server_path(self) -> pathlib.Path:
"""The path of the language server binary."""
return self.versioned_server_dir / self.THIS_TARBALL_BIN_PATH
@property
def is_installed(self) -> bool:
"""Checks if the server executable is already installed."""
return self.server_path.is_file()
def install_server(self) -> None:
"""Installs the server executable."""
rmtree_ex(self.plugin_storage_dir, ignore_errors=True) # delete old versions
log_info(f"Downloading server tarball: {self.server_download_url}")
data = simple_urlopen(self.server_download_url)
decompress_buffer(
io.BytesIO(data),
filename=self.THIS_TARBALL_NAME,
dst_dir=self.versioned_server_dir,
)
# make the server binary executable (required on Mac/Linux)
self.server_path.chmod(0o755)
version_manager = VersionManager()