Class: Pike::Tasks::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/pike/tasks/command.rb,
lib/pike/tasks/command/local.rb,
lib/pike/tasks/command/remote.rb

Defined Under Namespace

Classes: Local, Remote

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(task, where, cmd, vars = {}) ⇒ Command

Constructor



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/pike/tasks/command.rb', line 18

def initialize(task, where, cmd, vars = {})
    @task = task
	@where = where
    @vars = vars

    case cmd.split(' ').first
    when 'cd'
      @mode = :cd
      @cd_to = cmd.gsub('cd ', '')

    when 'bundle'
      @mode = :bundler
      @cmd = cmd.gsub('bundle ', '')

    when '!BUILD_RBA'
      @mode = :build_rba
      @cmd = nil

    when '!SCP'
      @mode = :scp

      splitted = cmd.gsub('!SCP ', '').split(' ')
      @local = splitted[0]
      @remote = splitted[1]

    else
      @mode = :cmd
	  @cmd = cmd
    end
end

Instance Attribute Details

#cmdObject (readonly)

Returns the value of attribute cmd.



7
8
9
# File 'lib/pike/tasks/command.rb', line 7

def cmd
  @cmd
end

#modeObject (readonly)

Returns the value of attribute mode.



7
8
9
# File 'lib/pike/tasks/command.rb', line 7

def mode
  @mode
end

#whereObject (readonly)

Returns the value of attribute where.



7
8
9
# File 'lib/pike/tasks/command.rb', line 7

def where
  @where
end

Instance Method Details

#build_rbaObject

Starts the build process for .rba file



137
138
139
140
141
142
# File 'lib/pike/tasks/command.rb', line 137

def build_rba
  target_file = '../' + Main.get_var(:name) + '.rba'
  Logger.log "Building RBA file: #{target_file} ..."
  RBA::Builder.build(target_file)
  target_file
end

#get_cmd_prefixObject

The cmd prefix for the log. Looks like a bash prompt



54
55
56
# File 'lib/pike/tasks/command.rb', line 54

def get_cmd_prefix
  "\n" + (where == :local ? Dir.pwd : SSH::Connection.cwd) + " (#{where})$  #{to_s}"
end

#log_time(start_time) ⇒ Object

Logs how long the command ran



112
113
114
115
116
117
# File 'lib/pike/tasks/command.rb', line 112

def log_time(start_time)
  time = Time.at(Time.now - start_time).utc
  Logger.log
  Logger.log "Time: #{time.strftime('%k:%M:%S.%L')}"
  Logger.log
end

#run(env) ⇒ Object

Runs the command



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/pike/tasks/command.rb', line 63

def run(env)
  # For logging the time
  start_time = Time.now

  # Log command
  Logger.log get_cmd_prefix
  report("#{where == :remote ? '>' : '<'} #{to_s}") if Main.verbose

  # Determine target environment
  target = (where == :local ? Local.new(@task) : Remote.new(@task))

  re = true

  unless Main.dry_run
    # Mode depending routines
    case @mode
    when :cmd
      re = target.run("#{var_string} #{sudo}#{@cmd}")
      log_time(start_time)

    when :cd
      re = target.cd(@cd_to)
      log_time(start_time)

    when :bundler
      re = target.bundler(@cmd, var_string, sudo?)
      log_time(start_time)

    when :build_rba
      re = build_rba
      log_time(start_time)

    when :scp
      re = SSH::Connection.scp(@local, @remote, env.get_var(:host))
      log_time(start_time)

    else
      Main.error("Unkown command mode #{@mode}")
    end
  end

  return re
end

#sudoObject



165
166
167
# File 'lib/pike/tasks/command.rb', line 165

def sudo
  sudo? ? 'sudo ' : ''
end

#sudo?Boolean

Returns:

  • (Boolean)


169
170
171
# File 'lib/pike/tasks/command.rb', line 169

def sudo?
  where == :remote && @task.current_env.get_var(:use_sudo) && !@cmd.strip.start_with?('source ')
end

#to_sObject

Converts command to string. Depending on the mode, the result may differ



149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/pike/tasks/command.rb', line 149

def to_s
  case @mode
  when :cmd
    "#{sudo}#{var_string}#{@cmd}"
  when :bundler
    "#{sudo}#{var_string}bundle #{@cmd}"
  when :cd
    "cd #{@cd_to}"
  when :scp
    "scp #{@local} #{@remote}"
  when :build_rba
    "build .rba file"
  end
end

#var_stringObject

Generates the vars for the shell command



124
125
126
127
128
129
130
# File 'lib/pike/tasks/command.rb', line 124

def var_string
  re = ''
  @vars.each do |key, value|
    re << "#{key.to_s.upcase.strip}=#{value} "
  end
  re
end