#!/usr/bin/env bash # btop, from https://get.chaipat.cc/btop # curl -fsSL get.chaipat.cc/btop | bash # Made by kura from apps.json; read it before you run it. Source: https://github.com/AppleBoiy/get main() { KURA_ID='btop' KURA_NAME='btop' KURA_REPO='AppleBoiy/btop' KURA_PRIVATE='1' KURA_HOST='get.chaipat.cc' KURA_RUN='./install.sh' # kura's installer library. build.mjs puts this, the item's settings and one of the actions below into a single # script per route, served at get.chaipat.cc/. Everything runs inside main(), called on the last line, so a # download cut short runs nothing. # # Settings the build fills in: KURA_ID, KURA_NAME, KURA_REPO, KURA_PRIVATE, KURA_HOST, plus per action: # app KURA_ASSET (a regex for the release asset) # cli KURA_ASSET, KURA_BIN # config KURA_RUN (a script in the repo) or KURA_LINKS ("src|dest" lines) # Overrides a user can set: KURA_DIR (where configs are cloned, default ~/.kura), KURA_APPS (default /Applications), # GITHUB_TOKEN (for private repos when gh is not signed in). set -euo pipefail if [ -t 1 ]; then B=$'\033[1m'; D=$'\033[2m'; R=$'\033[31m'; G=$'\033[32m'; A=$'\033[34m'; X=$'\033[0m'; else B= D= R= G= A= X=; fi say() { printf '%s\n' "${A}蔵${X} $*"; } step() { printf '%s\n' " ${D}·${X} $*"; } ok() { printf '%s\n' "${G}✓${X} $*"; } die() { printf '%s\n' "${R}✗${X} $*" >&2; exit 1; } TMP="" cleanup() { [ -n "$TMP" ] && rm -rf "$TMP"; return 0; } trap cleanup EXIT need_mac() { [ "$(uname -s)" = Darwin ] || die "$KURA_NAME is a Mac app; this machine is $(uname -s)." } # Talk to GitHub: anonymously for public repos, otherwise through gh (if signed in) or GITHUB_TOKEN. have_gh() { command -v gh >/dev/null 2>&1 && gh auth status >/dev/null 2>&1; } gh_get() { # gh_get [accept] local path=$1 out=$2 accept=${3:-application/vnd.github+json} if [ "$KURA_PRIVATE" = 1 ] && have_gh; then gh api -H "Accept: $accept" "$path" > "$out" elif [ -n "${GITHUB_TOKEN:-}" ]; then curl -fsSL -H "Accept: $accept" -H "Authorization: Bearer $GITHUB_TOKEN" "https://api.github.com/$path" -o "$out" elif [ "$KURA_PRIVATE" = 1 ]; then die "$KURA_REPO is private. Sign in with ${B}gh auth login${X} (or set GITHUB_TOKEN) and run this again." else curl -fsSL -H "Accept: $accept" "https://api.github.com/$path" -o "$out" fi } json() { plutil -extract "$1" raw -o - "$2" 2>/dev/null; } # macOS reads JSON with plutil; no jq needed # Fetch the newest release's asset whose name matches KURA_ASSET into $TMP/asset.zip; sets VERSION. fetch_release() { TMP=$(mktemp -d) say "Looking up the latest ${B}$KURA_NAME${X} release" gh_get "repos/$KURA_REPO/releases/latest" "$TMP/release.json" || die "Couldn't reach the releases of $KURA_REPO." VERSION=$(json tag_name "$TMP/release.json") local n i name id="" size=0 n=$(json assets "$TMP/release.json") || n=0 for ((i = 0; i < n; i++)); do name=$(json "assets.$i.name" "$TMP/release.json") if [[ $name =~ $KURA_ASSET ]]; then id=$(json "assets.$i.id" "$TMP/release.json"); size=$(json "assets.$i.size" "$TMP/release.json"); break fi done [ -n "$id" ] || die "Release $VERSION has no file matching $KURA_ASSET." step "$name ($((size / 1048576)) MB) from $VERSION" gh_get "repos/$KURA_REPO/releases/assets/$id" "$TMP/asset.zip" application/octet-stream mkdir "$TMP/x" && ditto -x -k "$TMP/asset.zip" "$TMP/x" } install_app() { need_mac fetch_release local src dir=${KURA_APPS:-/Applications} src=$(find "$TMP/x" -maxdepth 2 -name '*.app' -type d | head -1) [ -n "$src" ] || die "The download has no .app inside." [ -w "$dir" ] || { dir="$HOME/Applications"; mkdir -p "$dir"; } local app; app=$(basename "$src") if pgrep -xq "${app%.app}"; then step "quitting the running ${app%.app}"; osascript -e "quit app \"${app%.app}\"" >/dev/null 2>&1 || true; sleep 1; fi [ -e "$dir/$app" ] && { step "replacing the old copy"; rm -rf "$dir/$app"; } ditto "$src" "$dir/$app" ok "${B}$app${X} $VERSION is in $dir" printf '\n open "%s"\n\n' "$dir/$app" } install_cli() { need_mac fetch_release local home="$HOME/.local/share/$KURA_BIN" bin="$HOME/.local/bin" local src; src=$(find "$TMP/x" -maxdepth 2 -type f -name "$KURA_BIN" | head -1) [ -n "$src" ] || die "The download has no $KURA_BIN inside." rm -rf "$home"; mkdir -p "$home" "$bin" ditto "$(dirname "$src")" "$home" chmod +x "$home/$KURA_BIN" ln -sf "$home/$KURA_BIN" "$bin/$KURA_BIN" ok "${B}$KURA_BIN${X} $VERSION is at $bin/$KURA_BIN" case ":$PATH:" in *":$bin:"*) ;; *) printf '\n %s is not on your PATH. Add this to your shell profile:\n export PATH="$HOME/.local/bin:$PATH"\n' "$bin";; esac printf '\n' } # Clone (or update) the repo into $KURA_DIR/, then run its own installer or link files into place. install_config() { command -v git >/dev/null 2>&1 || die "git is needed (xcode-select --install)." local root=${KURA_DIR:-$HOME/.kura} dir dir="$root/$KURA_ID" mkdir -p "$root" # private repos: lend git the gh sign-in or GITHUB_TOKEN for this one command; nothing is written into the clone local auth=() if [ "$KURA_PRIVATE" = 1 ]; then if have_gh; then auth=(-c credential.helper= -c "credential.helper=!gh auth git-credential") elif [ -n "${GITHUB_TOKEN:-}" ]; then auth=(-c "http.extraHeader=Authorization: Basic $(printf 'x-access-token:%s' "$GITHUB_TOKEN" | base64)") else die "$KURA_REPO is private. Sign in with ${B}gh auth login${X} (or set GITHUB_TOKEN) and run this again."; fi fi export GIT_TERMINAL_PROMPT=0 if [ -d "$dir/.git" ]; then say "Updating ${B}$KURA_NAME${X} in $dir" [ -z "$(git -C "$dir" status --porcelain)" ] || die "$dir has changes of your own; commit or stash them, then run this again." git "${auth[@]}" -C "$dir" pull --ff-only --quiet || die "Couldn't update $dir from GitHub." else say "Cloning ${B}$KURA_NAME${X} into $dir" git "${auth[@]}" clone --quiet "https://github.com/$KURA_REPO.git" "$dir" || die "Couldn't clone $KURA_REPO." fi if [ -n "${KURA_RUN:-}" ]; then step "running $KURA_RUN" (cd "$dir" && bash "$KURA_RUN") else local src dest stamp; stamp=$(date +%Y%m%d-%H%M%S) while IFS='|' read -r src dest; do [ -n "$src" ] || continue dest=${dest/#\~/$HOME}; src="$dir/${src#./}"; src=${src%/.} mkdir -p "$(dirname "$dest")" if [ -L "$dest" ] && [ "$(readlink "$dest")" = "$src" ]; then step "$dest already points here"; continue; fi if [ -e "$dest" ] || [ -L "$dest" ]; then mv "$dest" "$dest.bak-$stamp"; step "kept the old one as $dest.bak-$stamp"; fi ln -s "$src" "$dest"; step "$dest → $src" done <<< "$KURA_LINKS" fi ok "${B}$KURA_NAME${X} config is in place. Run this again any time to update it." } install_config } main "$@"