Skip to content

Commit

Permalink
Add --version flag and version subcommand
Browse files Browse the repository at this point in the history
Prints bootsnap version
  • Loading branch information
dorianmariecom committed Apr 4, 2024
1 parent 87edda3 commit 79d12bd
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
27 changes: 25 additions & 2 deletions lib/bootsnap/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,35 @@ def match?(string)

attr_reader :cache_dir, :argv

attr_accessor :compile_gemfile, :exclude, :verbose, :iseq, :yaml, :json, :jobs
attr_accessor(
:compile_gemfile,
:exclude,
:verbose,
:iseq,
:yaml,
:json,
:jobs,
:version
)

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

def version_command
puts Bootsnap::VERSION
0
end

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

Expand Down Expand Up @@ -113,7 +128,7 @@ def list_files(path, pattern)
def run
parser.parse!(argv)
command = argv.shift
method = "#{command}_command"
method = version ? "version_command" : "#{command}_command"
if respond_to?(method)
public_send(method, *argv)
else
Expand Down Expand Up @@ -227,6 +242,13 @@ def parser
opts.separator "GLOBAL OPTIONS"
opts.separator ""

help = <<~HELP
Prints the version of bootsnap
HELP
opts.on("--version", help.strip) do
self.version = true
end

help = <<~HELP
Path to the bootsnap cache directory. Defaults to tmp/cache
HELP
Expand All @@ -252,6 +274,7 @@ def parser
opts.separator "COMMANDS"
opts.separator ""
opts.separator " precompile [DIRECTORIES...]: Precompile all .rb files in the passed directories"
opts.separator " version: Prints the version of bootsnap"

help = <<~HELP
Precompile the gems in Gemfile
Expand Down
18 changes: 18 additions & 0 deletions test/cli_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,24 @@ def test_no_yaml
assert_equal 0, CLI.new(["precompile", "-j", "0", "--no-yaml", path]).run
end

def test_version
out, err = capture_io do
assert_equal 0, CLI.new(["version"]).run
end

assert_equal Bootsnap::VERSION + "\n", out
assert_equal "", err
end

def test_version_flag
out, err = capture_io do
assert_equal 0, CLI.new(["--version"]).run
end

assert_equal Bootsnap::VERSION + "\n", out
assert_equal "", err
end

private

def skip_unless_iseq
Expand Down

0 comments on commit 79d12bd

Please sign in to comment.