Skip to content

Commit

Permalink
feat: support registering system Ruby
Browse files Browse the repository at this point in the history
  • Loading branch information
p0deje committed Nov 28, 2023
1 parent 63fbb9c commit 2dfc616
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ jobs:
- 3.3.0-preview3
- jruby-9.4.5.0
- truffleruby-23.1.1
- system
os:
- ubuntu
- macos
Expand All @@ -49,6 +50,8 @@ jobs:
- os: windows
ruby: truffleruby-23.1.1
# https://github.com/p0deje/rules_ruby/issues/23
- mode: bzlmod
ruby: system
- mode: bzlmod
ruby: 3.0.6
- mode: bzlmod
Expand All @@ -72,6 +75,10 @@ jobs:
build --announce_rc
${{ matrix.mode == 'bzlmod' && 'common --enable_bzlmod' || '' }}
- run: echo 'RUBY_VERSION = "${{ matrix.ruby }}"' > ruby_version.bzl
- if: matrix.ruby == 'system'
uses: ruby/setup-ruby@v1
with:
ruby-version: '3.0.6'
- run: bazel test ...
- run: bazel run lib/gem:add-numbers 2
- run: bazel run lib/gem:print-version
Expand Down
3 changes: 2 additions & 1 deletion docs/repository_rules.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions ruby/private/download.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ _RUBY_INSTALLER_URL = "https://github.com/oneclick/rubyinstaller2/releases/downl
def _rb_download_impl(repository_ctx):
if repository_ctx.attr.version.startswith("jruby"):
_install_jruby(repository_ctx)
elif repository_ctx.attr.version == "system":
_symlink_system_ruby(repository_ctx)
elif repository_ctx.os.name.startswith("windows"):
_install_via_rubyinstaller(repository_ctx)
else:
Expand Down Expand Up @@ -94,6 +96,14 @@ def _install_via_ruby_build(repository_ctx):
if result.return_code != 0:
fail("%s\n%s" % (result.stdout, result.stderr))

def _symlink_system_ruby(repository_ctx):
repository_ctx.symlink(repository_ctx.which("ruby"), "dist/bin/ruby")
repository_ctx.symlink(repository_ctx.which("gem"), "dist/bin/gem")
if repository_ctx.os.name.startswith("windows"):
repository_ctx.symlink(repository_ctx.which("bundle.cmd"), "dist/bin/bundle.cmd")
else:
repository_ctx.symlink(repository_ctx.which("bundle"), "dist/bin/bundle")

rb_download = repository_rule(
implementation = _rb_download_impl,
attrs = {
Expand Down
9 changes: 7 additions & 2 deletions ruby/private/toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def rb_register_toolchains(name = DEFAULT_RUBY_REPOSITORY, version = None, regis
* _(For MRI on Windows)_ Installed using [RubyInstaller](https://rubyinstaller.org).
* _(For JRuby on any OS)_ Downloaded and installed directly from [official website](https://www.jruby.org).
* _(For TruffleRuby on Linux and macOS)_ Installed using [ruby-build](https://github.com/rbenv/ruby-build).
* _(For "system") System Ruby is used. Please note that builds are not hermetic in this case.
`WORKSPACE`:
```bazel
Expand All @@ -25,14 +26,18 @@ def rb_register_toolchains(name = DEFAULT_RUBY_REPOSITORY, version = None, regis
Args:
name: base name of resulting repositories, by default "rules_ruby"
version: a semver version of Matz Ruby Interpreter, or a string like [interpreter type]-[version]
version: a semver version of Matz Ruby Interpreter, or a string like [interpreter type]-[version], or "system"
register: whether to register the resulting toolchains, should be False under bzlmod
**kwargs: additional parameters to the downloader for this interpreter type
"""
repo_name = name + "_dist"
proxy_repo_name = name + "_toolchains"
if repo_name not in native.existing_rules().values():
_rb_download(name = repo_name, version = version, **kwargs)
_rb_download(
name = repo_name,
version = version,
**kwargs
)
_rb_toolchain_repository_proxy(
name = proxy_repo_name,
toolchain = "@{}//:toolchain".format(repo_name),
Expand Down

0 comments on commit 2dfc616

Please sign in to comment.