Class: Bootsnap::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/bootsnap/cli.rb,
lib/bootsnap/cli/worker_pool.rb

Defined Under Namespace

Modules: RegexpMatchBackport Classes: WorkerPool

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ CLI

Returns a new instance of CLI.



26
27
28
29
30
31
32
33
34
35
# File 'lib/bootsnap/cli.rb', line 26

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 = Etc.nprocessors
  self.iseq = true
  self.yaml = true
end

Instance Attribute Details

#argvObject (readonly)

Returns the value of attribute argv.



22
23
24
# File 'lib/bootsnap/cli.rb', line 22

def argv
  @argv
end

#cache_dirObject

Returns the value of attribute cache_dir.



22
23
24
# File 'lib/bootsnap/cli.rb', line 22

def cache_dir
  @cache_dir
end

#compile_gemfileObject

Returns the value of attribute compile_gemfile.



24
25
26
# File 'lib/bootsnap/cli.rb', line 24

def compile_gemfile
  @compile_gemfile
end

#excludeObject

Returns the value of attribute exclude.



24
25
26
# File 'lib/bootsnap/cli.rb', line 24

def exclude
  @exclude
end

#iseqObject

Returns the value of attribute iseq.



24
25
26
# File 'lib/bootsnap/cli.rb', line 24

def iseq
  @iseq
end

#jobsObject

Returns the value of attribute jobs.



24
25
26
# File 'lib/bootsnap/cli.rb', line 24

def jobs
  @jobs
end

#verboseObject

Returns the value of attribute verbose.



24
25
26
# File 'lib/bootsnap/cli.rb', line 24

def verbose
  @verbose
end

#yamlObject

Returns the value of attribute yaml.



24
25
26
# File 'lib/bootsnap/cli.rb', line 24

def yaml
  @yaml
end

Instance Method Details

#list_files(path, pattern) ⇒ Object



83
84
85
86
87
88
89
90
91
# File 'lib/bootsnap/cli.rb', line 83

def list_files(path, pattern)
  if File.directory?(path)
    Dir[File.join(path,  pattern), sort: false]
  elsif File.exist?(path)
    [path]
  else
    []
  end
end

#precompile_command(*sources) ⇒ Object



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
# File 'lib/bootsnap/cli.rb', line 37

def precompile_command(*sources)
  require 'bootsnap/compile_cache/iseq'
  require 'bootsnap/compile_cache/yaml'

  fix_default_encoding do
    Bootsnap::CompileCache::ISeq.cache_dir = self.cache_dir
    Bootsnap::CompileCache::YAML.init!
    Bootsnap::CompileCache::YAML.cache_dir = self.cache_dir

    @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
      # Some gems embed their tests, they're very unlikely to be loaded, so not worth precompiling.
      gem_exclude = Regexp.union([exclude, '/spec/', '/test/'].compact)
      precompile_ruby_files($LOAD_PATH.map { |d| File.expand_path(d) }, exclude: gem_exclude)

      # Gems that include YAML files usually don't put them in `lib/`.
      # So we look at the gem root.
      gem_pattern = %r{^#{Regexp.escape(Bundler.bundle_path.to_s)}/?(?:bundler/)?gem\/[^/]+}
      gem_paths = $LOAD_PATH.map { |p| p[gem_pattern] }.compact.uniq
      precompile_yaml_files(gem_paths, exclude: gem_exclude)
    end

    if exitstatus = @work_pool.shutdown
      exit(exitstatus)
    end
  end
  0
end

#runObject



104
105
106
107
108
109
110
111
112
113
# File 'lib/bootsnap/cli.rb', line 104

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