Class: Sabre::Command

Inherits:
Object
  • Object
show all
Includes:
Blockenspiel::DSL, Base
Defined in:
lib/sabre/command.rb

Direct Known Subclasses

Bundler, Git, Release

Instance Method Summary collapse

Methods included from Base

#cd, #cp, #echo, #mv, #set, #synchronize

Constructor Details

#initialize(&block) ⇒ Command

Returns a new instance of Command.



9
10
11
12
13
14
15
# File 'lib/sabre/command.rb', line 9

def initialize(&block)
  @commands = Array.new
  @error_commands = Array.new
  @conditions = Array.new

  Blockenspiel.invoke(block, self) if block
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/sabre/command.rb', line 99

def method_missing(sym, *args, &block)
  # provides "undef.method" -> "run Undef.new.method"
  klass_name = sym.to_s.capitalize
  klass = Sabre.const_get(klass_name) rescue nil

  super unless klass

  command = klass.new(&block)
  run command # note: this object will be modified later!

  command
end

Instance Method Details

#indent(command) ⇒ Object



64
65
66
# File 'lib/sabre/command.rb', line 64

def indent(command)
  command.to_s.gsub(/^/, "  ")
end

#on(conditions, &block) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/sabre/command.rb', line 28

def on(conditions, &block)
  if block
    command = Sabre::Command.new(&block)
    command.on(conditions)

    run(command)
  else
    conditions = Array(conditions)
    @conditions += conditions
  end
end

#on_error(command = nil, &block) ⇒ Object



53
54
55
56
57
58
59
60
61
62
# File 'lib/sabre/command.rb', line 53

def on_error(command = nil, &block)
  if block
    command = Sabre::Command.new(&block)
  elsif command.is_a?(String)
    command = unindent(command)
  end

  @error_commands << command
  command
end

#on_host(*hosts, &block) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/sabre/command.rb', line 40

def on_host(*hosts, &block)
  conditions = hosts.collect do |h|
    if h.is_a?(Regexp)
      "[[ `hostname -f` =~ \"#{ h.source }\" ]]"
    else
      "[[ `hostname -f` == \"#{ h }\" ]]"
    end
  end

  condition = conditions.join(" || ")
  on(condition, &block)
end

#run(command = nil, &block) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/sabre/command.rb', line 17

def run(command = nil, &block)
  if block
    command = Sabre::Command.new(&block)
  elsif command.is_a?(String)
    command = unindent(command)
  end

  @commands << command
  command
end

#to_sObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/sabre/command.rb', line 78

def to_s
  command = @commands.join(" &&\n")

  unless @error_commands.empty?
    error_command = @error_commands.join(" &&\n")
    command = "{\n#{ indent(command) };\n} || {\n#{ indent(error_command) } && false;\n}"
  end

  unless @conditions.empty?
    condition = @conditions.join(" &&\n")

    command = unindent %{
      if #{ condition }; then
      #{ indent(command) };
      fi
    }
  end

  command
end

#unindent(command) ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'lib/sabre/command.rb', line 68

def unindent(command)
  command = command.to_s

  if command =~ /^\n([ \t]+)/
    command = command.gsub(/^#{ $1 }/, '')
  end

  command.strip
end