### Run a Ruby Script Example Source: https://github.com/bazel-contrib/rules_ruby/blob/main/docs/rules.md This example demonstrates how to run a Ruby script that prints its version. It utilizes rb_library to define the script as a library and rb_binary to create an executable target for it. The output shows the version being printed. ```ruby module GEM VERSION = '0.1.0' end puts "Version is: #{GEM::VERSION}" if __FILE__ == $PROGRAM_NAME ``` -------------------------------- ### Define rb_bundle_fetch with Gemfile and Lockfile Source: https://github.com/bazel-contrib/rules_ruby/blob/main/docs/repository_rules.md This example demonstrates the basic usage of the rb_bundle_fetch rule, specifying the required 'name', 'gemfile', and 'gemfile_lock' attributes. It also includes optional 'srcs' for additional files needed during installation. Dependencies fetched by this rule are accessible via the '@bundle' target. ```bazel load("@rules_ruby//ruby:deps.bzl", "rb_bundle_fetch") rb_bundle_fetch( name = "bundle", gemfile = "//:Gemfile", gemfile_lock = "//:Gemfile.lock", srcs = [ "//:gem.gemspec", "//:lib/gem/version.rb", ] ) ``` -------------------------------- ### rb_gem_install - Install Ruby Gem Source: https://context7.com/bazel-contrib/rules_ruby/llms.txt Installs a built .gem file locally using `gem install`. This rule is useful for testing gem installation before publishing. It depends on the output of `rb_gem_build`. ```bazel # BUILD load("@rules_ruby//ruby:defs.bzl", "rb_gem_build", "rb_gem_install") rb_gem_build( name = "gem-build", gemspec = "gem.gemspec", deps = ["//lib:gem"], ) rb_gem_install( name = "gem-install", gem = ":gem-build", ) ``` -------------------------------- ### Ruby Code Snippet Example in Jekyll Source: https://github.com/bazel-contrib/rules_ruby/blob/main/examples/jekyll/_posts/2024-02-03-welcome-to-jekyll.markdown Demonstrates how to highlight a Ruby code snippet within a Jekyll post using the `highlight` tag. This snippet defines a function to print a greeting and then calls it. It's useful for showcasing Ruby code examples on a Jekyll site. ```ruby def print_hi(name) puts "Hi, #{name}" end print_hi('Tom') #=> prints 'Hi, Tom' to STDOUT. ``` -------------------------------- ### Direct Toolchain Access - Run Gem Command Source: https://context7.com/bazel-contrib/rules_ruby/llms.txt Provides direct access to the `gem` command via Bazel. This allows for common gem operations like listing installed gems or installing specific versions. ```bash # Run gem command bazel run @ruby//:gem -- list # Install specific gem bazel run @ruby//:gem -- install rails --version 7.0.0 ``` -------------------------------- ### Run a Ruby Arithmetic Script Source: https://github.com/bazel-contrib/rules_ruby/blob/main/docs/rules.md This example shows how to execute a general-purpose Ruby script that performs addition. The script takes arguments from the command line and uses rb_binary to run it, demonstrating the ability to pass arguments to the script. ```ruby #!/usr/bin/env ruby a, b = *ARGV puts Integer(a) + Integer(b) ``` -------------------------------- ### Run All Bazel Tests Source: https://github.com/bazel-contrib/rules_ruby/blob/main/examples/deep_gem/README.md Executes all tests defined within the Bazel workspace. This command is used to verify the functionality and correctness of the Ruby gem and its associated specifications. ```bash bazel test //... ``` -------------------------------- ### Define and Install Ruby Gem with Bazel Source: https://github.com/bazel-contrib/rules_ruby/blob/main/docs/rules.md This snippet shows how to define a Ruby gem build target using rb_gem_build and then install the resulting gem package using rb_gem_install within a Bazel BUILD file. It assumes the gem's sources and gem definition are already set up. ```bazel load("@rules_ruby//ruby:defs.bzl", "rb_gem_build", "rb_gem_install") package(default_visibility = ["//:__subpackages__"]) rb_gem_build( name = "gem-build", gemspec = "gem.gemspec", deps = ["//lib:gem"], ) rb_gem_install( name = "gem-install", gem = ":gem-build", ) ``` -------------------------------- ### rb_bundle_install Source: https://github.com/bazel-contrib/rules_ruby/blob/main/docs/rules.md Installs Bundler dependencies from cached gems. This is an internal rule typically used by `rb_bundle_fetch()` and not intended for direct invocation. ```APIDOC ## rb_bundle_install ### Description Installs Bundler dependencies from cached gems. This rule is an internal dependency for `rb_bundle_fetch()`. ### Method N/A (Bazel rule) ### Endpoint N/A (Bazel rule) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```bazel rb_bundle_install( name = "install_deps", gemfile = "//:Gemfile", gemfile_lock = "//:Gemfile.lock", gems = ["@my_gems//:gem_bundle"], # Optional attributes # srcs = ["//ruby/lib:source.rb"], # env = {"MY_VAR": "value"}, # ruby = "@ruby_2_7//:ruby_toolchain", ) ``` ### Response #### Success Response (N/A) This is a Bazel rule, not an HTTP endpoint. Success is determined by Bazel's build process. #### Response Example N/A ``` -------------------------------- ### Run All RSpec Specification Tests Source: https://github.com/bazel-contrib/rules_ruby/blob/main/examples/deep_gem/README.md Executes all RSpec specification tests defined under the `//hello_world/spec` Bazel package. This command is useful for running a complete set of tests related to the gem's specifications. ```bash bazel test //hello_world/spec:all ``` -------------------------------- ### Install Bundler Dependencies with rb_bundle_rule (Bazel) Source: https://github.com/bazel-contrib/rules_ruby/blob/main/docs/repository_rules.md The `rb_bundle_rule` function, deprecated in favor of `rb_bundle_fetch`, installs Bundler dependencies and registers an external repository. It takes attributes like `name`, `srcs`, `env`, `gemfile`, `repo_mapping`, and `toolchain`. The `gemfile` attribute specifies the location of the Gemfile, and `srcs` lists Ruby source files. ```bazel load("@rules_ruby//ruby:deps.bzl", "rb_bundle") rb_bundle( name = "bundle", gemfile = "//:Gemfile", srcs = [ "//:gem.gemspec", "//:lib/gem/version.rb", ] ) ``` -------------------------------- ### Register Ruby Toolchains in WORKSPACE Source: https://github.com/bazel-contrib/rules_ruby/blob/main/docs/repository_rules.md Registers a Ruby toolchain using rb_register_toolchains in the WORKSPACE file. This example shows how to specify the Ruby version. ```bazel load("@rules_ruby//ruby:deps.bzl", "rb_register_toolchains") rb_register_toolchains( version = "3.0.6" ) ``` -------------------------------- ### rb_bundle_install Bazel Rule Source: https://github.com/bazel-contrib/rules_ruby/blob/main/docs/rules.md The `rb_bundle_install` rule is used to install Bundler dependencies from cached gems. It requires specifying the name, gemfile, gemfile_lock, and gems. Optional attributes include srcs, env, and ruby for environment configuration and toolchain overrides. This rule is typically an internal dependency of `rb_bundle_fetch`. ```bzl load("@rules_ruby//ruby:defs.bzl", "rb_bundle_install") rb_bundle_install( name = "my_bundle_install", gemfile = "@my_gemset//:Gemfile", gemfile_lock = "@my_gemset//:Gemfile.lock", gems = ["@my_gemset//:gem_a", "@my_gemset//:gem_b"], # Optional attributes: # srcs = [], # env = {"RAILS_ENV": "production"}, # ruby = "@my_ruby_toolchain//:ruby" ) ``` -------------------------------- ### Fetch Bundler Dependencies (WORKSPACE) Source: https://github.com/bazel-contrib/rules_ruby/blob/main/README.md Fetches Bundler dependencies for a Bazel project using the WORKSPACE setup. It requires the Gemfile and Gemfile.lock to be present in the project root. This rule ensures that gem dependencies are available during the build. ```bazel # WORKSPACE load(@rules_ruby//ruby:deps.bzl", "rb_bundle_fetch") rb_bundle_fetch( name = "bundle", gemfile = "//:Gemfile", gemfile_lock = "//:Gemfile.lock", ) ``` -------------------------------- ### Register Ruby Toolchains (Bzlmod) Source: https://github.com/bazel-contrib/rules_ruby/blob/main/README.md Registers Ruby toolchains for Bazel using the Bzlmod setup. This involves using a Bazel extension to define the toolchain, allowing for version specification or loading from a .ruby-version file. It's the Bzlmod equivalent of WORKSPACE's rb_register_toolchains. ```bazel # MODULE.bazel ruby = use_extension(@rules_ruby//ruby:extensions.bzl", "ruby") ruby.toolchain( name = "ruby", version = "3.0.6", # alternatively, load version from .ruby-version file # version_file = "//:.ruby-version", ) use_repo(ruby, "ruby") ``` -------------------------------- ### Configure Bundle Fetch for Deep Gem Structure Source: https://github.com/bazel-contrib/rules_ruby/blob/main/examples/deep_gem/README.md Configures the `bundle_fetch` rule to handle a Ruby gem located in a subdirectory. It specifies the gemspec, version file, Gemfile, and Gemfile.lock using Bazel's standard package notation, allowing for proper dependency management of gems not at the workspace root. ```starlark ruby.bundle_fetch( name = "bundle", srcs = [ "//hello_world:hello_world.gemspec", # Gemspec in subdirectory "//hello_world/lib/hello_world:version.rb", # Version file in nested structure ], gemfile = "//hello_world:Gemfile", gemfile_lock = "//hello_world:Gemfile.lock", ) ``` -------------------------------- ### Register Ruby Toolchains (WORKSPACE) Source: https://github.com/bazel-contrib/rules_ruby/blob/main/README.md Registers Ruby toolchains for Bazel using the WORKSPACE setup. It allows specifying the Ruby version directly or loading it from a .ruby-version file. This is a prerequisite for using Ruby rules in your project. ```bazel # WORKSPACE load(@rules_ruby//ruby:deps.bzl", "rb_register_toolchains") rb_register_toolchains( version = "3.3.9", # alternatively, load version from .ruby-version file # version_file = "//:.ruby-version", ) ``` -------------------------------- ### Run Specific RSpec Test Suite Source: https://github.com/bazel-contrib/rules_ruby/blob/main/examples/deep_gem/README.md Executes a specific RSpec test suite within the Bazel workspace. This allows for targeted testing of particular components or functionalities of the Ruby gem, such as the speaker or core hello_world logic. ```bash bazel test //hello_world/spec:hello_world_spec bazel test //hello_world/spec:speaker_spec ``` -------------------------------- ### Fetch Bundler Dependencies (Bzlmod) Source: https://github.com/bazel-contrib/rules_ruby/blob/main/README.md Fetches Bundler dependencies for a Bazel project using the Bzlmod setup. Similar to the WORKSPACE version, it requires Gemfile and Gemfile.lock. This rule integrates with Bzlmod's module system to make gem dependencies available. ```bazel # MODULE.bazel ruby.bundle_fetch( name = "bundle", gemfile = "//:Gemfile", gemfile_lock = "//:Gemfile.lock", ) use_repo(ruby, "bundle", "ruby_toolchains") ``` -------------------------------- ### RubyGem Specification (gemspec) Source: https://github.com/bazel-contrib/rules_ruby/blob/main/docs/rules.md This is an example of a `gemspec` file used to define metadata for a RubyGem. It includes information such as the gem's name, version, authors, license, summary, files to include, and runtime/development dependencies. This file is crucial for the `rb_gem_build` rule. ```ruby root = File.expand_path(__dir__) $LOAD_PATH.push(File.expand_path('lib', root)) require 'gem/version' Gem::Specification.new do |s| s.name = 'example' s.version = GEM::VERSION s.authors = ['Foo Bar'] s.email = ['foobar@gmail.com'] s.homepage = 'http://rubygems.org' s.license = 'MIT' s.summary = 'Example' s.description = 'Example gem' s.files = ['Gemfile'] + Dir['lib/**/*'] s.require_paths = ['lib'] s.add_dependency 'rake', '~> 10' s.add_development_dependency 'rspec', '~> 3.0' s.add_development_dependency 'rubocop', '~> 1.10' end ``` -------------------------------- ### Fetch Bundler Dependencies for Bazel Source: https://context7.com/bazel-contrib/rules_ruby/llms.txt Fetches and caches all gems specified in Gemfile.lock, making them available as a Bazel repository. This rule parses the lockfile, downloads gems, verifies checksums, and creates targets for accessing installed gems. ```bazel # WORKSPACE load("@rules_ruby//ruby:deps.bzl", "rb_bundle_fetch") rb_bundle_fetch( name = "bundle", gemfile = "//:Gemfile", gemfile_lock = "//:Gemfile.lock", srcs = [ "//:gem.gemspec", "//:lib/gem/version.rb", ], gem_checksums = { "ast-2.4.2": "1e280232e6a33754cde542bc5ef85520b74db2aac73ec14acef453784447cc12", "concurrent-ruby-1.2.2": "3879119b8b75e3b62616acc256c64a134d0b0a7a9a3fcba5a233025bcde22c4f", "rake-13.1.0": "9c8e0a4bdc0b62d958fc040e949d8ba2e8c1e7f5c03e5e3c3e8e2f2f2f2f2f2f", }, env = { "BUNDLE_FROZEN": "true", }, ) ``` -------------------------------- ### Register Toolchains (Bzlmod) Source: https://github.com/bazel-contrib/rules_ruby/blob/main/README.md Registers the previously defined Ruby toolchains for use within the Bazel build when using the Bzlmod setup. This step makes the configured Ruby toolchains available for targets in your BUILD files. ```bazel # MODULE.bazel register_toolchains(@ruby_toolchains//:all) ``` -------------------------------- ### Register Ruby Toolchain with Bazel Source: https://context7.com/bazel-contrib/rules_ruby/llms.txt Registers a Ruby interpreter toolchain with Bazel. It automatically downloads and installs the specified Ruby version (MRI, JRuby, TruffleRuby, or system Ruby). Supports WORKSPACE and Bzlmod configurations. ```bazel # WORKSPACE load("@rules_ruby//ruby:deps.bzl", "rb_register_toolchains") # Register specific Ruby version rb_register_toolchains( name = "ruby", version = "3.3.9", ) # Or load from .ruby-version file rb_register_toolchains( name = "ruby", version_file = "//:.ruby-version", ) # Use JRuby rb_register_toolchains( name = "jruby", version = "jruby-10.0", ) # Use system Ruby (non-hermetic) rb_register_toolchains( name = "ruby", version = "system", ) ``` -------------------------------- ### Use Bundled Gems in a Bazel Test Target Source: https://github.com/bazel-contrib/rules_ruby/blob/main/docs/repository_rules.md This example shows how to consume the gems fetched by rb_bundle_fetch within a Bazel BUILD file. It defines an 'rb_test' target named 'rubocop' that uses 'rubocop' executable provided by the '@bundle' repository. The 'deps' attribute includes '@bundle' to ensure the gem dependencies are available to the test. ```bazel load("@rules_ruby//ruby:defs.bzl", "rb_test") package(default_visibility = ["//:__subpackages__"]) rb_test( name = "rubocop", main = "@bundle//bin:rubocop", deps = ["@bundle"], ) ``` -------------------------------- ### Create Ruby Binary with rb_binary (Bazel) Source: https://github.com/bazel-contrib/rules_ruby/blob/main/docs/repository_rules.md The `rb_binary` rule allows creating executable Ruby scripts. It specifies the `main` entry point of the script, often referencing a binary provided by an external bundle repository (e.g., `@bundle//:bin/rubocop`), and lists its dependencies using the `deps` attribute. ```bazel load("@rules_ruby//ruby:defs.bzl", "rb_binary") package(default_visibility = ["//:__subpackages__"]) rb_binary( name = "rubocop", main = "@bundle//:bin/rubocop", deps = ["@bundle"], ) ``` -------------------------------- ### Define and Run Ruby Tests with rb_test Source: https://github.com/bazel-contrib/rules_ruby/blob/main/docs/rules.md This snippet demonstrates how to define and run Ruby tests using the `rb_test` rule. It includes setting up test files, specifying dependencies like `spec_helper` and Bundler, and running tests using `bazel test`. This is useful for testing individual Ruby gems or libraries. ```bazel load("@rules_ruby//ruby:defs.bzl", "rb_library", "rb_test") rb_library( name = "spec_helper", srcs = ["spec_helper.rb"], ) rb_test( name = "add", srcs = ["add_spec.rb"], args = ["spec/add_spec.rb"], main = "@bundle//bin:rspec", deps = [ ":spec_helper", "@bundle", ], ) rb_test( name = "subtract", srcs = ["subtract_spec.rb"], args = ["spec/subtract_spec.rb"], main = "@bundle//bin:rspec", deps = [ ":spec_helper", "@bundle", ], ) ``` -------------------------------- ### Run Ruby Scripts with rb_test and Bundler Binaries Source: https://github.com/bazel-contrib/rules_ruby/blob/main/docs/rules.md This snippet shows how to use `rb_test` to execute Ruby scripts, such as linters or formatters, that are available as binaries in Bundler dependencies. It configures the `main` attribute to point to the Bundler binary stub and specifies necessary dependencies. This is useful for integrating tools like RuboCop into the build process. ```bazel load("@rules_ruby//ruby:defs.bzl", "rb_test") package(default_visibility = ["//:__subpackages__"]) rb_test( name = "rubocop", args = ["lib/"], main = "@bundle//bin:rubocop", tags = ["no-sandbox"], deps = [ "//lib:gem", "@bundle", ], ) ``` -------------------------------- ### CSS Reset and Styling Source: https://github.com/bazel-contrib/rules_ruby/blob/main/examples/rails/people_tracker/public/422.html A CSS snippet that includes a universal reset for box-sizing and margins, along with base styles for HTML and body elements. It defines font, color, layout, and responsive font sizing. Dependencies: None. Inputs: None. Outputs: Styled HTML document. Limitations: Assumes a standard HTML structure. ```css *, *::before, *::after { box-sizing: border-box; } * { margin: 0; } html { font-size: 16px; } body { background: #FFF; color: #261B23; display: grid; font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, Aptos, Roboto, "Segoe UI", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"; font-size: clamp(1rem, 2.5vw, 2rem); -webkit-font-smoothing: antialiased; font-style: normal; font-weight: 400; letter-spacing: -0.0025em; line-height: 1.4; min-height: 100vh; place-items: center; text-rendering: optimizeLegibility; -webkit-text-size-adjust: 100%; } a { color: inherit; font-weight: 700; text-decoration: underline; text-underline-offset: 0.0925em; } b, strong { font-weight: 700; } i, em { font-style: italic; } main { display: grid; gap: 1em; padding: 2em; place-items: center; text-align: center; } main header { width: min(100%, 12em); } main header svg { height: auto; max-width: 100%; width: 100%; } main article { width: min(100%, 30em); } main article p { font-size: 75%; } main article br { display: none; } @media(min-width: 48em) { main article br { display: inline; } } ``` -------------------------------- ### Define and Push a Ruby Gem using rb_gem_push Source: https://github.com/bazel-contrib/rules_ruby/blob/main/docs/rules.md This snippet demonstrates how to define a Ruby gem build target using `rb_gem_build` and then push the resulting gem file using the `rb_gem_push` rule. It shows the necessary `load` statements and the structure of a `BUILD` file for releasing a gem. ```bazel load("@rules_ruby//ruby:defs.bzl", "rb_gem_build", "rb_gem_push") package(default_visibility = ["//:__subpackages__"]) rb_gem_build( name = "gem-build", gemspec = "gem.gemspec", deps = ["//lib:gem"], ) rb_gem_push( name = "gem-release", gem = ":gem-build", ) ``` -------------------------------- ### Define a Ruby Binary with rb_binary Source: https://github.com/bazel-contrib/rules_ruby/blob/main/docs/rules.md The rb_binary rule is used to create executable Ruby targets. It can run a specified main script, accept arguments, and manage dependencies. This is useful for both standalone scripts and applications relying on libraries defined with rb_library. ```bazel load("@rules_ruby//ruby:defs.bzl", "rb_binary", "rb_library") rb_library( name = "version", srcs = ["version.rb"], ) rb_binary( name = "print-version", args = ["lib/gem/version.rb"], deps = [":version"], ) ``` ```bazel load("@rules_ruby//ruby:defs.bzl", "rb_binary", "rb_library") rb_library( name = "add", srcs = ["add.rb"], ) rb_binary( name = "add-numbers", main = "add.rb", deps = [":add"], ) ``` ```bazel load("@rules_ruby//ruby:defs.bzl", "rb_binary") package(default_visibility = ["//:__subpackages__"]) rb_binary( name = "rake", main = "@bundle//bin:rake", deps = [ "//lib:gem", "@bundle", ], ) ``` -------------------------------- ### Create Ruby Binary Executable Source: https://context7.com/bazel-contrib/rules_ruby/llms.txt Creates an executable Ruby binary target. Can run custom Ruby scripts with interpreter support or execute binary stubs from bundled gems (e.g., rake, rails, rspec). Supports environment variables and data dependencies. ```bazel # BUILD load("@rules_ruby//ruby:defs.bzl", "rb_binary") # Run custom Ruby script rb_binary( name = "add-numbers", srcs = ["lib/gem/add.rb"], main = "lib/gem/add.rb", deps = [ "//lib/gem:add", "@bundle", ], ) # Run bundler binary stub rb_binary( name = "rake", main = "@bundle//bin:rake", deps = [ "//lib:gem", "@bundle", ], ) # Run with environment variables rb_binary( name = "server", srcs = ["server.rb"], main = "server.rb", env = { "PORT": "3000", "CONFIG": "$(location :config.json)", }, env_inherit = ["HOME", "USER"], data = ["config.json"], ) ``` -------------------------------- ### Create Rails System Test Macro Source: https://github.com/bazel-contrib/rules_ruby/blob/main/docs/rails.md Defines a Bazel macro for creating Rails system test targets. It accepts parameters like test package, application system test case, default includes, default size, and tags. Returns a macro function to define these targets. ```Starlark load("@rules_ruby//rails:rails_test_factory.bzl", "rails_test_factory") rails_test_factory.new_system_test(test_package, application_system_test_case, default_includes, default_size, tags) ``` -------------------------------- ### Direct Toolchain Access - Run Bundler Source: https://context7.com/bazel-contrib/rules_ruby/llms.txt Enables running Bundler commands directly through Bazel. This facilitates dependency management tasks such as updating gems within the Bazel environment. ```bash # Run bundler bazel run @ruby//:bundle -- update ``` -------------------------------- ### Bzlmod Extension Configuration for Ruby Source: https://context7.com/bazel-contrib/rules_ruby/llms.txt Configures Ruby toolchains and bundle dependencies using Bazel's module system (Bzlmod). This provides a modern alternative to WORKSPACE configuration by specifying the rules_ruby version, Ruby toolchain version, and bundle fetching details. ```bazel # MODULE.bazel module( name = "my_ruby_project", version = "1.0.0", ) bazel_dep(name = "rules_ruby", version = "0.3.0") # Configure Ruby toolchain ruby = use_extension("@rules_ruby//ruby:extensions.bzl", "ruby") ruby.toolchain( name = "ruby", version = "3.3.9", ) # Configure bundle dependencies ruby.bundle_fetch( name = "bundle", gemfile = "//:Gemfile", gemfile_lock = "//:Gemfile.lock", ) use_repo(ruby, "bundle", "ruby", "ruby_toolchains") register_toolchains("@ruby_toolchains//:all") ``` -------------------------------- ### rb_test - Run Rubocop as a Test Source: https://context7.com/bazel-contrib/rules_ruby/llms.txt Configures Rubocop to run as a Bazel test target. It specifies the main executable for Rubocop and the directories to analyze. Dependencies include the gem library and the bundle environment. ```bazel rb_test( name = "rubocop", size = "small", timeout = "moderate", main = "@bundle//bin:rubocop", args = ["lib/", "spec/"], tags = ["no-sandbox"], deps = [ "//lib:gem", "@bundle", ], ) ``` -------------------------------- ### rb_gem_push - Publish Ruby Gem Source: https://context7.com/bazel-contrib/rules_ruby/llms.txt Publishes a built .gem file to RubyGems.org or another gem server. This rule wraps `gem push` and handles authentication through standard Ruby gem credentials. It requires the gem to be built first using `rb_gem_build`. ```bazel # BUILD load("@rules_ruby//ruby:defs.bzl", "rb_gem_build", "rb_gem_push") rb_gem_build( name = "gem-build", gemspec = "gem.gemspec", deps = ["//lib:gem"], ) rb_gem_push( name = "gem-release", gem = ":gem-build", env = { "GEM_HOST_API_KEY": "$(cat ~/.gem/credentials)", }, ) ``` -------------------------------- ### Direct Toolchain Access - Run Ruby Interpreter Source: https://context7.com/bazel-contrib/rules_ruby/llms.txt Allows running the Ruby interpreter directly via Bazel. This is useful for ad-hoc commands, debugging, and simple script execution without defining specific BUILD targets. ```bash # Run Ruby interpreter bazel run @ruby -- -e "puts RUBY_VERSION" ``` -------------------------------- ### rb_gem_build - Build Ruby Gem Package Source: https://context7.com/bazel-contrib/rules_ruby/llms.txt Builds a .gem package file from a gemspec and associated source files. This rule executes `gem build` with the specified gemspec, producing a distributable gem artifact. It requires a gemspec file and source files, with optional data files. ```bazel # BUILD load("@rules_ruby//ruby:defs.bzl", "rb_gem_build") rb_gem_build( name = "gem-build", gemspec = "gem.gemspec", srcs = glob(["lib/**/*.rb"]), deps = [ "//lib:gem", "@bundle", ], data = [ "README.md", "LICENSE", ], ) ``` -------------------------------- ### rails_test_factory.new_system_test - Create Rails System Test Macro Source: https://context7.com/bazel-contrib/rules_ruby/llms.txt Creates a macro for defining Rails system tests (integration/browser tests) with appropriate defaults for Selenium or Capybara-based testing. It sets up common configurations like test case paths and default sizes. ```bazel # test/system/BUILD load("@rules_ruby//rails:rails_test_factory.bzl", "rails_test_factory") # Create system test macro rails_system_test = rails_test_factory.new_system_test( test_package = "//test", application_system_test_case = "//test:application_system_test_case", default_includes = [ "test/system", "app", ], default_size = "large", tags = ["no-sandbox", "system-test"], ) # Define system test rails_system_test( name = "login_flow", srcs = ["login_flow_test.rb"], deps = [ "//app/controllers:sessions", "@bundle", ], ) ``` -------------------------------- ### Load rb_register_toolchains rule Source: https://github.com/bazel-contrib/rules_ruby/blob/main/docs/repository_rules.md Loads the rb_register_toolchains rule from the rules_ruby dependencies. This rule is used to register Ruby toolchains and lazily download the Ruby Interpreter. ```bzl load("@rules_ruby//ruby:deps.bzl", "rb_register_toolchains") rb_register_toolchains(name, version, version_file, msys2_packages, register, **kwargs) ``` -------------------------------- ### Create Rails Test Macro Source: https://github.com/bazel-contrib/rules_ruby/blob/main/docs/rails.md Defines a Bazel macro for creating standard Rails test targets. It takes parameters such as test package, test helper, default includes, default size, and tags. The returned macro function encapsulates application-specific attributes for test targets. ```Starlark load("@rules_ruby//rails:rails_test_factory.bzl", "rails_test_factory") rails_test_factory.new_test(test_package, test_helper, default_includes, default_size, tags) ``` -------------------------------- ### Conditional Library Dependency using select() in BUILD Source: https://github.com/bazel-contrib/rules_ruby/blob/main/docs/repository_rules.md Defines a library target (rb_library) with conditional dependencies based on the selected Ruby engine. This allows for engine-specific implementations. ```bazel rb_library( name = "my_lib", srcs = ["my_lib.rb"], deps = select({ "@ruby//engine:jruby": [":my_jruby_lib"], "@ruby//engine:truffleruby": ["//:my_truffleruby_lib"], "@ruby//engine:ruby": ["//:my__lib"], "//conditions:default": [], }), ) ``` -------------------------------- ### Define a Ruby Library with rb_library in Bazel Source: https://github.com/bazel-contrib/rules_ruby/blob/main/docs/rules.md This Bazel BUILD file configuration demonstrates how to define a Ruby library using the rb_library rule. It specifies the library's name, source files, and dependencies on other Ruby libraries. ```bazel load("@rules_ruby//ruby:defs.bzl", "rb_library") package(default_visibility = ["//:__subpackages__"]) rb_library( name = "gem", deps = ["//lib:gem"], ) ``` ```bazel load("@rules_ruby//ruby:defs.bzl", "rb_library") package(default_visibility = ["//:__subpackages__"]) rb_library( name = "gem", srcs = ["gem.rb"], deps = [ "//lib/gem:add", "//lib/gem:subtract", "//lib/gem:version", ], ) ``` ```bazel load("@rules_ruby//ruby:defs.bzl", "rb_library") package(default_visibility = ["//:__subpackages__"]) rb_library( name = "add", srcs = ["add.rb"], ) rb_library( name = "subtract", srcs = ["subtract.rb"], ) rb_library( name = "version", srcs = ["version.rb"], ) ``` -------------------------------- ### Load rb_bundle rule Source: https://github.com/bazel-contrib/rules_ruby/blob/main/docs/repository_rules.md Loads the rb_bundle rule from the rules_ruby dependencies. This rule is used to wrap rb_bundle_rule() and provides a default toolchain name. ```bzl load("@rules_ruby//ruby:deps.bzl", "rb_bundle") rb_bundle(toolchain, **kwargs) ``` -------------------------------- ### Configure rb_bundle_fetch with Gem Checksums Source: https://github.com/bazel-contrib/rules_ruby/blob/main/docs/repository_rules.md This snippet illustrates how to enhance the rb_bundle_fetch configuration by providing explicit SHA-256 checksums for gems defined in the Gemfile.lock. This is a security best practice to ensure the integrity of fetched gems and improve build reproducibility. The checksums are provided as a dictionary mapping gem names and versions to their respective SHA-256 hashes. ```bazel rb_bundle_fetch( name = "bundle", gemfile = "//:Gemfile", gemfile_lock = "//:Gemfile.lock", gem_checksums = { "ast-2.4.2": "1e280232e6a33754cde542bc5ef85520b74db2aac73ec14acef453784447cc12", "concurrent-ruby-1.2.2": "3879119b8b75e3b62616acc256c64a134d0b0a7a9a3fcba5a233025bcde22c4f", }, ) ``` -------------------------------- ### Define Ruby Gem Build Target (Bazel) Source: https://github.com/bazel-contrib/rules_ruby/blob/main/docs/rules.md This snippet shows how to define a `rb_gem_build` target in a Bazel BUILD file. It specifies the name of the target, the gemspec file to use, and any dependencies required, such as libraries and external bundles. This configuration allows Bazel to build a RubyGem. ```bazel load("@rules_ruby//ruby:defs.bzl", "rb_gem_build") package(default_visibility = ["//:__subpackages__"]) rb_gem_build( name = "gem-build", gemspec = "gem.gemspec", deps = [ "//lib:gem", "@bundle", ], ) ``` -------------------------------- ### Define Ruby Test Target Source: https://context7.com/bazel-contrib/rules_ruby/llms.txt Defines a Ruby test target using frameworks like RSpec or Minitest. Tests are executed via `bazel test` and support standard Bazel test attributes such as size, timeout, and tags. Requires Bundler and libraries. ```bazel # spec/BUILD load("@rules_ruby//ruby:defs.bzl", "rb_library", "rb_test") rb_library( name = "spec_helper", srcs = ["spec_helper.rb"], deps = ["@bundle"], ) # RSpec test rb_test( name = "add", size = "small", srcs = ["add_spec.rb"], main = "@bundle//bin:rspec", args = ["spec/add_spec.rb"], deps = [ ":spec_helper", "//lib/gem:add", "@bundle", ], ) ``` -------------------------------- ### rb_gem Source: https://github.com/bazel-contrib/rules_ruby/blob/main/docs/rules.md Exposes a Ruby gem file. This rule is an internal helper, primarily used by `rb_bundle_fetch()`. ```APIDOC ## rb_gem ### Description Exposes a Ruby gem file. This rule is an internal helper for `rb_bundle_fetch()`. ### Method N/A (Bazel rule) ### Endpoint N/A (Bazel rule) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```bazel rb_gem( name = "my_gem", gem = "@my_gems//:my_gem.gem", ) ``` ### Response #### Success Response (N/A) This is a Bazel rule, not an HTTP endpoint. Success is determined by Bazel's build process. #### Response Example N/A ``` -------------------------------- ### rails_test_factory.new_test - Create Rails Test Macro Source: https://context7.com/bazel-contrib/rules_ruby/llms.txt Creates a customized macro for defining Rails tests with application-specific defaults. This factory function generates a macro that encapsulates test helper dependencies and configuration, simplifying test definitions. ```bazel # test/BUILD load("@rules_ruby//rails:rails_test_factory.bzl", "rails_test_factory") # Create custom test macro rails_test = rails_test_factory.new_test( test_package = "//test", test_helper = "//test:test_helper", default_includes = [ "test", "app", ], default_size = "small", tags = ["rails"], ) # Use generated macro to create tests rails_test( name = "user_test", srcs = ["models/user_test.rb"], deps = ["//app/models:user"], ) ``` -------------------------------- ### Define Ruby Library Target Source: https://context7.com/bazel-contrib/rules_ruby/llms.txt Defines a Ruby library target that can be depended on by other targets. Libraries contain Ruby source files and can depend on other libraries or bundled gems. Supports WORKSPACE and Bzlmod. ```bazel # lib/gem/BUILD load("@rules_ruby//ruby:defs.bzl", "rb_library") rb_library( name = "add", srcs = ["add.rb"], visibility = ["//:__subpackages__"], ) rb_library( name = "math_operations", srcs = ["math_ops.rb"], deps = [ ":add", ":subtract", "@bundle", ], data = ["config.yml"], bundle_env = { "BUNDLE_GEMFILE": "$(execpath //:Gemfile)", }, ) ``` -------------------------------- ### HTML Structure for 500 Error Page Source: https://github.com/bazel-contrib/rules_ruby/blob/main/examples/rails/people_tracker/public/500.html This HTML snippet defines the basic structure for a 500 Internal Server Error page. It includes global CSS resets and styling for the body, links, and main content area. The styling is responsive and aims to center the error message. ```html 500 Internal Server Error

We’re sorry, but something went wrong.

If you’re the application owner check the logs for more information.


Internal Server Error (500)

``` -------------------------------- ### rb_gem_push Rule Signature Source: https://github.com/bazel-contrib/rules_ruby/blob/main/docs/rules.md The signature for the `rb_gem_push` rule, outlining its parameters and their types. This is useful for understanding how to configure the rule in a Bazel `BUILD` file. ```bazel rb_gem_push(name, deps, srcs, data, bundle_env, env, env_inherit, gem, ruby) ``` -------------------------------- ### rb_gem Bazel Rule Source: https://github.com/bazel-contrib/rules_ruby/blob/main/docs/rules.md The `rb_gem` rule is used to expose a Ruby gem file within the Bazel build system. It requires a name for the target and a label pointing to the gem file itself. This rule is generally intended for internal use by other rules like `rb_bundle_fetch`. ```bzl load("@rules_ruby//ruby:defs.bzl", "rb_gem") rb_gem( name = "my_gem_exposure", gem = "@my_local_gems//:my_gem.gem" ) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.