Class: WinPathUtils::Path

Inherits:
Object
  • Object
show all
Defined in:
lib/win-path-utils.rb

Defined Under Namespace

Classes: SetxError, WrongOptionError

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Path

Returns a new instance of Path.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/win-path-utils.rb', line 10

def initialize(options = {})
  @separator = options[:separator] || ';'

  options[:type] ||= :system
  
  @hkey, @reg_path, @key_name = case options[:type]
  when :system
    [Win32::Registry::HKEY_LOCAL_MACHINE, 'SYSTEM\CurrentControlSet\Control\Session Manager\Environment', 'Path']
  when :local, :user
    [Win32::Registry::HKEY_CURRENT_USER, 'Environment', 'PATH']
  else
    raise WrongOptionError, "Unknown type!"
  end

  @hkey     = options[:hkey]     if options.key?(:hkey)
  @reg_path = options[:reg_path] if options.key?(:reg_path)
  @key_name = options[:key_name] if options.key?(:key_name)
end

Class Method Details

.commit!Object

Cause Windows to reload environment from registry

Raises:



123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/win-path-utils.rb', line 123

def self.commit!
  var = "WIN_PATH_UTILS_TMPVAR"

  fd_r, fd_w = IO.pipe
  result = system("setx", var, "", [:out, :err] => fd_w)
  fd_w.close
  output = fd_r.read

  raise SetxError.new("SETX error: #{output}") if result == false

  result
end

Instance Method Details

#add(value, options = {}) ⇒ Object

Adds value to the path



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/win-path-utils.rb', line 60

def add(value, options = {})
  # Set defaults
  options[:duplication_filter] = :do_not_add unless options.key?(:duplication_filter)

  # Get path
  path = get_array

  # Check duplicates
  if path.member?(value)
    case options[:duplication_filter]
    when :do_not_add, :deny
      # do nothing, we already have one in the list
      return
    when :remove_existing
      path.delete!(value)
    when :none
      # just pass through
    else
      raise WrongOptionError, "Unknown :duplication_filter!"
    end
  end

  # Change path array
  case options[:where]
  when :start, :left
    path.unshift value
  when :end, :right
    path.push value
  else
    raise WrongOptionError, "Unknown :where!"
  end

  # Save new array
  set_array(path)
end

#commit!Object

Alias



137
138
139
# File 'lib/win-path-utils.rb', line 137

def commit!
  self.class.commit!
end

#getObject

Returns the entire PATH variable as string



37
38
39
40
41
42
43
44
45
# File 'lib/win-path-utils.rb', line 37

def get
  with_reg do |reg|
    begin
      reg.read(@key_name)[1]
    rescue Win32::Registry::Error
      nil
    end
  end
end

#get_arrayObject

Returns the entire PATH variable as array Splits with @separator



55
56
57
# File 'lib/win-path-utils.rb', line 55

def get_array
  get.split(@separator)
end

#include?(value) ⇒ Boolean Also known as: member?

Checks the inclusion of value in path

Returns:

  • (Boolean)


117
118
119
# File 'lib/win-path-utils.rb', line 117

def include?(value)
  get_array.include?(value)
end

#push(value, options = {}) ⇒ Object Also known as: append

Adds element to the end of the path if not exists



97
98
99
# File 'lib/win-path-utils.rb', line 97

def push(value, options = {})
  add(value, options.merge(where: :end))
end

#remove(value) ⇒ Object Also known as: delete

Removes the item from the path



109
110
111
112
113
# File 'lib/win-path-utils.rb', line 109

def remove(value)
  path = get_array
  path.delete(value)
  set_array(path)
end

#set(value) ⇒ Object

Sets the entire PATH variable to provided string



30
31
32
33
34
# File 'lib/win-path-utils.rb', line 30

def set(value)
  with_reg do |reg|
    reg[@key_name] = value
  end
end

#set_array(value) ⇒ Object

Sets the entire PATH variable to provided array Joins with @separator



49
50
51
# File 'lib/win-path-utils.rb', line 49

def set_array(value)
  set(value.join(@separator))
end

#unshift(value, options = {}) ⇒ Object Also known as: prepend

Adds element to the start of the path if not exists



103
104
105
# File 'lib/win-path-utils.rb', line 103

def unshift(value, options = {})
  add(value, options.merge(where: :start))
end