Skip to content

Instantly share code, notes, and snippets.

@andkirby
Last active January 17, 2019 12:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andkirby/caf9e825e83f38ad98721956d6e0107c to your computer and use it in GitHub Desktop.
Save andkirby/caf9e825e83f38ad98721956d6e0107c to your computer and use it in GitHub Desktop.
Fetch Ruby interpreter from GitBash for Windows or Cygwin similar CLI.
#!/usr/bin/env bash
################################################################################
# This script can install RUBY binaries on Windows in Bash.
#
# Link to this file:
# https://gist.github.com/andkirby/caf9e825e83f38ad98721956d6e0107c/raw/ruby-bash-win.sh
#
# Download latest version by command in GitBash Windows console:
# $ curl -Ls bit.ly/ruby-bash-win | bash
#
# Example to download 2.5.x version:
# $ curl -Ls bit.ly/ruby-bash-win | bash -s -- 2.5
#
# Direct link usage:
# $ curl -Ls https://gist.github.com/andkirby/caf9e825e83f38ad98721956d6e0107c/raw/ruby-bash-win.sh | bash
#
# Downloaded files hierarchy:
# ~/bin/
# /ruby - Last installed RUBY version.
# /ruby25 - RUBY 2.5.x
# /ruby23 - RUBY 2.3.x
# ~/.ruby/
# /ruby-install - Script to execute this online file
################################################################################
gist_version=2018-08-07
set -o errexit
set -o pipefail
set -o nounset
#set -o xtrace
ERR_FATAL=1
ERR_LOGIC=2
ERR_PARAMS=3
ERR_FILE_SYSTEM=4
ERR_CONNECTION=6
zip7_bin=${BASH_INSTALLER_7ZIP_BIN:-7za}
# colors
t_red='\e[0;31m'
t_green='\e[0;32m'
t_yellow='\e[0;33m'
t_cyan='\e[0;36m'
t_reset='\e[0m'
echo_head() {
echo -e "${t_yellow}${@}${t_reset}"
}
echo_note() {
echo -e "${t_cyan}${@}${t_reset}"
}
echo_notice() {
echo -e "${t_yellow}notice:${t_reset} ${@}"
}
echo_warning() {
echo -e "${t_red}warning:${t_reset} ${@}"
}
show_help () {
cat << EOF
Download RUBY binaries
${gist_version}
Run this command in GitBash Windows console
$ curl -Ls https://gist.github.com/andkirby/caf9e825e83f38ad98721956d6e0107c/raw/ruby-bash-win.sh | bash
Example to download 2.5.x version.
$ curl -Ls https://gist.github.com/andkirby/caf9e825e83f38ad98721956d6e0107c/raw/ruby-bash-win.sh | bash -s -- 2.5
Short version (bit.ly):
$ curl -Ls bit.ly/ruby-bash-win | bash
OPTIONS
-3, --32
Download 32-bit version.
-f, --force
Force downloading. It will try to overwrite exist files.
EOF
}
check_error () {
local status=${1}
shift
if [ '0' != "${status}" ]; then
echo "error: $@" > /dev/stderr
exit ${status}
fi
}
init_options() {
x32='x64'
minor_version=''
if [ -n "${1:-}" ] && [ "${1:0:1}" != '-' ]; then
minor_version="${1:-}"
test -n "${1:-}" && shift
# check if not empty and it's not an option
if [[ ! "${minor_version}" =~ ^[0-9]+\.[0-9]+$ ]]; then
check_error ${ERR_PARAMS} "Invalid version format (${minor_version}). Please use this one: #.##" > /dev/stderr
exit
fi
fi
# Process options
# validate and redefine options
declare -A options
options=(
[3]=32
[f]=force
[D]=no-download
[h]=help
)
OPTS=`getopt -o $(echo ${!options[*]} | tr -d ' ') -l $(echo ${options[*]} | tr ' ' ',') -- "$@"`
eval set -- "${OPTS}"
nts='-nts'
force=0
xdebug_install=0
while true; do
case "${1}" in
-f|--force)
force=1
shift
;;
-3|--32)
x32='x86'
shift 1
;;
-h|--help)
show_help
exit 0
;;
-\?)
show_help
exit ${ERR_PARAMS}
;;
--)
shift
break
;;
*)
check_error ${ERR_PARAMS} "${0}: unparseable option ${1}."
;;
esac
done
}
init_user_home_bin() {
if [ ! -d "$(cd; pwd)/bin" ]; then
mkdir -p "$(cd; pwd)/bin"
fi
}
# Fetch 7z from https://www.7-zip.org/
init_7zip() {
# Unfortunately, that's the only link with .zip file
# The rest must installed by using GUI
# See: https://www.7-zip.org/download.html
local zip7_url='https://www.7-zip.org/a/7za920.zip'
if ! ${zip7_bin} --help 2> /dev/null > /dev/null; then
curl -Ls ${zip7_url} --output /tmp/7za920.zip
unzip -p /tmp/7za920.zip 7za.exe > ~/bin/7za.exe
zip7_bin='7za'
fi
}
find_archive() {
reg_version=${minor_version:-'[0-9]+.[0-9]+'}'.[0-9]+'
reg_version=$(echo ${reg_version} | sed 's|\.|\\.|g')
# find archive
curl -sL ${releases_url} \
| grep -E 'browser_download_url.*-('${reg_version}').*'${x32}'.7z\"' | head -1 | grep -oE 'http[^"]+'
}
has_installed?() {
test -f ${install_path}/bin/ruby.exe
}
######################
# Init
init_options ${@:-}
releases_url=https://api.github.com/repos/oneclick/rubyinstaller2/releases
root_dir=$(cd; pwd)'/.ruby'
if [ ! -d ${root_dir} ]; then
printf 'Making RUBY home dir ~/.ruby/...'
mkdir -p ${root_dir}
echo 'OK'
fi
######################
# Find target RUBY version
# download last binary
if [ -n "${minor_version:-}" ]; then
echo_head 'Looking for the latest RUBY v'${minor_version}'.x releases ('${releases_url}')...'
else
echo_head 'Looking for the latest RUBY release ('${releases_url}')...'
fi
archive_url=$(find_archive)
archive_file=$(echo ${archive_url} | sed -r 's|^.*/([^/]+)$|\1|')
[ -z "${archive_file}" ] && check_error ${ERR_CONNECTION} 'Cannot fetch last RUBY version.'
download_version=$(echo ${archive_file} | grep -Eo '[0-9]+\.[0-9]+\.[0-9]+(-[0-9]+)?')
install_path=${root_dir}/$(echo ${archive_file} | sed -r 's|\.7z$||')
# -----------------
init_user_home_bin
bin_path=$(cd; pwd)'/bin'
echo_head "Found RUBY version: ${download_version}"
######################
# Fetch RUBY archive
if [ ${force} != 1 ] && [ -f ${install_path}/bin/ruby.exe ]; then
echo_notice 'This version already downloaded.'
else
if [ ${force} != 1 ] && [ -f ${root_dir}/${archive_file} ]; then
echo_notice 'Archive already downloaded by path '${root_dir}/${archive_file}
else
echo 'Fetching '${archive_file}'...'
curl -Ls ${archive_url} --output ${root_dir}/${archive_file}
echo 'OK'
fi
echo "Unpacking: ${root_dir}/${archive_file}..."
init_7zip
${zip7_bin} x -y -o${root_dir} ${root_dir}/${archive_file} > /dev/null || check_error ${ERR_FILE_SYSTEM} 'Could not unpack archive: '${root_dir}/${archive_file}
fi
# validate unpacked ruby.exe
if ! has_installed?; then
check_error ${ERR_FATAL} "No binary file by path: ${install_path}/bin/ruby.exe"
fi
# Remove archive
rm -f ${root_dir}/${archive_file}
######################
# Generate binary input files
short_version="$(echo ${download_version} \
| grep -Eo '^([0-9]+)\.([0-9]+)' | tr -d '.')"${x32/x64/}
download_path_print=$(echo ${install_path} | sed -r 's|'$(cd; pwd)'|~|g')
cat << EOF > ${bin_path}/ruby
#!/usr/bin/env bash
${install_path}/bin/ruby.exe \${@}
EOF
cat << EOF > ${bin_path}/ruby${short_version}
#!/usr/bin/env bash
${install_path}/bin/ruby.exe \${@}
EOF
echo 'Binary files:'
echo ' '${bin_path/$(cd; pwd)/'~'}'/ruby => '${download_path_print}'/bin/ruby.exe'
echo ' '${bin_path/$(cd; pwd)/'~'}'/ruby'${short_version}' => '${download_path_print}'/bin/ruby.exe'
# generate install input file
if [ ! -f $(cd; pwd)/.ruby/ruby-install ]; then
cat << 'EOF' > $HOME/.ruby/ruby-install
#!/usr/bin/env bash
curl -Ls https://gist.github.com/andkirby/caf9e825e83f38ad98721956d6e0107c/raw/ruby-bash-win.sh | bash -s -- ${@}
EOF
fi
echo ' ~/.ruby/ruby-install'
echo
echo 'SWITCH'
echo 'To switch main '${bin_path/$(cd; pwd)/'~'}'/ruby binary file to any version. You may use "~/.ruby/ruby-install".'
echo 'It will use downloaded version but it will try to fetch latest #.##.x version.'
echo
@andkirby
Copy link
Author

andkirby commented Aug 7, 2018

If there is an issue with a required SSL file please take a look this solution:
https://stackoverflow.com/a/35452500/213759

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment