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: AptStagingServer, 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



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

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



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

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

.boolean_value(var) ⇒ Object



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

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



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

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

.confirm_ship(files) ⇒ Object



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

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



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

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



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

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.



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

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



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

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

.in_project_root(&blk) ⇒ Object



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

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.



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

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



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

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

.require_library_or_fail(library, library_name = nil) ⇒ Object



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

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