ruby-nuggets 0.6.6 → 0.6.7

Sign up to get free protection for your applications and to get access to all the features.
data/README CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  == VERSION
4
4
 
5
- This documentation refers to ruby-nuggets version 0.6.6
5
+ This documentation refers to ruby-nuggets version 0.6.7
6
6
 
7
7
 
8
8
  == DESCRIPTION
@@ -30,19 +30,15 @@ module Nuggets
30
30
  module UserHomeMixin
31
31
 
32
32
  # call-seq:
33
- # ENV.user_home => aString
33
+ # ENV.user_home(default = '/') => aString
34
34
  #
35
- # Finds the user's home directory. Stolen from RubyGems ;-)
35
+ # Returns the user's home directory, or +default+ if it could not be found.
36
36
  def user_home(default = ::File::ALT_SEPARATOR ? 'C:/' : '/')
37
- %w[HOME USERPROFILE].each { |homekey|
38
- home = self[homekey]
39
- return home if home
37
+ %w[HOME HOMEDRIVE:HOMEPATH USERPROFILE APPDATA].each { |key|
38
+ home = values_at(*key.split(':')).join
39
+ return home.gsub(/\\/, '/') if home && !home.empty?
40
40
  }
41
41
 
42
- if drive = self['HOMEDRIVE'] and path = self['HOMEPATH']
43
- return "#{drive}:#{path}"
44
- end
45
-
46
42
  begin
47
43
  ::File.expand_path('~')
48
44
  rescue ArgumentError
@@ -4,7 +4,7 @@
4
4
  # A component of ruby-nuggets, some extensions to the Ruby programming #
5
5
  # language. #
6
6
  # #
7
- # Copyright (C) 2007-2008 Jens Wille #
7
+ # Copyright (C) 2007-2011 Jens Wille #
8
8
  # #
9
9
  # Authors: #
10
10
  # Jens Wille <jens.wille@uni-koeln.de> #
@@ -31,7 +31,7 @@ module Nuggets
31
31
  class File
32
32
  module WhichMixin
33
33
 
34
- DEFAULT_EXTENSIONS = [Config::CONFIG['EXEEXT']]
34
+ DEFAULT_EXTENSIONS = [RbConfig::CONFIG['EXEEXT']]
35
35
 
36
36
  # call-seq:
37
37
  # File.which(executable, extensions = DEFAULT_EXTENSIONS) => aString or nil
@@ -56,7 +56,7 @@ class IO
56
56
  raise ArgumentError, "wrong number of arguments (#{args.size + 1} for 1-2)"
57
57
  end
58
58
 
59
- open_with_mode(name, 'r', binary) { |*a| yield(*a) }
59
+ open_with_mode(name, 'r', binary, &Proc.new)
60
60
  end
61
61
 
62
62
  # call-seq:
@@ -65,9 +65,7 @@ class IO
65
65
  #
66
66
  # Opens +name+ with mode +w+.
67
67
  def write(name, binary = false)
68
- block_given? ?
69
- open_with_mode(name, 'w', binary) { |*a| yield(*a) } :
70
- open_with_mode(name, 'w', binary)
68
+ open_with_mode(name, 'w', binary, &block_given? ? Proc.new : nil)
71
69
  end
72
70
 
73
71
  # call-seq:
@@ -76,9 +74,7 @@ class IO
76
74
  #
77
75
  # Opens +name+ with mode +a+.
78
76
  def append(name, binary = false)
79
- block_given? ?
80
- open_with_mode(name, 'a', binary) { |*a| yield(*a) } :
81
- open_with_mode(name, 'a', binary)
77
+ open_with_mode(name, 'a', binary, &block_given? ? Proc.new : nil)
82
78
  end
83
79
 
84
80
  # call-seq:
@@ -87,9 +83,7 @@ class IO
87
83
  #
88
84
  # Opens +name+ with mode <tt>r+</tt>.
89
85
  def read_write(name, binary = false)
90
- block_given? ?
91
- open_with_mode(name, 'r+', binary) { |*a| yield(*a) } :
92
- open_with_mode(name, 'r+', binary)
86
+ open_with_mode(name, 'r+', binary, &block_given? ? Proc.new : nil)
93
87
  end
94
88
 
95
89
  # call-seq:
@@ -98,9 +92,7 @@ class IO
98
92
  #
99
93
  # Opens +name+ with mode <tt>w+</tt>.
100
94
  def write_read(name, binary = false)
101
- block_given? ?
102
- open_with_mode(name, 'w+', binary) { |*a| yield(*a) } :
103
- open_with_mode(name, 'w+', binary)
95
+ open_with_mode(name, 'w+', binary, &block_given? ? Proc.new : nil)
104
96
  end
105
97
 
106
98
  # call-seq:
@@ -109,17 +101,14 @@ class IO
109
101
  #
110
102
  # Opens +name+ with mode <tt>a+</tt>.
111
103
  def append_read(name, binary = false)
112
- block_given? ?
113
- open_with_mode(name, 'a+', binary) { |*a| yield(*a) } :
114
- open_with_mode(name, 'a+', binary)
104
+ open_with_mode(name, 'a+', binary, &block_given? ? Proc.new : nil)
115
105
  end
116
106
 
117
107
  private
118
108
 
119
109
  # Just a helper to DRY things up.
120
110
  def open_with_mode(name, mode, binary = false)
121
- mode << 'b' if binary
122
- block_given? ? open(name, mode) { |io| yield io } : open(name, mode)
111
+ open(name, "#{mode}#{'b' if binary}", &block_given? ? Proc.new : nil)
123
112
  end
124
113
 
125
114
  end
@@ -0,0 +1,5 @@
1
+ require 'nuggets/io/null_mixin'
2
+
3
+ class IO
4
+ include Nuggets::IO::NullMixin
5
+ end
@@ -0,0 +1,41 @@
1
+ #--
2
+ ###############################################################################
3
+ # #
4
+ # A component of ruby-nuggets, some extensions to the Ruby programming #
5
+ # language. #
6
+ # #
7
+ # Copyright (C) 2007-2011 Jens Wille #
8
+ # #
9
+ # Authors: #
10
+ # Jens Wille <jens.wille@uni-koeln.de> #
11
+ # #
12
+ # ruby-nuggets is free software; you can redistribute it and/or modify it #
13
+ # under the terms of the GNU General Public License as published by the Free #
14
+ # Software Foundation; either version 3 of the License, or (at your option) #
15
+ # any later version. #
16
+ # #
17
+ # ruby-nuggets is distributed in the hope that it will be useful, but WITHOUT #
18
+ # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
19
+ # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for #
20
+ # more details. #
21
+ # #
22
+ # You should have received a copy of the GNU General Public License along #
23
+ # with ruby-nuggets. If not, see <http://www.gnu.org/licenses/>. #
24
+ # #
25
+ ###############################################################################
26
+ #++
27
+
28
+ module Nuggets
29
+ class IO
30
+ module NullMixin
31
+
32
+ NULL = case RUBY_PLATFORM
33
+ when /mswin|mingw/i then 'NUL'
34
+ when /openvms/i then 'NL:'
35
+ when /amiga/i then 'NIL:'
36
+ else '/dev/null'
37
+ end.freeze
38
+
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,328 @@
1
+ #--
2
+ ###############################################################################
3
+ # #
4
+ # A component of ruby-nuggets, some extensions to the Ruby programming #
5
+ # language. #
6
+ # #
7
+ # Copyright (C) 2007-2011 Jens Wille #
8
+ # #
9
+ # Authors: #
10
+ # Jens Wille <jens.wille@uni-koeln.de> #
11
+ # #
12
+ # ruby-nuggets is free software; you can redistribute it and/or modify it #
13
+ # under the terms of the GNU General Public License as published by the Free #
14
+ # Software Foundation; either version 3 of the License, or (at your option) #
15
+ # any later version. #
16
+ # #
17
+ # ruby-nuggets is distributed in the hope that it will be useful, but WITHOUT #
18
+ # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
19
+ # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for #
20
+ # more details. #
21
+ # #
22
+ # You should have received a copy of the GNU General Public License along #
23
+ # with ruby-nuggets. If not, see <http://www.gnu.org/licenses/>. #
24
+ # #
25
+ ###############################################################################
26
+ #++
27
+
28
+ require 'rbconfig'
29
+
30
+ module Util
31
+
32
+ # Heavily based on Phusion Passenger's PlatformInfo module; see
33
+ # {their code}[https://github.com/FooBarWidget/passenger/blob/release-3.0.2/lib/phusion_passenger/platform_info/ruby.rb].
34
+ #
35
+ #--
36
+ # Phusion Passenger - http://www.modrails.com/
37
+ # Copyright (c) 2010 Phusion
38
+ #
39
+ # "Phusion Passenger" is a trademark of Hongli Lai & Ninh Bui.
40
+ #
41
+ # Permission is hereby granted, free of charge, to any person obtaining a
42
+ # copy of this software and associated documentation files (the "Software"),
43
+ # to deal in the Software without restriction, including without limitation
44
+ # the rights to use, copy, modify, merge, publish, distribute, sublicense,
45
+ # and/or sell copies of the Software, and to permit persons to whom the
46
+ # Software is furnished to do so, subject to the following conditions:
47
+ #
48
+ # The above copyright notice and this permission notice shall be included in
49
+ # all copies or substantial portions of the Software.
50
+ #++
51
+
52
+ module Ruby
53
+
54
+ extend self
55
+
56
+ CONFIG = RbConfig::CONFIG
57
+
58
+ # Store original $GEM_HOME value so that even if the app customizes
59
+ # $GEM_HOME we can still work with the original value.
60
+ if gem_home = ENV['GEM_HOME']
61
+ gem_home = gem_home.strip.freeze
62
+ gem_home = nil if gem_home.empty?
63
+ end
64
+
65
+ GEM_HOME = gem_home
66
+
67
+ RUBY_ENGINE = defined?(::RUBY_ENGINE) ? ::RUBY_ENGINE : 'ruby'
68
+
69
+ OSX_RUBY_RE = %r{\A/System/Library/Frameworks/Ruby.framework/Versions/.*?/usr/bin/ruby\Z}
70
+
71
+ UPDATE_RVM = %q{Please update RVM by running 'rvm update --head && rvm reload && rvm repair all'.} # :nodoc:
72
+
73
+ # Returns correct command for invoking the current Ruby interpreter.
74
+ # In case of RVM this function will return the path to the RVM wrapper script
75
+ # that executes the current Ruby interpreter in the currently active gem set.
76
+ def ruby_command
77
+ return @ruby_command if defined?(@ruby_command)
78
+
79
+ return @ruby_command = ruby_executable unless rvm?
80
+
81
+ if name = rvm_ruby_string and dir = rvm_path
82
+ if File.exist?(filename = File.join(dir, 'wrappers', name, 'ruby'))
83
+ # Old wrapper scripts reference $HOME which causes
84
+ # things to blow up when run by a different user.
85
+ return @ruby_command = filename unless File.read(filename).include?('$HOME')
86
+ end
87
+
88
+ abort 'Your RVM wrapper scripts are too old. ' << UPDATE_RVM
89
+ end
90
+
91
+ # Something's wrong with the user's RVM installation.
92
+ # Raise an error so that the user knows this instead of
93
+ # having things fail randomly later on.
94
+ # 'name' is guaranteed to be non-nil because rvm_ruby_string
95
+ # already raises an exception on error.
96
+ abort 'Your RVM installation appears to be broken: the RVM path cannot be found. ' <<
97
+ 'Please fix your RVM installation or contact the RVM developers for support.'
98
+ end
99
+
100
+ # Returns the full path to the current Ruby interpreter's executable file.
101
+ # This might not be the actual correct command to use for invoking the Ruby
102
+ # interpreter; use ruby_command instead.
103
+ def ruby_executable
104
+ @ruby_executable ||= begin
105
+ dir, name, ext = CONFIG.values_at(*%w[bindir RUBY_INSTALL_NAME EXEEXT])
106
+ File.join(dir, name + ext).sub(/.*\s.*/m, '"\&"')
107
+ end
108
+ end
109
+
110
+ # Returns whether the Ruby interpreter supports process forking.
111
+ def ruby_supports_fork?
112
+ # MRI >= 1.9.2's respond_to? returns false for methods
113
+ # that are not implemented.
114
+ Process.respond_to?(:fork) &&
115
+ RUBY_ENGINE != 'jruby' &&
116
+ RUBY_ENGINE != 'macruby' &&
117
+ CONFIG['target_os'] !~ /mswin|windows|mingw/
118
+ end
119
+
120
+ # Returns whether the current Ruby interpreter is managed by RVM.
121
+ def rvm?
122
+ CONFIG['bindir'] =~ %r{/\.?rvm/}
123
+ end
124
+
125
+ # If the current Ruby interpreter is managed by RVM, returns the
126
+ # directory in which RVM places its working files. Otherwise returns
127
+ # nil.
128
+ def rvm_path
129
+ return @rvm_path if defined?(@rvm_path)
130
+
131
+ return @rvm_path = nil unless rvm?
132
+
133
+ [ENV['rvm_path'], '~/.rvm', '/usr/local/rvm'].compact.each { |path|
134
+ path = File.expand_path(path)
135
+ return @rvm_path = path if File.directory?(path)
136
+ }
137
+
138
+ # Failure to locate the RVM path is probably caused by the
139
+ # user customizing $rvm_path. Older RVM versions don't
140
+ # export $rvm_path, making us unable to detect its value.
141
+ abort 'Unable to locate the RVM path. Your RVM ' <<
142
+ 'installation is probably too old. ' << UPDATE_RVM
143
+ end
144
+
145
+ # If the current Ruby interpreter is managed by RVM, returns the
146
+ # RVM name which identifies the current Ruby interpreter plus the
147
+ # currently active gemset, e.g. something like this:
148
+ # "ruby-1.9.2-p0@mygemset"
149
+ #
150
+ # Returns nil otherwise.
151
+ def rvm_ruby_string
152
+ return @rvm_ruby_string if defined?(@rvm_ruby_string)
153
+
154
+ return @rvm_ruby_string = nil unless rvm?
155
+
156
+ # RVM used to export the necessary information through
157
+ # environment variables, but doesn't always do that anymore
158
+ # in the latest versions in order to fight env var pollution.
159
+ # Scanning $LOAD_PATH seems to be the only way to obtain
160
+ # the information.
161
+
162
+ # Getting the RVM name of the Ruby interpreter ("ruby-1.9.2")
163
+ # isn't so hard, we can extract it from the #ruby_executable
164
+ # string. Getting the gemset name is a bit harder, so let's
165
+ # try various strategies...
166
+
167
+ # $GEM_HOME usually contains the gem set name.
168
+ return @rvm_ruby_string = File.basename(GEM_HOME) if GEM_HOME && GEM_HOME.include?('rvm/gems/')
169
+
170
+ # User somehow managed to nuke $GEM_HOME. Extract info from $LOAD_PATH.
171
+ $LOAD_PATH.each { |path| return @rvm_ruby_string = $1 if path =~ %r{^.*rvm/gems/([^/]+)} }
172
+
173
+ # On Ruby 1.9, $LOAD_PATH does not contain any gem paths until
174
+ # at least one gem has been required so the above can fail.
175
+ # We're out of options now, we can't detect the gem set.
176
+ # Raise an exception so that the user knows what's going on
177
+ # instead of having things fail in obscure ways later.
178
+ abort 'Unable to autodetect the currently active RVM gem set ' <<
179
+ "name. Please contact this program's author for support."
180
+ end
181
+
182
+ # Returns either 'sudo' or 'rvmsudo' depending on whether the current
183
+ # Ruby interpreter is managed by RVM.
184
+ def ruby_sudo_command
185
+ "#{'rvm' if rvm?}sudo"
186
+ end
187
+
188
+ # Locates a Ruby tool command +name+, e.g. 'gem', 'rake', 'bundle', etc. Instead
189
+ # of naively looking in $PATH, this function uses a variety of search heuristics
190
+ # to find the command that's really associated with the current Ruby interpreter.
191
+ # It should never locate a command that's actually associated with a different
192
+ # Ruby interpreter.
193
+ #
194
+ # NOTE: The return value may not be the actual correct invocation for the tool.
195
+ # Use command_for_ruby_tool for that.
196
+ #
197
+ # Returns nil when nothing's found.
198
+ def locate_ruby_tool(name)
199
+ extensions = ['', CONFIG['EXEEXT']].compact.uniq
200
+
201
+ # Deduce Ruby's --program-prefix and --program-suffix from its install name
202
+ # and transform the given input name accordingly.
203
+ #
204
+ # "rake" => "jrake", "rake1.8", etc
205
+ [name, CONFIG['RUBY_INSTALL_NAME'].sub('ruby', name)].uniq.each { |basename|
206
+ extensions.each { |ext|
207
+ result = locate_ruby_tool_by_basename("#{basename}#{ext}")
208
+ return result if result
209
+ }
210
+ }
211
+
212
+ nil
213
+ end
214
+
215
+ # Returns the correct command string for invoking the +name+ executable
216
+ # that belongs to the current Ruby interpreter. Returns nil if the command
217
+ # is not found.
218
+ #
219
+ # If the command executable is a Ruby program, then we need to run it
220
+ # in the correct Ruby interpreter just in case the command doesn't
221
+ # have the correct shebang line; we don't want a totally different
222
+ # Ruby than the current one to be invoked.
223
+ #
224
+ # If it's not a Ruby program then it's probably a wrapper
225
+ # script as is the case with e.g. RVM (~/.rvm/wrappers).
226
+ def command_for_ruby_tool(name)
227
+ filename = respond_to?(name) ? send(name) : locate_ruby_tool(name)
228
+ shebang_command(filename) =~ /ruby/ ? "#{ruby_command} #{filename}" : filename
229
+ end
230
+
231
+ %w[gem rake rspec].each { |name|
232
+ class_eval <<-EOT, __FILE__, __LINE__ + 1
233
+ def #{name}
234
+ @#{name} ||= locate_ruby_tool('#{name}')
235
+ end
236
+
237
+ def #{name}_command
238
+ @#{name}_command ||= command_for_ruby_tool('#{name}')
239
+ end
240
+ EOT
241
+ }
242
+
243
+ def ruby_options_to_argv(args, ruby_command = ruby_command)
244
+ argv = [ruby_command]
245
+
246
+ args.pop.each { |key, val|
247
+ opt = "-#{key.to_s[0, 1]}"
248
+
249
+ if val.is_a?(Array)
250
+ val.each { |wal|
251
+ argv << opt << wal.to_s
252
+ }
253
+ else
254
+ if opt == '-e'
255
+ argv << opt << val.to_s
256
+ elsif val != false
257
+ argv << "#{opt}#{val unless val == true}"
258
+ end
259
+ end
260
+ } if args.last.is_a?(Hash)
261
+
262
+ argv.concat(args.map! { |arg| arg.to_s.strip })
263
+ end
264
+
265
+ private
266
+
267
+ def locate_ruby_tool_by_basename(name)
268
+ dir = if RUBY_PLATFORM =~ /darwin/ && ruby_command =~ OSX_RUBY_RE
269
+ # On OS X we must look for Ruby binaries in /usr/bin.
270
+ # RubyGems puts executables (e.g. 'rake') in there, not in
271
+ # /System/Libraries/(...)/bin.
272
+ '/usr/bin'
273
+ else
274
+ File.dirname(ruby_command)
275
+ end
276
+
277
+ filename = File.join(dir, name)
278
+ return filename if File.file?(filename) && File.executable?(filename)
279
+
280
+ # RubyGems might put binaries in a directory other
281
+ # than Ruby's bindir. Debian packaged RubyGems and
282
+ # DebGem packaged RubyGems are the prime examples.
283
+ begin
284
+ require 'rubygems' unless defined?(Gem)
285
+
286
+ filename = File.join(Gem.bindir, name)
287
+ return filename if File.file?(filename) && File.executable?(filename)
288
+ rescue LoadError
289
+ end
290
+
291
+ # Looks like it's not in the RubyGems bindir. Search in $PATH, but
292
+ # be very careful about this because whatever we find might belong
293
+ # to a different Ruby interpreter than the current one.
294
+ ENV['PATH'].split(File::PATH_SEPARATOR).each { |path|
295
+ filename = File.join(path, name)
296
+
297
+ if File.file?(filename) && File.executable?(filename)
298
+ return filename if shebang_command(filename) == ruby_command
299
+ end
300
+ }
301
+
302
+ nil
303
+ end
304
+
305
+ def shebang_command(filename)
306
+ File.foreach(filename) { |line|
307
+ return $1 if line =~ /\A#!\s*(\S*)/
308
+
309
+ # Allow one extra line for magic comment.
310
+ break if $. > 1
311
+ }
312
+ end
313
+
314
+ end
315
+
316
+ end
317
+
318
+ def File.ruby; Util::Ruby.ruby_command; end
319
+
320
+ begin
321
+ require 'open4'
322
+
323
+ def Process.ruby(*args)
324
+ argv = Util::Ruby.ruby_options_to_argv(args, File.ruby)
325
+ Open4.popen4(*argv, &block_given? ? Proc.new : nil)
326
+ end
327
+ rescue LoadError
328
+ end
@@ -4,7 +4,7 @@ module Nuggets
4
4
 
5
5
  MAJOR = 0
6
6
  MINOR = 6
7
- TINY = 6
7
+ TINY = 7
8
8
 
9
9
  class << self
10
10
 
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-nuggets
3
3
  version: !ruby/object:Gem::Version
4
- hash: 11
4
+ hash: 9
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 6
9
- - 6
10
- version: 0.6.6
9
+ - 7
10
+ version: 0.6.7
11
11
  platform: ruby
12
12
  authors:
13
13
  - Jens Wille
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-01-07 00:00:00 +01:00
18
+ date: 2011-01-21 00:00:00 +01:00
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
@@ -39,6 +39,7 @@ files:
39
39
  - lib/nuggets/integer/length.rb
40
40
  - lib/nuggets/util/added_methods/init.rb
41
41
  - lib/nuggets/util/i18n.rb
42
+ - lib/nuggets/util/ruby.rb
42
43
  - lib/nuggets/util/dotted_decimal.rb
43
44
  - lib/nuggets/util/content_type.rb
44
45
  - lib/nuggets/util/ansicolor2css.rb
@@ -94,6 +95,8 @@ files:
94
95
  - lib/nuggets/string/sub_with_md.rb
95
96
  - lib/nuggets/string/word_wrap.rb
96
97
  - lib/nuggets/io/agrep.rb
98
+ - lib/nuggets/io/null.rb
99
+ - lib/nuggets/io/null_mixin.rb
97
100
  - lib/nuggets/io/modes.rb
98
101
  - lib/nuggets/env/user_home_mixin.rb
99
102
  - lib/nuggets/env/set.rb
@@ -161,14 +164,14 @@ licenses: []
161
164
 
162
165
  post_install_message:
163
166
  rdoc_options:
164
- - --title
165
- - ruby-nuggets Application documentation
166
- - --line-numbers
167
+ - --charset
168
+ - UTF-8
167
169
  - --main
168
170
  - README
171
+ - --line-numbers
169
172
  - --inline-source
170
- - --charset
171
- - UTF-8
173
+ - --title
174
+ - ruby-nuggets Application documentation
172
175
  - --all
173
176
  require_paths:
174
177
  - lib