Skip to content

Commit

Permalink
Merge pull request #38 from bazelruby/graham/remove-host-fix-excludes
Browse files Browse the repository at this point in the history
[Fix] no spaces in files bug, remove deprecated code, remove host
  • Loading branch information
Graham Jenson authored Dec 16, 2019
2 parents dc837ff + e93b7b9 commit b1caead
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 98 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
**/bazel-*
**/.idea

.DS_Store
**/.vscode

6 changes: 2 additions & 4 deletions examples/simple_script/Gemfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# frozen_string_literal: true

source "https://rubygems.org"

git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
source 'https://rubygems.org'

gem 'awesome_print'
gem 'rspec', '~> 3.7.0'
gem 'awesome_print'
11 changes: 10 additions & 1 deletion examples/simple_script/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
# Simple Script Example

This Workspace includes a simple ruby script that includes and external gem and an internal library
This Workspace includes a simple ruby script that includes and external gem and an internal library


### Bundle

Update gemfile using

```
bundle lock --update
```
3 changes: 3 additions & 0 deletions examples/simple_script/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ load("@bazelruby_ruby_rules//ruby:defs.bzl", "bundle_install")

bundle_install(
name = "bundle",
excludes = {
"mini_portile": ["test/**/*"],
},
gemfile = "//:Gemfile",
gemfile_lock = "//:Gemfile.lock",
version = "2.0.2",
Expand Down
6 changes: 3 additions & 3 deletions examples/simple_script/script.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

require 'openssl'
require 'lib/foo'
require "awesome_print"
require 'awesome_print'

def oss_rand
OpenSSL::BN.rand(512).to_s
end

puts Foo.aha + " " + oss_rand
puts Foo.aha + ' ' + oss_rand

puts $LOAD_PATH

ap Class
ap Class
34 changes: 5 additions & 29 deletions ruby/private/binary.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -29,50 +29,26 @@ def _ruby_binary_impl(ctx):
"main",
)

if sdk.is_host:
interpreter_file_deps = []
interpreter_trans_deps = []
else:
interpreter_file_deps = [interpreter]
interpreter_trans_deps = [sdk.interpreter]

executable = ctx.actions.declare_file(ctx.attr.name)
deps = _transitive_deps(
ctx,
extra_files = interpreter_file_deps + [executable],
extra_deps = interpreter_trans_deps + ctx.attr._misc_deps,
extra_files = [executable],
extra_deps = ctx.attr._misc_deps,
)

rubyopt = reversed(deps.rubyopt.to_list())
intermediate_executable = ctx.actions.declare_file("%s.tpl.intermediate" % ctx.attr.name)

ctx.actions.expand_template(
template = ctx.file._wrapper_template,
output = intermediate_executable,
output = executable,
substitutions = {
"{loadpaths}": repr(deps.incpaths.to_list()),
"{rubyopt}": repr(rubyopt),
"{main}": repr(_to_manifest_path(ctx, main)),
"{interpreter}": _to_manifest_path(ctx, interpreter),
},
)
if sdk.is_host:
ctx.actions.run_shell(
inputs = [intermediate_executable],
outputs = [executable],
command = "grep -v '^#shell ' %s > %s" % (
intermediate_executable.path,
executable.path,
),
)
else:
ctx.actions.run_shell(
inputs = [intermediate_executable],
outputs = [executable],
command = "sed 's/^#shell //' %s > %s" % (
intermediate_executable.path,
executable.path,
),
)

