Sha256: d1b45238994799314cbda76aeaa03ebf91b203f4d56682b2e2c77c5cb158e019

Contents?: true

Size: 1.5 KB

Versions: 8

Compression:

Stored size: 1.5 KB

Contents

require 'dply/error'
require 'dply/logger'
require 'fileutils'
require 'tmpdir'

module Dply
  module Helper

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

      run_command = shell ? command_str : 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_str}\""
      error error_msg if return_value != 0
      return output
    end

    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

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

    def logger
      Logger.logger
    end

    def git
      Git
    end

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

  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
dply-0.2.19 lib/dply/helper.rb
dply-0.2.18 lib/dply/helper.rb
dply-0.2.17 lib/dply/helper.rb
dply-0.2.16 lib/dply/helper.rb
dply-0.2.15 lib/dply/helper.rb
dply-0.2.14 lib/dply/helper.rb
dply-0.2.13 lib/dply/helper.rb
dply-0.2.11 lib/dply/helper.rb