Skip to content

Commit 770e8e2

Browse files
authored
Merge pull request #2401 from Shopify/at-runtime-helpers
Move runtime helpers under Tapioca::Runtime
2 parents a92a5dd + e5da42d commit 770e8e2

9 files changed

Lines changed: 47 additions & 42 deletions

File tree

lib/tapioca.rb

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -9,42 +9,6 @@ module Tapioca
99

1010
@traces = [] #: Array[TracePoint]
1111

12-
NOOP_METHOD = ->(*_args, **_kwargs, &_block) {} #: ^() -> void
13-
private_constant :NOOP_METHOD
14-
15-
class << self
16-
extend T::Sig
17-
18-
#: [Result] { -> Result } -> Result
19-
def silence_warnings(&blk)
20-
original_verbosity = $VERBOSE
21-
$VERBOSE = nil
22-
::Gem::DefaultUserInteraction.use_ui(::Gem::SilentUI.new) do
23-
blk.call
24-
end
25-
ensure
26-
$VERBOSE = original_verbosity
27-
end
28-
29-
#: [Result] { -> Result } -> Result
30-
def with_disabled_exits(&block)
31-
original_abort = Kernel.instance_method(:abort)
32-
original_exit = Kernel.instance_method(:exit)
33-
34-
begin
35-
Kernel.define_method(:abort, NOOP_METHOD)
36-
Kernel.define_method(:exit, NOOP_METHOD)
37-
38-
Tapioca.silence_warnings do
39-
block.call
40-
end
41-
ensure
42-
Kernel.define_method(:exit, original_exit)
43-
Kernel.define_method(:abort, original_abort)
44-
end
45-
end
46-
end
47-
4812
class Error < StandardError; end
4913

5014
LIB_ROOT_DIR = T.must(__dir__) #: String

lib/tapioca/commands/abstract_gem.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def compile_gem_rbi(gem)
7070
reason: "types exported from the `#{gem.name}` gem",
7171
) if @file_header
7272

73-
rbi.root = Tapioca.with_disabled_exits do
73+
rbi.root = Runtime.with_disabled_exits do
7474
Tapioca::Gem::Pipeline.new(
7575
gem,
7676
include_doc: @include_doc,

lib/tapioca/commands/command.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def initialize
2121
# @final
2222
#: -> void
2323
def run
24-
Tapioca.silence_warnings do
24+
Runtime.silence_warnings do
2525
execute
2626
end
2727
end

lib/tapioca/helpers/test/content.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def remove_tmp_path
2828
#: (String name, String content, ?require_file: bool) -> String
2929
def add_ruby_file(name, content, require_file: true)
3030
add_content_file(name, content).tap do |file_name|
31-
Tapioca.silence_warnings { require(file_name) } if require_file
31+
Runtime.silence_warnings { require(file_name) } if require_file
3232
end
3333
end
3434

lib/tapioca/internal.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
require "set"
55

66
require "tapioca"
7+
require "tapioca/runtime/helpers"
78
require "tapioca/runtime/reflection"
89
require "tapioca/runtime/trackers"
910

lib/tapioca/loaders/gem.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def require_gem_file
4747

4848
bundle_loaded = true #: bool
4949

50-
Tapioca.with_disabled_exits do
50+
Runtime.with_disabled_exits do
5151
load_bundle(@bundle, @prerequire, @postrequire, @halt_upon_load_error)
5252
rescue LoadError => e
5353
bundle_loaded = false

lib/tapioca/runtime/helpers.rb

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# typed: strict
2+
# frozen_string_literal: true
3+
4+
module Tapioca
5+
module Runtime
6+
NOOP_METHOD = ->(*_args, **_kwargs, &_block) {} #: ^() -> void
7+
private_constant :NOOP_METHOD
8+
9+
class << self
10+
#: [Result] { -> Result } -> Result
11+
def silence_warnings(&blk)
12+
original_verbosity = $VERBOSE
13+
$VERBOSE = nil
14+
::Gem::DefaultUserInteraction.use_ui(::Gem::SilentUI.new) do
15+
blk.call
16+
end
17+
ensure
18+
$VERBOSE = original_verbosity
19+
end
20+
21+
#: [Result] { -> Result } -> Result
22+
def with_disabled_exits(&block)
23+
original_abort = Kernel.instance_method(:abort)
24+
original_exit = Kernel.instance_method(:exit)
25+
26+
begin
27+
Kernel.define_method(:abort, NOOP_METHOD)
28+
Kernel.define_method(:exit, NOOP_METHOD)
29+
30+
silence_warnings do
31+
block.call
32+
end
33+
ensure
34+
Kernel.define_method(:exit, original_exit)
35+
Kernel.define_method(:abort, original_abort)
36+
end
37+
end
38+
end
39+
end
40+
end

lib/tapioca/runtime/trackers/autoload.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class << self
1515

1616
#: -> void
1717
def eager_load_all!
18-
Tapioca.with_disabled_exits do
18+
Runtime.with_disabled_exits do
1919
until @constant_names_registered_for_autoload.empty?
2020
# Grab the next constant name
2121
constant_name = T.must(@constant_names_registered_for_autoload.shift)

spec/tapioca/dsl/compilers/protobuf_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,7 @@ def add_proto_file(name, content, require_file: true)
598598
_, stderr, status = Open3.capture3("protoc --proto_path=#{proto_dir} --ruby_out=#{lib_path} #{proto_path}")
599599
raise "Error executing protoc: #{stderr}" unless status.success?
600600

601-
Tapioca.silence_warnings { require("#{lib_path}/#{name}_pb.rb") } if require_file
601+
Runtime.silence_warnings { require("#{lib_path}/#{name}_pb.rb") } if require_file
602602
end
603603
end
604604
end

0 commit comments

Comments
 (0)