Skip to content

Commit

Permalink
feat(install): make script POSIX compliant (spicetify#2775)
Browse files Browse the repository at this point in the history
  • Loading branch information
rxri authored Jan 22, 2024
1 parent 883b4b9 commit 16d21e9
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 70 deletions.
4 changes: 1 addition & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,4 @@ package.json
# Logs
install.log

# VSCode
.vscode
pnpm-lock.yaml
pnpm-lock.yaml
3 changes: 3 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"recommendations": ["timonwong.shellcheck", "biomejs.biome", "golang.go", "ms-vscode.powershell"]
}
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"editor.defaultFormatter": "biomejs.biome"
}
136 changes: 69 additions & 67 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,52 +5,53 @@
set -e

while getopts ":r" arg; do
case "${arg}" in
"r") override_root=1 ;;
esac
case "${arg}" in
"r") override_root=1 ;;
*) echo "Invalid option -${OPTARG}" >&2; exit 1 ;;
esac
done

is_root() {
[ "${EUID:-$(id -u)}" -eq 0 ];
[ "$(id -u)" -ne 0 ]
}

if is_root && [ "${override_root:-0}" -eq 0 ]; then
echo "The script was ran as root. Script will now exit"
echo "If you did not intend to do this, please run the script without root permissions to avoid issues with Spicetify"
echo "You can override this behavior by passing `-r` or `--root` flag to this script"
exit
if ! is_root && [ "${override_root:-0}" -eq 0 ]; then
echo "The script was ran as root. Script will now exit"
echo "If you did not intend to do this, please run the script without root permissions to avoid issues with Spicetify"
echo "You can override this behavior by passing '-r' or '--root' flag to this script"
exit
fi

# wipe existing log
> install.log
> install.log :

log() {
echo $1
echo "["$(date +'%H:%M:%S %Y-%m-%d')"]" $1 >> install.log
echo "$1"
echo "[$(date +'%H:%M:%S %Y-%m-%d')]" "$1" >> install.log
}

case $(uname -sm) in
"Darwin x86_64") target="darwin-amd64" ;;
"Darwin arm64") target="darwin-arm64" ;;
"Linux x86_64") target="linux-amd64" ;;
"Linux aarch64") target="linux-arm64" ;;
*) log "Unsupported platform $(uname -sm). x86_64 and arm64 binaries for Linux and Darwin are available."; exit ;;
"Darwin x86_64") target="darwin-amd64" ;;
"Darwin arm64") target="darwin-arm64" ;;
"Linux x86_64") target="linux-amd64" ;;
"Linux aarch64") target="linux-arm64" ;;
*) log "Unsupported platform $(uname -sm). x86_64 and arm64 binaries for Linux and Darwin are available."; exit ;;
esac

# check for dependencies
command -v curl >/dev/null || { log "curl isn't installed\!" >&2; exit 1; }
command -v tar >/dev/null || { log "tar isn't installed\!" >&2; exit 1; }
command -v grep >/dev/null || { log "grep isn't installed\!" >&2; exit 1; }
command -v curl >/dev/null || { log "curl isn't installed!" >&2; exit 1; }
command -v tar >/dev/null || { log "tar isn't installed!" >&2; exit 1; }
command -v grep >/dev/null || { log "grep isn't installed!" >&2; exit 1; }

