module Pwsh::Util
Various helper methods
Public Instance Methods
# File lib/pwsh/util.rb, line 96 def apply_key_mutator(object, proc) return object.map { |item| apply_key_mutator(item, proc) } if object.is_a?(Array) return object unless object.is_a?(Hash) modified_hash = {} object.each do |key, value| modified_hash[proc.call(key)] = apply_key_mutator(value, proc) end modified_hash end
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.
@return [String] representation of a PowerShell hash with the keys 'Name' and 'Expression'
# File lib/pwsh/util.rb, line 140 def custom_powershell_property(name, expression) "@{Name = '#{name}'; Expression = {#{expression}}}" end
Ensure that quotes inside a passed string will continue to be passed
@return [String] the string with quotes escaped
# File lib/pwsh/util.rb, line 84 def escape_quotes(text) text.gsub("'", "''") end
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
@return [String] representation of the value for interpolation
# 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
Verify paths specified are valid directories which exist.
@return [Bool] true if any directories specified do not exist
# 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
Verifies whether or not the current context is running on a Windows node.
@return [Bool] true if on windows
# 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
Return a string or symbol converted to PascalCase
@return [String] PascalCased string
# 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
Iterate through a hashes keys, PascalCasing them
@return [Hash] Hash with all keys PascalCased
# 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
Return a string or symbol converted to snake_case
@return [String] snake_cased string
# 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
Iterate through a hashes keys, snake_casing them
@return [Hash] Hash with all keys snake_cased
# 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
Ensure that all keys in a hash are symbols, not strings.
@return [Hash] a hash whose keys have been converted to symbols.
# File lib/pwsh/util.rb, line 91 def symbolize_hash_keys(object) symbolize_proc = proc(&:to_sym) apply_key_mutator(object, symbolize_proc) end