Skip to content

Commit 56f5bc8

Browse files
author
Test
committed
Refactor install.sh and giv.sh for improved directory handling and version fetching
1 parent ce197e3 commit 56f5bc8

File tree

2 files changed

+104
-89
lines changed

2 files changed

+104
-89
lines changed

install.sh

Lines changed: 82 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,54 @@
11
#!/usr/bin/env sh
2-
32
set -eu
43

5-
# Repository and filenames
4+
# --- Configuration ---
65
REPO="giv-cli/giv"
76
SCRIPT_NAME="giv.sh"
8-
USER_BIN_DIR=""
9-
APP_DIR=""
107
VERSION=""
11-
USER_BIN_DIR_OVERRIDE=""
12-
13-
# 1. Remove existing giv binary or symlink
14-
if command -v giv >/dev/null 2>&1; then
15-
OLD_PATH="$(command -v giv)"
16-
rm -f "$OLD_PATH"
17-
fi
188

19-
# 2. Parse args
9+
# --- 1. Parse args (only --version now) ---
2010
while [ $# -gt 0 ]; do
2111
case "$1" in
22-
--version)
23-
VERSION="$2"
24-
shift 2
25-
;;
26-
--install-dir)
27-
USER_BIN_DIR_OVERRIDE="$2"
28-
shift 2
29-
;;
30-
*)
31-
printf 'Unknown argument: %s\n' "$1" >&2
32-
exit 1
33-
;;
12+
--version)
13+
VERSION="$2"; shift 2;;
14+
*)
15+
printf 'Unknown argument: %s\n' "$1" >&2
16+
exit 1;;
3417
esac
35-
# no extra shift
36-
break
3718
done
3819

39-
# 3. Determine USER_BIN_DIR
40-
if [ -n "$USER_BIN_DIR_OVERRIDE" ]; then
41-
USER_BIN_DIR="$USER_BIN_DIR_OVERRIDE"
42-
elif [ -w "/usr/local/bin" ]; then
20+
# --- 2. Determine USER_BIN_DIR ---
21+
if [ -w "/usr/local/bin" ]; then
4322
USER_BIN_DIR="/usr/local/bin"
4423
elif [ -d "$HOME/.local/bin" ]; then
4524
USER_BIN_DIR="$HOME/.local/bin"
4625
else
4726
USER_BIN_DIR="$HOME/bin"
4827
fi
4928

50-
# 4. Determine VERSION if not set
29+
# --- 3. Fetch latest version if unset ---
5130
if [ -z "$VERSION" ]; then
52-
VERSION="$(curl -s "https://api.github.com/repos/$REPO/releases/latest" | awk -F '"' '/"tag_name"/ {print $4; exit}')"
31+
VERSION="$(curl -s "https://api.github.com/repos/$REPO/releases/latest" \
32+
| awk -F '"' '/"tag_name"/ {print $4; exit}')"
33+
if [ -z "$VERSION" ]; then
34+
printf 'Warning: Could not determine latest release version, defaulting to "main".\n' >&2
35+
VERSION="main"
36+
fi
5337
fi
5438

55-
# 5. Detect platform
39+
# --- 4. Detect platform ---
5640
detect_platform() {
5741
OS="$(uname -s)"
5842
case "$OS" in
59-
Linux*)
60-
if [ -f /etc/wsl.conf ] || grep -qi microsoft /proc/version 2>/dev/null; then
61-
printf 'windows'
62-
else
63-
printf 'linux'
64-
fi
65-
;;
66-
Darwin*) printf 'macos' ;;
67-
CYGWIN* | MINGW* | MSYS*) printf 'windows' ;;
68-
*) printf 'unsupported' ;;
43+
Linux*)
44+
if [ -f /etc/wsl.conf ] || grep -qi microsoft /proc/version 2>/dev/null; then
45+
printf 'windows'
46+
else
47+
printf 'linux'
48+
fi;;
49+
Darwin*) printf 'macos';;
50+
CYGWIN*|MINGW*|MSYS*) printf 'windows';;
51+
*) printf 'unsupported';;
6952
esac
7053
}
7154
PLATFORM="$(detect_platform)"
@@ -74,60 +57,76 @@ if [ "$PLATFORM" = "unsupported" ]; then
7457
exit 1
7558
fi
7659

