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(command: DEFAULT_COMMAND, options: []) {|_self| ... } ⇒ Console

Initializes the console task.

Parameters:

  • command (String) (defaults to: DEFAULT_COMMAND)

    The Ruby Console command to run.

  • options (Array<String>) (defaults to: [])

    Additional options for the Ruby Console.

Yields:

  • (_self)

Yield Parameters:



37
38
39
40
41
42
43
44
45
# File 'lib/rubygems/tasks/console.rb', line 37

def initialize(command: DEFAULT_COMMAND, options: [])
  super()

  @command = command
  @options = options

  yield self if block_given?
  define
end

Instance Attribute Details

#commandString

The Ruby Console command

Returns:

  • (String)


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

def command
  @command
end

#optionsArray<String>

Additional options for the Ruby Console

Returns:

  • (Array<String>)


26
27
28
# File 'lib/rubygems/tasks/console.rb', line 26

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.



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

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.



50
51
52
53
54
55
56
57
58
59
# File 'lib/rubygems/tasks/console.rb', line 50

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