-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathinstall.sh
More file actions
154 lines (130 loc) · 4.04 KB
/
install.sh
File metadata and controls
154 lines (130 loc) · 4.04 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/bin/bash
set -e
REPO="yanmxa/gencode"
BINARY="gen"
INSTALL_DIR="${HOME}/.local/bin"
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
NC='\033[0m'
info() { echo -e "${GREEN}$1${NC}"; }
warn() { echo -e "${YELLOW}$1${NC}"; }
error() { echo -e "${RED}$1${NC}" >&2; exit 1; }
usage() {
echo "Usage: $0 [install|upgrade|uninstall]"
echo ""
echo "Commands:"
echo " install Install gen (default)"
echo " upgrade Upgrade to latest version"
echo " uninstall Remove gen and config"
exit 0
}
normalize_version() {
local version="$1"
version="${version#v}"
echo "$version"
}
# Detect OS and architecture
detect_platform() {
OS=$(uname -s | tr '[:upper:]' '[:lower:]')
ARCH=$(uname -m)
case "$ARCH" in
x86_64|amd64) ARCH="amd64" ;;
aarch64|arm64) ARCH="arm64" ;;
*) error "Unsupported architecture: $ARCH" ;;
esac
case "$OS" in
darwin|linux) ;;
*) error "Unsupported OS: $OS" ;;
esac
}
get_latest_version() {
curl -fsSL "https://api.github.com/repos/${REPO}/releases/latest" | grep '"tag_name"' | sed -E 's/.*"v([^"]+)".*/\1/'
}
get_download_url() {
local version="$1"
local asset_name="gen_${OS}_${ARCH}.tar.gz"
local api_url="https://api.github.com/repos/${REPO}/releases/tags/v${version}"
curl -fsSL "$api_url" | awk -v asset="$asset_name" '
/"name":/ {
if ($0 ~ "\"" asset "\"") {
found=1
}
}
found && /"browser_download_url":/ {
line=$0
sub(/^.*"browser_download_url":[[:space:]]*"/, "", line)
sub(/".*$/, "", line)
if (line != "") {
print line
exit
}
}
'
}
do_install() {
detect_platform
info "Fetching latest version..."
VERSION=$(get_latest_version)
[ -z "$VERSION" ] && error "Failed to get latest version"
# Check if already installed
if command -v "$BINARY" &>/dev/null; then
CURRENT=$("$BINARY" version 2>/dev/null | awk '{print $3}' || echo "unknown")
CURRENT="$(normalize_version "$CURRENT")"
if [ "$CURRENT" = "$VERSION" ]; then
info "✓ gen v${VERSION} is already installed"
return
fi
info "Upgrading gen from v${CURRENT} to v${VERSION}..."
else
info "Installing gen v${VERSION} for ${OS}/${ARCH}..."
fi
# Download and extract
DOWNLOAD_URL="$(get_download_url "$VERSION")"
[ -z "$DOWNLOAD_URL" ] && error "Release asset gen_${OS}_${ARCH}.tar.gz not found for v${VERSION}"
TMP_DIR=$(mktemp -d)
trap "rm -rf $TMP_DIR" EXIT
curl -fsSL "$DOWNLOAD_URL" -o "$TMP_DIR/gen.tar.gz" || error "Download failed"
if ! tar -tzf "$TMP_DIR/gen.tar.gz" >/dev/null 2>&1; then
error "Downloaded asset is not a valid tar.gz archive"
fi
tar -xzf "$TMP_DIR/gen.tar.gz" -C "$TMP_DIR" || error "Extract failed"
# Install
mkdir -p "$INSTALL_DIR"
mv "$TMP_DIR/$BINARY" "$INSTALL_DIR/"
chmod +x "$INSTALL_DIR/$BINARY"
# Hint if not in PATH
if ! echo "$PATH" | tr ':' '\n' | grep -qx "$INSTALL_DIR"; then
warn "Add $INSTALL_DIR to your PATH:"
warn " export PATH=\"\$HOME/.local/bin:\$PATH\""
fi
info "✓ gen v${VERSION} installed to $INSTALL_DIR/$BINARY"
}
do_uninstall() {
info "Uninstalling gen..."
# Remove binary
if [ -f "$INSTALL_DIR/$BINARY" ]; then
rm "$INSTALL_DIR/$BINARY"
info "✓ Removed $INSTALL_DIR/$BINARY"
else
warn "Binary not found at $INSTALL_DIR/$BINARY"
fi
# Ask about config
if [ -d "$HOME/.gen" ]; then
echo -n "Remove config directory ~/.gen? [y/N] "
read -r response
if [[ "$response" =~ ^[Yy]$ ]]; then
rm -rf "$HOME/.gen"
info "✓ Removed ~/.gen"
fi
fi
info "✓ Uninstall complete"
}
# Main
case "${1:-install}" in
install|upgrade) do_install ;;
uninstall|remove) do_uninstall ;;
-h|--help|help) usage ;;
*) error "Unknown command: $1" ;;
esac