-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #326 from Shopify/executable
Add a bootsnap command to allow to precompile gems without booting the application
- Loading branch information
Showing
7 changed files
with
210 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#!/usr/bin/env ruby | ||
# frozen_string_literal: true | ||
|
||
require 'bootsnap/cli' | ||
exit Bootsnap::CLI.new(ARGV).run |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'bootsnap' | ||
require 'optparse' | ||
require 'fileutils' | ||
|
||
module Bootsnap | ||
class CLI | ||
unless Regexp.method_defined?(:match?) | ||
module RegexpMatchBackport | ||
refine Regepx do | ||
def match?(string) | ||
!!match(string) | ||
end | ||
end | ||
end | ||
using RegexpMatchBackport | ||
end | ||
|
||
attr_reader :cache_dir, :argv | ||
|
||
attr_accessor :compile_gemfile, :exclude | ||
|
||
def initialize(argv) | ||
@argv = argv | ||
self.cache_dir = 'tmp/cache' | ||
self.compile_gemfile = false | ||
self.exclude = nil | ||
end | ||
|
||
def precompile_command(*sources) | ||
require 'bootsnap/compile_cache/iseq' | ||
|
||
Bootsnap::CompileCache::ISeq.cache_dir = self.cache_dir | ||
|
||
if compile_gemfile | ||
sources += $LOAD_PATH | ||
end | ||
|
||
sources.map { |d| File.expand_path(d) }.each do |path| | ||
if !exclude || !exclude.match?(path) | ||
list_ruby_files(path).each do |ruby_file| | ||
if !exclude || !exclude.match?(ruby_file) | ||
CompileCache::ISeq.fetch(ruby_file, cache_dir: cache_dir) | ||
end | ||
end | ||
end | ||
end | ||
0 | ||
end | ||
|
||
dir_sort = begin | ||
Dir['.', sort: false] | ||
true | ||
rescue ArgumentError | ||
false | ||
end | ||
|
||
if dir_sort | ||
def list_ruby_files(path) | ||
if File.directory?(path) | ||
Dir[File.join(path, '**/*.rb'), sort: false] | ||
elsif File.exist?(path) | ||
[path] | ||
else | ||
[] | ||
end | ||
end | ||
else | ||
def list_ruby_files(path) | ||
if File.directory?(path) | ||
Dir[File.join(path, '**/*.rb')] | ||
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 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 parser | ||
@parser ||= OptionParser.new do |opts| | ||
opts.banner = "Usage: bootsnap COMMAND [ARGS]" | ||
opts.separator "" | ||
opts.separator "GLOBAL OPTIONS" | ||
opts.separator "" | ||
|
||
help = <<~EOS | ||
Path to the bootsnap cache directory. Defaults to tmp/cache | ||
EOS | ||
opts.on('--cache-dir DIR', help.strip) do |dir| | ||
self.cache_dir = dir | ||
end | ||
|
||
opts.separator "" | ||
opts.separator "COMMANDS" | ||
opts.separator "" | ||
opts.separator " precompile [DIRECTORIES...]: Precompile all .rb files in the passed directories" | ||
|
||
help = <<~EOS | ||
Precompile the gems in Gemfile | ||
EOS | ||
opts.on('--gemfile', help) { self.compile_gemfile = true } | ||
|
||
help = <<~EOS | ||
Path pattern to not precompile. e.g. --exclude 'aws-sdk|google-api' | ||
EOS | ||
opts.on('--exclude PATTERN', help) { |pattern| self.exclude = Regexp.new(pattern) } | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# frozen_string_literal: true | ||
require('test_helper') | ||
require('bootsnap/cli') | ||
|
||
module Bootsnap | ||
class CLITest < Minitest::Test | ||
include(TmpdirHelper) | ||
|
||
def setup | ||
super | ||
@cache_dir = File.expand_path('tmp/cache/bootsnap-compile-cache') | ||
end | ||
|
||
def test_precompile_single_file | ||
path = Help.set_file('a.rb', 'a = a = 3', 100) | ||
CompileCache::ISeq.expects(:fetch).with(File.expand_path(path), cache_dir: @cache_dir) | ||
assert_equal 0, CLI.new(['precompile', path]).run | ||
end | ||
|
||
def test_precompile_directory | ||
path_a = Help.set_file('foo/a.rb', 'a = a = 3', 100) | ||
path_b = Help.set_file('foo/b.rb', 'b = b = 3', 100) | ||
|
||
CompileCache::ISeq.expects(:fetch).with(File.expand_path(path_a), cache_dir: @cache_dir) | ||
CompileCache::ISeq.expects(:fetch).with(File.expand_path(path_b), cache_dir: @cache_dir) | ||
assert_equal 0, CLI.new(['precompile', 'foo']).run | ||
end | ||
|
||
def test_precompile_exclude | ||
path_a = Help.set_file('foo/a.rb', 'a = a = 3', 100) | ||
path_b = Help.set_file('foo/b.rb', 'b = b = 3', 100) | ||
|
||
CompileCache::ISeq.expects(:fetch).with(File.expand_path(path_a), cache_dir: @cache_dir) | ||
assert_equal 0, CLI.new(['precompile', '--exclude', 'b.rb', 'foo']).run | ||
end | ||
|
||
def test_precompile_gemfile | ||
assert_equal 0, CLI.new(['precompile', '--gemfile']).run | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters