Module: Pwsh::Util

Defined in:
lib/pwsh/util.rb

Overview

Various helper methods

Class Method Summary collapse

Class Method Details

.custom_powershell_property(name, expression) ⇒ String

Return the representative string of a PowerShell hash for a custom object property to be used in selecting or filtering. The script block for the expression must be passed as the string you want interpolated into the hash; this method does not do any of the additional work of interpolation for you as the type sits inside a code block inside a hash.

Returns:

  • (String)

    representation of a PowerShell hash with the keys ‘Name’ and ‘Expression’



140
141
142
# File 'lib/pwsh/util.rb', line 140

def custom_powershell_property(name, expression)
  "@{Name = '#{name}'; Expression = {#{expression}}}"
end

.escape_quotes(text) ⇒ String

Ensure that quotes inside a passed string will continue to be passed

Returns:

  • (String)

    the string with quotes escaped



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

def escape_quotes(text)
  text.gsub("'", "''")
end

.format_powershell_value(object) ⇒ String

Convert a ruby value into a string to be passed along to PowerShell for interpolation in a command Handles:

  • Strings

  • Numbers

  • Booleans

  • Symbols

  • Arrays

  • Hashes

Returns:

  • (String)

    representation of the value for interpolation



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/pwsh/util.rb', line 119

def format_powershell_value(object)
  if %i[true false].include?(object) || %w[trueclass falseclass].include?(object.class.name.downcase)
    "$#{object}"
  elsif object.instance_of?(Symbol) || object.class.ancestors.include?(Numeric)
    object.to_s
  elsif object.instance_of?(String)
    "'#{escape_quotes(object)}'"
  elsif object.instance_of?(Array)
    "@(#{object.collect { |item| format_powershell_value(item) }.join(', ')})"
  elsif object.instance_of?(Hash)
    "@{#{object.collect { |k, v| "#{format_powershell_value(k)} = #{format_powershell_value(v)}" }.join('; ')}}"
  else
    raise "unsupported type #{object.class} of value '#{object}'"
  end
end

.invalid_directories?(path_collection) ⇒ Bool

Verify paths specified are valid directories which exist.

Returns:

  • (Bool)

    true if any directories specified do not exist



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/pwsh/util.rb', line 21

def invalid_directories?(path_collection)
  invalid_paths = false

  return invalid_paths if path_collection.nil? || path_collection.empty?

  paths = on_windows? ? path_collection.split(';') : path_collection.split(':')
  paths.each do |path|
    invalid_paths = true unless File.directory?(path) || path.empty?
  end

  invalid_paths
end

.on_windows?Bool

Verifies whether or not the current context is running on a Windows node.

Returns:

  • (Bool)

    true if on windows



12
13
14
15
16
# File 'lib/pwsh/util.rb', line 12

def on_windows?
  # Ruby only sets File::ALT_SEPARATOR on Windows and the Ruby standard
  # library uses that to test what platform it's on.
  !!File::ALT_SEPARATOR
end

.pascal_case(object) ⇒ String

Return a string or symbol converted to PascalCase

Returns:

  • (String)

    PascalCased string



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

def pascal_case(object)
  should_symbolize = object.is_a?(Symbol)
  raise "snake_case method only handles strings and symbols, passed a #{object.class}: #{object}" unless should_symbolize || object.is_a?(String)

  # Break word boundaries to snake case first
  text = snake_case(object.to_s).split('_').collect(&:capitalize).join
  should_symbolize ? text.to_sym : text
end

.pascal_case_hash_keys(object) ⇒ Hash

Iterate through a hashes keys, PascalCasing them

Returns:

  • (Hash)

    Hash with all keys PascalCased



76
77
78
79
# File 'lib/pwsh/util.rb', line 76

def pascal_case_hash_keys(object)
  pascal_case_proc = proc { |key| pascal_case(key) }
  apply_key_mutator(object, pascal_case_proc)
end

.snake_case(object) ⇒ String

Return a string or symbol converted to snake_case

Returns:

  • (String)

    snake_cased string



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/pwsh/util.rb', line 37

def snake_case(object)
  # Implementation copied from: https://github.com/rubyworks/facets/blob/master/lib/core/facets/string/snakecase.rb
  # gsub(/::/, '/').
  should_symbolize = object.is_a?(Symbol)
  raise "snake_case method only handles strings and symbols, passed a #{object.class}: #{object}" unless should_symbolize || object.is_a?(String)

  text = object.to_s
               .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
               .gsub(/([a-z\d])([A-Z])/, '\1_\2')
               .tr('-', '_')
               .gsub(/\s/, '_')
               .gsub(/__+/, '_')
               .downcase
  should_symbolize ? text.to_sym : text
end

.snake_case_hash_keys(object) ⇒ Hash

Iterate through a hashes keys, snake_casing them

Returns:

  • (Hash)

    Hash with all keys snake_cased



56
57
58
59
# File 'lib/pwsh/util.rb', line 56

def snake_case_hash_keys(object)
  snake_case_proc = proc { |key| snake_case(key) }
  apply_key_mutator(object, snake_case_proc)
end

.symbolize_hash_keys(object) ⇒ Hash

Ensure that all keys in a hash are symbols, not strings.

Returns:

  • (Hash)

    a hash whose keys have been converted to symbols.



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

def symbolize_hash_keys(object)
  symbolize_proc = proc(&:to_sym)
  apply_key_mutator(object, symbolize_proc)
end