Class: Gem::Tasks::Console

Inherits:
Task
  • Object
show all
Defined in:
lib/rubygems/tasks/console.rb

Overview

The console task.

Constant Summary collapse

DEFAULT_CONSOLE =

The default Interactive Ruby Console

'irb'
DEFAULT_COMMAND =

The default command to run

(ENV['RUBYCONSOLE'] || DEFAULT_CONSOLE)

Constants included from Printing

Printing::ANSI_BRIGHT, Printing::ANSI_CLEAR, Printing::ANSI_GREEN, Printing::ANSI_RED, Printing::ANSI_YELLOW, Printing::DEBUG_PREFIX, Printing::ERROR_PREFIX, Printing::STATUS_PREFIX

Instance Attribute Summary collapse

Attributes inherited from Task

#project

Instance Method Summary collapse

Methods inherited from Task

#bundle, #gem, #gemspec_tasks, #invoke, #namespaced_tasks, #run, #task?

Methods included from Printing

#debug, #error, #status

Constructor Details

#initialize(options = {}) {|_self| ... } ⇒ Console

Initializes the console task.

Parameters:

  • options (Hash) (defaults to: {})

    Additional options.

Options Hash (options):

  • :command (String) — default: DEFAULT_COMMAND

    The Ruby Console command to run.

  • :options (Array)

    Additional options for the Ruby Console.

Yields:

  • (_self)

Yield Parameters:


34
35
36
37
38
39
40
41
42
# File 'lib/rubygems/tasks/console.rb', line 34

def initialize(options={})
  super()

  @command = options.fetch(:command,DEFAULT_COMMAND)
  @options = Array(options[:options])

  yield self if block_given?
  define
end

Instance Attribute Details

#commandObject

The Ruby Console command


17
18
19
# File 'lib/rubygems/tasks/console.rb', line 17

def command
  @command
end

#optionsObject

Additional options for the Ruby Console


20
21
22
# File 'lib/rubygems/tasks/console.rb', line 20

def options
  @options
end

Instance Method Details

#console(name = nil) ⇒ Array<String>

Builds the complete arguments for the console command.

Parameters:

  • name (Symbol, String) (defaults to: nil)

    The name of the gemspec to load.

Returns:

  • (Array<String>)

    The arguments for the console command.


69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/rubygems/tasks/console.rb', line 69

def console(name=nil)
  gemspec = @project.gemspec(name)

  require_paths = gemspec.require_paths
  require_file  = gemspec.name.gsub('-',File::SEPARATOR)

  arguments = [@command]

  # add -I options for lib/ or ext/ directories
  arguments.push(*require_paths.map { |dir| "-I#{dir}" })

  # add an -r option to require the library
  arguments.push('-r' + require_file)

  # push on additional options
  arguments.push(*@options)

  if @project.bundler?
    # run under `bundle exec`
    arguments.unshift('bundle', 'exec')
  end

  return run(*arguments)
end

#defineObject

Defines the console task.


47
48
49
50
51
52
53
54
55
56
# File 'lib/rubygems/tasks/console.rb', line 47

def define
  @project.gemspecs.each_key do |name|
    namespace :console do
      task(name) { console(name) }
    end
  end

  desc "Spawns an Interactive Ruby Console"
  task :console => "console:#{@project.primary_gemspec}"
end