class Bun::Runner

Attributes

arguments[RW]
gemfile[RW]
parsed_arguments[RW]

Public Class Methods

call(*arguments) click to toggle source
# File lib/bun/runner.rb, line 6
def self.call(*arguments)
  new(*arguments).call
end
new(*arguments) click to toggle source
# File lib/bun/runner.rb, line 10
def initialize(*arguments)
  @arguments = arguments
  @gemfile = Gemfile.new

  parse_arguments
end

Public Instance Methods

add(gems = [], opts: {})
Alias for: install
call() click to toggle source
# File lib/bun/runner.rb, line 17
def call
  command = parsed_arguments.shift
  gems = parsed_arguments.take_while { |argument| argument !~ /^-|^--/}

  run_command(command, gems)
end
install(gems = [], opts: {}) click to toggle source
# File lib/bun/runner.rb, line 33
def install(gems = [], opts: {})
  gemfile.init

  gems.each do |gem|
    name, version = extract_name_and_version_from_gem(gem)

    if print?
      version ||= VersionFetcher.new(name, arguments).fetch_latest_version
      puts "gem \"#{name}\", \"#{version_string(version)}\"" 
    else
      gemfile.verify_unique!(name)
      version ||= VersionFetcher.new(name, arguments).fetch_latest_version
      gemfile.add(name,
                  version_string(version),
                  arguments.group)
    end
  end

  bundle_install
end
Also aliased as: add
remove(gems, opts: {})
Alias for: uninstall
uninstall(gems, opts: {}) click to toggle source
# File lib/bun/runner.rb, line 24
def uninstall(gems, opts: {})
  gems.each do |gem|
    gemfile.remove(gem)
  end

  bundle_install
end
Also aliased as: remove

Private Instance Methods

bundle_install() click to toggle source
# File lib/bun/runner.rb, line 92
def bundle_install
  return if arguments.skip_install? || print?

  Bundler.with_clean_env do
    system("bundle install")
  end
end
extract_name_and_version_from_gem(gem) click to toggle source
# File lib/bun/runner.rb, line 109
def extract_name_and_version_from_gem(gem)
  return gem unless gem =~ /:/

  gem.split(":")
end
parse_arguments() click to toggle source
# File lib/bun/runner.rb, line 104
def parse_arguments
  self.arguments = Arguments.new(arguments)
  self.parsed_arguments = arguments.parse
end
print?() click to toggle source
run_command(command, gems) click to toggle source
# File lib/bun/runner.rb, line 61
def run_command(command, gems)
  with_exception_handling do
    install if arguments.empty?

    case command
    when /^install$|^i$|^add$/
      install(gems)
    when /^uninstall$|^rm$|^remove$|^d$/
      uninstall(gems)
    end
  end
end
version_string(version) click to toggle source
# File lib/bun/runner.rb, line 82
def version_string(version)
  if arguments.optimistic?
    ">= #{version}"
  elsif arguments.strict?
    "#{version}"
  else
    "~> #{version}"
  end
end
with_exception_handling() { || ... } click to toggle source
# File lib/bun/runner.rb, line 74
def with_exception_handling
  yield
rescue Errors::DuplicateGemError, Errors::GemNotFoundError => ex
  puts
  puts Paint[ex.message, :red, :bright]
  exit(1)
end