Class: Crosstest::Psychic::Script

Inherits:
CommandTemplate show all
Includes:
OutputHelper, CodeHelper
Defined in:
lib/crosstest/psychic/script.rb

Overview

rubocop:disable Metrics/ClassLength

Instance Attribute Summary collapse

Attributes inherited from CommandTemplate

#psychic

Instance Method Summary collapse

Methods included from OutputHelper

#build_string, #cli, #color_pad, #colorize, #indent, #print_table, #reformat, #say, #status

Methods included from CodeHelper

#absolute_source_file, #code_block, #detect_language, #highlighted_code, #language, #source, #source?

Methods inherited from CommandTemplate

#command

Constructor Details

#initialize(psychic, name, source_file, opts = {}) ⇒ Script


16
17
18
19
20
21
22
23
24
# File 'lib/crosstest/psychic/script.rb', line 16

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 = Pathname(source_file)
  @opts ||= opts
  super psychic, ''
end

Instance Attribute Details

#nameString (readonly)


10
11
12
# File 'lib/crosstest/psychic/script.rb', line 10

def name
  @name
end

#optsHash (readonly)


14
15
16
# File 'lib/crosstest/psychic/script.rb', line 14

def opts
  @opts
end

#source_filePathname (readonly)


12
13
14
# File 'lib/crosstest/psychic/script.rb', line 12

def source_file
  @source_file
end

Instance Method Details

#basedirObject


26
27
28
# File 'lib/crosstest/psychic/script.rb', line 26

def basedir
  @psychic.basedir
end

#execute(params = nil, *extra_args) ⇒ Object


39
40
41
42
43
44
45
# File 'lib/crosstest/psychic/script.rb', line 39

def execute(params = nil, *extra_args)
  params ||= psychic.parameters
  process_parameters(params) if params
  command_params = { script: name, script_file: source_file }
  command_params.merge!(params) unless params.nil?
  super command_params, *extra_args
end

#extnameObject


30
31
32
# File 'lib/crosstest/psychic/script.rb', line 30

def extname
  source_file.extname
end

#interactive?Boolean


65
66
67
# File 'lib/crosstest/psychic/script.rb', line 65

def interactive?
  psychic.interactive?
end

#process_parameters(parameters) ⇒ Object


69
70
71
72
73
74
75
76
77
78
79
# File 'lib/crosstest/psychic/script.rb', line 69

def process_parameters(parameters)
  if tokenized?
    backup_and_overwrite(absolute_source_file)

    template = File.read(absolute_source_file)
    # Default token pattern/replacement (used by php-opencloud) should be configurable
    token_handler = Tokens::RegexpTokenHandler.new(template, /'\{(\w+)\}'/, "'\\1'")
    confirm_or_update_parameters(token_handler.tokens)
    File.write(absolute_source_file, token_handler.render(parameters))
  end
end

#to_pathObject


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

def to_path
  # So coercion to Pathname is possible
  source_file.to_s
end

#to_s(verbose = false) ⇒ Object


47
48
49
50
51
52
53
54
# File 'lib/crosstest/psychic/script.rb', line 47

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

#token_handlerObject


34
35
36
37
# File 'lib/crosstest/psychic/script.rb', line 34

def token_handler
  # Default token pattern/replacement (used by php-opencloud) should be configurable
  @token_handler ||= Tokens::RegexpTokenHandler.new(source, /'\{(\w+)\}'/, "'\\1'")
end

#tokenized?Boolean


61
62
63
# File 'lib/crosstest/psychic/script.rb', line 61

def tokenized?
  opts[:parameter_mode] == 'tokens'
end