module CLI::Kit::Util

Public Class Methods

begin(&block_that_might_raise) click to toggle source

Must call retry_after on the result in order to execute the block

Example usage:

CLI::Kit::Util.begin do

might_raise_if_costly_prep_not_done()

end.retry_after(ExpectedError) do

costly_prep()

end

# File lib/cli/kit/util.rb, line 161
def begin(&block_that_might_raise)
  Retrier.new(block_that_might_raise)
end
ci?() click to toggle source

Standard way of checking for CI

# File lib/cli/kit/util.rb, line 148
def ci?
  ENV['CI']
end
dash_case(camel_case) click to toggle source
# File lib/cli/kit/util.rb, line 13
def dash_case(camel_case)
  snake_case(camel_case, '-')
end
english_join(array) click to toggle source

Joins an array with commas and “and”, using the Oxford comma.

# File lib/cli/kit/util.rb, line 50
def english_join(array)
  return "" if array.nil?
  return array.join(" and ") if array.length < 3

  "#{array[0..-2].join(', ')}, and #{array[-1]}"
end
integration_test_session?() click to toggle source

Set only in IntegrationTest#session; indicates that the process was called by `session.execute` from an IntegrationTest subclass.

# File lib/cli/kit/util.rb, line 143
def integration_test_session?
  ENV['INTEGRATION_TEST_SESSION']
end
snake_case(camel_case, seperator = "_") click to toggle source
# File lib/cli/kit/util.rb, line 5
def snake_case(camel_case, seperator = "_")
  camel_case.to_s # MyCoolThing::MyAPIModule
    .gsub(/::/, '/') # MyCoolThing/MyAPIModule
    .gsub(/([A-Z]+)([A-Z][a-z])/, "\\1#{seperator}\\2") # MyCoolThing::MyAPI_Module
    .gsub(/([a-z\d])([A-Z])/, "\\1#{seperator}\\2") # My_Cool_Thing::My_API_Module
    .downcase # my_cool_thing/my_api_module
end
strip_heredoc(str) click to toggle source

The following methods is taken from activesupport All credit for this method goes to the original authors. github.com/rails/rails/blob/d66e7835bea9505f7003e5038aa19b6ea95ceea1/activesupport/lib/active_support/core_ext/string/strip.rb

Copyright © 2005-2018 David Heinemeier Hansson

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Strips indentation by removing the amount of leading whitespace in the least indented non-empty line in the whole string

# File lib/cli/kit/util.rb, line 45
def strip_heredoc(str)
  str.gsub(/^#{str.scan(/^[ \t]*(?=\S)/).min}/, "".freeze)
end
testing?() click to toggle source

Standard way of checking for CI / Tests

# File lib/cli/kit/util.rb, line 137
def testing?
  ci? || ENV['TEST']
end
to_filesize(bytes, precision: 2, space: false) click to toggle source

Converts an integer representing bytes into a human readable format

# File lib/cli/kit/util.rb, line 73
def to_filesize(bytes, precision: 2, space: false)
  to_si_scale(bytes, 'B', precision: precision, space: space, factor: 1024)
end
to_si_scale(number, unit = '', factor: 1000, precision: 2, space: false) click to toggle source

Converts a number to a human readable format on the SI scale

# File lib/cli/kit/util.rb, line 79
def to_si_scale(number, unit = '', factor: 1000, precision: 2, space: false)
  raise ArgumentError, "factor should only be 1000 or 1024" unless [1000, 1024].include?(factor)

  small_scale = %w(m µ n p f a z y)
  big_scale = %w(k M G T P E Z Y)
  negative = number < 0
  number = number.abs.to_f

  if number == 0 || number.between?(1, factor)
    prefix = ""
    scale = 0
  else
    scale = Math.log(number, factor).floor
    if number < 1
      index = [-scale - 1, small_scale.length].min
      scale = -(index + 1)
      prefix = small_scale[index]
    else
      index = [scale - 1, big_scale.length].min
      scale = index + 1
      prefix = big_scale[index]
    end
  end

  divider = (factor**scale)
  fnum = (number / divider).round(precision)

  # Trim useless decimal
  fnum = fnum.to_i if (fnum.to_i.to_f * divider) == number

  fnum = -fnum if negative
  prefix = " " + prefix if space

  "#{fnum}#{prefix}#{unit}"
end
with_dir(dir) { || ... } click to toggle source

Dir.chdir, when invoked in block form, complains when we call chdir again recursively. There's no apparent good reason for this, so we simply implement our own block form of Dir.chdir here.

# File lib/cli/kit/util.rb, line 118
def with_dir(dir)
  prev = Dir.pwd
  Dir.chdir(dir)
  yield
ensure
  Dir.chdir(prev)
end
with_environment(environment, value) { || ... } click to toggle source

Execute a block within the context of a variable enviroment

# File lib/cli/kit/util.rb, line 59
def with_environment(environment, value)
  return yield unless environment

  old_env = ENV[environment]
  begin
    ENV[environment] = value
    yield
  ensure
    old_env ? ENV[environment] = old_env : ENV.delete(environment)
  end
end
with_tmp_dir() { |dir| ... } click to toggle source
# File lib/cli/kit/util.rb, line 126
def with_tmp_dir
  require 'fileutils'
  dir = Dir.mktmpdir
  with_dir(dir) do
    yield(dir)
  end
ensure
  FileUtils.remove_entry(dir)
end