Skip to content

Commit d8c40de

Browse files
committed
Add support for specifying mirror url by a command
Signed-off-by: Mingxiang Xue <mingxiangxue@gmail.com>
1 parent 88dd8db commit d8c40de

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ The build process may be configured through the following environment variables:
8484
| `RUBY_BUILD_WGET_OPTS` | Additional options to pass to `wget` for downloading. |
8585
| `RUBY_BUILD_MIRROR_URL` | Custom mirror URL root. |
8686
| `RUBY_BUILD_MIRROR_PACKAGE_URL` | Custom complete mirror URL (e.g. http://mirror.example.com/package-1.0.0.tar.gz). |
87+
| `RUBY_BUILD_MIRROR_CMD` | Custom mirror command (see [below](#more-flexible-mirror-url) for the usage). |
8788
| `RUBY_BUILD_SKIP_MIRROR` | Bypass the download mirror and fetch all package files from their original URLs. |
8889
| `RUBY_BUILD_TARBALL_OVERRIDE` | Override the URL to fetch the ruby tarball from, optionally followed by `#checksum`. |
8990
| `RUBY_BUILD_DEFINITIONS` | Colon-separated list of paths to search for build definition files. |
@@ -150,6 +151,25 @@ complete URL by setting `RUBY_BUILD_MIRROR_PACKAGE_URL`. It behaves the same as
150151
The default ruby-build download mirror is sponsored by
151152
[Basecamp](https://basecamp.com/).
152153

154+
#### More flexible mirror URL
155+
156+
For more flexible mirror URL, you can provide a custom command to output the
157+
desired mirror URL, as shown below:
158+
159+
```sh
160+
# There are two arguments:
161+
# 1st arg: the original URL without checksum
162+
# 2nd arg: the checksum
163+
$ cat <<'EOF' >./get_mirror_url && chmod +x ./get_mirror_url
164+
#!/bin/sh
165+
echo "$1" | sed "s/cache.ruby-lang.org/mirror.example.com/"
166+
EOF
167+
168+
$ export RUBY_BUILD_MIRROR_CMD="$(pwd)/get_mirror_url"
169+
```
170+
171+
After executing the above script in your shell, install a version as usual.
172+
153173
#### Keeping the build directory after installation
154174

155175
Both `ruby-build` and `rbenv install` accept the `-k` or `--keep` flag, which

bin/ruby-build

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,8 @@ fetch_tarball() {
474474
fi
475475
elif [ -n "$RUBY_BUILD_MIRROR_PACKAGE_URL" ]; then
476476
mirror_url="$RUBY_BUILD_MIRROR_PACKAGE_URL"
477+
elif [ -n "$RUBY_BUILD_MIRROR_CMD" ]; then
478+
mirror_url="$("$RUBY_BUILD_MIRROR_CMD" "$package_url" "$checksum")"
477479
fi
478480
fi
479481

@@ -1537,7 +1539,7 @@ else
15371539
unset RUBY_BUILD_CACHE_PATH
15381540
fi
15391541

1540-
if [ -z "$RUBY_BUILD_MIRROR_URL" ] && [ -z "$RUBY_BUILD_MIRROR_PACKAGE_URL" ]; then
1542+
if [ -z "$RUBY_BUILD_MIRROR_URL" -a -z "$RUBY_BUILD_MIRROR_PACKAGE_URL" -a -z "$RUBY_BUILD_MIRROR_CMD" ]; then
15411543
RUBY_BUILD_MIRROR_URL="https://dqw8nmjcqpjn7.cloudfront.net"
15421544
RUBY_BUILD_DEFAULT_MIRROR=1
15431545
else
@@ -1546,7 +1548,7 @@ else
15461548
fi
15471549

15481550
if [ -n "$RUBY_BUILD_SKIP_MIRROR" ] || ! has_checksum_support compute_sha2; then
1549-
unset RUBY_BUILD_MIRROR_URL RUBY_BUILD_MIRROR_PACKAGE_URL
1551+
unset RUBY_BUILD_MIRROR_URL RUBY_BUILD_MIRROR_PACKAGE_URL RUBY_BUILD_MIRROR_CMD
15501552
fi
15511553

15521554
ARIA2_OPTS="${RUBY_BUILD_ARIA2_OPTS} ${IPV4+--disable-ipv6=true} ${IPV6+--disable-ipv6=false}"

test/mirror.bats

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,3 +182,27 @@ DEF
182182
unstub curl
183183
unstub shasum
184184
}
185+
186+
187+
@test "package is fetched from the URL returned by a commond" {
188+
export RUBY_BUILD_MIRROR_URL=
189+
export RUBY_BUILD_MIRROR_CMD=get_mirror_url
190+
191+
local checksum="ba988b1bb4250dee0b9dd3d4d722f9c64b2bacfc805d1b6eba7426bda72dd3c5"
192+
193+
stub get_mirror_url 'echo "${1/cache.ruby-lang.org/mirror.example.com}#$2"'
194+
stub shasum true "echo $checksum"
195+
stub curl "-*I* https://mirror.example.com/packages/package-1.0.0.tar.gz#$checksum : true" \
196+
"-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"
197+
198+
run_inline_definition <<DEF
199+
install_package "package-1.0.0" "https://cache.ruby-lang.org/packages/package-1.0.0.tar.gz#$checksum" copy
200+
DEF
201+
202+
assert_success
203+
assert [ -x "${INSTALL_ROOT}/bin/package" ]
204+
205+
unstub curl
206+
unstub shasum
207+
unstub get_mirror_url
208+
}

0 commit comments

Comments
 (0)