Skip to content

Commit

Permalink
Code style and CI improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
byroot committed Jan 28, 2022
1 parent e3ef615 commit 647969f
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 110 deletions.
20 changes: 15 additions & 5 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,19 @@ jobs:
- uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby }}
- run: bundle install
bundler-cache: true
- run: bundle exec rake

rubocop:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: ruby/setup-ruby@v1
with:
ruby-version: '2.3'
bundler-cache: true
- run: bundle exec rubocop

rubies:
strategy:
matrix:
Expand All @@ -36,7 +47,7 @@ jobs:
- uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby }}
- run: bundle install
bundler-cache: true
- run: bundle exec rake

psych4:
Expand All @@ -53,9 +64,8 @@ jobs:
- uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby }}
- run: bundle install
bundler-cache: true
- run: bundle exec rake


minimal:
strategy:
Expand All @@ -69,5 +79,5 @@ jobs:
- uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby }}
- run: bundle install
bundler-cache: true
- run: bin/test-minimal-support
12 changes: 6 additions & 6 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ Style/TrailingCommaInArguments:
Layout/LineLength:
Max: 120

Style/TrailingCommaInArguments:
EnforcedStyleForMultiline: comma

Metrics/AbcSize:
Enabled: false

Expand Down Expand Up @@ -62,9 +59,6 @@ Naming/MethodName:
Exclude:
- 'test/**/*'

Layout/SpaceInsideHashLiteralBraces:
EnforcedStyle: no_space

Naming/RescuedExceptionsVariableName:
PreferredName: error

Expand Down Expand Up @@ -158,3 +152,9 @@ Style/HashTransformKeys:

Style/HashTransformValues:
Enabled: true

Style/RedundantReturn:
Enabled: false

Style/YodaCondition:
Enabled: false
196 changes: 98 additions & 98 deletions lib/bootsnap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,128 +10,128 @@ module Bootsnap

class << self
attr_reader :logger
end

def self.log!
self.logger = $stderr.method(:puts)
end

def self.logger=(logger)
@logger = logger
self.instrumentation = if logger.respond_to?(:debug)
->(event, path) { @logger.debug("[Bootsnap] #{event} #{path}") }
else
->(event, path) { @logger.call("[Bootsnap] #{event} #{path}") }
def log!
self.logger = $stderr.method(:puts)
end
end

def self.instrumentation=(callback)
@instrumentation = callback
if respond_to?(:instrumentation_enabled=, true)
self.instrumentation_enabled = !!callback
def logger=(logger)
@logger = logger
self.instrumentation = if logger.respond_to?(:debug)
->(event, path) { @logger.debug("[Bootsnap] #{event} #{path}") }
else
->(event, path) { @logger.call("[Bootsnap] #{event} #{path}") }
end
end
end

def self._instrument(event, path)
@instrumentation.call(event, path)
end

def self.setup(
cache_dir:,
development_mode: true,
load_path_cache: true,
autoload_paths_cache: nil,
disable_trace: nil,
compile_cache_iseq: true,
compile_cache_yaml: true,
compile_cache_json: true
)
unless autoload_paths_cache.nil?
warn "[DEPRECATED] Bootsnap's `autoload_paths_cache:` option is deprecated and will be removed. " \
"If you use Zeitwerk this option is useless, and if you are still using the classic autoloader " \
"upgrading is recommended."
def instrumentation=(callback)
@instrumentation = callback
if respond_to?(:instrumentation_enabled=, true)
self.instrumentation_enabled = !!callback
end
end

unless disable_trace.nil?
warn "[DEPRECATED] Bootsnap's `disable_trace:` option is deprecated and will be removed. " \
"If you use Ruby 2.5 or newer this option is useless, if not upgrading is recommended."
def _instrument(event, path)
@instrumentation.call(event, path)
end

if compile_cache_iseq && !iseq_cache_supported?
warn "Ruby 2.5 has a bug that break code tracing when code is loaded from cache. It is recommened " \
"to turn `compile_cache_iseq` off on Ruby 2.5"
end
def setup(
cache_dir:,
development_mode: true,
load_path_cache: true,
autoload_paths_cache: nil,
disable_trace: nil,
compile_cache_iseq: true,
compile_cache_yaml: true,
compile_cache_json: true
)
unless autoload_paths_cache.nil?
warn "[DEPRECATED] Bootsnap's `autoload_paths_cache:` option is deprecated and will be removed. " \
"If you use Zeitwerk this option is useless, and if you are still using the classic autoloader " \
"upgrading is recommended."
end

if load_path_cache
Bootsnap::LoadPathCache.setup(
cache_path: cache_dir + "/bootsnap/load-path-cache",
development_mode: development_mode,
)
end
unless disable_trace.nil?
warn "[DEPRECATED] Bootsnap's `disable_trace:` option is deprecated and will be removed. " \
"If you use Ruby 2.5 or newer this option is useless, if not upgrading is recommended."
end

Bootsnap::CompileCache.setup(
cache_dir: cache_dir + "/bootsnap/compile-cache",
iseq: compile_cache_iseq,
yaml: compile_cache_yaml,
json: compile_cache_json,
)
end
if compile_cache_iseq && !iseq_cache_supported?
warn "Ruby 2.5 has a bug that break code tracing when code is loaded from cache. It is recommened " \
"to turn `compile_cache_iseq` off on Ruby 2.5"
end

