class DeployDply::Helper

Public Class Methods

new() click to toggle source
# File lib/deploy_dply/helper.rb, line 6
def initialize
  @env = {}
end

Public Instance Methods

load_task(task) click to toggle source
# File lib/deploy_dply/helper.rb, line 44
def load_task(task)
  @loaded ||= ::Set.new
  task_file = "#{__dir__}/../../tasks/#{task}.rake"
  real_path = ::Pathname.new(task_file).realpath
  if @loaded.include?(real_path)
    return
  else
    load real_path
    @loaded << real_path
  end
end
rake(task, *args, stdout: nil, stderr: nil) click to toggle source
# File lib/deploy_dply/helper.rb, line 10
def rake(task, *args, stdout: nil, stderr: nil)
  if not ( stdout || stderr)
    puts "#{arrow_char} rake #{task}"
    ::Rake::Task[task].invoke(*args)
    return
  end
  
  redirection_str = ""
  redirection_str << " 1> #{stdout}" if stdout
  redirection_str << " 2> #{stderr}" if stderr
  puts "#{arrow_char} rake #{task} #{redirection_str}"
  redirect_output(stdout: stdout, stderr: stderr) do
    ::Rake::Task[task].invoke(*args)
  end
end
redirect_output(stdout: nil, stderr: nil) { || ... } click to toggle source
# File lib/deploy_dply/helper.rb, line 56
def redirect_output(stdout: nil, stderr: nil)
  begin
    orig_stderr = $stderr.clone
    orig_stdout = $stdout.clone
    if stdout
      $stdout.reopen(stdout, "a+")
      $stdout.sync = true
    end
    if stderr
      $stderr.reopen(stderr, "a+")
      $stderr.sync = true
    end
    yield
  ensure
    $stdout.reopen orig_stdout
    $stderr.reopen orig_stderr
  end
end
set_env(key, value) click to toggle source
# File lib/deploy_dply/helper.rb, line 39
def set_env(key, value)
  ENV[key.to_s] = value.to_s
  @env[key.to_s] = value.to_s
end
sh(*command, bundled_env: false, env: {}, **kwargs, &block) click to toggle source
# File lib/deploy_dply/helper.rb, line 26
def sh(*command, bundled_env: false, env: {}, **kwargs, &block)
  print "#{arrow_char} "
  return RakeDsl.old_sh(*command, &block) if bundled_env
  Bundler.with_clean_env do
    ENV["PATH"] = cleaned_paths_str(ENV["PATH"])
    ENV.delete "GEM_HOME"
    ENV.delete "RUBYLIB"
    load_env @env
    load_env env
    RakeDsl.old_sh(*command, **kwargs, &block)
  end
end

Private Instance Methods

arrow_char() click to toggle source
# File lib/deploy_dply/helper.rb, line 77
def arrow_char
  "\u2023".green.bold
end
cleaned_paths_str(paths_str) click to toggle source
# File lib/deploy_dply/helper.rb, line 81
def cleaned_paths_str(paths_str)
  return "" if not paths_str
  paths = paths_str.split(File::PATH_SEPARATOR)
  paths.reject! { |x| x =~ /vendor\/bundle\/ruby/ }
  paths.uniq.join(File::PATH_SEPARATOR)
end
load_env(env) click to toggle source
# File lib/deploy_dply/helper.rb, line 88
def load_env(env)
  env.each {|k,v| ENV[k.to_s] = v.to_s }
end