forked from sobolevn/dotbot-asdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasdf.py
92 lines (72 loc) · 3 KB
/
asdf.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
import subprocess
import dotbot
class Brew(dotbot.Plugin):
_supported_directives = ["asdf"]
def __init__(self, context):
super(Brew, self).__init__(context)
output = self._run_command(
"asdf plugin-list-all",
error_message="Failed to get known plugins",
stdout=subprocess.PIPE,
)
plugins = output.decode("utf-8")
self._known_plugins = plugins.split()[::2]
# API methods
def can_handle(self, directive):
return directive in self._supported_directives
def handle(self, _directive, data):
try:
self._validate_plugins(data)
self._handle_install(data)
return True
except ValueError as e:
self._log.error(e)
return False
# Utility
@property
def cwd(self):
return self._context.base_directory()
# Inner logic
def _validate_plugins(self, plugins):
for plugin in plugins:
name = plugin.get("plugin", None)
if name is None:
raise ValueError("Invalid plugin definition: {}".format(str(plugin)))
elif "url" not in plugin and name not in self._known_plugins:
raise ValueError("Unknown plugin: {}\nPlease provide URL".format(name))
def _handle_install(self, data):
for plugin in data:
language = plugin["plugin"]
self._log.info("Installing " + language)
self._run_command(
"asdf plugin-add {} {}".format(language, plugin.get("url", "")).strip(),
"Installing {} plugin".format(language),
"Failed to install: {} plugin".format(language),
)
if "versions" in plugin:
for version in plugin["versions"]:
self._run_command(
"asdf install {} {}".format(language, version),
"Installing {} {}".format(language, version),
"Failed to install: {} {}".format(language, version),
)
if "global" in plugin:
global_version = plugin["global"]
self._run_command(
"asdf global {} {}".format(language, global_version),
"Setting global {} {}".format(language, global_version),
"Failed setting global: {} {}".format(language, global_version),
)
else:
self._log.lowinfo("No {} versions to install".format(language))
def _run_command(self, command, message=None, error_message=None, **kwargs):
if message is not None:
self._log.lowinfo(message)
p = subprocess.Popen(command, cwd=self.cwd, shell=True, **kwargs)
p.wait()
output, output_err = p.communicate()
if output_err is not None:
if error_message is None:
error_message = "Command failed: {}".format(command)
raise ValueError(error_message)
return output