def self.iseq_cache_supported?
return @iseq_cache_supported if defined? @iseq_cache_supported
if load_path_cache
Bootsnap::LoadPathCache.setup(
cache_path: cache_dir + "/bootsnap/load-path-cache",
development_mode: development_mode,
)
end

ruby_version = Gem::Version.new(RUBY_VERSION)
@iseq_cache_supported = ruby_version < Gem::Version.new("2.5.0") || ruby_version >= Gem::Version.new("2.6.0")
end
Bootsnap::CompileCache.setup(
cache_dir: cache_dir + "/bootsnap/compile-cache",
iseq: compile_cache_iseq,
yaml: compile_cache_yaml,
json: compile_cache_json,
)
end

def self.default_setup
env = ENV["RAILS_ENV"] || ENV["RACK_ENV"] || ENV["ENV"]
development_mode = ["", nil, "development"].include?(env)
def iseq_cache_supported?
return @iseq_cache_supported if defined? @iseq_cache_supported

unless ENV["DISABLE_BOOTSNAP"]
cache_dir = ENV["BOOTSNAP_CACHE_DIR"]
unless cache_dir
config_dir_frame = caller.detect do |line|
line.include?("/config/")
end
ruby_version = Gem::Version.new(RUBY_VERSION)
@iseq_cache_supported = ruby_version < Gem::Version.new("2.5.0") || ruby_version >= Gem::Version.new("2.6.0")
end

unless config_dir_frame
$stderr.puts("[bootsnap/setup] couldn't infer cache directory! Either:")
$stderr.puts("[bootsnap/setup] 1. require bootsnap/setup from your application's config directory; or")
$stderr.puts("[bootsnap/setup] 2. Define the environment variable BOOTSNAP_CACHE_DIR")
def default_setup
env = ENV["RAILS_ENV"] || ENV["RACK_ENV"] || ENV["ENV"]
development_mode = ["", nil, "development"].include?(env)

raise("couldn't infer bootsnap cache directory")
end
unless ENV["DISABLE_BOOTSNAP"]
cache_dir = ENV["BOOTSNAP_CACHE_DIR"]
unless cache_dir
config_dir_frame = caller.detect do |line|
line.include?("/config/")
end

path = config_dir_frame.split(/:\d+:/).first
path = File.dirname(path) until File.basename(path) == "config"
app_root = File.dirname(path)
unless config_dir_frame
$stderr.puts("[bootsnap/setup] couldn't infer cache directory! Either:")
$stderr.puts("[bootsnap/setup] 1. require bootsnap/setup from your application's config directory; or")
$stderr.puts("[bootsnap/setup] 2. Define the environment variable BOOTSNAP_CACHE_DIR")

cache_dir = File.join(app_root, "tmp", "cache")
end
raise("couldn't infer bootsnap cache directory")
end

setup(
cache_dir: cache_dir,
development_mode: development_mode,
load_path_cache: !ENV["DISABLE_BOOTSNAP_LOAD_PATH_CACHE"],
compile_cache_iseq: !ENV["DISABLE_BOOTSNAP_COMPILE_CACHE"] && iseq_cache_supported?,
compile_cache_yaml: !ENV["DISABLE_BOOTSNAP_COMPILE_CACHE"],
compile_cache_json: !ENV["DISABLE_BOOTSNAP_COMPILE_CACHE"],
)
path = config_dir_frame.split(/:\d+:/).first
path = File.dirname(path) until File.basename(path) == "config"
app_root = File.dirname(path)

cache_dir = File.join(app_root, "tmp", "cache")
end

if ENV["BOOTSNAP_LOG"]
log!
setup(
cache_dir: cache_dir,
development_mode: development_mode,
load_path_cache: !ENV["DISABLE_BOOTSNAP_LOAD_PATH_CACHE"],
compile_cache_iseq: !ENV["DISABLE_BOOTSNAP_COMPILE_CACHE"] && iseq_cache_supported?,
compile_cache_yaml: !ENV["DISABLE_BOOTSNAP_COMPILE_CACHE"],
compile_cache_json: !ENV["DISABLE_BOOTSNAP_COMPILE_CACHE"],
)

if ENV["BOOTSNAP_LOG"]
log!
end
end
end
end

if RbConfig::CONFIG["host_os"] =~ /mswin|mingw|cygwin/
def self.absolute_path?(path)
path[1] == ":"
end
else
def self.absolute_path?(path)
path.start_with?("/")
if RbConfig::CONFIG["host_os"] =~ /mswin|mingw|cygwin/
def absolute_path?(path)
path[1] == ":"
end
else
def absolute_path?(path)
path.start_with?("/")
end
end
end
end
2 changes: 1 addition & 1 deletion test/compile_cache/yaml_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def test_load_psych_4_with_alias
end

def test_load_psych_4_with_unsafe_class
Help.set_file("a.yml", "---\nfoo: !ruby/regexp /bar/\n", 100)
Help.set_file("a.yml", "---\nfoo: !ruby/regexp /bar/\n", 100)

expected = {"foo" => /bar/}
assert_equal(expected, FakeYaml.unsafe_load_file("a.yml"))
Expand Down

0 comments on commit 647969f

Please sign in to comment.