Skip to content

Commit 084fafc

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

File tree

3 files changed

+56
-3
lines changed

3 files changed

+56
-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 export a shell function 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+
mirror_cmd() {
156+
echo "${1/cache.ruby-lang.org/mirror.example.com}"
157+
}
158+
159+
export -f mirror_cmd
160+
export RUBY_BUILD_MIRROR_CMD=mirror_cmd
161+
```
162+
163+
After `source`ing 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

+31
Original file line numberDiff line numberDiff line change
@@ -185,3 +185,34 @@ 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+
193+
mirror_cmd() {
194+
local package_url="$1"
195+
local package_checksum="$2"
196+
197+
echo "${package_url/cache.ruby-lang.org/mirror.example.com}#$package_checksum"
198+
}
199+
200+
export -f mirror_cmd
201+
export RUBY_BUILD_MIRROR_CMD=mirror_cmd
202+
203+
local checksum="ba988b1bb4250dee0b9dd3d4d722f9c64b2bacfc805d1b6eba7426bda72dd3c5"
204+
205+
stub shasum true "echo $checksum"
206+
stub curl "-*I* https://mirror.example.com/packages/package-1.0.0.tar.gz#$checksum : true" \
207+
"-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"
208+
209+
run_inline_definition <<DEF
210+
install_package "package-1.0.0" "https://cache.ruby-lang.org/packages/package-1.0.0.tar.gz#$checksum" copy
211+
DEF
212+
213+
assert_success
214+
assert [ -x "${INSTALL_ROOT}/bin/package" ]
215+
216+
unstub curl
217+
unstub shasum
218+
}

0 commit comments

Comments
 (0)