8000 Revert "temp" · bazelruby/rules_ruby@e150e79 · GitHub
[go: up one dir, main page]

Skip to content

Commit e150e79

Browse files
committed
Revert "temp"
This reverts commit 16d66e4.
1 parent 16d66e4 commit e150e79

File tree

2 files changed

+110
-139
lines changed

2 files changed

+110
-139
lines changed

ruby/private/binary_wrapper.tpl

Lines changed: 110 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,119 @@
2323
# limitations under the License.
2424
#
2525

26+
require 'rbconfig'
27+
require 'shellwords'
28+
29+
def find_runfiles
30+
stub_filename = File.absolute_path($0)
31+
runfiles = "#{stub_filename}.runfiles"
32+
loop do
33+
case
34+
when File.directory?(runfiles)
35+
return runfiles
36+
when %r!(.*\.runfiles)/.*!o =~ stub_filename
37+
return $1
38+
when File.symlink?(stub_filename)
39+
target = File.readlink(stub_filename)
40+
stub_filename = File.absolute_path(target, File.dirname(stub_filename))
41+
else
42+
break
43+
end
44+
end
45+
raise "Cannot find .runfiles directory for #{$0}"
46+
end
47+
48+
def create_loadpath_entries(custom, runfiles)
49+
[runfiles] + custom.map {|path| File.join(runfiles, path) }
50+
end
51+
52+
def get_repository_imports(runfiles)
53+
Dir.children(runfiles).map {|d|
54+
File.join(runfiles, d)
55+
}.select {|d|
56+
File.directory? d
57+
}
58+
end
59+
60+
# Finds the runfiles manifest or the runfiles directory.
61+
def runfiles_envvar(runfiles)
62+
# If this binary is the data-dependency of another one, the other sets
63+
# RUNFILES_MANIFEST_FILE or RUNFILES_DIR for our sake.
64+
manifest = ENV['RUNFILES_MANIFEST_FILE']
65+
if manifest
66+
return ['RUNFILES_MANIFEST_FILE', manifest]
67+
end
68+
69+
dir = ENV['RUNFILES_DIR']
70+
if dir
71+
return ['RUNFILES_DIR', dir]
72+
end
73+
74+
# Look for the runfiles "output" manifest, argv[0] + ".runfiles_manifest"
75+
manifest = runfiles + '_manifest'
76+
if File.exists?(manifest)
77+
return ['RUNFILES_MANIFEST_FILE', manifest]
78+
end
79+
80+
# Look for the runfiles "input" manifest, argv[0] + ".runfiles/MANIFEST"
81+
manifest = File.join(runfiles, 'MANIFEST')
82+
if File.exists?(manifest)
83+
return ['RUNFILES_DIR', manifest]
84+
end
85+
86+
# If running in a sandbox and no environment variables are set, then
87+
# Look for the runfiles next to the binary.
88+
if runfiles.end_with?('.runfiles') and File.directory?(runfiles)
89+
return ['RUNFILES_DIR', runfiles]
90+
end
91+
end
92+
93+
def find_ruby_program
94+
File.join(
95+
RbConfig::CONFIG['bindir'],
96+
RbConfig::CONFIG['ruby_install_name'],
97+
)
98+
end
99+
100+
def expand_vars(args)
101+
args.map do |arg|
102+
arg.gsub(/\${(.+?)}/o) do
103+
case $1
104+
when 'RUNFILES_DIR'
105+
runfiles
106+
else
107+
ENV[$1]
108+
end
109+
end
110+
end
111+
end
112+
26113
def main(args)
27114
custom_loadpaths = {loadpaths}
28-
rubyopt = {rubyopt}
115+
runfiles = find_runfiles
116+
117+
loadpaths = create_loadpath_entries(custom_loadpaths, runfiles)
118+
loadpaths += get_repository_imports(runfiles)
119+
loadpaths += ENV['RUBYLIB'].split(':') if ENV.key?('RUBYLIB')
120+
ENV['RUBYLIB'] = loadpaths.join(':')
121+
122+
runfiles_envkey, runfiles_envvalue = runfiles_envvar(runfiles)
123+
ENV[runfiles_envkey] = runfiles_envvalue if runfiles_envkey
124+
125+
program_name = {program}
126+
if program_name
127+
program = File.join(runfiles, program_name)
128+
else
129+
program = find_ruby_program
130+
end
131+
program_opts = expand_vars({program_opts})
132+
29133
main = {main}
134+
main = File.join(runfiles, main)
135+
rubyopt = expand_vars({rubyopt})
136+
ENV['RUBYOPT'] = Shellwords.join(expand_vars({rubyopt}))
30137

31-
Runfiles.new(custom_loadpaths, rubyopt).exec(main, *args)
138+
exec(program, *program_opts, main, *args)
32139
# TODO(yugui) Support windows
33140
end
34141

@@ -52,4 +159,4 @@ end
52159
#shell { echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e
53160
#shell # --- end runfiles.bash initialization v2 ---
54161
#shell
55-
#shell exec "$(rlocation {interpreter})" -r"$(rlocation @bazelruby_ruby_rules//ruby/private/tools:runfiles.rb)" ${BASH_SOURCE:-$0} "$@"
162+
#shell exec "$(rlocation {interpreter})" ${BASH_SOURCE:-$0} "$@"

ruby/private/tools/runfiles.rb

Lines changed: 0 additions & 136 deletions
This file was deleted.

0 commit comments

Comments
 (0)
0