# download uri
releases_uri=https://github.com/spicetify/spicetify-cli/releases
if [ $# -gt 0 ]; then
tag=$1
tag=$1
else
tag=$(curl -LsH 'Accept: application/json' $releases_uri/latest)
tag=${tag%\,\"update_url*}
tag=${tag##*tag_name\":\"}
tag=${tag%\"}
tag=$(curl -LsH 'Accept: application/json' $releases_uri/latest)
tag=${tag%\,\"update_url*}
tag=${tag##*tag_name\":\"}
tag=${tag%\"}
fi

tag=${tag#v}
Expand Down Expand Up @@ -80,66 +81,67 @@ log "REMOVING $tar"
rm "$tar"

notfound() {
cat << EOINFO
cat << EOINFO
Manually add the directory to your \$PATH through your shell profile
export SPICETIFY_INSTALL="$spicetify_install"
export PATH="\$PATH:$spicetify_install"
EOINFO
}

endswith_newline() {
[[ $(tail -c1 "$1" | wc -l) -gt 0 ]]
[ "$(od -An -c "$1" | tail -1 | grep -o '.$')" = "\n" ]
}

check() {
local path="export PATH=\$PATH:$spicetify_install"
local shellrc=$HOME/$1
if [ "$1" == ".zshrc" ] && [ ! -z "${ZDOTDIR}" ]; then
shellrc=$ZDOTDIR/$1
fi

# Create shellrc if it doesn't exist
if ! [ -f $shellrc ]; then
log "CREATING $shellrc"
touch $shellrc
fi

# Still checking again, in case touch command failed
if [ -f $shellrc ]; then
if ! grep -q $spicetify_install $shellrc; then
log "APPENDING $spicetify_install to PATH in $shellrc"
if ! endswith_newline $shellrc; then
echo >> $shellrc
fi
echo ${2:-$path} >> $shellrc
log "Restart your shell to have spicetify in your PATH."
else
log "spicetify path already set in $shellrc, continuing..."
fi
else
notfound
fi
path="export PATH=\$PATH:$spicetify_install"
shellrc=$HOME/$1

if [ "$1" = ".zshrc" ] && [ -n "${ZDOTDIR}" ]; then
shellrc=$ZDOTDIR/$1
fi

# Create shellrc if it doesn't exist
if ! [ -f "$shellrc" ]; then
log "CREATING $shellrc"
touch "$shellrc"
fi

# Still checking again, in case touch command failed
if [ -f "$shellrc" ]; then
if ! grep -q "$spicetify_install" "$shellrc"; then
log "APPENDING $spicetify_install to PATH in $shellrc"
if ! endswith_newline "$shellrc"; then
echo >> "$shellrc"
fi
echo "${2:-$path}" >> "$shellrc"
log "Restart your shell to have spicetify in your PATH."
else
log "spicetify path already set in $shellrc, continuing..."
fi
else
notfound
fi
}

case $SHELL in
*zsh) check ".zshrc" ;;
*bash)
[ -f "$HOME/.bashrc" ] && check ".bashrc"
[ -f "$HOME/.bash_profile" ] && check ".bash_profile"
;;
*fish) check ".config/fish/config.fish" "fish_add_path $spicetify_install" ;;
*) notfound ;;
*zsh) check ".zshrc" ;;
*bash)
[ -f "$HOME/.bashrc" ] && check ".bashrc"
[ -f "$HOME/.bash_profile" ] && check ".bash_profile"
;;
*fish) check ".config/fish/config.fish" "fish_add_path $spicetify_install" ;;
*) notfound ;;
esac

echo
log "spicetify v$tag was installed successfully to $spicetify_install"
log "Run 'spicetify --help' to get started"

read -p "Do you want to install spicetify Marketplace? (Y/n) " choice
if [[ $choice == [Nn]* ]]; then
echo "spicetify Marketplace installation aborted"
exit
echo "Do you want to install spicetify Marketplace? (Y/n)"
read -r choice < /dev/tty
if [ "$choice" = "N" ] || [ "$choice" = "n" ]; then
echo "spicetify Marketplace installation aborted"
exit 0
fi
echo "Starting the spicetify Marketplace installation script.."
curl -fsSL "https://raw.githubusercontent.com/spicetify/spicetify-marketplace/main/resources/install.sh" | sh
curl -fsSL "https://raw.githubusercontent.com/spicetify/spicetify-marketplace/main/resources/install.sh" | sh

0 comments on commit 16d21e9

Please sign in to comment.