ggenv 0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. data/GGenv.gemspec +20 -0
  2. data/HISTORY.txt +2 -0
  3. data/lib/GGEnv.rb +342 -0
  4. metadata +41 -0
@@ -0,0 +1,20 @@
1
+ require "rubygems"
2
+
3
+ spec = Gem::Specification.new do |spec|
4
+ spec.name = "ggenv"
5
+ spec.version = '0.5'
6
+ spec.author = "Gonzalo Garramuno"
7
+ spec.email = 'GGarramuno@aol.com'
8
+ spec.homepage = 'http://www.rubyforge.org/projects/ggenv/'
9
+ spec.summary = 'Environment variable manipulation using ruby arrays.'
10
+ spec.require_path = "lib"
11
+ spec.autorequire = "GGEnv"
12
+ spec.files = ["lib/GGEnv.rb"]
13
+ spec.description = <<-EOF
14
+ Environment variable manipulation using a ruby array syntax.
15
+ EOF
16
+ spec.extra_rdoc_files = ["HISTORY.txt", "GGenv.gemspec"]
17
+ spec.has_rdoc = true
18
+ spec.rubyforge_project = 'ggenv'
19
+ spec.required_ruby_version = '>= 1.6.8'
20
+ end
@@ -0,0 +1,2 @@
1
+
2
+ v0.5 - First public beta release.
@@ -0,0 +1,342 @@
1
+ #
2
+ # GGEnv: Module to deal with environment variables as arrays.
3
+ #
4
+ # Copyright (c) 2005 Gonzalo Garramuno
5
+ # Released under the same terms as Ruby
6
+ #
7
+ # $Revision: 0.5 $
8
+ #
9
+
10
+ require "English"
11
+
12
+
13
+ #
14
+ # == Env
15
+ #
16
+ # == Documentation
17
+ #
18
+ # This file contains auxiliary routines to easily interact with
19
+ # environment variables that contain lists of paths, like PATH,
20
+ # LD_LIBRARY_PATH, MAYA_SCRIPT_PATH, etc.
21
+ #
22
+ # == Usage
23
+ #
24
+ # Env.check_directories = false # turn off verification that
25
+ # # directories exist (default: true)
26
+ # path = Env['PATH']
27
+ #
28
+ # path << "C:/newpath" # As path is modified, so
29
+ # # is ENV['PATH']
30
+ #
31
+ # path.delete_if { |x| x =~ /maya/ } # remove all paths that have maya
32
+ #
33
+ # path.unshift ["C:/", "E:/bin"] # add these paths at start
34
+ #
35
+ # Env['PATH'] = path[0,2] + path[4,6] # concat two slices and send to PATH
36
+ #
37
+ # path.check_directories # check existance of directories
38
+ # # for this variable only (unlike
39
+ # # Env.check_directories)
40
+ #
41
+ module Env
42
+
43
+ protected
44
+ @@_check_dirs = true
45
+ @@_envvars = {}
46
+
47
+ if RUBY_PLATFORM =~ /mswin/
48
+ SEP = ';'
49
+ else
50
+ SEP = ':'
51
+ end
52
+
53
+ public
54
+ #
55
+ # Clear the Env path cache to free some memory.
56
+ #
57
+ # == Example
58
+ #
59
+ # path = Env['PATH']
60
+ # path = nil
61
+ # Env.clear
62
+ #
63
+ def self.clear
64
+ @@_envvars = {}
65
+ GC.start
66
+ end
67
+
68
+ #
69
+ # Assign a new value (Array or String) to an environment variable.
70
+ #
71
+ # == Example
72
+ #
73
+ # Env['PATH'] = ['/usr/', '/usr/local/']
74
+ #
75
+ def self.[]=(a, b)
76
+ @@_envvars[a] = EnvVar.new( a, b )
77
+ end
78
+
79
+ #
80
+ # Access an environment variable as an array.
81
+ # The result is cached internally within the Env module for faster
82
+ # access.
83
+ #
84
+ # == Example
85
+ #
86
+ # puts Env['LD_LIBRARY_PATH']
87
+ # path = Env['PATH']
88
+ # path << ["/usr/local/bin"]
89
+ # path -= "C:/"
90
+ #
91
+ def self.[](a)
92
+ return @@_envvars[a] if @@_envvars[a]
93
+ @@_envvars[a] = EnvVar.new( a, ENV[a] )
94
+ end
95
+
96
+ #
97
+ # Are we checking directories of all variables automatically?
98
+ #
99
+ def self.check_directories?
100
+ @@_check_dirs
101
+ end
102
+
103
+ #
104
+ # If true, check directories of all Env variables automatically.
105
+ # Default value is true.
106
+ #
107
+ # == Example
108
+ #
109
+ # Env.check_directories = false
110
+ # path = Env['PATH']
111
+ # path << "/inexistent/path/"
112
+ # puts path
113
+ # Env.check_directories = true
114
+ # puts path
115
+ #
116
+ def self.check_directories=(a)
117
+ @@_check_dirs = (a == true)
118
+ if @@_check_dirs
119
+ @@_envvars.each_value { |v| v.check_directories }
120
+ end
121
+ end
122
+
123
+ private
124
+
125
+ # An auxiliary class that represents environment variable paths
126
+ # as an array. User is supposed to interact with this class by simply
127
+ # using Env[VARIABLE] or storing it in a variable.
128
+ class EnvVar < Array
129
+ attr_reader :name
130
+
131
+ def initialize(name, value)
132
+ @owner = true
133
+ @name = name
134
+ replace(value) if value
135
+ end
136
+
137
+ def replace(value)
138
+ if value.kind_of?(Array)
139
+ super
140
+ elsif value.kind_of?(String)
141
+ super( value.split(SEP) )
142
+ elsif value.kind_of?(NilClass)
143
+ clear; return self
144
+ else
145
+ raise Error,
146
+ "Cannot assign #{value} to #{@name}. Not an array or string."
147
+ end
148
+ uniq!
149
+ map! { |p| unify_path(p) }
150
+ if Env.check_directories?
151
+ check_directories
152
+ else
153
+ to_env
154
+ end
155
+ return self
156
+ end
157
+
158
+ def check_directories
159
+ delete_if { |p| not File.directory?(p) }
160
+ to_env
161
+ end
162
+
163
+ def clear
164
+ super
165
+ to_env
166
+ return self
167
+ end
168
+
169
+ def delete(a, &block)
170
+ super
171
+ to_env
172
+ return self
173
+ end
174
+
175
+ def delete_at(a)
176
+ super
177
+ to_env
178
+ return self
179
+ end
180
+
181
+ def delete_if(&block)
182
+ super
183
+ to_env
184
+ return self
185
+ end
186
+
187
+ def |(b)
188
+ @owner = false
189
+ EnvVar.new( @name, super )
190
+ end
191
+
192
+ def &(b)
193
+ @owner = false
194
+ EnvVar.new( @name, super )
195
+ end
196
+
197
+ def -(b)
198
+ @owner = false
199
+ if b.kind_of?(Array)
200
+ EnvVar.new( @name, super )
201
+ else
202
+ EnvVar.new( @name, super( [b] ) )
203
+ end
204
+ end
205
+
206
+ def +(b)
207
+ c = self.dup
208
+ @owner = false
209
+ c << b
210
+ end
211
+
212
+ def <<(*b)
213
+ b.each { |p|
214
+ if p.kind_of?(Array)
215
+ p.each { |x|
216
+ s = unify_path( x.to_s )
217
+ next if not verify(s)
218
+ super(s)
219
+ }
220
+ uniq!
221
+ else
222
+ s = unify_path( p.to_s )
223
+ next if self.include?(s)
224
+ next if not verify(s)
225
+ super(s)
226
+ end
227
+ }
228
+ to_env
229
+ return self
230
+ end
231
+
232
+ def push(*b)
233
+ self.<<(*b)
234
+ end
235
+
236
+ def remove(b)
237
+ self.delete_if { |p| p =~ /#{b}/ }
238
+ to_env
239
+ end
240
+
241
+ def unshift(*b)
242
+ b.reverse_each { |p|
243
+ if p.kind_of?(Array)
244
+ a = []
245
+ p.reverse_each { |x|
246
+ s = unify_path( x.to_s )
247
+ next if not verify(s)
248
+ a << s
249
+ }
250
+ super(*a)
251
+ uniq!
252
+ else
253
+ s = unify_path( p.to_s )
254
+ next if self.include?(s)
255
+ next if not verify(s)
256
+ super(s)
257
+ end
258
+ }
259
+ to_env
260
+ end
261
+
262
+ def to_s
263
+ from_env if not @owner
264
+ self.join(SEP)
265
+ end
266
+
267
+ def inspect
268
+ to_s
269
+ end
270
+
271
+ #
272
+ # Send value of variable to environment
273
+ #
274
+ def to_env
275
+ return if not @name
276
+ from_env if not @owner
277
+ ENV[@name] = to_s
278
+ end
279
+
280
+ #
281
+ # Replace value of variable from environment
282
+ #
283
+ def from_env
284
+ return if not @name
285
+ @owner = true
286
+ replace( ENV[@name] )
287
+ end
288
+
289
+ protected
290
+
291
+ REGEX_DISK = /^(\w:)\/?/
292
+ REGEX_CYGDRIVE = /^\/cygdrive\/(\w)\/?/
293
+ REGEX_CYGWIN = /cygwin/
294
+ REGEX_DISK2 = /^([A-Z]):\//
295
+
296
+ #
297
+ # Routine used to unify the names of paths to a consistant syntax
298
+ #
299
+ def unify_path(s)
300
+ p = s.frozen? ? s.dup : s
301
+ p.gsub!( /\\/, '/' )
302
+ p = "#{$1.upcase}/#{$POSTMATCH}" if p =~ REGEX_DISK
303
+ if RUBY_PLATFORM =~ REGEX_CYGWIN
304
+ if p =~ REGEX_CYGDRIVE
305
+ p = "/cygdrive/#{$1.upcase}/#{$POSTMATCH}"
306
+ else
307
+ p.sub!( REGEX_DISK2, '/cygdrive/\1/' ) if @name == 'PATH'
308
+ end
309
+ end
310
+ return p
311
+ end
312
+
313
+ #
314
+ # Routine used to verify if path is a directory on disk
315
+ #
316
+ def verify(path)
317
+ return false if Env.check_directories? and not File.directory?(path)
318
+ return true
319
+ end
320
+ end
321
+
322
+ end
323
+
324
+
325
+ if __FILE__ == $0
326
+ Env.check_directories = false
327
+
328
+ path = Env['PATH']
329
+ path << [ 'C:', '/' ]
330
+
331
+ path.unshift( ['E:', '/etc'] )
332
+
333
+ p Env['PATH']
334
+ puts "-- After checking dirs " + '-'*57
335
+ Env.check_directories = true
336
+ p Env['PATH']
337
+
338
+ puts "SLICE: #{path[3,2]}"
339
+
340
+ Env['PATH'] = path[0,2] + path[4,1]
341
+ puts "ADD: #{Env['PATH']}"
342
+ end
metadata ADDED
@@ -0,0 +1,41 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.8.10
3
+ specification_version: 1
4
+ name: ggenv
5
+ version: !ruby/object:Gem::Version
6
+ version: "0.5"
7
+ date: 2005-07-18
8
+ summary: Environment variable manipulation using ruby arrays.
9
+ require_paths:
10
+ - lib
11
+ email: GGarramuno@aol.com
12
+ homepage: http://www.rubyforge.org/projects/ggenv/
13
+ rubyforge_project: ggenv
14
+ description: Environment variable manipulation using a ruby array syntax.
15
+ autorequire: GGEnv
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ -
22
+ - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: 1.6.8
25
+ version:
26
+ platform: ruby
27
+ authors:
28
+ - Gonzalo Garramuno
29
+ files:
30
+ - lib/GGEnv.rb
31
+ - HISTORY.txt
32
+ - GGenv.gemspec
33
+ test_files: []
34
+ rdoc_options: []
35
+ extra_rdoc_files:
36
+ - HISTORY.txt
37
+ - GGenv.gemspec
38
+ executables: []
39
+ extensions: []
40
+ requirements: []
41
+ dependencies: []