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
36
# 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
  self.json = 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

#jsonObject

Returns the value of attribute json.



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

def json
  @json
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



92
93
94
95
96
97
98
99
100
# File 'lib/bootsnap/cli.rb', line 92

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



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

def precompile_command(*sources)
  require "bootsnap/compile_cache"

  fix_default_encoding do
    Bootsnap::CompileCache.setup(
      cache_dir: cache_dir,
      iseq: iseq,
      yaml: yaml,
      json: json,
      revalidation: true,
    )

    @work_pool = WorkerPool.create(size: jobs, jobs: {
      ruby: method(:precompile_ruby),
      yaml: method(:precompile_yaml),
      json: method(:precompile_json),
    })
    @work_pool.spawn

    main_sources = sources.map { |d| File.expand_path(d) }
    precompile_ruby_files(main_sources)
    precompile_yaml_files(main_sources)
    precompile_json_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)
      precompile_json_files(gem_paths, exclude: gem_exclude)
    end

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

#runObject



113
114
115
116
117
118
119
120
121
122
# File 'lib/bootsnap/cli.rb', line 113

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