77-
# 6. Compute APP_DIR based on PLATFORM
60+
# --- 5. Compute APP_DIR ---
7861
compute_app_dir() {
7962
case "$PLATFORM" in
80-
linux)
81-
printf '%s/giv' "${XDG_DATA_HOME:-$HOME/.local/share}"
82-
;;
83-
windows)
84-
printf '%s/giv' "${LOCALAPPDATA:-$HOME/AppData/Local}"
85-
;;
86-
macos)
87-
printf '%s/Library/Application Scripts/com.github.%s' "$HOME" "${REPO}"
88-
;;
89-
*)
90-
printf 'Error: Unsupported platform: %s\n' "${PLATFORM}" >&2
91-
exit 1
92-
;;
63+
linux)
64+
printf '%s/giv' "${XDG_DATA_HOME:-$HOME/.local/share}";;
65+
windows)
66+
printf '%s/giv' "${LOCALAPPDATA:-$HOME/AppData/Local}";;
67+
macos)
68+
printf '%s/Library/Application Scripts/com.github.%s' "$HOME" "${REPO}";;
9369
esac
9470
}
9571
APP_DIR="$(compute_app_dir)"
9672

97-
# 8. Create necessary directories
98-
mkdir -p "$USER_BIN_DIR" "$APP_DIR/templates"
99-
mkdir -p "$USER_BIN_DIR" "$APP_DIR/src"
73+
# --- 6. Prepare directories ---
74+
mkdir -p "$USER_BIN_DIR" \
75+
"$APP_DIR/lib/giv" \
76+
"$APP_DIR/templates" \
77+
"$APP_DIR/docs"
10078

101-
# 9. Check Git version for sparse-checkout
102-
if ! command -v git >/dev/null 2>&1; then
103-
printf 'Error: git is required to use giv.\n' >&2
104-
exit 1
79+
# --- 7. Create temp dir and ensure cleanup ---
80+
TMP="$(mktemp -d)"
81+
trap 'rm -rf "$TMP"' EXIT
82+
83+
# --- 8. Acquire release source (prefer tarball) ---
84+
SRCDIR=""
85+
if [ "$VERSION" != "main" ]; then
86+
if command -v curl >/dev/null 2>&1 && command -v tar >/dev/null 2>&1; then
87+
printf 'Downloading %s tarball…\n' "$VERSION"
88+
if curl -fsSL "https://github.com/$REPO/archive/refs/tags/$VERSION.tar.gz" \
89+
| tar -xz -C "$TMP"; then
90+
SRCDIR="$TMP/$(basename "$REPO")-$VERSION"
91+
else
92+
printf 'Tarball fetch failed, will fall back to git clone…\n'
93+
fi
94+
fi
10595
fi
10696

97+
if [ -z "$SRCDIR" ]; then
98+
if ! command -v git >/dev/null 2>&1; then
99+
printf 'Error: git is required if tarball download fails.\n' >&2
100+
exit 1
101+
fi
102+
printf 'Cloning %s at branch %s…\n' "$REPO" "$VERSION"
103+
git -c advice.detachedHead=false clone -q --depth 1 \
104+
--branch "$VERSION" "https://github.com/$REPO.git" "$TMP"
105+
SRCDIR="$TMP"
106+
fi
107107

108+
# --- 9. Install files ---
109+
# Copy library scripts into lib/giv
110+
cp -R "$SRCDIR/src/"* "$APP_DIR/lib/giv/"
111+
# Copy templates and docs to top-level dirs
112+
cp -R "$SRCDIR/templates/"* "$APP_DIR/templates/"
113+
cp -R "$SRCDIR/docs/"* "$APP_DIR/docs/"
108114

109-
# 10. Fetch repo
110-
TMP="$(mktemp -d)"
111-
printf 'Installing version %s\n' "$VERSION"
112-
git -c advice.detachedHead=false clone -q --depth 1 --branch "$VERSION" "https://github.com/$REPO.git" "$TMP"
113-
cp -Ri "$TMP/templates/*" "$APP_DIR/templates/"
114-
cp -Ri "$TMP/src/*" "$APP_DIR/"
115-
chmod +x "$APP_DIR/$SCRIPT_NAME"
116-
printf 'Prompts → %s/%s\n' "$APP_DIR" "templates"
117-
rm -rf "$TMP"
118-
119-
# 11. Install giv.sh and create symlink or fallback copy
115+
# --- 10. Link or copy the main script ---
120116
if [ "$PLATFORM" = "windows" ]; then
121-
cp -f "$APP_DIR/$SCRIPT_NAME" "$USER_BIN_DIR/giv"
117+
cp -f "$APP_DIR/lib/giv/$SCRIPT_NAME" "$USER_BIN_DIR/giv"
122118
else
123-
ln -sf "$APP_DIR/$SCRIPT_NAME" "$USER_BIN_DIR/giv"
119+
ln -sf "$APP_DIR/lib/giv/$SCRIPT_NAME" "$USER_BIN_DIR/giv"
124120
fi
125-
printf 'giv.sh → %s/%s (symlink/copy) %s/giv\n' "$APP_DIR" "$SCRIPT_NAME" "$USER_BIN_DIR"
126-
127121

