lib/dpl/ctx/bash.rb in dpl-2.0.3.beta.4 vs lib/dpl/ctx/bash.rb in dpl-2.0.5.beta
- old
+ new
@@ -1,5 +1,8 @@
+# frozen_string_literal: true
+
+require 'English'
require 'cl'
require 'fileutils'
require 'logger'
require 'open3'
require 'tmpdir'
@@ -12,22 +15,23 @@
include FileUtils
attr_accessor :folds, :stdout, :stderr, :last_out, :last_err
def initialize(stdout = $stdout, stderr = $stderr)
- @stdout, @stderr = stdout, stderr
+ @stdout = stdout
+ @stderr = stderr
@folds = 0
super('dpl', abort: false)
end
# Folds any log output from the given block
#
# Starts a log fold with the given fold message, calls the block, and
# closes the fold.
#
# @param msg [String] the message that will appear on the log fold
- def fold(msg, &block)
+ def fold(msg)
self.folds += 1
print "travis_fold:start:dpl.#{folds}\r\e[K"
time do
info "\e[33m#{msg}\e[0m"
yield
@@ -39,17 +43,17 @@
# Times the given block
#
# Starts a travis time log tag, calls the block, and closes the tag,
# including timing information. This makes a timing badge appear on
# the surrounding log fold.
- def time(&block)
+ def time
id = SecureRandom.hex[0, 8]
- start = Time.now.to_i * (10 ** 9)
+ start = Time.now.to_i * (10**9)
print "travis_time:start:#{id}\r\e[K"
yield
ensure
- finish = Time.now.to_i * (10 ** 9)
+ finish = Time.now.to_i * (10**9)
duration = finish - start
print "\ntravis_time:end:#{id}:start=#{start},finish=#{finish},duration=#{duration}\r\e[K"
end
# Outputs a deprecation warning for a given deprecated option key to stderr.
@@ -133,10 +137,11 @@
end
def apts_get(packages)
packages = packages.reject { |name, cmd = name| which(cmd || name) }
return unless packages.any?
+
apt_update
packages.each { |package, cmd| apt_get(package, cmd || package, update: false) }
end
# Installs an APT package
@@ -146,10 +151,11 @@
#
# @param package [String] the package name
# @param cmd [String] an executable installed by the package, defaults to the package name
def apt_get(package, cmd = package, opts = {})
return if which(cmd)
+
apt_update unless opts[:update].is_a?(FalseClass)
shell "sudo apt-get -qq install #{package}", retry: true
end
def apt_update
@@ -175,11 +181,11 @@
env = ENV.to_h
# Bundler.reset!
# Gem.loaded_specs.clear
gemfile do
source 'https://rubygems.org'
- gems.each { |g| gem *g }
+ gems.each { |g| gem(*g) }
end
# https://github.com/bundler/bundler/issues/7181
ENV.replace(env)
end
end
@@ -204,11 +210,11 @@
# @param cmd [String] Executable command installed by that package (optional, defaults to the package name).
# @param version [String] Package version (optional).
def pip_install(package, cmd = package, version = nil)
ENV['VIRTUAL_ENV'] = File.expand_path('~/dpl_venv')
ENV['PATH'] = File.expand_path("~/dpl_venv/bin:#{ENV['PATH']}")
- shell 'virtualenv --no-site-packages ~/dpl_venv', echo: true
+ shell 'virtualenv ~/dpl_venv', echo: true
shell 'pip install urllib3[secure]'
cmd = "pip install #{package}"
cmd << pip_version(version) if version
shell cmd, retry: true
end
@@ -263,12 +269,12 @@
@last_out, @last_err, @last_status = retrying(cmd.retry ? 2 : 0) do
send(cmd.capture? ? :open3 : :system, cmd.cmd, cmd.opts)
end
- info cmd.success % { out: last_out } if success? && cmd.success?
- error cmd.error % { err: last_err } if failed? && cmd.assert?
+ info format(cmd.success, out: last_out) if success? && cmd.success?
+ error format(cmd.error, err: last_err) if failed? && cmd.assert?
success? && cmd.capture? ? last_out.chomp : @last_status
end
def retrying(max, tries = 0, status = false)
@@ -322,16 +328,16 @@
# Returns the last child process' exit status
#
# Internal, and not to be used by implementors. $? is a read-only
# variable, so we use a method that we can stub during tests.
def last_process_status
- $?.success?
+ $CHILD_STATUS.success?
end
# Whether or not the current Ruby process runs with superuser priviledges.
def sudo?
- Process::UID.eid == 0
+ Process::UID.eid.zero?
end
# Returns current repository name
#
# Uses the environment variable `TRAVIS_REPO_SLUG` if present, or the
@@ -489,17 +495,17 @@
end
def move_files(paths)
paths.each do |path|
target = "#{tmp_dir}/#{File.basename(path)}"
- mv(path, target) if File.exists?(path)
+ mv(path, target) if File.exist?(path)
end
end
def unmove_files(paths)
paths.each do |path|
source = "#{tmp_dir}/#{File.basename(path)}"
- mv(source, path) if File.exists?(source)
+ mv(source, path) if File.exist?(source)
end
end
def mv(src, dest)
Kernel.system("sudo mv #{src} #{dest} 2> /dev/null")