return [DefaultInfo(
executable = executable,
default_runfiles = deps.default_files,
Expand Down
25 changes: 1 addition & 24 deletions ruby/private/binary_wrapper.tpl
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
#shell #!/usr/bin/env bash
#shell # This conditional is evaluated as true in Ruby, false in shell
#shell if [ ]; then
#shell eval <<'END_OF_RUBY'
#shell # -- begin Ruby --
#!/usr/bin/env ruby

# Ruby-port of the Bazel's wrapper script for Python
Expand Down Expand Up @@ -122,28 +117,10 @@ def main(args)
end
end
end
exec(ruby_program, '--disable-gems', *rubyopt, main, *args)
exec(ruby_program, *rubyopt, main, *args)
# TODO(yugui) Support windows
end

if __FILE__ == $0
main(ARGV)
end
#shell END_OF_RUBY
#shell __END__
#shell # -- end Ruby --
#shell fi
#shell # -- begin Shell Script --
#shell
#shell # --- begin runfiles.bash initialization v2 ---
#shell # Copy-pasted from the Bazel Bash runfiles library v2.
#shell set -uo pipefail; f=bazel_tools/tools/bash/runfiles/runfiles.bash
#shell source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \
#shell source "$(grep -sm1 "^$f " "${RUNFILES_MANIFEST_FILE:-/dev/null}" | cut -f2- -d' ')" 2>/dev/null || \
#shell source "$0.runfiles/$f" 2>/dev/null || \
#shell source "$(grep -sm1 "^$f " "$0.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
#shell source "$(grep -sm1 "^$f " "$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
#shell { echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e
#shell # --- end runfiles.bash initialization v2 ---
#shell
#shell exec "$(rlocation {interpreter})" ${BASH_SOURCE:-$0} "$@"
9 changes: 1 addition & 8 deletions ruby/private/bundle/bundle.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,6 @@ def bundle_install_impl(ctx):
if result.return_code:
fail("Failed to install gems: %s%s" % (result.stdout, result.stderr))

# exclude any specified files
exclude = []
for gem, globs in ctx.attr.excludes.items():
expanded = ["lib/ruby/*/gems/%s-*/%s" % (gem, glob) for glob in globs]
exclude.extend(expanded)

# Create the BUILD file to expose the gems to the WORKSPACE
args = [
"env",
Expand All @@ -74,7 +68,7 @@ def bundle_install_impl(ctx):
"BUILD.bazel", # Bazel build file (can be empty)
"Gemfile.lock", # Gemfile.lock where we list all direct and transitive dependencies
ctx.name, # Name of the target
repr(exclude),
repr(ctx.attr.excludes),
RULES_RUBY_WORKSPACE_NAME,
]
result = ctx.execute(args, quiet = False)
Expand All @@ -87,7 +81,6 @@ def bundle_install_impl(ctx):
ctx.attr._buildfile_template,
substitutions = {
"{repo_name}": ctx.name,
"{exclude}": repr(exclude),
"{workspace_name}": RULES_RUBY_WORKSPACE_NAME,
},
)
Expand Down
35 changes: 12 additions & 23 deletions ruby/private/bundle/create_bundle_build_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,13 @@
visibility = ["//visibility:private"],
)
# DEPRECATED: use :all instead
ruby_library(
name = "libs",
name = "bundler",
srcs = glob(
include = [
"lib/ruby/{ruby_version}/gems/*/**/*",
"lib/ruby/{ruby_version}/bin/**/*",
"bundler/**/*",
],
exclude = {exclude},
),
deps = [":bundler_setup"],
rubyopt = ["-r${RUNFILES_DIR}/{repo_name}/lib/bundler/setup.rb"],
)
Expand All @@ -42,33 +38,23 @@
srcs = glob(
include = [
"lib/ruby/{ruby_version}/gems/{name}-{version}/**/*",
# TODO scope down included bin files
"lib/ruby/{ruby_version}/bin/**/*",
],
exclude = {exclude},
),
deps = {deps},
rubyopt = ["-r${RUNFILES_DIR}/{repo_name}/lib/bundler/setup.rb"],
)
'

ALL_TEMPLATE = '
ruby_library(
name = "all",
deps = {deps},
rubyopt = ["-r${RUNFILES_DIR}/{repo_name}/lib/bundler/setup.rb"],
)
'
require "bundler"
require 'json'

