forked from Shopify/bootsnap
/
compile_cache.rb
52 lines (47 loc) · 1.66 KB
/
compile_cache.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# frozen_string_literal: true
module Bootsnap
module CompileCache
Error = Class.new(StandardError)
PermissionError = Class.new(Error)
def self.setup(cache_dir:, iseq:, yaml:, json:)
if iseq
if supported?
require_relative('compile_cache/iseq')
Bootsnap::CompileCache::ISeq.install!(cache_dir)
elsif $VERBOSE
warn("[bootsnap/setup] bytecode caching is not supported on this implementation of Ruby")
end
end
if yaml
if supported?
require_relative('compile_cache/yaml')
Bootsnap::CompileCache::YAML.install!(cache_dir)
elsif $VERBOSE
warn("[bootsnap/setup] YAML parsing caching is not supported on this implementation of Ruby")
end
end
if json
if supported?
require_relative('compile_cache/json')
Bootsnap::CompileCache::JSON.install!(cache_dir)
elsif $VERBOSE
warn("[bootsnap/setup] JSON parsing caching is not supported on this implementation of Ruby")
end
end
end
def self.permission_error(path)
cpath = Bootsnap::CompileCache::ISeq.cache_dir
raise(
PermissionError,
"bootsnap doesn't have permission to write cache entries in '#{cpath}' " \
"(or, less likely, doesn't have permission to read '#{path}')",
)
end
def self.supported?
# only enable on 'ruby' (MRI), POSIX (darwin, linux, *bsd), Windows (RubyInstaller2) and >= 2.3.0
RUBY_ENGINE == 'ruby' &&
RUBY_PLATFORM =~ /darwin|linux|bsd|mswin|mingw|cygwin/ &&
Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("2.3.0")
end
end
end