tomo/examples/tomo-install/tomo-install.tm

85 lines
3.0 KiB
Plaintext
Raw Permalink Normal View History

use shell
2025-04-01 12:15:28 -07:00
use patterns
2024-11-26 10:57:00 -08:00
_USAGE := "
tomo-install file.tm...
"
_HELP := "
tomo-install: a tool for installing libraries for the Tomo language
Usage: $_USAGE
"
2025-04-06 13:07:23 -07:00
func find_urls(path:Path -> [Text])
urls : @[Text]
2025-04-06 13:07:23 -07:00
if path.is_directory()
for f in path.children()
urls.insert_all(find_urls(f))
2025-04-06 13:07:23 -07:00
else if path.is_file() and path.extension() == ".tm"
for line in path.by_line()!
if captures := line.pattern_captures($Pat/use{space}{url}/) or line.pattern_captures($Pat/{id}{space}:={space}use{space}{url}/)
urls.insert(captures[-1])
2024-11-26 10:57:00 -08:00
return urls
2025-04-06 13:07:23 -07:00
func main(paths:[Path])
if paths.length == 0
2024-11-26 10:57:00 -08:00
paths = [(./)]
urls := (++: find_urls(p) for p in paths) or []
2024-11-26 10:57:00 -08:00
github_token := (~/.config/tomo/github-token).read()
2024-11-26 10:57:00 -08:00
(~/.local/share/tomo/installed).create_directory()
(~/.local/share/tomo/lib).create_directory()
2024-11-26 10:57:00 -08:00
2025-04-06 13:07:23 -07:00
for url in urls
2024-11-26 10:57:00 -08:00
original_url := url
url_without_protocol := url.trim_pattern($Pat"http{0-1 s}://", right=no)
hash := $Shell@(echo -n @url_without_protocol | sha256sum).get_output()!.slice(to=32)
2025-04-06 13:07:23 -07:00
if (~/.local/share/tomo/installed/$hash).is_directory()
2024-11-26 10:57:00 -08:00
say("Already installed: $url")
skip
alias : Text?
2024-11-26 10:57:00 -08:00
curl_flags := ["-L"]
2025-04-06 13:07:23 -07:00
if github := url_without_protocol.pattern_captures($Pat"github.com/{!/}/{!/}#{..}")
2024-11-26 10:57:00 -08:00
user := github[1]
repo := github[2]
tag := github[3]
url = "https://api.github.com/repos/$user/$repo/tarball/$tag"
alias = "$(repo.without_prefix("tomo-")).$(tag).$(user)"
2025-04-06 13:07:23 -07:00
if github_token
2024-11-26 10:57:00 -08:00
curl_flags ++= ["-H", "Authorization: Bearer $github_token"]
curl_flags ++= [
"-H", "Accept: application/vnd.github+json",
"-H", "X-GitHub-Api-Version: 2022-11-28",
]
(~/.local/share/tomo/downloads/$hash).create_directory()
2024-11-26 10:57:00 -08:00
say($Shell@`
set -euo pipefail
cd ~/.local/share/tomo/downloads/@hash
curl @curl_flags @url | tar xz -C ~/.local/share/tomo/installed --strip-components=1 --one-top-level=@hash
echo @original_url > ~/.local/share/tomo/installed/@hash/source.url
tomo -L ~/.local/share/tomo/installed/@hash
if [ "`uname -s`" = "Darwin" ]; then
ln -f -s ../installed/@hash/lib@hash.dylib ~/.local/share/tomo/lib/lib@hash.dylib
else
ln -f -s ../installed/@hash/lib@hash.so ~/.local/share/tomo/lib/lib@hash.so
fi
`.get_output()!)
2024-11-26 10:57:00 -08:00
2025-04-06 13:07:23 -07:00
if alias
say($Shell(
2024-11-26 10:57:00 -08:00
set -exuo pipefail
ln -f -s @hash ~/.local/share/tomo/installed/@alias
if [ "`uname -s`" = "Darwin" ]; then
ln -f -s lib@hash.dylib ~/.local/share/tomo/lib/lib@alias.dylib
else
ln -f -s lib@hash.so ~/.local/share/tomo/lib/lib@alias.so
fi
).get_output()!)
2024-11-26 10:57:00 -08:00
say("\[1]Installed $url!\[]")
2024-11-26 10:57:00 -08:00