Sha256: 8b9ff51f40ba2ce776a6f35561c93eec35aece1eee402a62fe9a7a5e497c38a1

Contents?: true

Size: 1.84 KB

Versions: 6

Compression:

Stored size: 1.84 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 => error
        error.message =~ /-- (\S+)\Z/
        $1 or raise # could not extract a gem name from the error message, re-raise the error

        gem_name = $1.strip
        install_command = 'gem install ' + gem_name

        # install missing gem
        warn 'Probably missing gem: ' + gem_name
        prompt('Install it now?', 'y', /y|yes/) or fail 'Missing Gems.'
        system! install_command, :show_cmd => true

        # retry
        Gem.clear_paths
        note 'Retrying ...'
        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

6 entries across 6 versions & 1 rubygems

Version Path
geordi-1.1.4 lib/geordi/util.rb
geordi-1.1.3 lib/geordi/util.rb
geordi-1.1.2 lib/geordi/util.rb
geordi-1.1.1 lib/geordi/util.rb
geordi-1.1.0 lib/geordi/util.rb
geordi-1.0.3 lib/geordi/util.rb