can add cargo crates now haha

master
Jordan Orelli 2 years ago
parent b5cd629cc2
commit 434ab9326f

@ -13,6 +13,7 @@ files:
cargo-config.toml > .cargo/config.toml cargo-config.toml > .cargo/config.toml
[home posix] [home posix]
when: not host.is_windows when: not host.is_windows
@ -24,12 +25,14 @@ files:
nvim/init.vim > .config/nvim/init.vim nvim/init.vim > .config/nvim/init.vim
[home wsl] [home wsl]
when: host.is_wsl when: host.is_wsl
files: files:
scripts/winmode > bin/winmode scripts/winmode > bin/winmode
[link-files AppData] [link-files AppData]
when: host.is_windows when: host.is_windows
@ -43,4 +46,11 @@ files:
.vim/ftplugin > nvim/ftplugin .vim/ftplugin > nvim/ftplugin
packer.nvim > nvim-data/site/pack/packer/start/packer.nvim packer.nvim > nvim-data/site/pack/packer/start/packer.nvim
# [cargo:gitui]
[cargo-install]
crates:
just
cargo-watch
cargo-edit
cargo-get

@ -0,0 +1,49 @@
import subprocess
import shutil
import re
class CargoInstall:
resource_name = 'cargo-install'
def __init__(self, label, section):
self.label = label or None
self.parse_crates(section.get('crates', ''))
def run(self):
if shutil.which('cargo') is None:
print(f"skip [cargo-install {self.label}]: cargo not found")
if missing := self.crates - self.installed:
print(f"installing missing cargo binaries: {missing}")
cmd = ["cargo", "install"]
cmd.extend(list(missing))
with subprocess.Popen(cmd, stdout=subprocess.PIPE) as proc:
print(proc.stdout.read())
def parse_crates(self, text):
self.crates = set()
for line in text.splitlines():
if not line:
continue
crate = line.strip()
self.crates.add(crate)
@property
def installed(self):
cmd = ["cargo", "install", "--list"]
found = set()
p = subprocess.run(cmd, capture_output=True, check=True)
for line in p.stdout.splitlines():
if not line:
# line is empty
continue
line = line.decode("utf-8")
if re.match(r'^\s', line):
# line starts with whitespace
continue
try:
item = line.split()[0]
found.add(item)
except IndexError:
continue
return found

@ -8,6 +8,7 @@ import os
import platform import platform
import sys import sys
import pathlib import pathlib
import shutil
from functools import cached_property from functools import cached_property
class Host: class Host:
@ -61,4 +62,10 @@ class Host:
""" """
return pathlib.Path.home() return pathlib.Path.home()
def has(self, cmd):
"""
whether or not the machine has the specified command
"""
return shutil.which(cmd) is not None
sys.modules[__name__] = Host() sys.modules[__name__] = Host()

@ -5,7 +5,7 @@ a logging object
import logging import logging
import sys import sys
class _Log: class Log:
def __init__(self): def __init__(self):
logging.basicConfig(level=logging.INFO, format='') logging.basicConfig(level=logging.INFO, format='')
self._target = logging.getLogger() self._target = logging.getLogger()
@ -16,4 +16,4 @@ class _Log:
def __dir__(self): def __dir__(self):
return dir(self._target) return dir(self._target)
sys.modules[__name__] = _Log() sys.modules[__name__] = Log()

@ -1,8 +1,9 @@
from .home import Home from .home import Home
from .linker import LinkFiles from .linker import LinkFiles
from .cargo import CargoInstall
class Resource: class Resource:
resource_types = [Home, LinkFiles] resource_types = [Home, LinkFiles, CargoInstall]
@classmethod @classmethod
def from_name(cls, name): def from_name(cls, name):

Loading…
Cancel
Save