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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
|
# frozen_string_literal: true
require "bootsnap"
require "bootsnap/cli/worker_pool"
require "optparse"
require "fileutils"
module Bootsnap
class CLI
unless Regexp.method_defined?(:match?)
module RegexpMatchBackport
refine Regexp do
def match?(string)
!!match(string)
end
end
end
using RegexpMatchBackport
end
attr_reader :cache_dir, :argv
attr_accessor :compile_gemfile, :exclude, :verbose, :iseq, :yaml, :jobs
def initialize(argv)
@argv = argv
self.cache_dir = ENV.fetch("BOOTSNAP_CACHE_DIR", "tmp/cache")
self.compile_gemfile = false
self.exclude = nil
self.verbose = false
self.jobs = nil
self.iseq = true
self.yaml = true
end
def precompile_command(*sources)
require "bootsnap/compile_cache"
fix_default_encoding do
Bootsnap::CompileCache.setup(
cache_dir: cache_dir,
iseq: iseq,
yaml: yaml,
revalidation: true,
)
@work_pool = WorkerPool.create(size: jobs, jobs: {
ruby: method(:precompile_ruby),
yaml: method(:precompile_yaml),
})
@work_pool.spawn
main_sources = sources.map { |d| File.expand_path(d) }
precompile_ruby_files(main_sources)
precompile_yaml_files(main_sources)
if compile_gemfile
# Gems that include JSON or YAML files usually don't put them in `lib/`.
# So we look at the gem root.
# Similarly, gems that include Rails engines generally file Ruby files in `app/`.
# However some gems embed their tests, they're very unlikely to be loaded, so not worth precompiling.
gem_exclude = Regexp.union([exclude, "/spec/", "/test/", "/features/"].compact)
gem_pattern = %r{^#{Regexp.escape(Bundler.bundle_path.to_s)}/?(?:bundler/)?gems/[^/]+}
gem_paths = $LOAD_PATH.map { |p| p[gem_pattern] || p }.uniq
precompile_ruby_files(gem_paths, exclude: gem_exclude)
precompile_yaml_files(gem_paths, exclude: gem_exclude)
end
if (exitstatus = @work_pool.shutdown)
exit(exitstatus)
end
end
0
end
dir_sort = begin
Dir[__FILE__, sort: false]
true
rescue ArgumentError, TypeError
false
end
if dir_sort
def list_files(path, pattern)
if File.directory?(path)
Dir[File.join(path, pattern), sort: false]
elsif File.exist?(path)
[path]
else
[]
end
end
else
def list_files(path, pattern)
if File.directory?(path)
Dir[File.join(path, pattern)]
elsif File.exist?(path)
[path]
else
[]
end
end
end
def run
parser.parse!(argv)
command = argv.shift
method = "#{command}_command"
if respond_to?(method)
public_send(method, *argv)
else
invalid_usage!("Unknown command: #{command}")
end
end
private
def precompile_yaml_files(load_paths, exclude: self.exclude)
return unless yaml
load_paths.each do |path|
if !exclude || !exclude.match?(path)
list_files(path, "**/*.{yml,yaml}").each do |yaml_file|
# We ignore hidden files to not match the various .ci.yml files
if !File.basename(yaml_file).start_with?(".") && (!exclude || !exclude.match?(yaml_file))
@work_pool.push(:yaml, yaml_file)
end
end
end
end
end
def precompile_yaml(*yaml_files)
Array(yaml_files).each do |yaml_file|
if CompileCache::YAML.precompile(yaml_file) && verbose
$stderr.puts(yaml_file)
end
end
end
def precompile_ruby_files(load_paths, exclude: self.exclude)
return unless iseq
load_paths.each do |path|
if !exclude || !exclude.match?(path)
list_files(path, "**/{*.rb,*.rake,Rakefile}").each do |ruby_file|
if !exclude || !exclude.match?(ruby_file)
@work_pool.push(:ruby, ruby_file)
end
end
end
end
end
def precompile_ruby(*ruby_files)
Array(ruby_files).each do |ruby_file|
if CompileCache::ISeq.precompile(ruby_file) && verbose
$stderr.puts(ruby_file)
end
end
end
def fix_default_encoding
if Encoding.default_external == Encoding::US_ASCII
Encoding.default_external = Encoding::UTF_8
begin
yield
ensure
Encoding.default_external = Encoding::US_ASCII
end
else
yield
end
end
def invalid_usage!(message)
$stderr.puts message
$stderr.puts
$stderr.puts parser
1
end
def cache_dir=(dir)
@cache_dir = File.expand_path(File.join(dir, "bootsnap/compile-cache"))
end
def exclude_pattern(pattern)
(@exclude_patterns ||= []) << Regexp.new(pattern)
self.exclude = Regexp.union(@exclude_patterns)
end
def parser
@parser ||= OptionParser.new do |opts|
opts.version = Bootsnap::VERSION
opts.program_name = "bootsnap"
opts.banner = "Usage: bootsnap COMMAND [ARGS]"
opts.separator ""
opts.separator "GLOBAL OPTIONS"
opts.separator ""
help = <<~HELP
Path to the bootsnap cache directory. Defaults to tmp/cache
HELP
opts.on("--cache-dir DIR", help.strip) do |dir|
self.cache_dir = dir
end
help = <<~HELP
Print precompiled paths.
HELP
opts.on("--verbose", "-v", help.strip) do
self.verbose = true
end
help = <<~HELP
Number of workers to use. Default to number of processors, set to 0 to disable multi-processing.
HELP
opts.on("--jobs JOBS", "-j", help.strip) do |jobs|
self.jobs = Integer(jobs)
end
opts.separator ""
opts.separator "COMMANDS"
opts.separator ""
opts.separator " precompile [DIRECTORIES...]: Precompile all .rb files in the passed directories"
help = <<~HELP
Precompile the gems in Gemfile
HELP
opts.on("--gemfile", help) { self.compile_gemfile = true }
help = <<~HELP
Path pattern to not precompile. e.g. --exclude 'aws-sdk|google-api'
HELP
opts.on("--exclude PATTERN", help) { |pattern| exclude_pattern(pattern) }
help = <<~HELP
Disable ISeq (.rb) precompilation.
HELP
opts.on("--no-iseq", help) { self.iseq = false }
help = <<~HELP
Disable YAML precompilation.
HELP
opts.on("--no-yaml", help) { self.yaml = false }
help = <<~HELP
Disable JSON precompilation. Deprecated.
HELP
opts.on("--no-json", help) { $stderr.puts("The --no-json option is deprecated and now a noop.") }
end
end
end
end
|