class Psychic::Runner

Constants

VERSION

Attributes

hot_read_task_factory[R]
runners[R]
sample_factories[R]
task_factories[R]

Public Class Methods

new(opts = { cwd: Dir.pwd }) click to toggle source
# File lib/psychic/runner.rb, line 30
def initialize(opts = { cwd: Dir.pwd }) # rubocop:disable Metrics/MethodLength
  # TODO: Will reduce method length after further splitting Runner vs TaskFactory
  fail 'cwd is required' unless opts[:cwd]
  # must be a string on windows...
  opts[:cwd] = Pathname(opts[:cwd]).to_s
  @opts = opts
  init_attr(:cwd) { Dir.pwd }
  init_hints
  init_attr(:logger) { new_logger }
  init_attr(:env) { ENV.to_hash }
  init_attrs :cli, :interactive, :parameter_mode, :restore_mode, :dry_run
  @shell_opts = select_shell_opts
  @parameters = load_parameters(opts[:parameters])
  # super
  @hot_read_task_factory = HotReadTaskFactory.new(opts)
  @sample_finder = SampleFinder.new(opts[:cwd], @hot_read_task_factory.hints['samples'])
  @task_factories = TaskFactoryRegistry.active_task_factories(opts)
  @runners = [@hot_read_task_factory, @task_factories].flatten
  @known_tasks = @runners.flat_map(&:known_tasks).uniq
end

Public Instance Methods

known_samples() click to toggle source
# File lib/psychic/runner.rb, line 51
def known_samples
  @sample_finder.known_samples
end
task_for(task_name) click to toggle source
# File lib/psychic/runner.rb, line 55
def task_for(task_name)
  runner = runners.find { |r| r.known_task?(task_name) }
  return nil unless runner
  runner.task_for(task_name)
end

Private Instance Methods

init_attr(var) { || ... } click to toggle source
# File lib/psychic/runner.rb, line 63
def init_attr(var)
  var_name = "@#{var}"
  var_value = @opts[var]
  var_value = yield if var_value.nil? && block_given?
  instance_variable_set(var_name, var_value)
end
init_attrs(*vars) click to toggle source
# File lib/psychic/runner.rb, line 70
def init_attrs(*vars)
  vars.each do | var |
    init_attr var
  end
end
init_hints() click to toggle source
# File lib/psychic/runner.rb, line 76
def init_hints
  @hints = Psychic::Util.stringified_hash(@opts[:hints] || load_hints || {})
  if @hints['options']
    @opts.merge! Psychic::Util.symbolized_hash(@hints['options'])
  end
end
load_hints() click to toggle source
# File lib/psychic/runner.rb, line 88
def load_hints
  hints_file = Dir["#{@cwd}/psychic.{yaml,yml}"].first
  YAML.load(File.read(hints_file)) unless hints_file.nil?
end
load_parameters(parameters) click to toggle source
# File lib/psychic/runner.rb, line 93
def load_parameters(parameters)
  if parameters.nil? || parameters.is_a?(String)
    load_parameters_file(parameters)
  else
    parameters
  end
end
load_parameters_file(file = nil) click to toggle source
# File lib/psychic/runner.rb, line 101
def load_parameters_file(file = nil)
  if file.nil?
    file ||= File.expand_path(DEFAULT_PARAMS_FILE, cwd)
    return {} unless File.exist? file
  end
  parameters = Psychic::Util.replace_tokens(File.read(file), @env)
  YAML.load(parameters)
end
select_shell_opts() click to toggle source
# File lib/psychic/runner.rb, line 83
def select_shell_opts
  # Make sure to delete any option that isn't a MixLib::ShellOut option
  @opts.select { |key, _| Psychic::Shell::AVAILABLE_OPTIONS.include? key }
end