Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Formerly canonical rules for ruby, that are about 2-3 years behind current Bazel. If they work for you great, but if not — please try the new rules ruby by Alex Radionov:https://github.com/bazel-contrib/rules_ruby

License

NotificationsYou must be signed in to change notification settings

bazelruby/rules_ruby

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Table of Contents

1. New Rules Ruby!

This repo will be retired eventually, as these rules have not been actively maintained and are in the process of being replaced by the new rules, which you can find here:https://github.com/bazel-contrib/rules_ruby

Tip
You can read or print this README in a proper PDF format by grabbing ourREADME.pdf.

2. Build Status & Activity

CI StatusActivity & Documentation

CircleCI  

activity  

Build Status  

changelog</a>readme.pdf</a>

3. Rules Development Status

ReadinessTypes of Applications

Ready

ruby apps, ruby gems, micro-services, ideally in a mono-repo

Wait

medium-sized Ruby on Rails apps, ideally in a mono-repo

Not Ready

complex Ruby on Rails monoliths, single-repo

Note
we have a short guide onBuilding your first Ruby Project on the Wiki. We encourage you to check it out.

5. Usage

5.1.WORKSPACE File

5.1.1. Load dependencies, select Ruby SDK and define one or more Bundles

workspace(name="my_ruby_project")load("@bazel_tools//tools/build_defs/repo:http.bzl","http_archive")load("@bazel_tools//tools/build_defs/repo:git.bzl","git_repository")#———————————————————————————————————————————————————————————————————————# To get the latest ruby rules, grab the 'master' branch.#———————————————————————————————————————————————————————————————————————git_repository(name="bazelruby_rules_ruby",remote="https://github.com/bazelruby/rules_ruby.git",branch="master")load("@bazelruby_rules_ruby//ruby:deps.bzl","rules_ruby_dependencies","rules_ruby_select_sdk",)rules_ruby_dependencies()#———————————————————————————————————————————————————————————————————————# Specify Ruby version — this will either build Ruby or use a local# RBENV installation if the Ruby version matches.#———————————————————————————————————————————————————————————————————————load("@bazel_skylib//:workspace.bzl","bazel_skylib_workspace")bazel_skylib_workspace()rules_ruby_select_sdk(version="3.0.2")#———————————————————————————————————————————————————————————————————————# Now, load the ruby_bundle rule & install gems specified in the Gemfile#———————————————————————————————————————————————————————————————————————load("@bazelruby_rules_ruby//ruby:defs.bzl","ruby_bundle",)ruby_bundle(name="bundle",# Specify additional paths to be loaded from the gems at runtime, if any.# Since spec.require_paths in Gem specifications are auto-included, directory paths# in spec.require_paths do not need to be listed in includes hash.includes= {"grpc": ["etc"],    },excludes= {"mini_portile": ["test/**/*"],    },gemfile="//:Gemfile",gemfile_lock="//:Gemfile.lock",)# You can specify more than one bundle in the WORKSPACE fileruby_bundle(name="bundle_app_shopping",gemfile="//:apps/shopping/Gemfile",gemfile_lock="//:apps/shopping/Gemfile.lock",)# You can also install from Gemfile using `gemspec`.ruby_bundle(name="bundle_gemspec",srcs= ["//:lib/my_gem/my_gem.gemspec"],gemfile="//:lib/my_gem/Gemfile",gemfile_lock="//:lib/my_gem/Gemfile.lock",)

5.2.BUILD.bazel file(s)

Any of the projectBUILD files can now reference any gems included in theGemfile referenced by theruby_bundle rule, and defined in the project’sWORKSPACE file.

5.2.1. Define Ruby Executable, Library and an RSpec

Addruby_library,ruby_binary,ruby_rspec orruby_test into yourBUILD.bazel files.

