class Object
Public Instance Methods
capture_command(*options)
click to toggle source
# File lib/ci_scripts/helpers.rb, line 42 def capture_command(*options) output, status = Open3.capture2(*options) unless status.success? log_error("Attempted to capture output of `#{options.join ' '}` but received exit code #{status.to_i}") log_error(output) exit status.to_i end output end
classify(s)
click to toggle source
helpers
# File lib/ci_scripts/helpers.rb, line 88 def classify(s) s = s.to_s.split('_').collect(&:capitalize).join s[0] = s[0].capitalize s end
command(*options)
click to toggle source
Timed runs
# File lib/ci_scripts/helpers.rb, line 24 def command(*options) log_info(options.join(" ")) t = Time.now system(*options) log_success("#{(Time.now - t).round(2)}s\n ") if $CHILD_STATUS && $CHILD_STATUS.exitstatus != 0 log_error("Exit status #{$CHILD_STATUS.exitstatus}") exit $CHILD_STATUS.exitstatus end end
env_check(key, value)
click to toggle source
env helpers
# File lib/ci_scripts/helpers.rb, line 63 def env_check(key, value) unless ENV[key] puts "Setting #{key} to #{value}" ENV[key] = value end end
env_fetch(key, default = nil)
click to toggle source
# File lib/ci_scripts/helpers.rb, line 79 def env_fetch(key, default = nil) if ENV[key] ENV[key] else default end end
env_require(key)
click to toggle source
# File lib/ci_scripts/helpers.rb, line 70 def env_require(key) val = ENV[key] if val.nil? || val == "" log_error "Required environment variable #{key} not set" exit 1 end val end
installed?(binary)
click to toggle source
# File lib/ci_scripts/helpers.rb, line 58 def installed?(binary) test_command?("command", "-v", binary) end
log_error(s)
click to toggle source
# File lib/ci_scripts/helpers.rb, line 14 def log_error(s) puts("\x1b[31m#{s}\x1b[0m") end
log_info(s)
click to toggle source
Logging
# File lib/ci_scripts/helpers.rb, line 6 def log_info(s) puts("\x1b[34m#{s}\x1b[0m") end
log_success(s)
click to toggle source
# File lib/ci_scripts/helpers.rb, line 10 def log_success(s) puts("\x1b[32m#{s}\x1b[0m") end
nice_exit(code, msg = "Something happened")
click to toggle source
# File lib/ci_scripts/helpers.rb, line 18 def nice_exit(code, msg = "Something happened") log_error(msg) exit code end
test_command?(*options)
click to toggle source
system helpers
# File lib/ci_scripts/helpers.rb, line 53 def test_command?(*options) system(*options, out: File::NULL) $CHILD_STATUS == 0 end
timed_run(name) { || ... }
click to toggle source
# File lib/ci_scripts/helpers.rb, line 35 def timed_run(name) log_info(name) t = Time.now yield log_success("#{(Time.now - t).round(2)}s\n ") end
unindent(s)
click to toggle source
# File lib/ci_scripts/helpers.rb, line 94 def unindent(s) indent = s.split("\n").select { |line| !line.strip.empty? }.map { |line| line.index(/[^\s]/) }.compact.min || 0 s.gsub(/^[[:blank:]]{#{indent}}/, '') end