switch to nushell variant

This commit is contained in:
Daylin Morgan 2024-08-07 12:11:10 -05:00 committed by Daylin Morgan
parent 81339fdff3
commit 0e188ee054
Signed by: daylin
GPG key ID: 950D13E9719334AD

View file

@ -1,63 +1,87 @@
#!/usr/bin/env python3 #!/usr/bin/env nu
import json def "check gh" [repo] {
import subprocess if $in.exit_code != 0 {
from typing import List print $"(ansi red) failed to run gh request for repo: ($repo)\n($in | reject exit_code)"
exit $in.exit_code
};
$in.stdout
}
def drop-v [] {
$in | each {|it|
if ($it | str starts-with 'v') {
$it | str substring 1..
} else { $it }
}
}
class Exe: def "get tags" [] {
def __init__(self, owner: str, name: str, exe: str | None = None) -> None: let repo = $in
self.owner = owner (
self.name = name gh api
self.exe = name if exe is None else exe -H "Accept: application/vnd.github+json"
self.repo = f"{owner}/{name}" -H "X-GitHub-Api-Version: 2022-11-28"
--paginate $"repos/($repo)/tags"
def fetch_tags(self) -> List[str]:
output = subprocess.check_output(
[
"gh",
"api",
"-H",
"Accept: application/vnd.github+json",
"-H",
"X-GitHub-Api-Version: 2022-11-28",
"--paginate",
f"repos/{self.owner}/{self.name}/tags",
],
text=True,
) )
if self.name == "ripgrep": | complete
tags = [n for o in json.loads(output) if (n := o["name"])[0].isdigit()] | check gh $repo
else: | from json
tags = [n for o in json.loads(output) if (n := o["name"]).startswith("v")] | where name =~ '^v?\d'
if not tags: | get name
print(f"recieved no tags for {self.owner}/{self.name}") | drop-v
return tags }
def current_version(self) -> str: def repo-to-bin [] {
return subprocess.check_output([self.exe, "--version"], text=True) let name = $in | split row '/' | last
match $name {
"ripgrep" => "rg"
"cli" => "gh"
"neovim" => "nvim"
"nushell" => "nu"
_ => $name
}
}
def display(self) -> None: def current-version [] {
print(f""" run-external $in "--version"
name: {self.owner}/{self.name} | split row "\n"
latest: {self.fetch_tags()[0]} | each {|s| $'| ' + $s}
current:""") | str join "\n"
}
print("\n".join([f" | {line} " for line in self.current_version().splitlines()])) def check-exe [] {
if (which $in | length ) == 0 {
print $"(ansi red_bold)($in) is not installed(ansi reset)"
return false
}
return true
}
def compare-versions [] {
let repo = $in
let $bin = $repo | repo-to-bin
if not ($bin | check-exe) { return }
let latest = $repo | get tags | first
let current = $bin | current-version
print $"(ansi cyan_bold)($repo)(ansi reset):
latest: ($latest)
current:
($current)
"
}
EXECUTABLES = [ [
Exe(*args) junegunn/fzf
for args in ( cli/cli
("junegunn","fzf"), sharkdp/fd
("sharkdp", "fd"), jesseduffield/lazygit
("BurntSushi", "ripgrep", "rg"), neovim/neovim
("cli", "cli", "gh"), BurntSushi/ripgrep
("jesseduffield", "lazygit"), nushell/nushell
("neovim", "neovim", "nvim"), zyedidia/eget
) prefix-dev/pixi
] ]
| each {|| $in | compare-versions}
| ignore
for exe in EXECUTABLES:
exe.display()