class Bundix::CommandLine

Constants

DEFAULT_OPTIONS

Attributes

options[RW]

Public Class Methods

new() click to toggle source
# File lib/bundix/commandline.rb, line 25
def initialize
  @options = DEFAULT_OPTIONS.clone
end
run() click to toggle source
# File lib/bundix/commandline.rb, line 21
def self.run
  self.new.run
end

Public Instance Methods

build_gemset() click to toggle source
# File lib/bundix/commandline.rb, line 148
def build_gemset
  Bundix.new(options).convert
end
handle_init() click to toggle source
# File lib/bundix/commandline.rb, line 123
def handle_init
  if options[:init]
    if File.file?('shell.nix')
      warn "won't override existing shell.nix but here is what it'd look like:"
      puts shell_nix_string
    else
      File.write('shell.nix', shell_nix_string)
    end
  end
end
handle_lock() click to toggle source
# File lib/bundix/commandline.rb, line 134
def handle_lock
  if options[:lock]
    lock = !File.file?(options[:lockfile])
    lock ||= File.mtime(options[:gemfile]) > File.mtime(options[:lockfile])
    if lock
      ENV.delete('BUNDLE_PATH')
      ENV.delete('BUNDLE_FROZEN')
      ENV.delete('BUNDLE_BIN_PATH')
      system('bundle', 'lock')
      fail 'bundle lock failed' unless $?.success?
    end
  end
end
handle_magic() click to toggle source
# File lib/bundix/commandline.rb, line 99
def handle_magic
  ENV['BUNDLE_GEMFILE'] = options[:gemfile]

  if options[:magic]
    fail unless system(
      Bundix::NIX_SHELL, '-p', options[:ruby],
      "bundler.override { ruby = #{options[:ruby]}; }",
      "--command", "bundle lock --lockfile=#{options[:lockfile]}")
    fail unless system(
      Bundix::NIX_SHELL, '-p', options[:ruby],
      "bundler.override { ruby = #{options[:ruby]}; }",
      "--command", "bundle pack --all --path #{options[:bundle_pack_path]}")
  end
end
object2nix(obj) click to toggle source
# File lib/bundix/commandline.rb, line 152
def object2nix(obj)
  Nixer.serialize(obj)
end
parse_options() click to toggle source
# File lib/bundix/commandline.rb, line 40
def parse_options
  op = OptionParser.new do |o|
    o.on '-m', '--magic', 'lock, pack, and write dependencies' do
      options[:magic] = true
    end

    o.on "--ruby=#{options[:ruby]}", 'ruby version to use for magic and init, defaults to latest' do |value|
      options[:ruby] = value
    end

    o.on "--bundle-pack-path=#{options[:bundle_pack_path]}", "path to pack the magic" do |value|
      options[:bundle_pack_path] = value
    end

    o.on '-i', '--init', "initialize a new shell.nix for nix-shell (won't overwrite old ones)" do
      options[:init] = true
    end

    o.on "--gemset=#{options[:gemset]}", 'path to the gemset.nix' do |value|
      options[:gemset] = File.expand_path(value)
    end

    o.on "--lockfile=#{options[:lockfile]}", 'path to the Gemfile.lock' do |value|
      options[:lockfile] = File.expand_path(value)
    end

    o.on "--gemfile=#{options[:gemfile]}", 'path to the Gemfile' do |value|
      options[:gemfile] = File.expand_path(value)
    end

    o.on '-d', '--dependencies', 'include gem dependencies (deprecated)' do
      warn '--dependencies/-d is deprecated because'
      warn 'dependencies will always be fetched'
    end

    o.on '-q', '--quiet', 'only output errors' do
      options[:quiet] = true
    end

    o.on '-l', '--lock', 'generate Gemfile.lock first' do
      options[:lock] = true
    end

    o.on '-v', '--version', 'show the version of bundix' do
      puts Bundix::VERSION
      exit
    end

    o.on '--env', 'show the environment in bundix' do
      system('env')
      exit
    end
  end

  op.parse!
  $VERBOSE = !options[:quiet]
  options
end
run() click to toggle source
# File lib/bundix/commandline.rb, line 31
def run
  parse_options
  handle_magic
  handle_lock
  handle_init
  gemset = build_gemset
  save_gemset(gemset)
end
save_gemset(gemset) click to toggle source
# File lib/bundix/commandline.rb, line 156
def save_gemset(gemset)
  tempfile = Tempfile.new('gemset.nix', encoding: 'UTF-8')
  begin
    tempfile.write(object2nix(gemset))
    tempfile.flush
    FileUtils.cp(tempfile.path, options[:gemset])
    FileUtils.chmod(0644, options[:gemset])
  ensure
    tempfile.close!
    tempfile.unlink
  end
end
shell_nix_context() click to toggle source
# File lib/bundix/commandline.rb, line 114
def shell_nix_context
  ShellNixContext.from_hash(options)
end
shell_nix_string() click to toggle source
# File lib/bundix/commandline.rb, line 118
def shell_nix_string
  tmpl = ERB.new(File.read(File.expand_path('../../template/shell-nix.erb', __dir__)))
  tmpl.result(shell_nix_context.bind)
end