class FPM::Fry::Plugin::Alternatives::DSL

Public Class Methods

new( b, a = {}) click to toggle source
Calls superclass method
# File lib/fpm/fry/plugin/alternatives.rb, line 10
def initialize( b, a = {})
  super
end

Public Instance Methods

[]=(name, options={}, value) click to toggle source
# File lib/fpm/fry/plugin/alternatives.rb, line 14
def []=(name, options={}, value)
  name = name.to_s
  if value.kind_of? String
    options = normalize(name, options.merge(path: value) )
  else
    options = normalize(name, options.merge(value) )
  end
  alternatives[name] = options
end
add(name, value, options={}) click to toggle source
# File lib/fpm/fry/plugin/alternatives.rb, line 24
def add(name, value, options={})
  self[name, options] = value
end
finish!() click to toggle source
# File lib/fpm/fry/plugin/alternatives.rb, line 28
def finish!
  install   = alternatives.map{|_,v|   install_command(v) }
  uninstall = alternatives.map{|_,v| uninstall_command(v) }
  builder.plugin('script_helper') do
    after_install_or_upgrade(*install)
    before_remove_entirely(*uninstall)
  end
end

Private Instance Methods

install_command(options) click to toggle source
# File lib/fpm/fry/plugin/alternatives.rb, line 65
def install_command(options)
  slaves = options.fetch(:slaves,[]).flat_map{|options| ['--slave', options[:link],options[:name],options[:path]] }
  Shellwords.join(['update-alternatives','--install',options[:link],options[:name],options[:path],options[:priority].to_s, *slaves])
end
normalize( name, options ) click to toggle source
# File lib/fpm/fry/plugin/alternatives.rb, line 53
def normalize( name, options )
  slaves  = {}
  if options.kind_of?(Hash) && options.key?(:slaves)
    options = options.dup
    slaves  = options.delete(:slaves)
  end
  options = normalize_without_slaves(name, options)
  options[:slaves] = slaves.map{|k,v| normalize_without_slaves(k, v) }
  options[:priority] ||= DEFAULT_PRIORITY
  return options
end
normalize_without_slaves(name, options) click to toggle source
# File lib/fpm/fry/plugin/alternatives.rb, line 38
def normalize_without_slaves(name, options)
  if options.kind_of? String
    options = {path: options}
  elsif options.kind_of? Hash
    additional_keys = options.keys - EXPECTED_KEYS
    raise ArgumentError, "Unexpected options: #{additional_keys.inspect}" if additional_keys.any?
    options = options.dup
  else
    raise ArgumentError, "Options must be either a Hash or a String, got #{options.inspect}"
  end
  options[:name] = name
  options[:link] ||= File.join('/usr/bin',name)
  return options
end
uninstall_command(options) click to toggle source
# File lib/fpm/fry/plugin/alternatives.rb, line 70
def uninstall_command(options)
  Shellwords.join(['update-alternatives','--remove',options[:name],options[:path]])
end