require_bench 1.0.4 → 1.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,36 +3,37 @@
3
3
  # std libs
4
4
  require "forwardable"
5
5
 
6
- # third party libs
7
- # You will need to have gem "colorize" installed!
8
- # NOTE: You will need to require "colorized_string" in your own code,
9
- # in order for this alternate Printer class to load
6
+ # internal colorizer (replaces the GPL-2.0-licensed colorize gem)
7
+ require "require_bench/colors"
8
+
9
+ # Default Printer, unless NO_COLOR is set (see lib/require_bench.rb).
10
10
 
11
11
  class Printer
12
12
  attr_accessor :colors, :color
13
13
 
14
14
  extend Forwardable
15
+
15
16
  def_delegators :@colors, :rotate!, :first
16
17
  def initialize
17
- @colors = ColorizedString.colors.dup.reject { |x| x.match?(/black|white/) }
18
+ @colors = RequireBench::Colors::NAMES.dup
18
19
  end
19
20
 
20
21
  # Log statement when a file starts loading
21
22
  def out_start(file, type)
22
- printf("🚥 #{ColorizedString["[RequireBench-#{type}]"].colorize(first)} 📖 %s 🚥\n", file)
23
+ printf("🚥 #{RequireBench::Colors.colorize("[RequireBench-#{type}]", first)} 📖 %s 🚥\n", file)
23
24
  end
24
25
 
25
26
  # Log statement when a file completed loading
26
27
  def out_consume(seconds, file, type)
27
- printf("🚥 #{ColorizedString["[RequireBench-#{type}]"].colorize(first)} ☑️ %10f %s 🚥\n", seconds, file)
28
+ printf("🚥 #{RequireBench::Colors.colorize("[RequireBench-#{type}]", first)} ☑️ %10f %s 🚥\n", seconds, file)
28
29
  rotate!
29
30
  end
30
31
 
31
32
  # Log statement when a file raises an error while loading
32
33
  def out_err(error, file, type)
33
34
  printf(
34
- "🚥 #{ColorizedString["[RequireBench-#{type}]"].colorize(first)} ❌ '#{error.class}: #{error.message}' loading %s 🚥\n#{error.backtrace.join("\n")}",
35
- file,
35
+ "🚥 #{RequireBench::Colors.colorize("[RequireBench-#{type}]", first)} ❌ '#{error.class}: #{error.message}' loading %s 🚥\n#{error.backtrace.join("\n")}",
36
+ file
36
37
  )
37
38
  rotate!
38
39
  end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RequireBench
4
+ # Minimal internal ANSI colorizer for the color printer.
5
+ #
6
+ # Replaces the GPL-2.0-licensed colorize gem, which was previously the only
7
+ # way to enable colored output. The color printer is the default; see
8
+ # lib/require_bench.rb (set NO_COLOR for the plain printer).
9
+ module Colors
10
+ # ANSI SGR color codes. Black and white are intentionally omitted:
11
+ # they are illegible on common terminal themes.
12
+ CODES = {
13
+ red: 31,
14
+ green: 32,
15
+ yellow: 33,
16
+ blue: 34,
17
+ magenta: 35,
18
+ cyan: 36,
19
+ light_red: 91,
20
+ light_green: 92,
21
+ light_yellow: 93,
22
+ light_blue: 94,
23
+ light_magenta: 95,
24
+ light_cyan: 96
25
+ }.freeze
26
+
27
+ # Ordered color names the color printer rotates through.
28
+ NAMES = CODES.keys.freeze
29
+
30
+ # Wrap a string in the ANSI escape sequence for the named color.
31
+ #
32
+ # @param string [String] the text to colorize
33
+ # @param name [Symbol] one of {NAMES}
34
+ # @return [String] the colorized text
35
+ # @raise [KeyError] when the color name is unknown
36
+ def self.colorize(string, name)
37
+ "\e[#{CODES.fetch(name)}m#{string}\e[0m"
38
+ end
39
+ end
40
+ end
@@ -15,7 +15,7 @@ class Printer
15
15
  def out_err(error, file, type)
16
16
  printf(
17
17
  "🚥 [RequireBench-#{type}] ❌ '#{error.class}: #{error.message}' loading %s 🚥\n#{error.backtrace.join("\n")}",
18
- file,
18
+ file
19
19
  )
20
20
  end
21
21
  end
@@ -1,7 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RequireBench
4
+ # Version namespace for this gem.
4
5
  module Version
5
- VERSION = "1.0.4"
6
+ # Current gem version.
7
+ VERSION = "1.0.5"
6
8
  end
9
+ # Current gem version exposed at the traditional constant location.
10
+ VERSION = Version::VERSION # Traditional Constant Location
7
11
  end
data/lib/require_bench.rb CHANGED
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative "require_bench/version"
4
+
3
5
  REQUIRE_BENCH_ENABLED = ENV.fetch('REQUIRE_BENCH', 'false').casecmp?('true')
4
6
 
5
7
  # STD Libs
@@ -12,10 +14,21 @@ end
12
14
  require "version_gem"
13
15
 
14
16
  # This Gem
15
- require_relative 'require_bench/version'
16
17
 
17
18
  # Namespace for this gem
18
19
  module RequireBench
20
+ # Keep the unwrapped methods out of Kernel's method table. The old alias-based
21
+ # hook was global and could be redefined by another load of this file.
22
+ ORIGINAL_KERNEL_METHODS = {
23
+ require: Kernel.instance_method(:require),
24
+ load: Kernel.instance_method(:load)
25
+ }.freeze
26
+ SEMAPHORE_KEY = :__require_bench_semaphore
27
+
28
+ def self.semaphore_active?
29
+ Thread.current[SEMAPHORE_KEY] == true
30
+ end
31
+
19
32
  if REQUIRE_BENCH_ENABLED
20
33
  TIMINGS = Hash.new { |h, k| h[k] = 0.0 }
21
34
  skips = ENV['REQUIRE_BENCH_SKIP_PATTERN']
@@ -36,10 +49,12 @@ module RequireBench
36
49
  preferred_grouping = precedence.first
37
50
  prefer_not_path = preferred_grouping != 'path' # path correlates to default behavior of regexp matching
38
51
 
39
- if defined?(ColorizedString)
40
- require 'require_bench/color_printer'
41
- else
52
+ no_color = ENV.key?('NO_COLOR') && !ENV['NO_COLOR'].empty?
53
+ if no_color
42
54
  require 'require_bench/printer'
55
+ else
56
+ require 'require_bench/colors'
57
+ require 'require_bench/color_printer'
43
58
  end
44
59
  PRINTER = Printer.new
45
60
 
@@ -94,7 +109,8 @@ module RequireBench
94
109
  TRACKED_METHODS = tracked_methods
95
110
 
96
111
  def consume_with_timing(type, file, *args)
97
- $require_bench_semaphore = true
112
+ previous_semaphore = Thread.current[SEMAPHORE_KEY]
113
+ Thread.current[SEMAPHORE_KEY] = true
98
114
  short_type = type[0]
99
115
  ret = nil
100
116
  # Not sure if this is actually a useful abstraction...
@@ -129,58 +145,69 @@ module RequireBench
129
145
  RequireBench::TIMINGS[prefix] += seconds
130
146
  ret
131
147
  ensure
132
- $require_bench_semaphore = nil
148
+ Thread.current[SEMAPHORE_KEY] = previous_semaphore
133
149
  end
134
150
  module_function :consume_with_timing
135
151
  end
136
- end
137
152
 
138
- if REQUIRE_BENCH_ENABLED
139
- # A Kernel hack that adds require timing to find require problems in app.
140
- module Kernel
141
- alias require_without_timing require
142
- alias load_without_timing load
143
-
144
- def require(file)
145
- _require_bench_consume_file('require', file)
146
- end
153
+ if REQUIRE_BENCH_ENABLED
154
+ # A Kernel prepend keeps one stable interception point and lets us call the
155
+ # original methods without mutating Kernel with aliases.
156
+ module KernelHook
157
+ def require(file)
158
+ _require_bench_consume_file('require', file)
159
+ end
147
160
 
148
- def load(file, *args)
149
- _require_bench_consume_file('load', file, *args)
150
- end
161
+ def load(file, *args)
162
+ _require_bench_consume_file('load', file, *args)
163
+ end
151
164
 
152
- def _require_bench_consume_file(type, file, *args)
153
- file_path = file.to_s
154
- # byebug if file_path.match?(/no_group_fox/)
165
+ def require_without_timing(file, *args)
166
+ RequireBench::ORIGINAL_KERNEL_METHODS[:require].bind(self).call(file, *args)
167
+ end
155
168
 
156
- # Global $ variable, which is always truthy while inside the hack, is to
157
- # prevent a scenario that might result in infinite recursion.
158
- return send("#{type}_without_timing", file_path, *args) if $require_bench_semaphore
169
+ def load_without_timing(file, *args)
170
+ RequireBench::ORIGINAL_KERNEL_METHODS[:load].bind(self).call(file, *args)
171
+ end
159
172
 
160
- short_type = type[0]
161
- measure = RequireBench::INCLUDE_PATTERN && file_path.match?(RequireBench::INCLUDE_PATTERN)
162
- skippy = RequireBench::SKIP_PATTERN && file_path.match?(RequireBench::SKIP_PATTERN)
163
- RequireBench::PRINTER.out_start(file, short_type) if RequireBench::LOG_START
164
- if RequireBench::RESCUED_CLASSES.any?
165
- begin
173
+ def _require_bench_consume_file(type, file, *args)
174
+ file_path = file.to_s
175
+ return send("#{type}_without_timing", file_path, *args) if RequireBench.semaphore_active?
176
+
177
+ short_type = type[0]
178
+ measure = RequireBench::INCLUDE_PATTERN && file_path.match?(RequireBench::INCLUDE_PATTERN)
179
+ skippy = RequireBench::SKIP_PATTERN && file_path.match?(RequireBench::SKIP_PATTERN)
180
+ RequireBench::PRINTER.out_start(file, short_type) if RequireBench::LOG_START
181
+ if RequireBench::RESCUED_CLASSES.any?
182
+ begin
183
+ _require_bench_file(type, measure, skippy, file_path, *args)
184
+ rescue *RequireBench::RESCUED_CLASSES => e
185
+ RequireBench::PRINTER.out_err(e, file, short_type, *args)
186
+ end
187
+ else
166
188
  _require_bench_file(type, measure, skippy, file_path, *args)
167
- rescue *RequireBench::RESCUED_CLASSES => e
168
- RequireBench::PRINTER.out_error(e, file, short_type, *args)
169
189
  end
170
- else
171
- _require_bench_file(type, measure, skippy, file_path, *args)
172
190
  end
173
- end
174
191
 
175
- def _require_bench_file(type, measure, skippy, file_path, *args)
176
- if !measure && skippy
177
- send("#{type}_without_timing", file_path, *args)
178
- elsif RequireBench::INCLUDE_PATTERN.nil? || measure
179
- RequireBench.consume_with_timing(type, file_path, *args)
180
- else
181
- send("#{type}_without_timing", file_path, *args)
192
+ def _require_bench_file(type, measure, skippy, file_path, *args)
193
+ if !measure && skippy
194
+ send("#{type}_without_timing", file_path, *args)
195
+ elsif RequireBench::INCLUDE_PATTERN.nil? || measure
196
+ RequireBench.consume_with_timing(type, file_path, *args)
197
+ else
198
+ send("#{type}_without_timing", file_path, *args)
199
+ end
182
200
  end
201
+
202
+ private :require, :load, :require_without_timing, :load_without_timing,
203
+ :_require_bench_consume_file, :_require_bench_file
183
204
  end
205
+
206
+ Kernel.prepend(KernelHook) unless Kernel.ancestors.include?(KernelHook)
207
+ # Before Ruby 3.0, prepending to Kernel does not reach Object (or any class that
208
+ # already included Kernel), so the hook never engaged. Prepend to Object as well;
209
+ # on Ruby >= 3.0 Object already sees the hook through Kernel, making this a no-op.
210
+ Object.prepend(KernelHook) unless Object.ancestors.include?(KernelHook)
184
211
  end
185
212
  end
186
213
 
@@ -0,0 +1,6 @@
1
+ module RequireBench
2
+ module Version
3
+ VERSION: String
4
+ end
5
+ VERSION: String
6
+ end
data.tar.gz.sig CHANGED
Binary file