diff --git a/README.md b/README.md index c7ff73cdb5..1f5ceceff3 100644 --- a/README.md +++ b/README.md @@ -74,7 +74,8 @@ The build process may be configured through the following environment variables: | `RUBY_BUILD_CURL_OPTS` | Additional options to pass to `curl` for downloading. | | `RUBY_BUILD_WGET_OPTS` | Additional options to pass to `wget` for downloading. | | `RUBY_BUILD_MIRROR_URL` | Custom mirror URL root. | -| `RUBY_BUILD_MIRROR_PACKAGE_URL` | Custom complete mirror URL (e.g. http://mirror.example.com/package-1.0.0.tar.gz). | +| `RUBY_BUILD_MIRROR_PACKAGE_URL` | Custom complete mirror URL (e.g. http://mirror.example.com/package-1.0.0.tar.gz). | +| `RUBY_BUILD_MIRROR_CMD` | Custom mirror command (see [below](#more-flexible-mirror-url) for the usage). | | `RUBY_BUILD_SKIP_MIRROR` | Bypass the download mirror and fetch all package files from their original URLs. | | `RUBY_BUILD_ROOT` | Custom build definition directory. (Default: `share/ruby-build`) | | `RUBY_BUILD_DEFINITIONS` | Additional paths to search for build definitions. (Colon-separated list) | @@ -142,6 +143,25 @@ complete URL by setting `RUBY_BUILD_MIRROR_PACKAGE_URL`. It behaves the same as The default ruby-build download mirror is sponsored by [Basecamp](https://basecamp.com/). +#### More flexible mirror URL + +For more flexible mirror URL, you can provide a custom command to output the +desired mirror URL, as shown below: + +```sh +# There are two arguments: +# 1st arg: the original URL without checksum +# 2nd arg: the checksum +$ cat <<'EOF' >./get_mirror_url && chmod +x ./get_mirror_url +#!/bin/sh +echo "$1" | sed "s/cache.ruby-lang.org/mirror.example.com/" +EOF + +$ export RUBY_BUILD_MIRROR_CMD="$(pwd)/get_mirror_url" +``` + +After executing the above script in your shell, install a version as usual. + #### Keeping the build directory after installation Both `ruby-build` and `rbenv install` accept the `-k` or `--keep` flag, which diff --git a/bin/ruby-build b/bin/ruby-build index ad60efd2cf..7652345adc 100755 --- a/bin/ruby-build +++ b/bin/ruby-build @@ -367,6 +367,8 @@ fetch_tarball() { fi elif [ -n "$RUBY_BUILD_MIRROR_PACKAGE_URL" ]; then mirror_url="$RUBY_BUILD_MIRROR_PACKAGE_URL" + elif [ -n "$RUBY_BUILD_MIRROR_CMD" ]; then + mirror_url="$(command "$RUBY_BUILD_MIRROR_CMD" "$package_url" "$checksum")" fi fi @@ -1421,7 +1423,7 @@ else unset RUBY_BUILD_CACHE_PATH fi -if [ -z "$RUBY_BUILD_MIRROR_URL" -a -z "$RUBY_BUILD_MIRROR_PACKAGE_URL" ]; then +if [ -z "$RUBY_BUILD_MIRROR_URL" -a -z "$RUBY_BUILD_MIRROR_PACKAGE_URL" -a -z "$RUBY_BUILD_MIRROR_CMD" ]; then RUBY_BUILD_MIRROR_URL="https://dqw8nmjcqpjn7.cloudfront.net" RUBY_BUILD_DEFAULT_MIRROR=1 else @@ -1430,7 +1432,7 @@ else fi if [ -n "$RUBY_BUILD_SKIP_MIRROR" ] || ! has_checksum_support compute_sha2; then - unset RUBY_BUILD_MIRROR_URL RUBY_BUILD_MIRROR_PACKAGE_URL + unset RUBY_BUILD_MIRROR_URL RUBY_BUILD_MIRROR_PACKAGE_URL RUBY_BUILD_MIRROR_CMD fi ARIA2_OPTS="${RUBY_BUILD_ARIA2_OPTS} ${IPV4+--disable-ipv6=true} ${IPV6+--disable-ipv6=false}" diff --git a/test/mirror.bats b/test/mirror.bats index e1b8d2e10b..daeb169c10 100755 --- a/test/mirror.bats +++ b/test/mirror.bats @@ -185,3 +185,27 @@ DEF unstub curl unstub shasum } + + +@test "package is fetched from the URL returned by a commond" { + export RUBY_BUILD_MIRROR_URL= + export RUBY_BUILD_MIRROR_CMD=get_mirror_url + + local checksum="ba988b1bb4250dee0b9dd3d4d722f9c64b2bacfc805d1b6eba7426bda72dd3c5" + + stub get_mirror_url 'echo "${1/cache.ruby-lang.org/mirror.example.com}#$2"' + stub shasum true "echo $checksum" + stub curl "-*I* https://mirror.example.com/packages/package-1.0.0.tar.gz#$checksum : true" \ + "-q -o * -*S* https://mirror.example.com/packages/package-1.0.0.tar.gz#$checksum : cp $FIXTURE_ROOT/package-1.0.0.tar.gz \$3" + + run_inline_definition <