Skip to content

Commit 3306271

Browse files
committed
Add support for specifying mirror url by a command
Signed-off-by: Mingxiang Xue <[email protected]>
1 parent 57c397d commit 3306271

File tree

3 files changed

+49
-3
lines changed

3 files changed

+49
-3
lines changed

README.md

+21-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ The build process may be configured through the following environment variables:
7474
| `RUBY_BUILD_CURL_OPTS` | Additional options to pass to `curl` for downloading. |
7575
| `RUBY_BUILD_WGET_OPTS` | Additional options to pass to `wget` for downloading. |
7676
| `RUBY_BUILD_MIRROR_URL` | Custom mirror URL root. |
77-
| `RUBY_BUILD_MIRROR_PACKAGE_URL` | Custom complete mirror URL (e.g. http://mirror.example.com/package-1.0.0.tar.gz). |
77+
| `RUBY_BUILD_MIRROR_PACKAGE_URL` | Custom complete mirror URL (e.g. http://mirror.example.com/package-1.0.0.tar.gz). |
78+
| `RUBY_BUILD_MIRROR_CMD` | Custom mirror command (see [below](#more-flexible-mirror-url) for the usage). |
7879
| `RUBY_BUILD_SKIP_MIRROR` | Bypass the download mirror and fetch all package files from their original URLs. |
7980
| `RUBY_BUILD_ROOT` | Custom build definition directory. (Default: `share/ruby-build`) |
8081
| `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
142143
The default ruby-build download mirror is sponsored by
143144
[Basecamp](https://basecamp.com/).
144145

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

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

bin/ruby-build

+4-2
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,8 @@ fetch_tarball() {
367367
fi
368368
elif [ -n "$RUBY_BUILD_MIRROR_PACKAGE_URL" ]; then
369369
mirror_url="$RUBY_BUILD_MIRROR_PACKAGE_URL"
370+
elif [ -n "$RUBY_BUILD_MIRROR_CMD" ]; then
371+
mirror_url="$("$RUBY_BUILD_MIRROR_CMD" "$package_url" "$checksum")"
370372
fi
371373
fi
372374

@@ -1421,7 +1423,7 @@ else
14211423
unset RUBY_BUILD_CACHE_PATH
14221424
fi
14231425

1424-
if [ -z "$RUBY_BUILD_MIRROR_URL" -a -z "$RUBY_BUILD_MIRROR_PACKAGE_URL" ]; then
1426+
if [ -z "$RUBY_BUILD_MIRROR_URL" -a -z "$RUBY_BUILD_MIRROR_PACKAGE_URL" -a -z "$RUBY_BUILD_MIRROR_CMD" ]; then
14251427
RUBY_BUILD_MIRROR_URL="https://dqw8nmjcqpjn7.cloudfront.net"
14261428
RUBY_BUILD_DEFAULT_MIRROR=1
14271429
else
@@ -1430,7 +1432,7 @@ else
14301432
fi
14311433

14321434
if [ -n "$RUBY_BUILD_SKIP_MIRROR" ] || ! has_checksum_support compute_sha2; then
1433-
unset RUBY_BUILD_MIRROR_URL RUBY_BUILD_MIRROR_PACKAGE_URL
1435+
unset RUBY_BUILD_MIRROR_URL RUBY_BUILD_MIRROR_PACKAGE_URL RUBY_BUILD_MIRROR_CMD
14341436
fi
14351437

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

test/mirror.bats

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

0 commit comments

Comments
 (0)