win-path-utils 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +51 -0
- data/Rakefile +1 -0
- data/example/.gitignore +1 -0
- data/example/example.rb +19 -0
- data/lib/win-path-utils/version.rb +3 -0
- data/lib/win-path-utils.rb +128 -0
- data/win-path-utils.gemspec +23 -0
- metadata +82 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 02c116e701ee6f656b4bf6ffb6d080af3f6f63a5
|
4
|
+
data.tar.gz: 0742e5b289decafb9997a7d96b2cdab3ae24b2d7
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: de0d7916cc04a6d42ae72f5a537cc085d4eb141a6416922ca3afa7330ae2df5bf8b58f110dd1f68a5324b770e8ce4a73b3d50b9eedc045bbcf19682c802f6a3d
|
7
|
+
data.tar.gz: 6200874b47c1a1d0f422cb8d233959677a0b49ddad736eb3dab8e36bfba12ca803b35faaa12c4ddb16f28bcfd1e55632c9cd862862c2bca1c6d4789793c9d6d1
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 MOZGIII
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
# WinPathUtils
|
2
|
+
|
3
|
+
This gem allows you to manupulate Windows' system `PATH` variable via registry.
|
4
|
+
It provides convenient methods to add and remove items to `PATH`.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
gem 'win-path-utils'
|
11
|
+
|
12
|
+
And then execute:
|
13
|
+
|
14
|
+
$ bundle
|
15
|
+
|
16
|
+
Or install it yourself as:
|
17
|
+
|
18
|
+
$ gem install win-path-utils
|
19
|
+
|
20
|
+
## Usage
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
require 'win-path-utils'
|
24
|
+
|
25
|
+
path = WinPathUtils::Path.new
|
26
|
+
|
27
|
+
# Get the PATH
|
28
|
+
path_value = path.get # => "C:\Ruby\bin;C:\..."
|
29
|
+
|
30
|
+
# Append something
|
31
|
+
path.append('C:\test\at\the\end')
|
32
|
+
|
33
|
+
# Prepend something
|
34
|
+
path.prepend('C:\test\at\the\beginning')
|
35
|
+
|
36
|
+
# Get the PATH now - it's updated
|
37
|
+
new_path_value = path.get # => "C:\test\at\the\beginning;C:\Ruby\bin;C:\..."
|
38
|
+
|
39
|
+
# Remove something
|
40
|
+
path.remove('C:\test')
|
41
|
+
```
|
42
|
+
|
43
|
+
Just read the code, it's pretty straightforward.
|
44
|
+
|
45
|
+
## Contributing
|
46
|
+
|
47
|
+
1. Fork it
|
48
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
49
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
50
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
51
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/example/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
path_backup.txt
|
data/example/example.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
$:<<'../lib'
|
2
|
+
|
3
|
+
require 'win-path-utils'
|
4
|
+
|
5
|
+
path = WinPathUtils::Path.new
|
6
|
+
|
7
|
+
path_value = path.get
|
8
|
+
|
9
|
+
# We store it before we can mess it
|
10
|
+
File.open "path_backup.txt", "a+" do |f|
|
11
|
+
f << path_value << "\n"
|
12
|
+
end
|
13
|
+
|
14
|
+
puts "Your %Path% is:", path_value
|
15
|
+
|
16
|
+
puts "Appending something to the %Path%..."
|
17
|
+
path.append('C:\devenv\bin')
|
18
|
+
|
19
|
+
puts "%Path% now is:", path.get
|
@@ -0,0 +1,128 @@
|
|
1
|
+
require "win-path-utils/version"
|
2
|
+
|
3
|
+
require "win32/registry"
|
4
|
+
|
5
|
+
module WinPathUtils
|
6
|
+
class Path
|
7
|
+
class WrongOptionError < StandardError; end
|
8
|
+
|
9
|
+
def initialize(options = {})
|
10
|
+
@separator = options[:separator] || ';'
|
11
|
+
|
12
|
+
options[:type] ||= :system
|
13
|
+
|
14
|
+
@hkey, @reg_path, @key_name = case options[:type]
|
15
|
+
when :system
|
16
|
+
[Win32::Registry::HKEY_LOCAL_MACHINE, 'SYSTEM\CurrentControlSet\Control\Session Manager\Environment', 'Path']
|
17
|
+
when :local, :user
|
18
|
+
[Win32::Registry::HKEY_CURRENT_USER, 'Environment', 'PATH']
|
19
|
+
else
|
20
|
+
raise WrongOptionError, "Unknown type!"
|
21
|
+
end
|
22
|
+
|
23
|
+
@hkey = options[:hkey] if options.key?(:hkey)
|
24
|
+
@reg_path = options[:reg_path] if options.key?(:reg_path)
|
25
|
+
@key_name = options[:key_name] if options.key?(:key_name)
|
26
|
+
end
|
27
|
+
|
28
|
+
# Sets the entire PATH variable to provided string
|
29
|
+
def set(value)
|
30
|
+
with_reg do |reg|
|
31
|
+
reg[@key_name] = value
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
# Returns the entire PATH variable as string
|
36
|
+
def get
|
37
|
+
with_reg do |reg|
|
38
|
+
begin
|
39
|
+
reg.read(@key_name)[1]
|
40
|
+
rescue Win32::Registry::Error
|
41
|
+
nil
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
# Sets the entire PATH variable to provided array
|
47
|
+
# Joins with @separator
|
48
|
+
def set_array(value)
|
49
|
+
set(value.join(@separator))
|
50
|
+
end
|
51
|
+
|
52
|
+
# Returns the entire PATH variable as array
|
53
|
+
# Splits with @separator
|
54
|
+
def get_array
|
55
|
+
get.split(@separator)
|
56
|
+
end
|
57
|
+
|
58
|
+
# Adds value to the path
|
59
|
+
def add(value, options = {})
|
60
|
+
# Set defaults
|
61
|
+
options[:duplication_filter] = :do_not_add unless options.key?(:duplication_filter)
|
62
|
+
|
63
|
+
# Get path
|
64
|
+
path = get_array
|
65
|
+
|
66
|
+
# Check duplicates
|
67
|
+
if path.member?(value)
|
68
|
+
case options[:duplication_filter]
|
69
|
+
when :do_not_add, :deny
|
70
|
+
# do nothing, we already have one in the list
|
71
|
+
return
|
72
|
+
when :remove_existing
|
73
|
+
path.delete!(value)
|
74
|
+
when :none
|
75
|
+
# just pass through
|
76
|
+
else
|
77
|
+
raise WrongOptionError, "Unknown :duplication_filter!"
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
# Change path array
|
82
|
+
case options[:where]
|
83
|
+
when :start, :left
|
84
|
+
path.unshift value
|
85
|
+
when :end, :right
|
86
|
+
path.push value
|
87
|
+
else
|
88
|
+
raise WrongOptionError, "Unknown :where!"
|
89
|
+
end
|
90
|
+
|
91
|
+
# Save new array
|
92
|
+
set_array(path)
|
93
|
+
end
|
94
|
+
|
95
|
+
# Adds element to the end of the path if not exists
|
96
|
+
def push(value, options = {})
|
97
|
+
add(value, options.merge(where: :end))
|
98
|
+
end
|
99
|
+
alias_method :append, :push
|
100
|
+
|
101
|
+
# Adds element to the start of the path if not exists
|
102
|
+
def unshift(value, options = {})
|
103
|
+
add(value, options.merge(where: :start))
|
104
|
+
end
|
105
|
+
alias_method :prepend, :unshift
|
106
|
+
|
107
|
+
# Removes the item from the path
|
108
|
+
def remove(value)
|
109
|
+
path = get_array
|
110
|
+
path.delete(value)
|
111
|
+
set_array(path)
|
112
|
+
end
|
113
|
+
alias_method :delete, :remove
|
114
|
+
|
115
|
+
# Checks the inclusion of value in path
|
116
|
+
def include?(value)
|
117
|
+
get_array.include?(value)
|
118
|
+
end
|
119
|
+
alias_method :member?, :include?
|
120
|
+
|
121
|
+
private
|
122
|
+
|
123
|
+
# Execute block with the current reg settings
|
124
|
+
def with_reg(access_mask = Win32::Registry::Constants::KEY_ALL_ACCESS, &block)
|
125
|
+
@hkey.open(@reg_path, access_mask, &block)
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'win-path-utils/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "win-path-utils"
|
8
|
+
spec.version = WinPathUtils::VERSION
|
9
|
+
spec.authors = ["MOZGIII"]
|
10
|
+
spec.email = ["mike-n@narod.ru"]
|
11
|
+
spec.description = %q{Provides an API to add and remove values from Windows' system PATH variable}
|
12
|
+
spec.summary = %q{Provides an API to add and remove values from Windows' system PATH variable}
|
13
|
+
spec.homepage = "https://github.com/MOZGIII/win-path-utils"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: win-path-utils
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- MOZGIII
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-11-23 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: Provides an API to add and remove values from Windows' system PATH variable
|
42
|
+
email:
|
43
|
+
- mike-n@narod.ru
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- .gitignore
|
49
|
+
- Gemfile
|
50
|
+
- LICENSE.txt
|
51
|
+
- README.md
|
52
|
+
- Rakefile
|
53
|
+
- example/.gitignore
|
54
|
+
- example/example.rb
|
55
|
+
- lib/win-path-utils.rb
|
56
|
+
- lib/win-path-utils/version.rb
|
57
|
+
- win-path-utils.gemspec
|
58
|
+
homepage: https://github.com/MOZGIII/win-path-utils
|
59
|
+
licenses:
|
60
|
+
- MIT
|
61
|
+
metadata: {}
|
62
|
+
post_install_message:
|
63
|
+
rdoc_options: []
|
64
|
+
require_paths:
|
65
|
+
- lib
|
66
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - '>='
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '0'
|
71
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
requirements: []
|
77
|
+
rubyforge_project:
|
78
|
+
rubygems_version: 2.1.10
|
79
|
+
signing_key:
|
80
|
+
specification_version: 4
|
81
|
+
summary: Provides an API to add and remove values from Windows' system PATH variable
|
82
|
+
test_files: []
|