patheditor 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/README.txt +74 -0
  2. data/Rakefile +35 -0
  3. data/bin/path_editor +141 -0
  4. data/lib/windows_path.rb +74 -0
  5. metadata +69 -0
data/README.txt ADDED
@@ -0,0 +1,74 @@
1
+ = PathEditor
2
+
3
+ Anyone who's had to change their Windows PATH environment variable more than once in a week knows it's a frustrating and error prone process.
4
+
5
+ This command line utility intends to fix that. It allows you to view, add, or remove paths from the PATH variable. It will also clean the variable of directorries that no longer exist. It's even smart enough to expand system variables such as %SystemRoot% when cleaning the path.
6
+
7
+ When the path is modified, all system processes are notified immediately. Not all programs will respond to the notification, but the current shell and future shells opened from the Start | Run box will.
8
+
9
+ = Usage
10
+
11
+ For help, just type
12
+
13
+ path_editor --help
14
+
15
+ To list the current path settings, just run the program without any arguments:
16
+
17
+ path_editor
18
+
19
+ == Adding a Path
20
+
21
+ To add a directory to the path:
22
+
23
+ path_editor --add "full path" --no-test
24
+
25
+ Note, because of the way the Windows shell works, if you wish to add a path with a trailing slash, you must use a double slash at the end:
26
+
27
+ path_editor --add "c:\program files\\" --no-test
28
+
29
+ If the path already given already exists, it will not be added again.
30
+
31
+ == Removing a path
32
+
33
+ To remove a directory from the path:
34
+
35
+ path_editor --remove "full path"
36
+
37
+ Limited regular expressions can be used - essentially "*" or "?", like filename wild cards. If more than one path matches the expression given, you will be asked for confirmation before the path variable is updated.
38
+
39
+ == Cleaning the Path
40
+
41
+ To clean your path:
42
+
43
+ path_editor --clean
44
+
45
+ This process will remove directories which no longer exist, and will remove any duplicate directories from the path.
46
+
47
+ = Acknowledgements
48
+
49
+ Of course, my thanks to Matz for creating Ruby. What a joy it is to program in. I'm also indebted to the fine coders of the "commandline" and "highline" libraries. Finally, my thanks to the King of Kings, Jesus Christ, for the gift of this life. Amen.
50
+
51
+ = LICENSE:
52
+
53
+ (The MIT License)
54
+
55
+ Copyright (c) 2007 Justin Bailey
56
+
57
+ Permission is hereby granted, free of charge, to any person obtaining
58
+ a copy of this software and associated documentation files (the
59
+ 'Software'), to deal in the Software without restriction, including
60
+ without limitation the rights to use, copy, modify, merge, publish,
61
+ distribute, sublicense, and/or sell copies of the Software, and to
62
+ permit persons to whom the Software is furnished to do so, subject to
63
+ the following conditions:
64
+
65
+ The above copyright notice and this permission notice shall be
66
+ included in all copies or substantial portions of the Software.
67
+
68
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
69
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
70
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
71
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
72
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
73
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
74
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,35 @@
1
+ require 'rubygems'
2
+ Gem::manage_gems
3
+ require 'rake/gempackagetask'
4
+
5
+ spec = Gem::Specification.new do |s|
6
+ s.name = "patheditor"
7
+ s.summary = "A simple program to manage the Windows PATH environment variable."
8
+ s.version = "1.0.0"
9
+ s.author = "Justin Bailey"
10
+ s.email = "jgbailey@nospam@gmail.com"
11
+
12
+ s.description = <<EOS
13
+ This gem provides an application, path_editor, which makes it easy to add, remove, display or clean up the Windows PATH environment variable.
14
+ EOS
15
+
16
+ s.platform = Gem::Platform::RUBY
17
+ s.files = FileList["lib/**/*", "*.txt", "Rakefile"].to_a
18
+
19
+ s.bindir = "bin"
20
+ s.executables = ["path_editor"]
21
+ s.require_path = "lib"
22
+
23
+ s.has_rdoc = true
24
+ s.extra_rdoc_files = ["README.txt"]
25
+ s.rdoc_options << '--title' << 'PathEditor -- A PATH environment variable editor' <<
26
+ '--main' << 'README.txt' <<
27
+ '--line-numbers'
28
+
29
+ s.add_dependency("highline", ">= 1.2.3")
30
+ s.add_dependency("commandline", ">= 0.7.10")
31
+ end
32
+
33
+ Rake::GemPackageTask.new(spec) do |pkg|
34
+ pkg.need_tar = true
35
+ end
data/bin/path_editor ADDED
@@ -0,0 +1,141 @@
1
+ # A simple application for editing the environment's path information.
2
+ require 'rubygems'
3
+ require 'highline'
4
+ require 'commandline'
5
+ require 'pathname'
6
+
7
+ $: << (Pathname.new(__FILE__).dirname + '..\lib') unless $:.include?(Pathname.new(__FILE__).dirname + '..\lib')
8
+ require 'windows_path'
9
+
10
+ class App < CommandLine::Application
11
+ def initialize
12
+ synopsis "[[-c, --clean] [-a, --add PATH] [r, --remove EXPR]] [--case-sensitive] [-n, --no-confirmation] [-h, --help]"
13
+ short_description <<-EOS
14
+ With no arguments, displays the current path, broken into separate entries. Arguments allow directories to be added to or removed from the path. The path can also be cleaned of directories which no longer exist."
15
+ EOS
16
+
17
+ long_description <<-EOS
18
+ A script for manipulating the environment's PATH variable. Editing this variable is a huge pain in Windows - this script aims to make it easier.
19
+ EOS
20
+
21
+ option :names => %w(-c --clean), :arity => 0,
22
+ :opt_found => proc { |opt, user_option, opt_args|
23
+ @clean = true
24
+ },
25
+ :opt_description => <<-EOS
26
+ Cleans directories which no longer exist from the path, and removes any duplicate directories.
27
+ EOS
28
+
29
+ option :names => %w(-a --add), :arity => 1, :arg_description => "path",
30
+ :opt_found => proc { |opt, user_option, opt_args|
31
+ @add = Pathname.new opt_args.first
32
+ },
33
+ :opt_description => <<-EOS
34
+ Adds the given path to the PATH environment variable. If the path already exists (in the exact form given), it will not be added.
35
+ EOS
36
+
37
+ option :names => %w(-r --remove), :arity => 1, :arg_description => "expression",
38
+ :opt_found => proc { |opt, user_option, opt_args|
39
+ @remove = opt_args.first
40
+ },
41
+ :opt_description => <<-EOS
42
+ Removes the given path from the environment variable. If characters such as "*" or "?" appear in the path, it will be treated as a regular expression, and any back slashes should be escaped (i.e. "C:\\Program Files\\*" vs "C:\Program Files").
43
+
44
+ When a non-regular expression is given, the path must match exactly to be removed.
45
+ EOS
46
+
47
+ option :names => %w(--case-sensitive), :arity => 0,
48
+ :opt_found => proc { |opt, user_option, opt_args|
49
+ @case_sensitive = true
50
+ },
51
+ :opt_description => <<-EOS
52
+ Makes path comparisons case-sensitive if present. Path comparisons are case-insensitive otherwise.
53
+ EOS
54
+
55
+ option :names => %w(-n --no-confirmation), :arity => 0,
56
+ :opt_found => proc { @confirm_disabled = true},
57
+ :opt_description => <<-EOS
58
+ Forces changes to occur with confirmation. True by default unless paths are being removed with a regular expression and more than one path is matched - then it is false. This is intended to prevent the accidental remove of all paths or a similar disaster.
59
+ EOS
60
+
61
+ option :debug, :help
62
+ end
63
+
64
+ def main
65
+ @hl = HighLine.new
66
+ @paths = WindowsPath.new
67
+
68
+ if @clean || @add || @remove
69
+ if @clean
70
+ paths_to_remove = []
71
+ @paths.each { |p| paths_to_remove << p unless Pathname.new(p).exist? }
72
+ remove_paths(paths_to_remove) unless paths_to_remove.empty?
73
+ end
74
+
75
+ if @add
76
+ unless @paths.include? @add
77
+ @paths.add(@add)
78
+ @path_updated = true
79
+ @hl.say "Added #{@add} to path."
80
+ else
81
+ @hl.say "#{@add} not added because it already exists in the path."
82
+ end
83
+ end
84
+
85
+ if @remove
86
+ # See if @remove looks like a regular expression
87
+ if @remove.index("*") || @remove.index("?")
88
+ # File glob "*" not quite the same as reg ex "*" - need to add "." character
89
+ # to make sure any and all characters following are matched
90
+ @remove = @remove.gsub('\\', '\\\\\\\\').gsub("*", ".*").gsub("?", ".")
91
+ exp = @case_sensitive ? Regexp.new("^#{@remove}$") : Regexp.new("^#{@remove}$", Regexp::IGNORECASE)
92
+ paths_to_remove = []
93
+
94
+ @paths.each { |p| paths_to_remove << p if exp.match(p) }
95
+ remove_paths(paths_to_remove) unless paths_to_remove.empty?
96
+ elsif @paths.include?(@remove)
97
+ @hl.say "Removing path #{@remove}"
98
+ @paths.remove(@remove)
99
+ @path_updated = true
100
+ end
101
+ end
102
+
103
+ if @path_updated
104
+ if confirmation_needed?
105
+ @paths.update if @hl.agree("Would you like to commit the changes made? [Yn] ", true)
106
+ else
107
+ @paths.update
108
+ end
109
+ end
110
+ else
111
+ # Print path
112
+ @hl.say "Current path entries:"
113
+ @paths.each { |p| @hl.say "\t#{p}" }
114
+ end
115
+ rescue
116
+ puts "Exception: #{$!.message}"
117
+ puts $!.backtrace.join("\n")
118
+ end
119
+
120
+ def confirmation_needed?
121
+ # No need to confirm if either the user specified no confirmation explicitly, or
122
+ # the automatic confirmation never got set.
123
+ (@automatic_confirm && ! @confirm_disabled) || false
124
+ end
125
+
126
+ def remove_paths(paths_to_remove)
127
+ cnt = 0
128
+ paths_to_remove.each do |p|
129
+ @hl.say "Removing #{p}"
130
+ @paths.remove p
131
+ @path_updated = true
132
+ # If more than one path matches and the user did not specify "--no-confirmation", make
133
+ # sure to enable it here.
134
+ @automatic_confirm = true if cnt > 0
135
+ cnt += 1
136
+ end
137
+ end
138
+
139
+ end
140
+
141
+ App.run unless $0 == __FILE__
@@ -0,0 +1,74 @@
1
+ require 'win32ole'
2
+
3
+ # Manages access to and updating of windows path setting. Also manages
4
+ # enumerating paths available, and checking if a given path exists.
5
+ class WindowsPath
6
+ include Enumerable
7
+ FILE_SEPARATOR = File::ALT_SEPARATOR
8
+
9
+ def initialize
10
+ @shell = WIN32OLE.new("WScript.Shell")
11
+ @system = @shell.Environment("System")
12
+ @paths = []
13
+ @paths = @system.Item("Path").split(";").collect { |p| p.strip }
14
+ end
15
+
16
+ def expand_vars(val)
17
+ while m = val.match(/%.*?%/)
18
+ val = val.gsub(m[0], @shell.ExpandEnvironmentStrings(m[0]))
19
+ end
20
+
21
+ val
22
+ end
23
+
24
+ def add(p)
25
+ @paths << p.to_s unless self.include?(p)
26
+ end
27
+
28
+ alias_method :<<, :add
29
+
30
+ def remove(r)
31
+ # Find all paths that match and delete them.
32
+ @paths = @paths.delete_if { |p| compare_paths(r, p) }
33
+ end
34
+
35
+ # Update any changes
36
+ def update
37
+ @system["Item", "Path"] = @paths.join(";")
38
+ end
39
+
40
+ def each
41
+ @paths.each { |p| yield expand_vars(p) }
42
+ end
43
+
44
+ def compare_paths(left, right)
45
+ left = expand_vars(left.to_s).downcase
46
+ right = expand_vars(right.to_s).downcase
47
+
48
+ if left == right
49
+ true
50
+ else
51
+ left_sep = left.length > 1 && left.rindex(FILE_SEPARATOR) == left.length - 1
52
+ right_sep = right.length > 1 && right.rindex(FILE_SEPARATOR) == right.length - 1
53
+
54
+ if ! left_sep && right_sep
55
+ left == right[0, right.length - 1]
56
+ elsif left_sep && ! right_sep
57
+ left[0, left.length - 1] == right
58
+ else
59
+ false
60
+ end
61
+ end
62
+ end
63
+
64
+ def include?(path)
65
+ # Compare paths case-insensitive, and also checking
66
+ # trailing separators (so c:\program files == c:\program files\)
67
+ @paths.any? { |p| compare_paths(path, p) }
68
+ end
69
+
70
+ def inspect
71
+ @paths.inspect
72
+ end
73
+
74
+ end
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.0
3
+ specification_version: 1
4
+ name: patheditor
5
+ version: !ruby/object:Gem::Version
6
+ version: 1.0.0
7
+ date: 2007-02-05 00:00:00 -08:00
8
+ summary: A simple program to manage the Windows PATH environment variable.
9
+ require_paths:
10
+ - lib
11
+ email: jgbailey@nospam@gmail.com
12
+ homepage:
13
+ rubyforge_project:
14
+ description: This gem provides an application, path_editor, which makes it easy to add, remove, display or clean up the Windows PATH environment variable.
15
+ autorequire:
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
29
+ authors:
30
+ - Justin Bailey
31
+ files:
32
+ - lib/windows_path.rb
33
+ - README.txt
34
+ - Rakefile
35
+ test_files: []
36
+
37
+ rdoc_options:
38
+ - --title
39
+ - PathEditor -- A PATH environment variable editor
40
+ - --main
41
+ - README.txt
42
+ - --line-numbers
43
+ extra_rdoc_files:
44
+ - README.txt
45
+ executables:
46
+ - path_editor
47
+ extensions: []
48
+
49
+ requirements: []
50
+
51
+ dependencies:
52
+ - !ruby/object:Gem::Dependency
53
+ name: highline
54
+ version_requirement:
55
+ version_requirements: !ruby/object:Gem::Version::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: 1.2.3
60
+ version:
61
+ - !ruby/object:Gem::Dependency
62
+ name: commandline
63
+ version_requirement:
64
+ version_requirements: !ruby/object:Gem::Version::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: 0.7.10
69
+ version: