@@ -8,6 +8,14 @@ TMP_DIR="$(mktemp -d)"
88LATEST_RELEASE_URL=" https://api.github.com/repos/${GITHUB_REPO} /releases/latest"
99USER_AGENT=" Grimoire-Installer-Script"
1010
11+ cleanup () {
12+ echo " Cleaning up temporary files..."
13+ rm -rf " ${TMP_DIR} "
14+ }
15+
16+ # Set up trap to clean up on exit
17+ trap cleanup EXIT
18+
1119# Detect OS and architecture
1220OS=" $( uname -s | tr ' [:upper:]' ' [:lower:]' ) "
1321ARCH=" $( uname -m) "
2836
2937# Handle OS-specific variations
3038case " ${OS} " in
31- darwin|linux|windows)
32- # These operating systems are supported
39+ darwin)
40+ OS=" darwin"
41+ EXT=" tar.gz"
42+ ;;
43+ linux)
44+ OS=" linux"
45+ EXT=" tar.gz"
46+ ;;
47+ windows* )
48+ OS=" windows"
49+ EXT=" zip"
3350 ;;
3451 * )
3552 echo " Unsupported operating system: ${OS} "
@@ -42,68 +59,116 @@ echo "Detected OS: ${OS}, Architecture: ${ARCH}"
4259# Fetch the latest release information
4360echo " Fetching latest release information..."
4461RELEASE_INFO=$( curl -s -H " User-Agent: ${USER_AGENT} " " ${LATEST_RELEASE_URL} " )
45- VERSION=$( echo " ${RELEASE_INFO} " | grep -o ' "tag_name": * "[^"]*"' | grep -o ' [^"]*$ ' )
62+ VERSION=$( echo " ${RELEASE_INFO} " | grep -o ' "tag_name":"[^"]*"' | sed -E ' s/"tag_name":"( [^"]*)"/\1/ ' )
4663
47- # Construct download URL
48- if [ " ${OS} " = " windows" ]; then
49- ARCHIVE_FORMAT=" zip"
50- else
51- ARCHIVE_FORMAT=" tar.gz"
64+ if [ -z " ${VERSION} " ]; then
65+ echo " Error: Could not extract version from release info."
66+ exit 1
5267fi
5368
54- DOWNLOAD_URL=" https://github.com/${GITHUB_REPO} /releases/download/${VERSION} /grimoire-${VERSION# v} -${OS} -${ARCH} .${ARCHIVE_FORMAT} "
55- echo " Downloading Grimoire ${VERSION} for ${OS} -${ARCH} ..."
69+ echo " Latest version: ${VERSION} "
70+
71+ # Create asset name pattern based on the naming convention in .goreleaser.yml
72+ ASSET_NAME=" grimoire-${VERSION# v} -${OS} -${ARCH} .${EXT} "
73+ echo " Looking for asset: ${ASSET_NAME} "
74+
75+ # Extract the download URL for the specific asset
76+ DOWNLOAD_URL=$( echo " ${RELEASE_INFO} " | grep -o " \" browser_download_url\" :\" [^\" ]*${ASSET_NAME} \" " | sed -E ' s/"browser_download_url":"([^"]*)"/\1/' )
77+
78+ if [ -z " ${DOWNLOAD_URL} " ]; then
79+ echo " Error: Could not find download URL for ${ASSET_NAME} ."
80+ echo " Available assets:"
81+ echo " ${RELEASE_INFO} " | grep -o ' "browser_download_url":"[^"]*"' | sed -E ' s/"browser_download_url":"([^"]*)"/\1/'
82+ exit 1
83+ fi
84+
85+ echo " Download URL: ${DOWNLOAD_URL} "
5686
57- # Download the binary
58- curl -L -s -o " ${TMP_DIR} /grimoire.${ARCHIVE_FORMAT} " " ${DOWNLOAD_URL} "
87+ # Create a temporary directory for downloading and extracting
88+ echo " Downloading Grimoire ${VERSION} for ${OS} -${ARCH} ..."
89+ curl -L -o " ${TMP_DIR} /${ASSET_NAME} " " ${DOWNLOAD_URL} "
5990
60- # Extract the binary
61- echo " Extracting..."
91+ # Extract the archive
92+ echo " Extracting archive ..."
6293cd " ${TMP_DIR} "
63- if [ " ${ARCHIVE_FORMAT } " = " zip" ]; then
64- unzip -q " grimoire. ${ARCHIVE_FORMAT }"
94+ if [ " ${EXT } " = " zip" ]; then
95+ unzip -q " ${ASSET_NAME }"
6596else
66- tar -xzf " grimoire. ${ARCHIVE_FORMAT }"
97+ tar -xzf " ${ASSET_NAME }"
6798fi
6899
69100# Find the binary in the extracted directory
70- BINARY_PATH=$( find . -type f -name " grimoire" -o -name " grimoire.exe" | head -n 1)
101+ # According to .goreleaser.yml, it should be in a directory with the same name as the project
102+ cd grimoire-*
103+ BINARY_PATH=" grimoire"
104+ if [ " ${OS} " = " windows" ]; then
105+ BINARY_PATH=" grimoire.exe"
106+ fi
71107
72- if [ -z " ${BINARY_PATH} " ]; then
73- echo " Error: Could not find grimoire binary in the downloaded archive."
108+ if [ ! -f " ${BINARY_PATH} " ]; then
109+ echo " Error: Could not find grimoire binary in the extracted archive."
110+ find . -type f | sort
74111 exit 1
75112fi
76113
77- # If running as root, install to system directory
114+ # Installation logic based on permissions
78115if [ " $( id -u) " -eq 0 ]; then
79- # Install to system-wide directory
116+ # Running as root
80117 echo " Installing Grimoire to ${INSTALL_DIR} ..."
81118 cp " ${BINARY_PATH} " " ${INSTALL_DIR} /grimoire"
82119 chmod +x " ${INSTALL_DIR} /grimoire"
83120else
84- # Check if the user can write to the default location
121+ # Not running as root, try to install or guide the user
85122 if [ -w " ${INSTALL_DIR} " ]; then
86123 echo " Installing Grimoire to ${INSTALL_DIR} ..."
87124 cp " ${BINARY_PATH} " " ${INSTALL_DIR} /grimoire"
88125 chmod +x " ${INSTALL_DIR} /grimoire"
89126 else
90- # Try to use sudo
91- echo " Installing Grimoire to ${INSTALL_DIR} (requires sudo)..."
92- sudo cp " ${BINARY_PATH} " " ${INSTALL_DIR} /grimoire"
93- sudo chmod +x " ${INSTALL_DIR} /grimoire"
127+ if command -v sudo > /dev/null 2>&1 ; then
128+ echo " Installing Grimoire to ${INSTALL_DIR} (requires sudo)..."
129+ sudo cp " ${BINARY_PATH} " " ${INSTALL_DIR} /grimoire"
130+ sudo chmod +x " ${INSTALL_DIR} /grimoire"
131+ else
132+ # No sudo, offer to install to user's bin directory
133+ USER_BIN=" ${HOME} /.local/bin"
134+ echo " Cannot install to ${INSTALL_DIR} (permission denied and sudo not available)"
135+
136+ if [ ! -d " ${USER_BIN} " ]; then
137+ echo " Creating directory ${USER_BIN} ..."
138+ mkdir -p " ${USER_BIN} "
139+ fi
140+
141+ echo " Installing to ${USER_BIN} instead..."
142+ cp " ${BINARY_PATH} " " ${USER_BIN} /grimoire"
143+ chmod +x " ${USER_BIN} /grimoire"
144+
145+ if [[ " :${PATH} :" != * " :${USER_BIN} :" * ]]; then
146+ echo " Note: ${USER_BIN} is not in your PATH. You may need to add it:"
147+ echo " export PATH=\"\$ PATH:${USER_BIN} \" "
148+ echo " Add this line to your shell's profile file (~/.bashrc, ~/.zshrc, etc.) to make it permanent."
149+ fi
150+ fi
94151 fi
95152fi
96153
97- # Clean up
98- echo " Cleaning up..."
99- rm -rf " ${TMP_DIR} "
100-
101154# Verify installation
102155echo " Verifying installation..."
103- if command -v grimoire > /dev/null 2>&1 ; then
104- echo " Grimoire ${VERSION} has been successfully installed!"
156+ GRIMOIRE_PATH=$( command -v grimoire 2> /dev/null || echo " " )
157+
158+ if [ -n " ${GRIMOIRE_PATH} " ]; then
159+ echo " Grimoire ${VERSION} has been successfully installed to ${GRIMOIRE_PATH} !"
105160 echo " Run 'grimoire --help' to get started"
106161else
107- echo " Installation successful, but grimoire is not in your PATH."
108- echo " Make sure ${INSTALL_DIR} is in your PATH or manually move the binary to a directory in your PATH."
109- fi
162+ echo " Installation completed, but grimoire command is not in your PATH yet."
163+
164+ # Check if we installed to a non-PATH location
165+ if [ -x " ${HOME} /.local/bin/grimoire" ]; then
166+ echo " You can run it with: ${HOME} /.local/bin/grimoire"
167+ echo " Or add ${HOME} /.local/bin to your PATH to use 'grimoire' directly."
168+ elif [ -x " ${INSTALL_DIR} /grimoire" ]; then
169+ echo " You can run it with: ${INSTALL_DIR} /grimoire"
170+ echo " Make sure ${INSTALL_DIR} is in your PATH to use 'grimoire' directly."
171+ fi
172+ fi
173+
174+ echo " Installation complete!"
0 commit comments