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: BuildMetadata, Date, DistributionServer, EZbake, Execution, File, Git, Gpg, Jenkins, Misc, Net, OS, Platform, RakeUtils, Repo, Serialization, Ship, Sign, Tool, Version, Windows Classes: Git_tag

Class Method Summary collapse

Class Method Details

.ask_yes_or_no(force = false) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
# File 'lib/packaging/util.rb', line 94

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



74
75
76
# File 'lib/packaging/util.rb', line 74

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

.boolean_value(var) ⇒ Object



30
31
32
33
# File 'lib/packaging/util.rb', line 30

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



60
61
62
63
# File 'lib/packaging/util.rb', line 60

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

.confirm_ship(files) ⇒ Object



106
107
108
109
110
111
112
113
114
115
# File 'lib/packaging/util.rb', line 106

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



145
146
147
148
149
150
151
# File 'lib/packaging/util.rb', line 145

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



117
118
119
120
# File 'lib/packaging/util.rb', line 117

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.



81
82
83
84
85
86
87
88
# File 'lib/packaging/util.rb', line 81

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



49
50
51
52
# File 'lib/packaging/util.rb', line 49

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

.in_project_root(&blk) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/packaging/util.rb', line 35

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.



135
136
137
138
139
140
141
142
143
# File 'lib/packaging/util.rb', line 135

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



90
91
92
# File 'lib/packaging/util.rb', line 90

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

.require_library_or_fail(library, library_name = nil) ⇒ Object



65
66
67
68
69
70
71
72
# File 'lib/packaging/util.rb', line 65

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