Skip to content

Commit

Permalink
Merge pull request #404 from xu-cheng/vendor
Browse files Browse the repository at this point in the history
vendor ruby
  • Loading branch information
xu-cheng committed Jul 11, 2016
2 parents 4a73249 + dff6bfd commit 7508da2
Show file tree
Hide file tree
Showing 6 changed files with 304 additions and 27 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Expand Up @@ -20,6 +20,9 @@
/Library/PinnedTaps
/Library/Taps

# Ignore vendored files within `Library`
/Library/Homebrew/vendor/portable-ruby/*

# Ignore `bin` contents (again).
/bin

Expand Down
196 changes: 196 additions & 0 deletions Library/Homebrew/cmd/vendor-install.sh
@@ -0,0 +1,196 @@
#: @hide_from_man_page
#: * `vendor-install` [<target>]:
#: Install vendor version of Homebrew dependencies.

# Hide shellcheck complaint:
# shellcheck source=/dev/null
source "$HOMEBREW_LIBRARY/Homebrew/utils/lock.sh"

VENDOR_DIR="$HOMEBREW_LIBRARY/Homebrew/vendor"

if [[ -n "$HOMEBREW_OSX" ]]
then
if [[ "$HOMEBREW_PROCESSOR" = "Intel" ]]
then
ruby_URL="https://homebrew.bintray.com/bottles-portable/portable-ruby-2.0.0-p648.leopard_64.bottle.tar.gz"
ruby_SHA="5c1240abe4be91c9774a0089c2a38a8ccfff87c009e8e5786730c659d5e633f7"
else
ruby_URL=""
ruby_SHA=""
fi
else
ruby_URL="https://homebrew.bintray.com/bottles-portable/portable-ruby-2.0.0-p648.x86_64_linux.bottle.tar.gz"
ruby_SHA="dbb5118a22a6a75cc77e62544a3d8786d383fab1bdaf8c154951268807357bf0"
fi

fetch() {
local -a curl_args
local sha
local temporary_path

curl_args=(
--fail \
--remote-time \
--location \
--user-agent "$HOMEBREW_USER_AGENT_CURL" \
)

if [[ -n "$HOMEBREW_QUIET" ]]
then
curl_args+=(--silent)
elif [[ -z "$HOMEBREW_VERBOSE" ]]
then
curl_args+=(--progress-bar)
fi

temporary_path="${CACHED_LOCATION}.incomplete"

mkdir -p "$HOMEBREW_CACHE"
[[ -n "$HOMEBREW_QUIET" ]] || echo "==> Downloading $VENDOR_URL"
if [[ -f "$CACHED_LOCATION" ]]
then
[[ -n "$HOMEBREW_QUIET" ]] || echo "Already downloaded: $CACHED_LOCATION"
else
if [[ -f "$temporary_path" ]]
then
"$HOMEBREW_CURL" "${curl_args[@]}" -C - "$VENDOR_URL" -o "$temporary_path"
if [[ $? -eq 33 ]]
then
[[ -n "$HOMEBREW_QUIET" ]] || echo "Trying a full download"
rm -f "$temporary_path"
"$HOMEBREW_CURL" "${curl_args[@]}" "$VENDOR_URL" -o "$temporary_path"
fi
else
"$HOMEBREW_CURL" "${curl_args[@]}" "$VENDOR_URL" -o "$temporary_path"
fi

if [[ ! -f "$temporary_path" ]]
then
odie "Download failed: ${VENDOR_URL}"
fi

trap '' SIGINT
mv "$temporary_path" "$CACHED_LOCATION"
trap - SIGINT
fi

if [[ -n "$(which shasum)" ]]
then
sha="$(shasum -a 256 "$CACHED_LOCATION" | cut -d' ' -f1)"
elif [[ -n "$(which sha256sum)" ]]
then
sha="$(sha256sum "$CACHED_LOCATION" | cut -d' ' -f1)"
else
odie "Cannot verify the checksum ('shasum' or 'sha256sum' not found)!"
fi

if [[ "$sha" != "$VENDOR_SHA" ]]
then
odie <<EOS
Checksum mismatch.
Expected: $VENDOR_SHA
Actual: $sha
Archive: $CACHED_LOCATION
To retry an incomplete download, remove the file above.
EOS
fi
}

install() {
local tar_args
local verb

if [[ -n "$HOMEBREW_VERBOSE" ]]
then
tar_args="xvzf"
else
tar_args="xzf"
fi

mkdir -p "$VENDOR_DIR/portable-$VENDOR_NAME"
safe_cd "$VENDOR_DIR/portable-$VENDOR_NAME"

trap '' SIGINT

if [[ -d "$VENDOR_VERSION" ]]
then
verb="reinstall"
mv "$VENDOR_VERSION" "$VENDOR_VERSION.reinstall"
elif [[ -n "$(ls -A .)" ]]
then
verb="upgrade"
else
verb="install"
fi

safe_cd "$VENDOR_DIR"
[[ -n "$HOMEBREW_QUIET" ]] || echo "==> Unpacking $(basename "$VENDOR_URL")"
tar "$tar_args" "$CACHED_LOCATION"
safe_cd "$VENDOR_DIR/portable-$VENDOR_NAME"

if "./$VENDOR_VERSION/bin/$VENDOR_NAME" --version >/dev/null 2>&1
then
ln -sfn "$VENDOR_VERSION" current
# remove old vendor installations by sorting files with modified time.
ls -t | grep -Ev "^(current|$VENDOR_VERSION)" | tail -n +4 | xargs rm -rf
if [[ -d "$VENDOR_VERSION.reinstall" ]]
then
rm -rf "$VENDOR_VERSION.reinstall"
fi
else
rm -rf "$VENDOR_VERSION"
if [[ -d "$VENDOR_VERSION.reinstall" ]]
then
mv "$VENDOR_VERSION.reinstall" "$VENDOR_VERSION"
fi
odie "Failed to $verb vendor $VENDOR_NAME."
fi

trap - SIGINT
}

homebrew-vendor-install() {
local option
local url_var
local sha_var

for option in "$@"
do
case "$option" in
-\?|-h|--help|--usage) brew help vendor-install; exit $? ;;
--verbose) HOMEBREW_VERBOSE=1 ;;
--quiet) HOMEBREW_QUIET=1 ;;
--debug) HOMEBREW_DEBUG=1 ;;
--*) ;;
-*)
[[ "$option" = *v* ]] && HOMEBREW_VERBOSE=1
[[ "$option" = *q* ]] && HOMEBREW_QUIET=1
[[ "$option" = *d* ]] && HOMEBREW_DEBUG=1
;;
*)
[[ -n "$VENDOR_NAME" ]] && odie "This command does not take multiple vendor targets"
VENDOR_NAME="$option"
;;
esac
done

[[ -z "$VENDOR_NAME" ]] && odie "This command requires one vendor target."
[[ -n "$HOMEBREW_DEBUG" ]] && set -x

url_var="${VENDOR_NAME}_URL"
sha_var="${VENDOR_NAME}_SHA"
VENDOR_URL="${!url_var}"
VENDOR_SHA="${!sha_var}"

if [[ -z "$VENDOR_URL" || -z "$VENDOR_SHA" ]]
then
odie "Cannot find a vendored version of $VENDOR_NAME."
fi

VENDOR_VERSION="$(<"$VENDOR_DIR/portable-${VENDOR_NAME}-version")"
CACHED_LOCATION="$HOMEBREW_CACHE/$(basename "$VENDOR_URL")"

lock "vendor-install-$VENDOR_NAME"
fetch
install
}
12 changes: 6 additions & 6 deletions Library/Homebrew/config.rb
@@ -1,9 +1,3 @@
# Where downloads (bottles, source tarballs, etc.) are cached
HOMEBREW_CACHE = Pathname.new(ENV["HOMEBREW_CACHE"] || "~/Library/Caches/Homebrew").expand_path

# Where brews installed via URL are cached
HOMEBREW_CACHE_FORMULA = HOMEBREW_CACHE/"Formula"

if ENV["HOMEBREW_BREW_FILE"]
# Path to `bin/brew` main executable in {HOMEBREW_PREFIX}
HOMEBREW_BREW_FILE = Pathname.new(ENV["HOMEBREW_BREW_FILE"])
Expand All @@ -29,6 +23,12 @@
# Where we store built products
HOMEBREW_CELLAR = Pathname.new(ENV["HOMEBREW_CELLAR"])

# Where downloads (bottles, source tarballs, etc.) are cached
HOMEBREW_CACHE = Pathname.new(ENV["HOMEBREW_CACHE"])

# Where brews installed via URL are cached
HOMEBREW_CACHE_FORMULA = HOMEBREW_CACHE/"Formula"

# Where build, postinstall, and test logs of formulae are written to
HOMEBREW_LOGS = Pathname.new(ENV["HOMEBREW_LOGS"] || "~/Library/Logs/Homebrew/").expand_path

Expand Down
86 changes: 86 additions & 0 deletions Library/Homebrew/utils/ruby.sh
@@ -0,0 +1,86 @@
origin-setup-ruby-path() {
if [[ -z "$HOMEBREW_DEVELOPER" ]]
then
unset HOMEBREW_RUBY_PATH
fi

if [[ -z "$HOMEBREW_RUBY_PATH" ]]
then
if [[ -n "$HOMEBREW_OSX" ]]
then
HOMEBREW_RUBY_PATH="/System/Library/Frameworks/Ruby.framework/Versions/Current/usr/bin/ruby"
else
HOMEBREW_RUBY_PATH="$(which ruby)"
if [[ -z "$HOMEBREW_RUBY_PATH" ]]
then
odie "No Ruby found, cannot proceed."
fi
fi
fi

export HOMEBREW_RUBY_PATH
}

setup-ruby-path() {
if [[ -z "$HOMEBREW_USE_VENDOR_RUBY" ]]
then
origin-setup-ruby-path
return
fi

local vendor_dir
local vendor_ruby_current_version
local vendor_ruby_path
local ruby_version_major

vendor_dir="$HOMEBREW_LIBRARY/Homebrew/vendor"
vendor_ruby_current_version="$vendor_dir/portable-ruby/current"
vendor_ruby_path="$vendor_ruby_current_version/bin/ruby"

if [[ -z "$HOMEBREW_DEVELOPER" ]]
then
unset HOMEBREW_RUBY_PATH
fi

if [[ -z "$HOMEBREW_RUBY_PATH" && "$HOMEBREW_COMMAND" != "vendor-install" ]]
then
if [[ -x "$vendor_ruby_path" ]]
then
HOMEBREW_RUBY_PATH="$vendor_ruby_path"

if [[ $(readlink "$vendor_ruby_current_version") != "$(<"$vendor_dir/portable-ruby-version")" ]]
then
if ! brew vendor-install ruby --quiet
then
onoe "Failed to upgrade vendor Ruby."
fi
fi
else
if [[ -n "$HOMEBREW_OSX" ]]
then
HOMEBREW_RUBY_PATH="/System/Library/Frameworks/Ruby.framework/Versions/Current/usr/bin/ruby"
else
HOMEBREW_RUBY_PATH="$(which ruby)"
fi

if [[ -n "$HOMEBREW_RUBY_PATH" ]]
then
ruby_version_major="$("$HOMEBREW_RUBY_PATH" --version)"
ruby_version_major="${ruby_version_major#ruby }"
ruby_version_major="${ruby_version_major%%.*}"
fi

if [[ "$ruby_version_major" != "2" ]]
then
brew vendor-install ruby --quiet
if [[ ! -x "$vendor_ruby_path" ]]
then
odie "Failed to install vendor Ruby."
fi
HOMEBREW_RUBY_PATH="$vendor_ruby_path"
fi
fi
fi

export HOMEBREW_RUBY_PATH
}
1 change: 1 addition & 0 deletions Library/Homebrew/vendor/portable-ruby-version
@@ -0,0 +1 @@
2.0.0-p648
33 changes: 12 additions & 21 deletions Library/brew.sh
Expand Up @@ -64,31 +64,12 @@ fi
unset GEM_HOME
unset GEM_PATH

if [[ -z "$HOMEBREW_DEVELOPER" ]]
then
unset HOMEBREW_RUBY_PATH
fi

HOMEBREW_SYSTEM="$(uname -s)"
case "$HOMEBREW_SYSTEM" in
Darwin) HOMEBREW_OSX="1";;
Linux) HOMEBREW_LINUX="1";;
esac

if [[ -z "$HOMEBREW_RUBY_PATH" ]]
then
if [[ -n "$HOMEBREW_OSX" ]]
then
HOMEBREW_RUBY_PATH="/System/Library/Frameworks/Ruby.framework/Versions/Current/usr/bin/ruby"
else
HOMEBREW_RUBY_PATH="$(which ruby)"
if [[ -z "$HOMEBREW_RUBY_PATH" ]]
then
odie "No Ruby found, cannot proceed."
fi
fi
fi

HOMEBREW_CURL="/usr/bin/curl"
if [[ -n "$HOMEBREW_OSX" ]]
then
Expand Down Expand Up @@ -116,6 +97,11 @@ HOMEBREW_USER_AGENT="$HOMEBREW_PRODUCT/$HOMEBREW_VERSION ($HOMEBREW_SYSTEM; $HOM
HOMEBREW_CURL_VERSION="$("$HOMEBREW_CURL" --version 2>/dev/null | head -n1 | /usr/bin/awk '{print $1"/"$2}')"
HOMEBREW_USER_AGENT_CURL="$HOMEBREW_USER_AGENT $HOMEBREW_CURL_VERSION"

if [[ -z "$HOMEBREW_CACHE" ]]
then
HOMEBREW_CACHE="$HOME/Library/Caches/Homebrew"
fi

# Declared in bin/brew
export HOMEBREW_BREW_FILE
export HOMEBREW_PREFIX
Expand All @@ -124,8 +110,8 @@ export HOMEBREW_LIBRARY

# Declared in brew.sh
export HOMEBREW_VERSION
export HOMEBREW_CACHE
export HOMEBREW_CELLAR
export HOMEBREW_RUBY_PATH
export HOMEBREW_SYSTEM
export HOMEBREW_CURL
export HOMEBREW_PROCESSOR
Expand Down Expand Up @@ -212,7 +198,7 @@ fi
if [[ "$(id -u)" = "0" && "$(/usr/bin/stat -f%u "$HOMEBREW_BREW_FILE")" != "0" ]]
then
case "$HOMEBREW_COMMAND" in
analytics|install|reinstall|postinstall|link|pin|update|upgrade|create|migrate|tap|tap-pin|switch)
analytics|install|reinstall|postinstall|link|pin|update|upgrade|vendor-install|create|migrate|tap|tap-pin|switch)
odie <<EOS
Cowardly refusing to 'sudo brew $HOMEBREW_COMMAND'
You can use brew with sudo, but only if the brew executable is owned by root.
Expand All @@ -223,6 +209,11 @@ EOS
esac
fi

# Hide shellcheck complaint:
# shellcheck source=/dev/null
source "$HOMEBREW_LIBRARY/Homebrew/utils/ruby.sh"
setup-ruby-path

# Hide shellcheck complaint:
# shellcheck source=/dev/null
source "$HOMEBREW_LIBRARY/Homebrew/utils/analytics.sh"
Expand Down

0 comments on commit 7508da2

Please sign in to comment.