128-
# 12. Final PATH check
122+
# --- 11. Warn if PATH isn’t set ---
129123
case ":$PATH:" in
130-
*":$USER_BIN_DIR:"*) ;;
131-
*) printf 'Warning: %s is not in your PATH.\n' "$USER_BIN_DIR" ;;
124+
*":$USER_BIN_DIR:"*) ;;
125+
*)
126+
printf 'Warning: %s is not in your PATH.\n' "$USER_BIN_DIR";;
132127
esac
133-
printf 'Installation complete!\n'
128+
129+
printf 'Installed giv %s → %s\n' "$VERSION" "$USER_BIN_DIR/giv"
130+
printf 'Installed templates to %s\n' "$APP_DIR/templates"
131+
printf 'Installed docs to %s\n' "$APP_DIR/docs"
132+
printf 'Run "giv --help" to get started.\n'

src/giv.sh

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,18 @@ get_script_dir() {
2525
cd "$(dirname "${target}")" 2>/dev/null && pwd
2626
fi
2727
}
28+
# --- 5. Compute APP_DIR ---
29+
compute_app_dir() {
30+
case "$PLATFORM" in
31+
linux)
32+
printf '%s/giv' "${XDG_DATA_HOME:-$HOME/.local/share}";;
33+
windows)
34+
printf '%s/giv' "${LOCALAPPDATA:-$HOME/AppData/Local}";;
35+
macos)
36+
printf '%s/Library/Application Scripts/com.github.%s' "$HOME" "${REPO}";;
37+
esac
38+
}
39+
2840
get_is_sourced(){
2941
# Detect if sourced (works in bash, zsh, dash, sh)
3042
_is_sourced=0
@@ -57,15 +69,17 @@ fi
5769
SCRIPT_DIR="$(get_script_dir "${SCRIPT_PATH}")"
5870

5971
# Allow overrides for advanced/testing/dev
72+
APP_DIR="$(compute_app_dir)"
73+
printf 'Using giv app directory: %s\n' "${APP_DIR}"
6074
LIB_DIR=""
6175
TEMPLATE_DIR=""
6276
DOCS_DIR=""
6377

6478
# Library location (.sh files)
6579
if [ -n "${GIV_LIB_DIR:-}" ]; then
6680
LIB_DIR="${GIV_LIB_DIR}"
67-
elif [ -d "/usr/local/lib/giv" ]; then
68-
LIB_DIR="/usr/local/lib/giv"
81+
elif [ -d "${APP_DIR}/src" ]; then
82+
LIB_DIR="${APP_DIR}/src"
6983
elif [ -d "${SCRIPT_DIR}" ]; then
7084
# Local or system install: helpers in same dir
7185
LIB_DIR="${SCRIPT_DIR}"
@@ -77,11 +91,13 @@ else
7791
fi
7892
GIV_LIB_DIR="${LIB_DIR}"
7993

94+
printf 'Using giv lib directory: %s\n' "${GIV_LIB_DIR}"
95+
8096
# Template location
8197
if [ -n "${GIV_TEMPLATE_DIR:-}" ]; then
8298
TEMPLATE_DIR="${GIV_TEMPLATE_DIR}"
83-
elif [ -d "${LIB_DIR}/../templates" ]; then
84-
TEMPLATE_DIR="${LIB_DIR}/../templates"
99+
elif [ -d "${APP_DIR}/templates" ]; then
100+
TEMPLATE_DIR="${APP_DIR}/templates"
85101
elif [ -d "/usr/local/share/giv/templates" ]; then
86102
TEMPLATE_DIR="/usr/local/share/giv/templates"
87103
elif [ -n "${SNAP:-}" ] && [ -d "${SNAP}/share/giv/templates" ]; then
@@ -95,8 +111,8 @@ GIV_TEMPLATE_DIR="${TEMPLATE_DIR}"
95111
# Docs location (optional)
96112
if [ -n "${GIV_DOCS_DIR:-}" ]; then
97113
DOCS_DIR="${GIV_DOCS_DIR}"
98-
elif [ -d "${LIB_DIR}/../docs" ]; then
99-
DOCS_DIR="${LIB_DIR}/../docs"
114+
elif [ -d "${APP_DIR}/docs" ]; then
115+
DOCS_DIR="${APP_DIR}/docs"
100116
elif [ -d "/usr/local/share/giv/docs" ]; then
101117
DOCS_DIR="/usr/local/share/giv/docs"
102118
elif [ -n "${SNAP:-}" ] && [ -d "${SNAP}/share/giv/docs" ]; then

0 commit comments

Comments
 (0)