Refactor helper installers and add more go helpers

This commit is contained in:
ViViDboarder 2024-11-01 16:42:32 -07:00
parent 2495f34d35
commit 5591ee3582

View File

@ -89,6 +89,35 @@ def maybe_npm_install(*args: str) -> bool:
return maybe_run("npm", "install", "-g", *user_bins) return maybe_run("npm", "install", "-g", *user_bins)
def maybe_go_install(**kwargs: str) -> bool:
urls = [url for name, url in kwargs.items() if should_install_user(name)]
if not urls:
return True
return maybe_run("go", "install", *urls)
def maybe_cargo_install(*args: str) -> bool:
user_bins = [arg for arg in args if should_install_user(arg)]
if not user_bins:
return True
return maybe_run("cargo", "install", *user_bins)
def maybe_release_gitter(**commands: list[str]) -> bool:
command_names = [key for key in commands.keys() if should_install_user(key)]
if not command_names:
return True
result = True
for command in command_names:
args = commands[command]
result = result and maybe_run("release-gitter", *args)
return result
def install_language_servers(langs: set[Language]): def install_language_servers(langs: set[Language]):
if Language.PYTHON in langs: if Language.PYTHON in langs:
maybe_npm_install("pyright") maybe_npm_install("pyright")
@ -98,13 +127,12 @@ def install_language_servers(langs: set[Language]):
"component", "component",
"add", "add",
"rustfmt", "rustfmt",
"rust-analysis",
"rust-src", "rust-src",
"clippy", "clippy",
"rust-analyzer", "rust-analyzer",
) )
if Language.GO in langs: if Language.GO in langs:
maybe_run("go", "install", "golang.org/x/tools/gopls@latest") maybe_go_install(gopls="golang.org/x/tools/gopls@latest")
def install_linters(langs: set[Language]): def install_linters(langs: set[Language]):
@ -122,15 +150,16 @@ def install_linters(langs: set[Language]):
if Language.ANSIBLE in langs: if Language.ANSIBLE in langs:
maybe_pip_install("ansible-lint") maybe_pip_install("ansible-lint")
if Language.GO in langs: if Language.GO in langs:
# NOTE: Can't use maybe_release_gitter because name has a -
maybe_run( maybe_run(
"release-gitter", "release-gitter",
"--git-url", "--git-url",
"https://github.com/golangci/golangci-lint", "https://github.com/golangci/golangci-lint",
"--map-arch",
"armv7l=armv7",
"--extract-all", "--extract-all",
"--exec", "--exec",
os.path.expanduser("mv /tmp/$(echo {}|sed s/\\.tar\\.gz$//)/golangci-lint ~/bin/"), os.path.expanduser(
"mv /tmp/$(echo {}|sed s/\\.tar\\.gz$//)/golangci-lint ~/bin/"
),
"golangci-lint-{version}-{system}-{arch}.tar.gz", "golangci-lint-{version}-{system}-{arch}.tar.gz",
"/tmp/", "/tmp/",
) )
@ -139,12 +168,11 @@ def install_linters(langs: set[Language]):
maybe_run("luarocks", "--local", "install", "luafilesystem") maybe_run("luarocks", "--local", "install", "luafilesystem")
maybe_run("luarocks", "--local", "install", "luacheck", "1.1.0") maybe_run("luarocks", "--local", "install", "luacheck", "1.1.0")
if Language.DOCKER in langs: if Language.DOCKER in langs:
if should_install_user("hadolint"): hadolint_arm64 = "arm64"
hadolint_arm64 = "arm64" if sys.platform == "darwin":
if sys.platform == "darwin": hadolint_arm64 = "x86_64"
hadolint_arm64 = "x86_64" maybe_release_gitter(
maybe_run( hadolint=[
"release-gitter",
"--git-url", "--git-url",
"https://github.com/hadolint/hadolint", "https://github.com/hadolint/hadolint",
"--map-arch", "--map-arch",
@ -152,29 +180,32 @@ def install_linters(langs: set[Language]):
"--map-arch", "--map-arch",
f"arm64={hadolint_arm64}", f"arm64={hadolint_arm64}",
"--exec", "--exec",
os.path.expanduser("mv ~/bin/{} ~/bin/hadolint && chmod +x ~/bin/hadolint"), os.path.expanduser(
"mv ~/bin/{} ~/bin/hadolint && chmod +x ~/bin/hadolint"
),
"hadolint-{system}-{arch}", "hadolint-{system}-{arch}",
os.path.expanduser("~/bin"), os.path.expanduser("~/bin"),
) ]
if Language.TERRAFORM in langs:
maybe_run(
"release-gitter",
"--git-url",
"https://github.com/aquasecurity/tfsec",
"--exec",
os.path.expanduser("mv ~/bin/{} ~/bin/tfsec && chmod +x ~/bin/tfsec"),
"tfsec-{system}-{arch}",
os.path.expanduser("~/bin"),
) )
maybe_run( if Language.TERRAFORM in langs:
"release-gitter", maybe_release_gitter(
"--git-url", tfsec=[
"https://github.com/terraform-linters/tflint", "--git-url",
"--extract-all", "https://github.com/aquasecurity/tfsec",
"--exec", "--exec",
os.path.expanduser("chmod +x ~/bin/tflint"), os.path.expanduser("mv ~/bin/{} ~/bin/tfsec && chmod +x ~/bin/tfsec"),
"tflint_{system}_{arch}.zip", "tfsec-{system}-{arch}",
os.path.expanduser("~/bin"), os.path.expanduser("~/bin"),
],
tflint=[
"--git-url",
"https://github.com/terraform-linters/tflint",
"--extract-all",
"--exec",
os.path.expanduser("chmod +x ~/bin/tflint"),
"tflint_{system}_{arch}.zip",
os.path.expanduser("~/bin"),
],
) )
@ -192,14 +223,31 @@ def install_fixers(langs: set[Language]):
if Language.RUST in langs: if Language.RUST in langs:
maybe_run("rustup", "component", "add", "rustfmt") maybe_run("rustup", "component", "add", "rustfmt")
if {Language.LUA, Language.NEOVIM} in langs: if {Language.LUA, Language.NEOVIM} in langs:
maybe_run("luarocks", "--local", "install", "stylua", "0.19.1") _ = maybe_release_gitter(
stylua=[
"--git-url",
"https://github.com/JohnnyMorganz/StyLua",
"--extract",
"stylua",
"--exec",
os.path.expanduser("chmod +x ~/bin/stylua"),
"stylua-{system}-{arch}.zip",
os.path.expanduser("~/bin"),
]
) or maybe_cargo_install("stylua")
if Language.GO in langs:
maybe_go_install(
gofumpt="mvdan.cc/gofumpt@latest",
goimports="golang.org/x/tools/cmd/goimports@latest",
)
def install_debuggers(langs): def install_debuggers(langs):
if Language.PYTHON in langs: if Language.PYTHON in langs:
maybe_pip_install("debugpy") maybe_pip_install("debugpy")
if Language.GO in langs: if Language.GO in langs:
maybe_run("go", "install", "github.com/go-delve/delve/cmd/dlv@latest") maybe_go_install(dlv="github.com/go-delve/delve/cmd/dlv@latest")
def main(): def main():