Skip to content

Commit

Permalink
Add support for specifying mirror url by a command
Browse files Browse the repository at this point in the history
Signed-off-by: Mingxiang Xue <mingxiangxue@gmail.com>
  • Loading branch information
uzxmx committed Aug 11, 2021
1 parent 57c397d commit 3306271
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 3 deletions.
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) |
Expand Down Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions bin/ruby-build
Original file line number Diff line number Diff line change
Expand Up @@ -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="$("$RUBY_BUILD_MIRROR_CMD" "$package_url" "$checksum")"
fi
fi

Expand Down Expand Up @@ -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
Expand All @@ -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}"
Expand Down
24 changes: 24 additions & 0 deletions test/mirror.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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 <<DEF
install_package "package-1.0.0" "https://cache.ruby-lang.org/packages/package-1.0.0.tar.gz#$checksum" copy
DEF

assert_success
assert [ -x "${INSTALL_ROOT}/bin/package" ]

unstub curl
unstub shasum
unstub get_mirror_url
}

0 comments on commit 3306271

Please sign in to comment.