-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplugin.py
101 lines (76 loc) · 2.68 KB
/
plugin.py
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
94
95
96
97
98
99
100
101
import os
import shutil
import urllib.request
import zipfile
import sublime
# LSP
from LSP.plugin import AbstractPlugin, register_plugin, unregister_plugin
SESSION_NAME = "vale-ls"
# Update this single git tag to download a newer version.
#
# After changing this tag, go through the server settings again to see if any
# new server settings are added or old ones removed.
TAG = "v0.3.8"
URL = "https://github.com/errata-ai/vale-ls/releases/download/{tag}/vale-ls-{arch}-{platform}.zip"
def arch() -> str:
if sublime.arch() == "x64":
return "x86_64"
elif sublime.arch() == "x32":
raise RuntimeError("Unsupported platform: 32-bit is not supported")
elif sublime.arch() == "arm64":
return "aarch64"
else:
raise RuntimeError("Unknown architecture: " + sublime.arch())
def platform() -> str:
if sublime.platform() == "windows":
return "pc-windows-gnu"
elif sublime.platform() == "osx":
return "apple-darwin"
else:
return "unknown-linux-gnu"
class ValeLS(AbstractPlugin):
@classmethod
def name(cls) -> str:
return SESSION_NAME
@classmethod
def basedir(cls) -> str:
return os.path.join(cls.storage_path(), __package__)
@classmethod
def server_version(cls) -> str:
return TAG
@classmethod
def current_server_version(cls) -> str:
with open(os.path.join(cls.basedir(), "VERSION"), "r") as fp:
return fp.read()
@classmethod
def needs_update_or_installation(cls) -> bool:
try:
return cls.server_version() != cls.current_server_version()
except OSError:
return True
@classmethod
def install_or_update(cls) -> None:
try:
if os.path.isdir(cls.basedir()):
shutil.rmtree(cls.basedir())
os.makedirs(cls.basedir(), exist_ok=True)
version = cls.server_version()
url = URL.format(tag=TAG, arch=arch(), platform=platform())
zip_path, _ = urllib.request.urlretrieve(url)
with zipfile.ZipFile(zip_path, "r") as f:
f.extractall(cls.basedir())
serverfile = os.path.join(
cls.basedir(),
"vale-ls.exe" if sublime.platform() == "windows" else "vale-ls",
)
os.remove(zip_path)
os.chmod(serverfile, 0o744)
with open(os.path.join(cls.basedir(), "VERSION"), "w") as fp:
fp.write(version)
except BaseException:
shutil.rmtree(cls.basedir(), ignore_errors=True)
raise
def plugin_loaded() -> None:
register_plugin(ValeLS)
def plugin_unloaded() -> None:
unregister_plugin(ValeLS)