Class: Heyterminal::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/heyterminal/runner.rb

Constant Summary collapse

DEFAULT_DSL_FILE_NAME =
'heyterminal.rb'.freeze

Class Method Summary collapse

Class Method Details

.add_expression(expression) ⇒ Object



56
57
58
59
# File 'lib/heyterminal/runner.rb', line 56

def self.add_expression(expression)
  @expressions ||= []
  @expressions << expression
end

.dsl_path(file) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/heyterminal/runner.rb', line 24

def self.dsl_path(file)
  if file.nil? || file.empty?
    file_path = File.join(Dir.pwd, "/#{DEFAULT_DSL_FILE_NAME}")

    return file_path if File.exist?(file_path)

    file_path = File.join(Dir.home, "/#{DEFAULT_DSL_FILE_NAME}")

    return file_path if File.exist?(file_path)
  else 
    file_path = File.join(Dir.pwd, file)

    return file_path if File.exist?(file_path)
  end
end

.editorObject



10
11
12
# File 'lib/heyterminal/runner.rb', line 10

def self.editor
  @editor
end

.expressionsObject



84
85
86
# File 'lib/heyterminal/runner.rb', line 84

def self.expressions
  @expressions
end

.expressions_commandsObject



88
89
90
# File 'lib/heyterminal/runner.rb', line 88

def self.expressions_commands
  expressions.map { |e| e[0].instance_variable_get('@expression') }
end

.file_pathObject



45
46
47
# File 'lib/heyterminal/runner.rb', line 45

def self.file_path
  @file_path
end

.load(file) ⇒ Object



49
50
51
52
53
54
# File 'lib/heyterminal/runner.rb', line 49

def self.load(file)
  contents = File.read(file)
  parser = Heyterminal::Parser.new
  parser.instance_eval(contents)
  parser
end

.load_default(file) ⇒ Object



40
41
42
43
# File 'lib/heyterminal/runner.rb', line 40

def self.load_default(file)
  @file_path = dsl_path(file)
  load(@file_path)
end

.lod_and_run(file, command) ⇒ Object

Raises:



14
15
16
17
18
19
20
21
22
# File 'lib/heyterminal/runner.rb', line 14

def self.lod_and_run(file, command)
  @file_path = dsl_path(file)

  raise Heyterminal::Error, 'file not found in any of directories' if file_path.nil?

  puts "Using... #{file_path}"
  load(file_path)
  run(command)
end

.run(command) ⇒ Object

Raises:



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/heyterminal/runner.rb', line 61

def self.run(command)
  result = nil
  found = false
  @expressions.each do |expression|
    cucumber, block = expression
    expression_text = cucumber.instance_variable_get('@expression')
    matched = cucumber.match(command)

    next if matched.nil?

    puts Rainbow('=' * 20).yellow
    puts Rainbow("Hey running.... #{expression_text}").yellow
    puts Rainbow('=' * 20).yellow

    block.call(*matched.map { |m| m.value(nil)})
    found = true
    break
  end
  raise Heyterminal::Error, "command \"#{command}\" not found in config file #{file_path}" unless found

  result
end

.set_editor(editor) ⇒ Object



6
7
8
# File 'lib/heyterminal/runner.rb', line 6

def self.set_editor(editor)
  @editor = editor
end