Sha256: ca64b769f033da3160c236324a7decd69efb60bf18a0891ed5226683ee1980cc

Contents?: true

Size: 1.7 KB

Versions: 3

Compression:

Stored size: 1.7 KB

Contents

require 'geordi/interaction'

module Geordi
  class Util
    class << self
      include Geordi::Interaction

      # Geordi commands sometimes require external gems. However, we don't want
      # all employed gems as runtime dependencies because that would
      # unnecessarily slow down all commands.
      # Thus, we have this handy method here.
      def installing_missing_gems(&block)
        yield
      rescue LoadError => e
        gem_name = e.message.split('--').last.strip
        install_command = 'gem install ' + gem_name

        # install missing gem
        warn 'Probably missing gem: ' + gem_name
        wait 'Auto-install it?'
        system! install_command, :show_cmd => true

        # retry
        Gem.clear_paths
        note 'Trying again ...'
        require gem_name
        retry
      end

      # Run a command with a clean environment.
      # Print an error message and exit if the command fails.
      #
      # Options: show_cmd, fail_message
      def system!(*commands)
        options = commands.last.is_a?(Hash) ? commands.pop : {}
        note_cmd commands.join(' ') if options[:show_cmd]

        # Remove Geordi's Bundler environment when running commands.
        Bundler.clean_system(*commands) or fail(options[:fail_message] || 'Something went wrong.')
      end

      def console_command(environment)
        if File.exists?('script/console')
          'script/console ' + environment # Rails 2
        else
          'bundle exec rails console ' + environment
        end
      end

      def server_command
        if File.exists?('script/server')
          'script/server' # Rails 2
        else
          'bundle exec rails server' # Rails 3+
        end
      end

    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
geordi-1.0.2 lib/geordi/util.rb
geordi-1.0.1 lib/geordi/util.rb
geordi-1.0.0 lib/geordi/util.rb