def create_bundle_build_file(build_out_file, lock_file, repo_name, excludes, workspace_name)
# TODO: properly calculate path/ruby version here
# ruby_version = RUBY_VERSION # doesnt work because verion is 2.5.5 path is 2.5.0
ruby_version = "*"

template_out = TEMPLATE.gsub("{workspace_name}", workspace_name)
.gsub("{exclude}", excludes)
.gsub("{repo_name}", repo_name)
.gsub("{ruby_version}", ruby_version)

Expand All @@ -78,18 +64,19 @@ def create_bundle_build_file(build_out_file, lock_file, repo_name, excludes, wor
bundle.specs.each { |spec|
deps = spec.dependencies.map(&:name)
deps += [":bundler_setup"]
template_out += GEM_TEMPLATE.gsub("{exclude}", excludes)

exclude_array = excludes[spec.name] || []
# We want to exclude files and folder with spaces in them
exclude_array += ["**/* *.*", "**/* */*"]

template_out += GEM_TEMPLATE.gsub("{exclude}", exclude_array.to_s)
.gsub("{name}", spec.name)
.gsub("{version}", spec.version.to_s)
.gsub("{deps}", deps.to_s)
.gsub("{repo_name}", repo_name)
.gsub("{ruby_version}", ruby_version)
}

# Append collection of all gems
template_out += ALL_TEMPLATE.gsub("{repo_name}", repo_name)
.gsub("{deps}", ([":bundler_setup"] + bundle.specs.map(&:name)).to_s)

# Write the actual BUILD file
::File.open(build_out_file, 'w') { |f|
f.puts template_out
Expand All @@ -106,7 +93,9 @@ def create_bundle_build_file(build_out_file, lock_file, repo_name, excludes, wor
build_out_file = ARGV[0]
lock_file = ARGV[1]
repo_name = ARGV[2]
excludes = ARGV[3]

excludes = JSON.parse(ARGV[3])

workspace_name = ARGV[4]

create_bundle_build_file(build_out_file, lock_file, repo_name, excludes, workspace_name)
Expand Down
5 changes: 0 additions & 5 deletions ruby/private/toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ RubyRuntimeInfo = provider(
"bundler": "A label which points bundler command",
"runtime": "A list of labels which points runtime libraries",
"rubyopt": "A list of strings which should be passed to the interpreter as command line options",
"is_host": "True only if the runtime is the host ruby ",
},
)

Expand All @@ -17,7 +16,6 @@ def _ruby_toolchain_impl(ctx):
interpreter = ctx.attr.interpreter,
runtime = ctx.files.runtime,
rubyopt = ctx.attr.rubyopt,
is_host = ctx.attr.is_host,
),
)]

Expand All @@ -38,7 +36,6 @@ _ruby_toolchain = rule(
"rubyopt": attr.string_list(
default = [],
),
"is_host": attr.bool(default = False),
},
)

Expand All @@ -47,7 +44,6 @@ def ruby_toolchain(
interpreter,
runtime,
rubyopt = [],
is_host = False,
rules_ruby_workspace = RULES_RUBY_WORKSPACE_NAME,
**kwargs):
impl_name = name + "_sdk"
Expand All @@ -56,7 +52,6 @@ def ruby_toolchain(
interpreter = interpreter,
runtime = runtime,
rubyopt = rubyopt,
is_host = is_host,
)

native.toolchain(
Expand Down
1 change: 0 additions & 1 deletion ruby/private/toolchains/BUILD.runtime.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ ruby_toolchain(
name = "toolchain",
interpreter = "//:ruby_bin",
runtime = "//:runtime",
is_host = True,
rules_ruby_workspace = "{rules_ruby_workspace}",
# TODO(yugui) Extract platform info from RbConfig
# exec_compatible_with = [],
Expand Down

0 comments on commit b1caead

Please sign in to comment.