class Omnitest::Psychic::Script

Attributes

arguments[RW]
env[RW]
name[R]

@return [String] a name for the script

opts[R]

@return [Hash] options controlling how the script is executed

params[RW]

@return [Hash] params key/value pairs to bind to script input

psychic[R]
source_file[R]

@return [Pathname] the location of the script

Public Class Methods

new(psychic, name, source_file, opts = {}) click to toggle source
# File lib/omnitest/psychic/script.rb, line 22
def initialize(psychic, name, source_file, opts = {})
  fail ArgumentError if psychic.nil?
  fail ArgumentError if name.nil?
  fail ArgumentError if source_file.nil?
  @name = name.to_s
  @source_file, *@arguments = Shellwords.shellsplit(source_file.to_s)
  @source_file = Pathname(@source_file)
  @opts ||= opts
  @env = opts[:env] || psychic.env
  @params = opts[:params] ||= psychic.parameters
  @psychic = psychic
end

Public Instance Methods

absolute_source_file() click to toggle source
# File lib/omnitest/psychic/script.rb, line 70
def absolute_source_file
  return nil if source_file.nil?

  File.expand_path source_file, basedir
end
basedir() click to toggle source
# File lib/omnitest/psychic/script.rb, line 35
def basedir
  @psychic.basedir
end
command() click to toggle source
# File lib/omnitest/psychic/script.rb, line 43
def command
  execution_strategy.command
end
detection_strategy() click to toggle source
# File lib/omnitest/psychic/script.rb, line 76
def detection_strategy
  @detection_strategy ||= create_detection_strategy
end
execute(*extra_args) click to toggle source
# File lib/omnitest/psychic/script.rb, line 47
def execute(*extra_args)
  execution_strategy.execute *extra_args
end
execution_strategy() click to toggle source
# File lib/omnitest/psychic/script.rb, line 80
def execution_strategy
  @execution_strategy ||= create_execution_strategy
end
extname() click to toggle source
# File lib/omnitest/psychic/script.rb, line 39
def extname
  source_file.extname
end
interactive?() click to toggle source
# File lib/omnitest/psychic/script.rb, line 84
def interactive?
  @psychic.interactive?
end
to_path() click to toggle source
# File lib/omnitest/psychic/script.rb, line 65
def to_path
  # So coercion to Pathname is possible
  source_file.to_s
end
to_s(verbose = false) click to toggle source
# File lib/omnitest/psychic/script.rb, line 56
def to_s(verbose = false)
  build_string do
    status('Script Name', name)
    display_tokens
    status('Source File', formatted_file_name)
    display_source if verbose
  end
end
tokens() click to toggle source
# File lib/omnitest/psychic/script.rb, line 51
def tokens
  return [] unless detection_strategy.respond_to? :tokens
  @tokens ||= detection_strategy.tokens
end

Private Instance Methods

create_detection_strategy() click to toggle source
# File lib/omnitest/psychic/script.rb, line 132
def create_detection_strategy
  strategy = opts[:detection_strategy] || opts[:execution_strategy]
  case strategy
  when nil
    nil
  when 'tokens'
    Tokens::RegexpTokenHandler.new(source, /'\{(\w+)\}'/, "'\\1'")
  else
    # TODO: Need support for detecting tokens from comments, help commands, etc.
    fail "Unknown token detection strategy #{strategy}"
  end
end
create_execution_strategy() click to toggle source
# File lib/omnitest/psychic/script.rb, line 115
def create_execution_strategy
  strategy = opts[:execution_strategy]
  case strategy
  when nil
    Execution::DefaultStrategy.new self
  when 'tokens'
    Execution::TokenStrategy.new self
  when 'environment_variables'
    Execution::EnvStrategy.new self
  when 'flags'
    Execution::FlagStrategy.new self
  else
    # TODO: Need support for custom commands with positional args
    fail "Unknown binding strategy #{strategy}"
  end
end
display_source() click to toggle source
# File lib/omnitest/psychic/script.rb, line 90
def display_source
  return unless source?
  status 'Source Code'
  say highlighted_code
end
display_tokens() click to toggle source
# File lib/omnitest/psychic/script.rb, line 96
def display_tokens
  return status 'Tokens', '(None)' if tokens.empty?

  status 'Tokens'
  indent do
    tokens.each do | token |
      say "- #{token}"
    end
  end
end
formatted_file_name() click to toggle source
# File lib/omnitest/psychic/script.rb, line 107
def formatted_file_name
  if source?
    Omnitest::Core::FileSystem.relativize(absolute_source_file, Dir.pwd)
  else
    colorize('<No script>', :red)
  end
end