class RokuBuilder::Options

Public Class Methods

new(options: nil) click to toggle source
# File lib/roku_builder/options.rb, line 5
def initialize(options: nil)
  @logger = Logger.instance
  setup_plugin_commands
  options ||= parse
  merge!(options)
end

Public Instance Methods

command() click to toggle source
# File lib/roku_builder/options.rb, line 18
def command
  (keys & commands).first
end
device_command?() click to toggle source
# File lib/roku_builder/options.rb, line 30
def device_command?
  device_commands.include?(command)
end
exclude_command?() click to toggle source
# File lib/roku_builder/options.rb, line 22
def exclude_command?
  exclude_commands.include?(command)
end
has_source?() click to toggle source
# File lib/roku_builder/options.rb, line 38
def has_source?
  !(keys & sources).empty?
end
keyed_command?() click to toggle source
# File lib/roku_builder/options.rb, line 34
def keyed_command?
  keyed_commands.include?(command)
end
source_command?() click to toggle source
# File lib/roku_builder/options.rb, line 26
def source_command?
  source_commands.include?(command)
end
validate() click to toggle source
# File lib/roku_builder/options.rb, line 12
def validate
  validate_commands
  validate_sources
  validate_deprivated
end

Private Instance Methods

add_plugin_options(parser:, options:) click to toggle source
# File lib/roku_builder/options.rb, line 69
def add_plugin_options(parser:, options:)
  RokuBuilder.plugins.each do |plugin|
    parser.separator ""
    parser.separator "Options for #{plugin}:"
    plugin.parse_options(parser: parser, options: options)
  end
end
commands() click to toggle source

List of command options @return [Array<Symbol>] List of command symbols that can be used in the options hash

# File lib/roku_builder/options.rb, line 122
def commands
  @commands ||= []
end
depricated_options() click to toggle source

List of depricated options @return [Hash] Hash of depricated options and the warning message for each

# File lib/roku_builder/options.rb, line 128
def depricated_options
  @depricated_options ||= {}
end
device_commands() click to toggle source

List of commands that require a device @return [Array<Symbol>] List of commands that require a device

# File lib/roku_builder/options.rb, line 152
def device_commands
  @device_commands ||= []
end
exclude_commands() click to toggle source

List of commands the activate the exclude files @return [Array<Symbol] List of commands the will activate the exclude files lists

# File lib/roku_builder/options.rb, line 146
def exclude_commands
  @exclude_commands ||= []
end
keyed_commands() click to toggle source

List of commands that require a key @return [Array<Symbol>] List of commands that require a key

# File lib/roku_builder/options.rb, line 158
def keyed_commands
  @keyed_commands ||= []
end
parse() click to toggle source
# File lib/roku_builder/options.rb, line 57
def parse
  options = {}
  options[:config] = '~/.roku_config.json'
  options[:update_manifest] = false
  parser = OptionParser.new
  parser.banner = "Usage: roku <command> [options]"
  add_plugin_options(parser: parser, options:options)
  validate_parser(parser: parser)
  parser.parse!
  options
end
setup_plugin_commands() click to toggle source
# File lib/roku_builder/options.rb, line 44
def setup_plugin_commands
  RokuBuilder.plugins.each do |plugin|
    plugin.commands.each do |command, attributes|
      commands << command
      [:device, :source, :exclude, :keyed].each do |type|
        if attributes[type]
          send("#{type}_commands".to_sym) << command
        end
      end
    end
  end
end
source_commands() click to toggle source

List of commands requiring a source option @return [Array<Symbol>] List of command symbols that require a source in the options hash

# File lib/roku_builder/options.rb, line 140
def source_commands
  @source_commands ||= []
end
sources() click to toggle source

List of source options @return [Array<Symbol>] List of source symbols that can be used in the options hash

# File lib/roku_builder/options.rb, line 134
def sources
  [:ref, :stage, :working, :current, :in]
end
validate_commands() click to toggle source
# File lib/roku_builder/options.rb, line 97
def validate_commands
  all_commands = keys & commands
  raise InvalidOptions, "Only specify one command" if all_commands.count > 1
  raise InvalidOptions, "Specify at least one command" if all_commands.count < 1
end
validate_deprivated() click to toggle source
# File lib/roku_builder/options.rb, line 111
def validate_deprivated
  depricated = keys & depricated_options.keys
  if depricated.count > 0
    depricated.each do |key|
      @logger.warn depricated_options[key]
    end
  end
end
validate_parser(parser:) click to toggle source
# File lib/roku_builder/options.rb, line 77
def validate_parser(parser:)
  short = []
  long = []
  stack = parser.instance_variable_get(:@stack)
  stack.each do |optionsList|
    optionsList.each_option do |option|
      if option.respond_to?(:short)
        if short.include?(option.short.first)
          raise ImplementationError, "Duplicate short option defined: #{option.short.first}"
        end
        short.push(option.short.first) if option.short.first
        if long.include?(option.long.first)
          raise ImplementationError, "Duplicate long option defined: #{option.long.first}"
        end
        long.push(option.long.first) if option.long.first
      end
    end
  end
end
validate_sources() click to toggle source
# File lib/roku_builder/options.rb, line 103
def validate_sources
  all_sources = keys & sources
  raise InvalidOptions, "Only spefify one source" if all_sources.count > 1
  if source_command? and !has_source?
    raise InvalidOptions, "Must specify a source for that command"
  end
end