-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathcodemagic_cli_tools.py
83 lines (65 loc) · 2.7 KB
/
codemagic_cli_tools.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
import pathlib
import shutil
import textwrap
import argcomplete
from codemagic import __version__
from codemagic import cli
from codemagic.cli import Colors
class CodemagicCliTools(cli.CliApp):
"""
Show general information of installed Codemagic CLI tools
"""
@cli.action("version")
def version(self) -> str:
"""
Show version of installed Codemagic CLI tools
"""
executable = self.get_executable_name()
self.echo(f"{executable} {__version__}")
return __version__
@cli.action("installed-tools")
def installed_tools(self):
"""
Show installed Codemagic CLI tools
"""
for tool_class in cli.CliApp.__subclasses__():
executable = tool_class.get_executable_name()
self.echo(f"{executable} installed at {shutil.which(executable) or executable}")
@cli.action("enable-shell-autocompletion")
def enable_shell_autocompletion(self):
"""
Enable tab autocompletion for Codemagic CLI tools in your shell
"""
# TODO: Add required shell argument
# TODO: Add optional argument for completion script location
executables = [tool_class.get_executable_name() for tool_class in cli.CliApp.__subclasses__()]
completion_scripts_dir = pathlib.Path("~/.codemagic-cli-tools/completions").expanduser()
completion_scripts_dir.mkdir(parents=True, exist_ok=True)
zsh_completion_path = completion_scripts_dir / "zsh_autocomplete.sh"
zsh_completion_path.write_text(argcomplete.shellcode(executables, shell="zsh"))
bash_completion_path = completion_scripts_dir / "bash_autocomplete.sh"
bash_completion_path.write_text(argcomplete.shellcode(executables, shell="bash"))
completion_path = completion_scripts_dir / "autocomplete.sh"
completion_path.write_text(
textwrap.dedent(
f"""\
#!/bin/sh
if [ -n "$BASH_VERSION" ]; then
source {bash_completion_path}
elif [ -n "$ZSH_VERSION" ]; then
source {zsh_completion_path}
fi
""",
),
)
self.echo(Colors.GREEN(f"Shell autocompletion instructions were saved to {completion_path}"))
self.echo(
"To use autocomplete for Codemagic CLI tools, add the following "
"line to your shell profile (~/.zshrc, ~/.bashrc, etc):",
)
self.echo("")
self.echo(Colors.WHITE(f" source {completion_path}"))
self.echo("")
self.echo("Don't forget to source the completion script in your current shell.")
if __name__ == "__main__":
CodemagicCliTools.invoke_cli()