-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·137 lines (124 loc) · 3.75 KB
/
install.sh
File metadata and controls
executable file
·137 lines (124 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/env bash
set -euo pipefail
if [ "${BASH_SOURCE+set}" = "set" ] && [ -n "${BASH_SOURCE[0]}" ]; then
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
else
SCRIPT_DIR="$(pwd)"
fi
TARGET_DIR="${HOME}/.local/bin"
TARGET_BIN="${TARGET_DIR}/theme-manager"
RUST_DIR="${SCRIPT_DIR}/rust"
REPO="${THEME_MANAGER_REPO:-OldJobobo/theme-manager-plus}"
VERSION="${THEME_MANAGER_VERSION:-}"
ASSET_SUFFIX="linux-x86_64"
fetch_latest_version() {
if ! command -v curl >/dev/null 2>&1; then
return 1
fi
local tag
tag="$(
curl -fsSL "https://api.github.com/repos/${REPO}/releases/latest" \
| sed -n 's/.*"tag_name":[[:space:]]*"v\{0,1\}\([^"]*\)".*/\1/p' \
| head -n1
)"
if [ -n "${tag}" ]; then
VERSION="${tag}"
return 0
fi
return 1
}
download_release() {
if [ -z "${VERSION}" ]; then
fetch_latest_version || return 1
fi
if ! command -v curl >/dev/null 2>&1; then
return 1
fi
local asset url tmp
asset="theme-manager-plus-v${VERSION}-${ASSET_SUFFIX}"
url="https://github.com/${REPO}/releases/download/v${VERSION}/${asset}"
tmp="$(mktemp)"
if curl -fL "${url}" -o "${tmp}"; then
install -m 0755 "${tmp}" "${TARGET_BIN}"
rm -f "${tmp}"
printf 'Installed %s (v%s) to %s\n' "${asset}" "${VERSION}" "${TARGET_BIN}"
return 0
fi
rm -f "${tmp}"
return 1
}
build_from_source() {
if ! command -v cargo >/dev/null 2>&1; then
echo "theme-manager: cargo is required to build the Rust CLI" >&2
return 1
fi
if [ ! -d "${RUST_DIR}" ]; then
echo "theme-manager: rust source not found; clone the repo to build from source" >&2
return 1
fi
(
cd "${RUST_DIR}"
cargo build --release
)
local source_bin
source_bin="${RUST_DIR}/target/release/theme-manager"
install -m 0755 "${source_bin}" "${TARGET_BIN}"
printf 'Installed %s\n' "${TARGET_BIN}"
}
mkdir -p "${TARGET_DIR}"
PATH_UPDATE_SKIPPED=0
PATH_UPDATE_SKIPPED_FILES=""
mark_path_update_skipped() {
local file="$1"
PATH_UPDATE_SKIPPED=1
if [ -z "${PATH_UPDATE_SKIPPED_FILES}" ]; then
PATH_UPDATE_SKIPPED_FILES="${file}"
else
PATH_UPDATE_SKIPPED_FILES="${PATH_UPDATE_SKIPPED_FILES}, ${file}"
fi
}
ensure_path_entry() {
local file="$1"
local path_line='export PATH="$HOME/.local/bin:$PATH"'
if [ -f "${file}" ] && [ -r "${file}" ] && grep -q 'HOME/\.local/bin' "${file}"; then
return 0
fi
if [ ! -f "${file}" ]; then
if ! touch "${file}" 2>/dev/null; then
echo "theme-manager: warning: cannot create ${file}; skipping PATH update" >&2
mark_path_update_skipped "${file}"
return 0
fi
fi
if [ ! -w "${file}" ]; then
echo "theme-manager: warning: cannot write ${file}; skipping PATH update" >&2
mark_path_update_skipped "${file}"
return 0
fi
if ! printf '\n%s\n' "${path_line}" >> "${file}" 2>/dev/null; then
echo "theme-manager: warning: failed to update ${file}; skipping PATH update" >&2
mark_path_update_skipped "${file}"
return 0
fi
}
os="$(uname -s)"
arch="$(uname -m)"
if [ "${os}" != "Linux" ] || [ "${arch}" != "x86_64" ]; then
echo "theme-manager: no prebuilt binary for ${os}/${arch}; building from source" >&2
build_from_source
exit 0
fi
if ! download_release; then
echo "theme-manager: release download failed, attempting local build" >&2
build_from_source
fi
mkdir -p "${HOME}/.config/starship-themes"
ensure_path_entry "${HOME}/.profile"
ensure_path_entry "${HOME}/.bashrc"
ensure_path_entry "${HOME}/.zshrc"
if [ "${PATH_UPDATE_SKIPPED}" -eq 1 ]; then
echo "theme-manager: warning: could not update PATH in: ${PATH_UPDATE_SKIPPED_FILES}" >&2
echo 'theme-manager: add ~/.local/bin to PATH manually: export PATH="$HOME/.local/bin:$PATH"' >&2
else
echo "theme-manager: ensured ~/.local/bin is on PATH in ~/.profile, ~/.bashrc, and ~/.zshrc"
fi