2024-06-12 07:44:43 -07:00
#!/usr/bin/env bash
2023-05-10 08:55:36 -07:00
install_dir = ""
2024-01-05 23:33:52 -08:00
themes_dir = ""
executable = ""
2024-09-29 02:14:15 -07:00
version = ""
2023-05-10 08:55:36 -07:00
2023-05-18 00:29:32 -07:00
error( ) {
2024-06-12 07:44:43 -07:00
printf " \e[31m $1 \e[0m\n "
2023-05-18 00:29:32 -07:00
exit 1
}
info( ) {
printf " $1 \n "
}
warn( ) {
2024-06-12 07:44:43 -07:00
printf " ⚠️ \e[33m $1 \e[0m\n "
2023-05-18 00:29:32 -07:00
}
2023-05-10 08:55:36 -07:00
help( ) {
# Display Help
2024-01-05 23:33:52 -08:00
echo "Install script for Oh My Posh"
2023-05-10 08:55:36 -07:00
echo
2024-09-29 02:14:15 -07:00
echo "Syntax: install.sh [-h] [-d <dir>] [-t <dir>] [-v <ver>]"
2024-06-13 10:11:25 -07:00
echo "options:"
2024-01-05 23:33:52 -08:00
echo "-h Print this help."
2024-07-16 23:23:08 -07:00
echo " -d Specify the installation directory. Defaults to $HOME /bin, $HOME /.local/bin or the directory where oh-my-posh is installed. "
2024-01-05 23:33:52 -08:00
echo "-t Specify the themes installation directory. Defaults to the oh-my-posh cache directory."
2024-09-29 02:14:15 -07:00
echo "-v Version to download, defaults to latest"
2023-05-10 08:55:36 -07:00
echo
}
2024-09-29 02:14:15 -07:00
while getopts ":hd:t:v:" option; do
2023-05-10 08:55:36 -07:00
case $option in
h) # display Help
help
exit; ;
d) # Enter a name
2024-01-05 23:33:52 -08:00
install_dir = ${ OPTARG } ; ;
t) # themes directory
themes_dir = ${ OPTARG } ; ;
2024-09-29 02:14:15 -07:00
v) # version
version = ${ OPTARG } ; ;
2023-05-10 08:55:36 -07:00
\? ) # Invalid option
echo "Invalid option command line option. Use -h for help."
exit 1
esac
done
2024-01-06 03:36:53 -08:00
SUPPORTED_TARGETS = "linux-386 linux-amd64 linux-arm linux-arm64 darwin-amd64 darwin-arm64 freebsd-386 freebsd-amd64 freebsd-arm freebsd-arm64"
2023-05-10 08:55:36 -07:00
validate_dependency( ) {
if ! command -v $1 >/dev/null; then
2023-05-18 00:29:32 -07:00
error " $1 is required to install Oh My Posh. Please install $1 and try again.\n "
2023-05-10 08:55:36 -07:00
fi
}
validate_dependencies( ) {
validate_dependency curl
validate_dependency unzip
validate_dependency realpath
validate_dependency dirname
}
set_install_directory( ) {
if [ -n " $install_dir " ] ; then
# expand directory
install_dir = " ${ install_dir /# \~ / $HOME } "
return 0
fi
# check if we have oh-my-posh installed, if so, use the executable directory
# to install into and follow symlinks
if command -v oh-my-posh >/dev/null; then
posh_dir = $( command -v oh-my-posh)
real_dir = $( realpath $posh_dir )
install_dir = $( dirname $real_dir )
2023-05-18 00:29:32 -07:00
info "Oh My Posh is already installed, updating existing installation in:"
info " ${ install_dir } "
2024-07-16 23:23:08 -07:00
return 0
fi
# check if $HOME/bin exists and is writable
if [ -d " $HOME /bin " ] && [ -w " $HOME /bin " ] ; then
install_dir = " $HOME /bin "
return 0
fi
# check if $HOME/.local/bin exists and is writable
2024-08-08 20:02:37 -07:00
if ( [ -d " $HOME /.local/bin " ] && [ -w " $HOME /.local/bin " ] ) || mkdir -p " $HOME /.local/bin " ; then
2024-07-16 23:23:08 -07:00
install_dir = " $HOME /.local/bin "
return 0
2023-05-10 08:55:36 -07:00
fi
2024-07-16 23:23:08 -07:00
error "Cannot determine installation directory. Please specify a directory and try again: \ncurl -s https://ohmyposh.dev/install.sh | bash -s -- -d {directory}"
2023-05-10 08:55:36 -07:00
}
validate_install_directory( ) {
2024-06-13 10:11:25 -07:00
#check if installation dir exists
2023-05-10 08:55:36 -07:00
if [ ! -d " $install_dir " ] ; then
2023-05-18 00:29:32 -07:00
error " Directory ${ install_dir } does not exist, set a different directory and try again. "
2023-05-10 08:55:36 -07:00
fi
2024-07-16 23:23:08 -07:00
2024-06-12 07:44:43 -07:00
# Check if regular user has write permission
2024-06-13 10:11:25 -07:00
if [ ! -w " $install_dir " ] ; then
error " Cannot write to ${ install_dir } . Please check write permissions or set a different directory and try again: \ncurl -s https://ohmyposh.dev/install.sh | bash -s -- -d {directory} "
2023-05-18 00:29:32 -07:00
fi
# check if the directory is in the PATH
good = $(
IFS = :
for path in $PATH ; do
if [ " ${ path %/ } " = " ${ install_dir } " ] ; then
printf 1
break
fi
done
)
if [ " ${ good } " != "1" ] ; then
2024-07-16 23:23:08 -07:00
warn " Installation directory ${ install_dir } is not in your \$PATH, add it using \nexport PATH=\$PATH: ${ install_dir } "
2023-05-10 08:55:36 -07:00
fi
}
2024-01-05 23:33:52 -08:00
validate_themes_directory( ) {
2024-07-16 23:23:08 -07:00
2024-06-13 10:11:25 -07:00
# Validate if the themes directory exists
if ! mkdir -p " $themes_dir " > /dev/null 2>& 1; then
error " Cannot write to ${ themes_dir } . Please check write permissions or set a different directory and try again: \ncurl -s https://ohmyposh.dev/install.sh | bash -s -- -t {directory} "
2024-01-05 23:33:52 -08:00
fi
2024-06-13 10:11:25 -07:00
#check user write permission
if [ ! -w " $themes_dir " ] ; then
error " Cannot write to ${ themes_dir } . Please check write permissions or set a different directory and try again: \ncurl -s https://ohmyposh.dev/install.sh | bash -s -- -t {directory} "
2024-01-05 23:33:52 -08:00
fi
}
install_themes( ) {
if [ -n " $themes_dir " ] ; then
# expand directory
themes_dir = " ${ themes_dir /# \~ / $HOME } "
fi
cache_dir = $( $executable cache path)
# validate if the user set the path to the themes directory
if [ -z " $themes_dir " ] ; then
themes_dir = " ${ cache_dir } /themes "
fi
2024-06-13 10:11:25 -07:00
validate_themes_directory
2024-01-05 23:33:52 -08:00
info " 🎨 Installing oh-my-posh themes in ${ themes_dir } \n "
zip_file = " ${ cache_dir } /themes.zip "
url = "https://github.com/JanDeDobbeleer/oh-my-posh/releases/latest/download/themes.zip"
http_response = $( curl -s -f -L $url -o $zip_file -w "%{http_code}" )
2024-06-12 07:44:43 -07:00
if [ $http_response = "200" ] && [ -f $zip_file ] ; then
2024-01-05 23:33:52 -08:00
unzip -o -q $zip_file -d $themes_dir
# make sure the files are readable and writable for all users
chmod a+rwX ${ themes_dir } /*.omp.*
rm $zip_file
else
warn " Unable to download themes at ${ url } \nPlease validate your curl, connection and/or proxy settings "
fi
}
2023-05-10 08:55:36 -07:00
install( ) {
arch = $( detect_arch)
platform = $( detect_platform)
target = " ${ platform } - ${ arch } "
good = $(
IFS = " "
for t in $SUPPORTED_TARGETS ; do
if [ " ${ t } " = " ${ target } " ] ; then
printf 1
break
fi
done
)
if [ " ${ good } " != "1" ] ; then
2023-05-18 00:29:32 -07:00
error " ${ arch } builds for ${ platform } are not available for Oh My Posh "
2023-05-10 08:55:36 -07:00
fi
2023-05-18 00:29:32 -07:00
info " \nℹ ️ Installing oh-my-posh for ${ target } in ${ install_dir } "
2023-05-10 08:55:36 -07:00
executable = ${ install_dir } /oh-my-posh
url = https://github.com/JanDeDobbeleer/oh-my-posh/releases/latest/download/posh-${ target }
2024-09-29 02:14:15 -07:00
if [ " $version " ] ; then
url = https://github.com/JanDeDobbeleer/oh-my-posh/releases/download/${ version } /posh-${ target }
fi
2023-05-10 08:55:36 -07:00
2023-05-18 00:29:32 -07:00
info " ⬇️ Downloading oh-my-posh from ${ url } "
2023-05-10 08:55:36 -07:00
2023-08-28 04:40:29 -07:00
http_response = $( curl -s -f -L $url -o $executable -w "%{http_code}" )
2023-10-18 01:31:57 -07:00
if [ $http_response != "200" ] || [ ! -f $executable ] ; then
error " Unable to download executable at ${ url } \nPlease validate your curl, connection and/or proxy settings "
2023-08-28 04:40:29 -07:00
fi
2023-05-10 08:55:36 -07:00
chmod +x $executable
2024-01-05 23:33:52 -08:00
install_themes
2023-05-10 08:55:36 -07:00
2023-05-18 00:29:32 -07:00
info "🚀 Installation complete.\n\nYou can follow the instructions at https://ohmyposh.dev/docs/installation/prompt"
2023-08-28 04:40:29 -07:00
info "to setup your shell to use oh-my-posh."
2024-06-12 07:44:43 -07:00
if [ $http_response = "200" ] ; then
2024-06-13 10:11:25 -07:00
info " \nIf you want to use a built-in theme, you can find them in the ${ themes_dir } directory: "
info " oh-my-posh init {shell} --config ${ themes_dir } /{theme}.omp.json\n "
2023-08-28 04:40:29 -07:00
fi
2023-05-10 08:55:36 -07:00
}
detect_arch( ) {
arch = " $( uname -m | tr '[:upper:]' '[:lower:]' ) "
case " ${ arch } " in
2023-05-11 08:19:12 -07:00
x86_64) arch = "amd64" ; ;
2023-05-10 08:55:36 -07:00
armv*) arch = "arm" ; ;
arm64) arch = "arm64" ; ;
2023-05-24 15:09:43 -07:00
aarch64) arch = "arm64" ; ;
2023-10-29 07:59:10 -07:00
i686) arch = "386" ; ;
2023-05-10 08:55:36 -07:00
esac
if [ " ${ arch } " = "arm64" ] && [ " $( getconf LONG_BIT) " -eq 32 ] ; then
arch = arm
fi
printf '%s' " ${ arch } "
}
detect_platform( ) {
2023-06-04 23:57:30 -07:00
platform = " $( uname -s | awk '{print tolower($0)}' ) "
2023-05-10 08:55:36 -07:00
case " ${ platform } " in
linux) platform = "linux" ; ;
darwin) platform = "darwin" ; ;
esac
printf '%s' " ${ platform } "
}
validate_dependencies
set_install_directory
validate_install_directory
install