Module: Nuggets::RubyMixin
- Included in:
- Ruby
- Defined in:
- lib/nuggets/ruby_mixin.rb
Overview
Originally based on Phusion Passenger’s PlatformInfo module.
– Phusion Passenger - www.modrails.com/ Copyright © 2010 Phusion
“Phusion Passenger” is a trademark of Hongli Lai & Ninh Bui.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. ++
Constant Summary collapse
- CONFIG =
::RbConfig::CONFIG
- GEM_HOME =
gem_home
- RUBY_ENGINE =
defined?(::RUBY_ENGINE) ? ::RUBY_ENGINE : 'ruby'
- RUBY_PLATFORM =
::RUBY_ENGINE == 'jruby' ? CONFIG['target_os'] : ::RUBY_PLATFORM
- OSX_RUBY_RE =
%r{\A/System/Library/Frameworks/Ruby.framework/Versions/.*?/usr/bin/ruby\Z}
Instance Attribute Summary collapse
-
#ruby_command ⇒ Object
Returns correct command for invoking the current Ruby interpreter.
-
#ruby_executable ⇒ Object
Returns the full path to the current Ruby interpreter’s executable file.
Class Method Summary collapse
Instance Method Summary collapse
-
#command_for_ruby_tool(name) ⇒ Object
Returns the correct command string for invoking the
name
executable that belongs to the current Ruby interpreter. -
#locate_ruby_tool(name, extensions = ['', CONFIG['EXEEXT']].compact.uniq) ⇒ Object
Locates a Ruby tool command
name
, e.g. - #ruby_options_from_hash(hash, argv = []) ⇒ Object
- #ruby_options_to_argv(args, ruby_command = ruby_command()) ⇒ Object
Instance Attribute Details
#ruby_command ⇒ Object
Returns correct command for invoking the current Ruby interpreter.
72 73 74 |
# File 'lib/nuggets/ruby_mixin.rb', line 72 def ruby_command defined?(@ruby_command) ? @ruby_command : @ruby_command = ruby_executable end |
#ruby_executable ⇒ Object
Returns the full path to the current Ruby interpreter’s executable file. This might not be the actual correct command to use for invoking the Ruby interpreter; use ruby_command instead.
81 82 83 84 85 86 |
# File 'lib/nuggets/ruby_mixin.rb', line 81 def ruby_executable @ruby_executable ||= begin dir, name, ext = CONFIG.values_at(*%w[bindir RUBY_INSTALL_NAME EXEEXT]) ::File.join(dir, name + ext).sub(/.*\s.*/m, '"\&"') end end |
Class Method Details
.define_ruby_tool(name) ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/nuggets/ruby_mixin.rb', line 127 def self.define_ruby_tool(name) class_eval <<-EOT, __FILE__, __LINE__ + 1 def #{name} @#{name} ||= locate_ruby_tool('#{name}') end attr_writer :#{name} def #{name}_command @#{name}_command ||= command_for_ruby_tool('#{name}') end attr_writer :#{name}_command EOT end |
Instance Method Details
#command_for_ruby_tool(name) ⇒ Object
Returns the correct command string for invoking the name
executable that belongs to the current Ruby interpreter. Returns nil
if the command is not found.
If the command executable is a Ruby program, then we need to run it in the correct Ruby interpreter just in case the command doesn’t have the correct shebang line; we don’t want a totally different Ruby than the current one to be invoked.
122 123 124 125 |
# File 'lib/nuggets/ruby_mixin.rb', line 122 def command_for_ruby_tool(name) filename = respond_to?(name) ? send(name) : locate_ruby_tool(name) shebang_command(filename) =~ /ruby/ ? "#{ruby_command} #{filename}" : filename end |
#locate_ruby_tool(name, extensions = ['', CONFIG['EXEEXT']].compact.uniq) ⇒ Object
Locates a Ruby tool command name
, e.g. ‘gem’, ‘rake’, ‘bundle’, etc. Instead of naively looking in $PATH, this function uses a variety of search heuristics to find the command that’s really associated with the current Ruby interpreter. It should never locate a command that’s actually associated with a different Ruby interpreter.
NOTE: The return value may not be the actual correct invocation for the tool. Use command_for_ruby_tool for that.
Returns nil
when nothing’s found.
100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/nuggets/ruby_mixin.rb', line 100 def locate_ruby_tool(name, extensions = ['', CONFIG['EXEEXT']].compact.uniq) # Deduce Ruby's --program-prefix and --program-suffix from its install name # and transform the given input name accordingly. # # "rake" => "jrake", "rake1.8", etc. [name, CONFIG['RUBY_INSTALL_NAME'].sub('ruby', name)].uniq.each { |basename| extensions.each { |ext| result = locate_ruby_tool_by_basename(basename + ext) and return result } } nil end |
#ruby_options_from_hash(hash, argv = []) ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/nuggets/ruby_mixin.rb', line 153 def (hash, argv = []) hash.each { |key, val| opt = "-#{key.to_s[0, 1]}" if val.is_a?(::Array) val.each { |v| argv << opt << v.to_s } elsif opt == '-e' argv << opt << val.to_s elsif val != false argv << "#{opt}#{val unless val == true}" end } argv end |
#ruby_options_to_argv(args, ruby_command = ruby_command()) ⇒ Object
145 146 147 148 149 150 151 |
# File 'lib/nuggets/ruby_mixin.rb', line 145 def (args, ruby_command = ruby_command()) argv = [ruby_command] (args.pop, argv) if args.last.is_a?(::Hash) argv.concat(args.map! { |arg| arg.to_s.strip }) end |