Class: Crosstest::Psychic

Inherits:
Object
  • Object
show all
Includes:
Core::Logger, ScriptRunner, TaskRunner, Shell
Defined in:
lib/crosstest/psychic.rb,
lib/crosstest/psychic/cli.rb,
lib/crosstest/psychic/task.rb,
lib/crosstest/psychic/error.rb,
lib/crosstest/psychic/hints.rb,
lib/crosstest/psychic/tokens.rb,
lib/crosstest/psychic/script.rb,
lib/crosstest/psychic/version.rb,
lib/crosstest/psychic/code_helper.rb,
lib/crosstest/psychic/task_runner.rb,
lib/crosstest/psychic/file_finder.rb,
lib/crosstest/psychic/script_runner.rb,
lib/crosstest/psychic/script_factory.rb,
lib/crosstest/psychic/factory_manager.rb,
lib/crosstest/psychic/command_template.rb,
lib/crosstest/psychic/magic_task_factory.rb,
lib/crosstest/psychic/task_factory_manager.rb,
lib/crosstest/psychic/script_factory_manager.rb,
lib/crosstest/psychic/factories/ruby_factories.rb,
lib/crosstest/psychic/factories/shell_factories.rb,
lib/crosstest/psychic/factories/travis_factories.rb,
lib/crosstest/psychic/factories/powershell_factories.rb,
lib/crosstest/psychic/factories/hot_read_task_factory.rb

Overview

The primary interface for using Psychic as an API.

Detects scripts and tools that can run tasks in the instance's working directory, so that Psychic can act as a universal task/script selection and execution system.

Defined Under Namespace

Modules: CodeHelper, Factories, ScriptRunner, TaskRunner, Tokens Classes: BaseCLI, CLI, CommandTemplate, FactoryManager, FileFinder, Hints, MagicTaskFactory, Script, ScriptFactory, ScriptFactoryManager, ScriptNotRunnable, Task, TaskFactoryManager, TaskNotImplementedError

Constant Summary collapse

DEFAULT_PARAMS_FILE =
'psychic-parameters.yaml'
VERSION =
'0.0.8'

Constants included from Shell

Shell::AVAILABLE_OPTIONS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ScriptRunner

#known_scripts, #script, #script_factory_manager

Methods included from TaskRunner

#known_tasks, #task, #task_factory_manager

Methods included from Shell

#shell

Constructor Details

#initialize(opts = { cwd: Dir.pwd }) ⇒ Psychic

Creates a new Psychic instance that can be used to execute tasks and scripts. All options are

Parameters:

  • opts (Hash) (defaults to: { cwd: Dir.pwd })

    a customizable set of options

Options Hash (opts):

  • :cwd (Dir)

    sets the current working directory

  • :logger (Logger)

    assigns a logger

  • :env (Hash)

    sets environment variables

  • :name (String)

    a name for logging and reporting

  • :os (String)

    the target operating system

  • :interactive (String)

    run psychic in interactive mode, where it will prompt for input


72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/crosstest/psychic.rb', line 72

def initialize(opts = { cwd: Dir.pwd  }) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
  opts[:cwd] ||= Dir.pwd
  # must be a string on windows...
  @cwd = opts[:cwd] = Pathname(opts[:cwd]).to_s
  @opts = opts
  init_attr(:name) { File.basename cwd }
  init_hints
  init_attr(:logger) { new_logger }
  init_attr(:env) { ENV.to_hash }
  init_attr(:os) { RbConfig::CONFIG['host_os'] }
  init_attrs :cli, :interactive, :parameter_mode, :restore_mode, :print
  @shell_opts = select_shell_opts
  @parameters = load_parameters(opts[:parameters])
end

Instance Attribute Details

#cwdDir (readonly) Also known as: basedir

Returns Current working directory for running commands.

Returns:

  • (Dir)

    Current working directory for running commands.


47
48
49
# File 'lib/crosstest/psychic.rb', line 47

def cwd
  @cwd
end

#envHash (readonly)

Returns Environment variables to use when executing commands. The default is to pass all environment variables to the command.

Returns:

  • (Hash)

    Environment variables to use when executing commands. The default is to pass all environment variables to the command.


51
52
53
# File 'lib/crosstest/psychic.rb', line 51

def env
  @env
end

#hintsHints (readonly)

Returns Psychic “hints” that are used to help Psychic locate tasks or scripts.

Returns:

  • (Hints)

    Psychic “hints” that are used to help Psychic locate tasks or scripts.


55
56
57
# File 'lib/crosstest/psychic.rb', line 55

def hints
  @hints
end

#nameString (readonly)

The default value is the name of the current working directory.

Returns:

  • (String)

    A name for logging and reporting.


45
46
47
# File 'lib/crosstest/psychic.rb', line 45

def name
  @name
end

#optsHash (readonly)

Returns Additional options

Returns:

  • (Hash)

    Additional options


59
60
61
# File 'lib/crosstest/psychic.rb', line 59

def opts
  @opts
end

#osString (readonly)

Returns The Operating System to target. Autodetected if unset.

Returns:

  • (String)

    The Operating System to target. Autodetected if unset.


53
54
55
# File 'lib/crosstest/psychic.rb', line 53

def os
  @os
end

#parametersHash (readonly)

Returns Parameters to use as input for scripts.

Returns:

  • (Hash)

    Parameters to use as input for scripts.


57
58
59
# File 'lib/crosstest/psychic.rb', line 57

def parameters
  @parameters
end

Instance Method Details

#execute(command, *args) ⇒ Object

Executes a command using the options set on this Psychic instance.

@param [String] command the command to execute
@param [*args] *args additional arguments to join to the command
@return [ExecutionResult] the result of running the command
@raises [ExecutionError] if the command

Examples:

psychic.execute('echo', 'hello', 'world')
#<Crosstest::Shell::ExecutionResult:0x007fdfe15208f0 @command="echo hello world",
  @exitstatus=0, @stderr="", @stdout="hello world\n">
psychic.execute('foo')
# Crosstest::Shell::ExecutionError: No such file or directory - foo

101
102
103
104
105
# File 'lib/crosstest/psychic.rb', line 101

def execute(command, *args)
  full_cmd = [command, *args].join(' ')
  logger.info("Executing: #{full_cmd}")
  shell.execute(full_cmd, @shell_opts)
end

#interactive?Boolean

Returns true if Psychic is in interactive mode and will prompt for decisions

Returns:

  • (Boolean)

    true if Psychic is in interactive mode and will prompt for decisions


125
126
127
# File 'lib/crosstest/psychic.rb', line 125

def interactive?
  @opts[:interactive]
end

#os_familySymbol

Detects the Operating System family for the selected Operating System.

Returns:

  • (Symbol)

    The operating system family for #os.


109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/crosstest/psychic.rb', line 109

def os_family
  case os
  when /mswin|msys|mingw|cygwin|bccwin|wince|emc/
    :windows
  when /darwin|mac os/
    :macosx
  when /linux/
    :linux
  when /solaris|bsd/
    :unix
  else
    :unknown
  end
end