Skip to content

Commit 15ddc01

Browse files
committed
refactor: introduce helper for executing commands in repository
1 parent 3c7a6ac commit 15ddc01

File tree

1 file changed

+20
-26
lines changed

1 file changed

+20
-26
lines changed

ruby/private/download.bzl

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ def _install_via_rubyinstaller(repository_ctx, version):
192192
)
193193

194194
repository_ctx.report_progress("Installing Ruby %s" % version)
195-
result = repository_ctx.execute([
195+
_execute_command(repository_ctx, [
196196
"./ruby-installer.exe",
197197
"/components=ruby,msys2",
198198
"/currentuser",
@@ -201,30 +201,19 @@ def _install_via_rubyinstaller(repository_ctx, version):
201201
"/verysilent",
202202
])
203203
repository_ctx.delete("ruby-installer.exe")
204-
if result.return_code != 0:
205-
fail("%s\n%s" % (result.stdout, result.stderr))
206204

207-
result = repository_ctx.execute(["./dist/bin/ridk.cmd", "install", "1", "3"])
208-
if result.return_code != 0:
209-
fail("%s\n%s" % (result.stdout, result.stderr))
205+
_execute_command(repository_ctx, ["./dist/bin/ridk.cmd", "install", "1", "3"])
210206

211207
if len(repository_ctx.attr.msys2_packages) > 0:
212-
mingw_package_prefix = None
213-
result = repository_ctx.execute([
208+
mingw_package_prefix = _execute_command(repository_ctx, [
214209
"./dist/bin/ruby.exe",
215210
"-rruby_installer",
216211
"-e",
217212
"puts RubyInstaller::Runtime::Msys2Installation.new.mingw_package_prefix",
218213
])
219-
if result.return_code != 0:
220-
fail("%s\n%s" % (result.stdout, result.stderr))
221-
else:
222-
mingw_package_prefix = result.stdout.strip()
223214

224215
packages = ["%s-%s" % (mingw_package_prefix, package) for package in repository_ctx.attr.msys2_packages]
225-
result = repository_ctx.execute(["./dist/bin/ridk.cmd", "exec", "pacman", "--sync", "--noconfirm"] + packages)
226-
if result.return_code != 0:
227-
fail("%s\n%s" % (result.stdout, result.stderr))
216+
_execute_command(repository_ctx, ["./dist/bin/ridk.cmd", "exec", "pacman", "--sync", "--noconfirm"] + packages)
228217

229218
# Ruby 3.0 compatibility
230219
binpath = repository_ctx.path("dist/bin")
@@ -245,38 +234,43 @@ def _install_via_ruby_build(repository_ctx, version):
245234
)
246235

247236
repository_ctx.report_progress("Installing Ruby %s" % version)
248-
result = repository_ctx.execute(
237+
_execute_command(
238+
repository_ctx,
249239
["ruby-build/bin/ruby-build", "--verbose", version, "dist", "--", "--disable-install-doc"],
250240
timeout = 1200,
251241
quiet = not repository_ctx.os.environ.get("RUBY_RULES_DEBUG", default = False),
252242
)
253243

254244
repository_ctx.delete("ruby-build")
255245

256-
if result.return_code != 0:
257-
fail("%s\n%s" % (result.stdout, result.stderr))
258-
259246
def _symlink_system_ruby(repository_ctx):
260-
result = repository_ctx.execute(["ruby", "-e", "puts RbConfig.ruby"])
261-
if result.return_code != 0:
262-
fail("Failed to determine the system Ruby path:\n%s\n%s" % (result.stdout, result.stderr))
263-
264247
_symlink_system_ruby_dir("bindir", repository_ctx)
265248
_symlink_system_ruby_dir("libdir", repository_ctx)
266249
_symlink_system_ruby_dir("rubyhdrdir", repository_ctx)
267250

268-
engine = repository_ctx.execute(["ruby", "-e", "puts RbConfig::CONFIG['RUBY_BASE_NAME']"]).stdout.strip()
251+
engine = _execute_command(repository_ctx, ["ruby", "-e", "puts RbConfig::CONFIG['RUBY_BASE_NAME']"])
269252
return engine
270253

271254
def _symlink_system_ruby_dir(dirname, repository_ctx):
272-
prefix = repository_ctx.execute(["ruby", "-e", "puts RbConfig::CONFIG['prefix']"]).stdout.strip()
273-
path = repository_ctx.execute(["ruby", "-e", "puts RbConfig::CONFIG['{dirname}']".format(dirname = dirname)]).stdout.strip()
255+
prefix = _execute_command(repository_ctx, ["ruby", "-e", "puts RbConfig::CONFIG['prefix']"])
256+
path = _execute_command(repository_ctx, ["ruby", "-e", "puts RbConfig::CONFIG['{dirname}']".format(dirname = dirname)])
274257
src = repository_ctx.path(path)
275258
dirname = path.removeprefix(prefix).removeprefix("/")
276259
dst = repository_ctx.path("dist/{dirname}".format(dirname = dirname))
277260
if not dst.exists:
278261
repository_ctx.symlink(src, dst)
279262

263+
def _execute_command(repository_ctx, command, timeout = 600, quiet = False):
264+
result = repository_ctx.execute(command, timeout = timeout, quiet = quiet)
265+
if result.return_code != 0:
266+
fail("Command '{command}' failed ({code} exit code):\n{stdout}\n{stderr}".format(
267+
command = " ".join(command),
268+
code = result.return_code,
269+
stdout = result.stdout,
270+
stderr = result.stderr,
271+
))
272+
return result.stdout.strip()
273+
280274
rb_download = repository_rule(
281275
implementation = _rb_download_impl,
282276
attrs = {

0 commit comments

Comments
 (0)