Module: Dply::Helper

Instance Method Summary collapse

Instance Method Details

#cmd(command, display: true, error_msg: nil, return_output: false, env: {}, shell: false) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/dply/helper.rb', line 9

def cmd(command, display: true, error_msg: nil, return_output: false, env:{}, shell: false)
  stringify_values!(env)
  if display
    logger.bullet command
  else
    logger.debug command
  end
  command_arr = command.split
  run_command = shell ? command : command_arr

  output = if return_output
    IO.popen(env, run_command) { |f| f.read }
  else
    system(env, *run_command, 2 => 1)
  end
  return_value = $?.exitstatus
  error_msg ||= "non zero exit for \"#{command}\""
  error error_msg if return_value != 0
  return output
end

#error(msg) ⇒ Object

Raises:



58
59
60
# File 'lib/dply/helper.rb', line 58

def error(msg)
  raise ::Dply::Error, msg
end

#gitObject



54
55
56
# File 'lib/dply/helper.rb', line 54

def git
  Git
end

#loggerObject



50
51
52
# File 'lib/dply/helper.rb', line 50

def logger
  Logger.logger
end

#stringify_values!(hash) ⇒ Object



44
45
46
47
48
# File 'lib/dply/helper.rb', line 44

def stringify_values!(hash)
  hash.each do |k,v|
    hash[k] = v.to_s
  end
end


30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/dply/helper.rb', line 30

def symlink(src, dst)
  if File.symlink? dst
    Dir.mktmpdir("sym-", "./") do |d|
      dst_tmp = "#{d}/#{File.basename dst}"
      FileUtils.ln_s src, dst_tmp
      cmd "mv #{dst_tmp} #{File.dirname dst}", display: false
    end
  elsif File.exist? dst
    error "cannot create symlink #{dst} => #{src}"
  else
    FileUtils.ln_s src, dst
  end
end