From 91c2f4ff0ccfd1ed85ddd3ade0c42a26f377685b Mon Sep 17 00:00:00 2001 From: Alex Rodionov Date: Tue, 28 Nov 2023 13:54:06 -0800 Subject: [PATCH] feat: support registering system Ruby --- .github/workflows/ci.yml | 7 +++++++ docs/repository_rules.md | 3 ++- ruby/private/download.bzl | 7 +++++++ ruby/private/toolchain.bzl | 9 +++++++-- 4 files changed, 23 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index bbdf4ad5..aadcb17c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -34,6 +34,7 @@ jobs: - 3.3.0-preview3 - jruby-9.4.5.0 - truffleruby-23.1.1 + - system os: - ubuntu - macos @@ -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 @@ -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 diff --git a/docs/repository_rules.md b/docs/repository_rules.md index d829273f..8737698b 100644 --- a/docs/repository_rules.md +++ b/docs/repository_rules.md @@ -91,6 +91,7 @@ rb_register_toolchains(name, name | base name of resulting repositories, by default "rules_ruby" | "rules_ruby" | -| version | a semver version of Matz Ruby Interpreter, or a string like [interpreter type]-[version] | None | +| version | a semver version of Matz Ruby Interpreter, or a string like [interpreter type]-[version], or "system" | None | | register | whether to register the resulting toolchains, should be False under bzlmod | True | | kwargs | additional parameters to the downloader for this interpreter type | none | diff --git a/ruby/private/download.bzl b/ruby/private/download.bzl index c6ecee4b..c0b778c4 100644 --- a/ruby/private/download.bzl +++ b/ruby/private/download.bzl @@ -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: @@ -94,6 +96,11 @@ 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("bundle"), "dist/bin/bundle") + repository_ctx.symlink(repository_ctx.which("gem"), "dist/bin/gem") + rb_download = repository_rule( implementation = _rb_download_impl, attrs = { diff --git a/ruby/private/toolchain.bzl b/ruby/private/toolchain.bzl index e934058b..8fbbc31a 100644 --- a/ruby/private/toolchain.bzl +++ b/ruby/private/toolchain.bzl @@ -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 @@ -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),