Module: Pkg::Util

Defined in:
lib/packaging/util.rb,
lib/packaging/util/git_tags.rb

Overview

Utility methods used by the various rake tasks

Defined Under Namespace

Modules: Date, Execution, File, Git, Gpg, Jenkins, Misc, Net, OS, Platform, RakeUtils, Serialization, Ship, Tool, Version Classes: Git_tag

Class Method Summary collapse

Class Method Details

.ask_yes_or_no(force = false) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/packaging/util.rb', line 88

def self.ask_yes_or_no(force = false)
  unless force
    return Pkg::Util.boolean_value(Pkg::Config.answer_override) unless Pkg::Config.answer_override.nil?
  end

  answer = Pkg::Util.get_input
  return true if answer =~ /^y$|^yes$/
  return false if answer =~ /^n$|^no$/
  puts "Nope, try something like yes or no or y or n, etc:"
  Pkg::Util.ask_yes_or_no
end

.base64_encode(string) ⇒ Object



68
69
70
# File 'lib/packaging/util.rb', line 68

def self.base64_encode(string)
  Base64.encode64(string).strip
end

.boolean_value(var) ⇒ Object



24
25
26
27
# File 'lib/packaging/util.rb', line 24

def self.boolean_value(var)
  return true if var == true || ( var.is_a?(String) && ( var.downcase == 'true' || var.downcase =~ /^y$|^yes$/))
  return false
end

.check_var(varname, var) ⇒ String, ...

Utility to check if a variable is set

Parameters:

  • varname (String)

    the name of the variable to be checked

  • var (String, Boolean, Hash, Array, nil)

    the contents of the variable to be checked

Returns:

  • (String, Boolean, Hash, Array, nil)

    the contents of var

Raises:

  • (RuntimeError)

    raises an exception if the variable is not set and is required



54
55
56
57
# File 'lib/packaging/util.rb', line 54

def self.check_var(varname, var)
  fail "Requires #{varname} be set!" if var.nil?
  var
end

.confirm_ship(files) ⇒ Object



100
101
102
103
104
105
106
107
108
109
# File 'lib/packaging/util.rb', line 100

def self.confirm_ship(files)
  $stdout.puts "Artifacts will be shipped to the following hosts:"
  Pkg::Util.filter_configs('host').each { |key, value| puts "#{key}: #{value}" }
  $stdout.puts "Does this look right?? [y,n]"
  Pkg::Util.ask_yes_or_no(true)
  $stdout.puts "The following files have been built and are ready to ship:"
  files.each { |file| puts "\t#{file}\n" unless File.directory?(file) }
  $stdout.puts "Ship these files?? [y,n]"
  Pkg::Util.ask_yes_or_no(true)
end

.deprecate(old_cmd, new_cmd = nil) ⇒ Object



139
140
141
142
143
144
145
# File 'lib/packaging/util.rb', line 139

def self.deprecate(old_cmd, new_cmd = nil)
  msg = "!! #{old_cmd} is deprecated."
  if new_cmd
    msg << " Please use #{new_cmd} instead."
  end
  $stdout.puts("\n#{msg}\n")
end

.filter_configs(filter = nil) ⇒ Object



111
112
113
114
# File 'lib/packaging/util.rb', line 111

def self.filter_configs(filter = nil)
  return Pkg::Config.instance_values.select { |key, _| key.match(/#{filter}/) } if filter
  Pkg::Config.instance_values
end

.get_input(echo = true) ⇒ Object

Utility to retrieve command line input

Parameters:

  • noecho (Boolean, nil)

    if we are retrieving command line input with or without privacy. This is mainly for sensitive information like passwords.



75
76
77
78
79
80
81
82
# File 'lib/packaging/util.rb', line 75

def self.get_input(echo = true)
  fail "Cannot get input on a noninteractive terminal" unless $stdin.tty?

  system 'stty -echo' unless echo
  $stdin.gets.chomp!
ensure
  system 'stty echo'
end

.get_var(var) ⇒ String, ...

Utility to get the contents of an Environment variable

Parameters:

  • var (String)

    The name of the environment variable to return

Returns:

  • (String, Boolean, Hash, Array, nil)

    The contents of ENV



43
44
45
46
# File 'lib/packaging/util.rb', line 43

def self.get_var(var)
  self.check_var(var, ENV[var])
  ENV[var]
end

.in_project_root(&blk) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/packaging/util.rb', line 29

def self.in_project_root(&blk)
  result = nil
  fail "Cannot execute in project root if Pkg::Config.project_root is not set" unless Pkg::Config.project_root

  Dir.chdir Pkg::Config.project_root do
    result = blk.call
  end
  result
end

.pseudo_uri(opts = {}) ⇒ String?

Construct a probably-correct (or correct-enough) URI for tools like ssh or rsync. Currently lacking support for intuitive joins, ports, protocols, fragments, or 75% of what Addressable::URI or URI would provide out of the box. The “win” here is that the returned String should “just work”.

Parameters:

  • opts (Hash) (defaults to: {})

    fragments used to build the pseudo URI

Options Hash (opts):

  • :path (String)

    URI-ish path component

  • :host (String)

    URI-ish host component

Returns:

  • (String, nil)

    a string representing either a hostname:/path pair, a hostname without a path, or a path without a hostname. Returns nil if it is unable to construct a useful URI-like string.



129
130
131
132
133
134
135
136
137
# File 'lib/packaging/util.rb', line 129

def self.pseudo_uri(opts = {})
  options = { path: nil, host: nil }.merge(opts)

  # Prune empty values to determine what is returned
  options.delete_if { |_, v| v.to_s.empty? }
  return nil if options.empty?

  [options[:host], options[:path]].compact.join(':')
end

.rand_stringObject



84
85
86
# File 'lib/packaging/util.rb', line 84

def self.rand_string
  rand.to_s.split('.')[1]
end

.require_library_or_fail(library, library_name = nil) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/packaging/util.rb', line 59

def self.require_library_or_fail(library, library_name = nil)
  library_name ||= library
  begin
    require library
  rescue LoadError
    fail "Could not load #{library_name}. #{library_name} is required by the packaging repo for this task"
  end
end