Skip to content

Commit

Permalink
chore(scripts/install.sh): Add check for curl dependency and improve …
Browse files Browse the repository at this point in the history
…shell detection

- Add check for curl dependency and exit with error message if not found
- Improve shell detection logic to handle more shell types
- Simplify BINARY_URL logic to use a single variable
- Use ensure function to handle command failures

BREAKING CHANGE: None
  • Loading branch information
sidedwards committed Oct 27, 2024
1 parent c67a52a commit adfe8f3
Showing 1 changed file with 55 additions and 44 deletions.
99 changes: 55 additions & 44 deletions scripts/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@
set -e

main() {
# Check for curl
if ! command -v curl &> /dev/null; then
echo "curl is required but not found."
exit 1
fi

# Check for required dependencies
if ! command -v gh &> /dev/null; then
echo "GitHub CLI (gh) is required but not found."
echo "Install instructions: https://cli.github.com/manual/installation"
exit 1
}
fi

# Check if gh is authenticated
if ! gh auth status &> /dev/null; then
Expand All @@ -17,60 +23,63 @@ main() {
fi

BIN_DIR=${BIN_DIR-"$HOME/.bin"}
mkdir -p $BIN_DIR
mkdir -p "$BIN_DIR"

case $SHELL in
*/zsh)
PROFILE=$HOME/.zshrc
PREF_SHELL=zsh
;;
*/bash)
PROFILE=$HOME/.bashrc
PREF_SHELL=bash
;;
*/fish)
PROFILE=$HOME/.config/fish/config.fish
PREF_SHELL=fish
;;
*/ash)
PROFILE=$HOME/.profile
PREF_SHELL=ash
;;
*)
echo "could not detect shell, manually add ${BIN_DIR} to your PATH."
exit 1
case "$SHELL" in
*/zsh)
PROFILE="$HOME/.zshrc"
PREF_SHELL="zsh"
;;
*/bash)
PROFILE="$HOME/.bashrc"
PREF_SHELL="bash"
;;
*/fish)
PROFILE="$HOME/.config/fish/config.fish"
PREF_SHELL="fish"
;;
*/ash)
PROFILE="$HOME/.profile"
PREF_SHELL="ash"
;;
*)
echo "could not detect shell, manually add ${BIN_DIR} to your PATH."
exit 1
;;
esac

if [[ ":$PATH:" != *":${BIN_DIR}:"* ]]; then
echo >> $PROFILE && echo "export PATH=\"\$PATH:$BIN_DIR\"" >> $PROFILE
echo >> "$PROFILE" && echo "export PATH=\"\$PATH:$BIN_DIR\"" >> "$PROFILE"
fi

PLATFORM="$(uname -s)"
case $PLATFORM in
Linux)
SUFFIX="linux-x64"
;;
Darwin)
ARCH="$(uname -m)"
if [ "${ARCH}" = "arm64" ]; then
SUFFIX="darwin-arm64"
else
SUFFIX="darwin-x64"
fi
;;
*)
err "unsupported platform: $PLATFORM"
;;
case "$PLATFORM" in
Linux)
SUFFIX="linux-x64"
;;
Darwin)
ARCH="$(uname -m)"
if [ "${ARCH}" = "arm64" ]; then
SUFFIX="darwin-arm64"
else
SUFFIX="darwin-x64"
fi
;;
MINGW*|MSYS*|CYGWIN*)
BINARY_URL="https://github.com/sidedwards/auto-commit/releases/latest/download/auto-commit-windows-x64.exe"
;;
*)
err "unsupported platform: $PLATFORM"
;;
esac

BINARY_URL="https://github.com/sidedwards/auto-commit/releases/latest/download/auto-commit-${SUFFIX}"
if [ "$PLATFORM" = "MINGW"* ] || [ "$PLATFORM" = "MSYS"* ] || [ "$PLATFORM" = "CYGWIN"* ]; then
BINARY_URL="https://github.com/sidedwards/auto-commit/releases/latest/download/auto-commit-windows-x64.exe"
if [ -z "$BINARY_URL" ]; then
BINARY_URL="https://github.com/sidedwards/auto-commit/releases/latest/download/auto-commit-${SUFFIX}"
fi

echo "Downloading latest auto-commit binary..."
ensure curl -L "$BINARY_URL" -o "$BIN_DIR/auto-commit"
chmod +x "$BIN_DIR/auto-commit"
ensure curl -fsSL "$BINARY_URL" -o "$BIN_DIR/auto-commit"
ensure chmod +x "$BIN_DIR/auto-commit"

echo "auto-commit installed successfully!"
echo "Run 'auto-commit --help' to get started"
Expand All @@ -80,7 +89,9 @@ main() {
# will immediately terminate with an error showing the failing
# command.
ensure() {
if ! "$@"; then err "command failed: $*"; fi
if ! "$@"; then
err "command failed: $*"
fi
}

err() {
Expand Down

0 comments on commit adfe8f3

Please sign in to comment.