Class: CommandButler::Mediator

Inherits:
Object
  • Object
show all
Defined in:
lib/command_butler/mediator.rb

Instance Method Summary collapse

Instance Method Details

#execute(file_name, options) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/command_butler/mediator.rb', line 11

def execute(file_name, options)
  inputs = {}
  @commands = CommandParser.parse(file_name:file_name)

  @vals = {}
  if options[:val_file]
    @vals = ValParser.parse(file_name:options[:val_file])
    @vals.each_pair do |k,v|
      @commands.each {|c| c.replace_command(val:{k=>v})}
    end
  end
  jump_index = -1 # jump制御

  contents = (0 < @vals.size)? ValDecorator.decoration(vals:@vals) : ""
  puts TitleDecorator.decoration(file_name:file_name, contents:contents)

  @commands.each_with_index do |command, index|

    # jumpが設定されていたらskipしたことにする
    if (index < jump_index)
      inputs[index] = Input.skip_instance
      next
    end

    # show all comamnds
    show_commands(current_index: index, inputs: inputs) # 1件実行ごとにコマンドを表示する
    # execute
    input = command.need_confirm?? Input.start : Input.execute_instance
    exit 1 if input.abort?
    jump_index = (input.input_value - 1) if input.jump?
    inputs[index] = input
    if input.execute?
      res = execute_command(command:command, index:index) 
      @commands.each {|c| c.replace_command(val:res[:set_val])} if res[:set_val]
    end
  end
end

#execute_command(command: command, index: index) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/command_butler/mediator.rb', line 49

def execute_command(command:command, index:index)
  res = {set_val:nil}
  Dir.chdir(command.chdir) if command.chdir
  return  res unless command.command
  # set_valコマンドの時は標準出力を取りたいのでバッククオート実行
  # その他は、都度出力されるものを表示したいのでsystemで実行

  ResultDecorator.decoration(command: command, index: index) do
    if command.set_val_command?
      val = `#{command.command}`.chomp
      puts val
      res[:set_val] = {command.set_val => val}
      puts " (set_val :  #{command.set_val})"
    else
      system command.command
    end
  end
  sleep 0.5 # 表示がいっきに流れて見失しなうのでsleep
  res
end

#show_commands(current_index: current_index, inputs: inputs) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/command_butler/mediator.rb', line 70

def show_commands(current_index:current_index, inputs:inputs)
  @commands.each_with_index do |command, index|
    puts LineDecorator.decoration command: command, index: index, current_index: current_index, input: inputs[index]
    detail = LineDetailDecorator.decoration  command: command
    puts detail if 0 < detail.size
  end
end