module SyncWrap::RubySupport

A Support module for Ruby VM components, also providing gem handling utilities which are largely common to all Ruby VMs.

Attributes

gem_command[RW]

The name of the gem command to be installed/used (default: gem)

gem_install_args[RW]

Default gem install arguments (default: –no-rdoc, –no-ri)

ruby_command[RW]

Ruby VM command name (default: ruby; alt example: jruby)

Public Class Methods

new( *args ) click to toggle source
Calls superclass method
# File lib/syncwrap/ruby_support.rb, line 32
def initialize( *args )
  @ruby_command = 'ruby'
  @gem_command = 'gem'
  @gem_install_args = %w[ --no-rdoc --no-ri ]

  super( *args )
end

Public Instance Methods

gem_install( gem, opts = {} ) click to toggle source

Install the specified gem.

Options

:version

Version specifier array or single value, like in a gemspec. (Default: nil -> latest) Examples:

'1.1.0'
'~> 1.1'
['>=1.0', '<1.2']
:user_install

Perform a –user-install if true, as the indicated user if a String or as the login user. Otherwise system install with sudo (the default, false).

:check

If true, capture output and return the number of gems actually installed. Combine with :minimize to only install what is required, and short circuit when zero gems installed. (Default: false)

:minimize

Use –conservative and –minimal-deps (rubygems 2.1.5+, min_deps_supported?) flags to reduce installs to the minimum required to satisfy the version requirements. (Default: true)

:format_executable

Use –format-executable to prefix commands for specific ruby VMs if needed.

# File lib/syncwrap/ruby_support.rb, line 78
def gem_install( gem, opts = {} )
  cmd = [ gem_command, 'install',
          gem_install_args,
          ( '--user-install' if opts[ :user_install ] ),
          ( '--format-executable' if opts[ :format_executable ] ),
          ( '--conservative' if opts[ :minimize] != false ),
          ( '--minimal-deps' if opts[ :minimize] != false &&
            min_deps_supported? ),
          gem_version_flags( opts[ :version ] ),
          gem ].flatten.compact.join( ' ' )

  shopts = {}

  case opts[ :user_install ]
  when String
    shopts[ :user ] = opts[ :user_install ]
  when nil, false
    shopts[ :user ] = :root
  end

  clean_env( opts[ :user_install ] ) do
    if opts[ :check ]
      _,out = capture( cmd, shopts )

      count = 0
      out.split( "\n" ).each do |oline|
        if oline =~ /^\s*(\d+)\s+gem(s)?\s+installed/
          count = $1.to_i
        end
      end
      count
    else
      sh( cmd, shopts )
    end
  end

end
gemrc_path() click to toggle source
# File lib/syncwrap/ruby_support.rb, line 40
def gemrc_path
  "/etc/gemrc"
end
install_gemrc() click to toggle source

Install gemrc file to #gemrc_path

# File lib/syncwrap/ruby_support.rb, line 45
def install_gemrc
  rput( 'etc/gemrc', gemrc_path, user: :root )
end

Protected Instance Methods

clean_env( doit ) { || ... } click to toggle source

Execute within Bundler clean environment if Bundler is defined, doit is passed true (i.e. :user_install, sudo restricted environment should also avoid the issue), and running on localhost. Otherwise #gem_install may fail attempting to reload the wrong bundle/r in the shell sub-process.

# File lib/syncwrap/ruby_support.rb, line 131
def clean_env( doit )
  ret = nil
  if defined?( ::Bundler ) && doit && host.name.to_s == 'localhost'
    ::Bundler.with_clean_env do
      # Oddly, GEM_HOME remains in clean_env even when bundler
      # adds it. Unfortunately now it is hard to tell whom added
      # it.  Best guess given not using RVM, etc. is to delete and
      # let return from block restore it.
      ENV.delete( 'GEM_HOME' )
      ret = yield
      flush # otherwise may be deferred till outside of clean block
    end
  else
    ret = yield
  end
  ret
end
gem_version_flags( reqs ) click to toggle source
# File lib/syncwrap/ruby_support.rb, line 122
def gem_version_flags( reqs )
  Array( reqs ).flatten.compact.map { |req| "-v'#{req}'" }
end
min_deps_supported?() click to toggle source
# File lib/syncwrap/ruby_support.rb, line 118
def min_deps_supported?
  true
end