#———————————————————————————————————————————————————————————————————————# Define Ruby executable, test, spec and package a gem#———————————————————————————————————————————————————————————————————————load("@bazelruby_rules_ruby//ruby:defs.bzl","ruby_binary","ruby_library","ruby_test","ruby_rspec",)ruby_library(name="foo",srcs=glob(["lib/**/*.rb"]),includes= ["lib"],deps= ["@bundle//:activesupport","@bundle//:awesome_print","@bundle//:rubocop",    ])ruby_binary(name="bar",srcs= ["bin/bar"],deps= [":foo"],)ruby_test(name="foo-test",srcs= ["test/foo_test.rb"],deps= [":foo"],)ruby_rspec(name="foo-spec",specs=glob(["spec/**/*.rb"]),rspec_args= {"--format":"progress" },deps= [":foo"])

5.2.2. Package Ruby files as a Gem

Useruby_gem rule to package any number of ruby files or folders into a Ruby-Gem compatible ZIP archive.

load("@bazelruby_rules_ruby//ruby:defs.bzl","ruby_gem",)ruby_gem(name="awesome-sauce-gem",# name of the build targetgem_name="awesome-sauce",# name of the gemgem_version="0.1.0",gem_summary="Example gem to demonstrate Bazel Gem packaging",gem_description="Example gem to demonstrate Bazel Gem packaging",gem_homepage="https://github.com/bazelruby/rules_ruby",gem_authors= ["BazelRuby","Konstantin Gredeskoul"    ],gem_author_emails= ["bazelruby@googlegroups.com",    ],gem_runtime_dependencies= {"colored2":"~> 3.1.2","hashie":"",    },gem_development_dependencies= {"rspec":"","rspec-its":"","rubocop":"",    },srcs= [glob("{bin,exe,lib,spec}/**/*.rb")    ],deps= ["//lib:example_gem",    ],)

5.3. Tool Specific Setup

5.3.1. ASDF

If you are using ASDF to manage your ruby installs, you can use them by adding.bazelrc:

build --test_env=ASDF_DIR --test_env=ASDF_DATA_DIRbuild --action_env=ASDF_DIR --test_env=ASDF_DATA_DIR

You will have to be sure to export theASDF_DATA_DIR in your profile since it’s not set by default. e.g.export ASDF_DATA_DIR="$HOME/.asdf"

5.4. Rule Dependency Diagram

Note
this diagram is somewhat outdated.

The following diagram attempts to capture the implementation behindruby_library that depends on the result ofbundle install, and aruby_binary that depends on both:

Ruby Rules

6. Rules

6.1.ruby_library

ruby_library(name,deps,srcs,data,compatible_with,deprecation,distribs,features,licenses,restricted_to,tags,testonly,toolchains,visibility)
Attributes

name

Name, required

A unique name for this rule.

srcs

List of Labels, optional

List of.rb files.

At leastsrcs ordeps must be present

deps

List of labels, optional

List of targets that are required by thesrcs Ruby files.

At leastsrcs ordeps must be present

includes

List of strings, optional

List of paths to be added to$LOAD_PATH at runtime. The paths must be relative to the the workspace which this rule belongs to.

rubyopt

List of strings, optional

List of options to be passed to the Ruby interpreter at runtime.

Note
-I option should usually go toincludes attribute.

And othercommon attributes.

6.2.ruby_binary

ruby_binary(name,deps,srcs,data,main,compatible_with,deprecation,distribs,features,licenses,restricted_to,tags,testonly,toolchains,visibility,args,output_licenses)
Attributes

name

Name, required

A unique name for this rule.

srcs

List of Labels, required

List of.rb files.

deps

List of labels, optional

List of targets that are required by thesrcs Ruby files.

main

Label, optional

The entrypoint file. It must be also insrcs.

If not specified,$(NAME).rb where$(NAME) is thename of this rule.

includes

List of strings, optional

List of paths to be added to$LOAD_PATH at runtime. The paths must be relative to the the workspace which this rule belongs to.

rubyopt

List of strings, optional

List of options to be passed to the Ruby interpreter at runtime.

Note
-I option should usually go toincludes attribute.

And othercommon attributes.

6.3.ruby_test

ruby_test(name,deps,srcs,data,main,compatible_with,deprecation,distribs,features,licenses,restricted_to,tags,testonly,toolchains,visibility,args,size,timeout,flaky,local,shard_count)
Attributes

name

Name, required

A unique name for this rule.

srcs

List of Labels, required

List of.rb files.

deps

List of labels, optional

List of targets that are required by thesrcs Ruby files.

main

Label, optional

The entrypoint file. It must be also insrcs.

If not specified,$(NAME).rb where$(NAME) is thename of this rule.

includes

List of strings, optional

List of paths to be added to$LOAD_PATH at runtime. The paths must be relative to the the workspace which this rule belongs to.

rubyopt

List of strings, optional

List of options to be passed to the Ruby interpreter at runtime.

Note
-I option should usually go toincludes attribute.

And othercommon attributes.

6.4.ruby_bundle

NOTE: This is a repository rule, and can only be used in aWORKSPACE file.

This rule installs gems defined in a Gemfile using Bundler, and exports individual gems from the bundle, as well as the entire bundle, available as aruby_library that can be depended upon from other targets.

ruby_bundle(name,gemfile,gemfile_lock,bundler_version="2.1.4",includes= {},excludes= {},srcs= [],vendor_cache=False,ruby_sdk="@org_ruby_lang_ruby_toolchain",ruby_interpreter="@org_ruby_lang_ruby_toolchain//:ruby",)
Attributes

name

Name, required

A unique name for this rule.

gemfile

Label, required

TheGemfile which Bundler runs with.

gemfile_lock

Label, optional

TheGemfile.lock which Bundler runs with.

Note
This rule never updates theGemfile.lock. It is your responsibility to generate/updateGemfile.lock

srcs

List of Labels, optional

List of additional files required for Bundler to install gems. This could usually include*.gemspec files.

vendor_cache

Bool, optional

Symlink the vendor directory into the Bazel build space, this allows Bundler to access vendored Gems

bundler_version

String, optional

The Version of Bundler to use. Defaults to 2.1.4.

Note
This rule never updates theGemfile.lock. It is your responsibility to generate/updateGemfile.lock

includes

Dictionary of key-value-pairs (key: string, value: list of strings), optional

List of glob patterns per gem to be additionally loaded from the library. Keys are the names of the gems which require some file/directory paths not listed in therequire_paths attribute of the gemspecs to be also added to$LOAD_PATH at runtime. Values are lists of blob path patterns, which are relative to the root directories of the gems.

excludes

Dictionary of key-value-pairs (key: string, value: list of strings), optional

List of glob patterns per gem to be excluded from the library. Keys are the names of the gems. Values are lists of blob path patterns, which are relative to the root directories of the gems. The default value is["/*.", "/*/"]

And othercommon attributes.

6.4.1. Conventions

ruby_bundle creates several targets that can be used downstream. In the examples below we assume that yourruby_bundle has a nameapp_bundle:

  • @app_bundle//:bundler — references just the Bundler from the bundle.

  • @app_bundle//:gems — referencesall gems in the bundle (i.e. "the entire bundle").

  • @app_bundle//:gem-name — referencesjust the specified gem in the bundle, eg.@app_bundle//:awesome_print.

  • @app_bundle//:bin — references to all installed executables from this bundle, with individual executables accessible via eg.@app_bundle//:bin/rubocop

6.4.2.WORKSPACE:

load("@bazelruby_rules_ruby//ruby:defs.bzl","ruby_bundle")ruby_bundle(name="gems",bundler_version='2.1.4',gemfile="//:Gemfile",gemfile_lock="//:Gemfile.lock",)
Vendor directory handling

To use the vendor cache, you have to declare amanaged_directory inyour workspace. The name should match the name of the bundle.

load("@bazelruby_rules_ruby//ruby:defs.bzl","ruby_bundle")workspace(name="my_wksp",managed_directories= {"@bundle": ["vendor"]},)ruby_bundle(name="bundle",bundler_version="2.1.2",vendor_cache=True,gemfile="//:Gemfile",gemfile_lock="//:Gemfile.lock",)

6.4.3.BUILD.bazel:

# Reference the entire bundle with :gemsruby_library(name="foo",srcs= ["foo.rb"],deps= ["@gems//:gems"],)# Or, reference specific gems from the bundle like so:ruby_binary(name="rubocop",srcs= [":foo",".rubocop.yml"],args= ["-P","-D","-c"".rubocop.yml"],main="@gems//:bin/rubocop",deps= ["@gems//:rubocop"],)

6.5.ruby_rspec

ruby_rspec(name,deps,srcs,data,main,rspec_args,bundle,compatible_with,deprecation,distribs,features,licenses,restricted_to,tags,testonly,toolchains,visibility,args,size,timeout,flaky,local,shard_count)
Attributes

name

Name, required

A unique name for this rule.

srcs

List of Labels, required

List of.rb files.

deps

List of labels, optional

List of targets that are required by thesrcs Ruby files.

main

Label, optional

The entrypoint file. It must be also insrcs.

If not specified,$(NAME).rb where$(NAME) is thename of this rule.

rspec_args

List of strings, optional

Command line arguments to therspec binary, eg["--progress", "-p2", "-b"]

If not specified, the default arguments defined inconstants.bzl are used:--format=documentation --force-color.

includes

List of strings, optional

List of paths to be added to$LOAD_PATH at runtime. The paths must be relative to the the workspace which this rule belongs to.

rubyopt

List of strings, optional

List of options to be passed to the Ruby interpreter at runtime.

Note
-I option should usually go toincludes attribute.

And othercommon attributes.

6.6.ruby_gem

Used to generate a zipped gem containing its srcs, dependencies and a gemspec.

ruby_gem(name,gem_name,gem_version,gem_summary,gem_description,gem_homepage,gem_authors,gem_author_emails,gem_runtime_dependencies,gem_development_dependencies,require_paths= ["lib"],srcs=srcs,deps=deps,data=data)
Attributes

name

Name, required

A unique name for this build target.

gem_name

Name of the gem, required

The name of the gem to be generated.

gem_version

String, optional

The version of the gem. Is used to name the output file, which becomesname-version.zip, and also included in the Gemspec.

gem_summary

String, optional

One line summary of the gem purpose.

gem_description

String, required

Single-line, paragraph-sized description text for the gem.

gem_homepage

String, optional

Homepage URL of the gem.

gem_authors

List of Strings, required

List of human readable names of the gem authors. Required to generate a valid gemspec.

gem_author_emails

List of Strings, optional

List of email addresses of the authors.

srcs

List of Labels, optional

List of.rb files.

At leastsrcs ordeps must be present

deps

List of labels, optional

List of targets that are required by thesrcs Ruby files.

At leastsrcs ordeps must be present

require_paths

List of Strings, optional

List of paths to be added to the Ruby LOAD_PATH when using this gem. Typically this value is justlib (which is also the default).

gem_runtime_dependencies

String Dictionary, optional

This is a dictionary where keys are gem names, and values are either an empty string or agem version specification. For instance, the pessimistic version specifier~> 3.0 means that all versions up to4.0 are accepted.

gem_development_dependencies

String Dictionary, optional

Similar to the above, this specifies gems necessary for the development of the above gem, such as testing gems, linters, code coverage and more.

And othercommon attributes.

7. Potential Future Features

[check square] Using various versions of Ruby installed locally

[square] Building native extensions in gems with Bazel

[square] Releasing your gems with Bazel (Coinbase fork might have this feature, worth checking)

8. Contributing

We welcome contributions to RulesRuby. Please make yourself familiar with thecode of conduct, which basically says — don’t be an a-hole.

You may notice that there is more than one Bazel WORKSPACE inside this repo. There is one inexamples/simple_script for instance, becausewe use this example to validate and test the rules. So be mindful whether your current directory containsWORKSPACE file or not.

8.1. Setup

8.1.1. Using the Script

You will need Homebrew installed prior to running the script.

After that, cd into the top level folder and run the setup script in your Terminal:

❯ bin/setup

This runs a complete setup, shouldn’t take too long. You can explore various script options with thehelp command:

❯ bin/setup -hUSAGE# without any arguments runs a complete setup.  bin/setup# alternatively, a sub-setup function name can be passed:  bin/setup [ gems| git-hook|help| main| os-specific| rbenv| remove-git-hook ]DESCRIPTION:  Runs full setup without any arguments.  Accepts one optional argument — one of the actions that typically run  as part of setup, with one exception — remove-git-hook.  This action removes the git commit hook installed by the setup.EXAMPLES:    bin/setup  Or, to run only one of the sub-functions (actions), pass  it as an argument:    bin/setuphelp    bin/setup remove-git-hook

8.1.2. OS-Specific Setup

Note that the setup containsos-specific section. This is because there are two extension scripts:

  • bin/setup-linux

  • bin/setup-darwin

Those will install Bazel and everything else you need on either platform. In fact, we use the linux version on CI.

8.2. Verifying Your Environment

We provided a handy scriptbin/show-env to display where your dependencies are coming from. Here is an example of running it on a Mac OS-X system:

❯ bin/show-env
bin/show-env

8.2.1. Issues During Setup

Please report any errors tobin/setup as Issues on Github. You can assign them to @kigster. If I am not responding fast enough, and you are in a hurry, please email kigster AT gmail directly.

8.3. Developing Rules

Besides making yourself familiar with the existing code, andBazel documentation on writing rules, you might want to follow this order:

  1. Setup dev tools as described in thesetup section.

  2. hack, hack, hack…​

  3. Make sure all tests pass — you can run a single command for that (but see more on itbelow.

bin/test-suite

OR, you can run individual Bazel test commands from the inside.

  • bazel test //…​

  • cd examples/simple_script && bazel test //…​

    1. Open a pull request in Github, and please be as verbose as possible in your description.

In general, it’s always a good idea to ask questions first — you can do so by creating an issue.

8.4. Running Tests

After running setup, and since this is a bazel repo you can use Bazel commands:

bazelbuild//...:allbazelquery//...:allbazeltest//...:all

But to run tests inside each sub-WORKSPACE, you will need to repeat that in each sub-folder. Luckily, there is a better way.

8.4.1. Test Script

This script runs all tests (including sub-workspaces) when ran without arguments:

bin/test-suite

Run it withhelp command to see other options, and to see what parts you can run individually. At the moment they are:

# alternatively, a partial test name can be passed:bin/test-suite [ all| bazel-info| buildifier|help| rspec| rubocop| simple-script|  workspace ]

On a MacBook Pro it takes about 3 minutes to run.

8.5. Linter

We are using RuboCop for ruby and Buildifier for Bazel. Both are represented by a single scriptbin/linter, which just like the scripts above runs ALL linters when ran without arguments, acceptshelp commnd, and can be run on a subset of linting strategies:

bin/linter

The following are the partial linting functions you can run:

# alternatively, a partial linter name can be passed:bin/linter [ all| buildifier|help| rubocop ]

8.6. Regenerating README.pdf & Changelog

To regenerate, first you may need to grab anAPI token and export theGITHUB_TOKEN variable:

export GITHUB_TOKEN=....

Then use themake target:

make update

Or, manually:

gem install github_changelog_generatorgithub_changelog_generator -u bazelruby -p rules_ruby -t your-github-token

9. Copyright

© 2018-2021 BazelRuby Contributors.

Core Team:

Core Team (Emeritus):

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

About

Formerly canonical rules for ruby, that are about 2-3 years behind current Bazel. If they work for you great, but if not — please try the new rules ruby by Alex Radionov:https://github.com/bazel-contrib/rules_ruby

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Contributors19


[8]ページ先頭

©2009-2025 Movatter.jp