Module: Rib::Runner

Defined in:
lib/rib/runner.rb

Class Method Summary collapse

Class Method Details

.command_descriptionsObject



51
52
53
54
55
56
57
58
59
# File 'lib/rib/runner.rb', line 51

def command_descriptions
  @command_descriptions ||=
  {'all'    => 'Load all recommended plugins'                ,
   'min'    => 'Run the minimum essence'                     ,
   'auto'   => 'Run as Rails or Ramaze console (auto-detect)',
   'rails'  => 'Run as Rails console'                        ,
   'ramaze' => 'Run as Ramaze console'                       ,
   'rack'   => 'Run as Rack console'                         }
end

.command_descriptions_find(path) ⇒ Object

Extract the text below __END__ in the bin file as the description



62
63
64
65
# File 'lib/rib/runner.rb', line 62

def command_descriptions_find path
  File.read(path) =~ /Gem\.bin_path\(['"](.+)['"], ['"](.+)['"],/
  (File.read(Gem.bin_path($1, $2))[/\n__END__\n(.+)$/m, 1] || '').strip
end

.command_pathsObject



41
42
43
44
45
46
47
48
49
# File 'lib/rib/runner.rb', line 41

def command_paths
  @command_paths ||=
  Gem.path.map{ |path|
    Dir["#{path}/bin/*"].map{ |f|
      (File.executable?(f) && File.basename(f) =~ /^rib\-.+$/ && f) ||
       nil    # a trick to make false to be nil and then
    }.compact # this compact could eliminate them
  }.flatten
end

.commandsObject



33
34
35
36
37
38
39
# File 'lib/rib/runner.rb', line 33

def commands
   @commands ||=
    command_paths.map{ |path|
      name = File.basename(path)[/^rib\-(.+)$/, 1]
      [name, command_descriptions[name]      ||
             command_descriptions_find(path) || ' '] }
end

.helpObject



151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/rib/runner.rb', line 151

def help
  optt = options.transpose
  maxn = optt.first.map(&:size).max
  maxd = optt.last .map(&:size).max
  "Usage: #{Rib.config[:name]}"                    \
  " [ruby OPTIONS] [rib OPTIONS] [rib COMMANDS]\n" +
  options.map{ |(name, desc)|
    if name.end_with?(':')
      name
    else
      sprintf("  %-*s  %-*s", maxn, name, maxd, desc)
    end
  }.join("\n")
end

.load_command(command) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/rib/runner.rb', line 166

def load_command command
  bin  = "rib-#{command}"
  path = which_bin(bin)
  if path == ''
    Rib.warn(
      "Can't find #{bin} in $PATH. Please make sure it is installed,",
      "or is there any typo? You can try this to install it:\n"      ,
      "    gem install #{bin}")
  else
    Rib.config[:name] = bin
    load(path)
  end
end

.loop(retry_times = 5) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/rib/runner.rb', line 80

def loop retry_times=5
  Rib.shell.loop
rescue => e
  if retry_times <= 0
    Rib.warn("Error: #{e}. Too many retries, give up.")
  elsif Rib.shells.last.running?
    Rib.warn("Error: #{e}. Relaunching a new shell... ##{retry_times}")
    Rib.warn("Backtrace: #{e.backtrace}") if $VERBOSE
    Rib.shells.pop
    Rib.shells << Rib::Shell.new(Rib.config)
    retry_times -= 1
    retry
  else
    Rib.warn("Error: #{e}. Closing.")
    Rib.warn("Backtrace: #{e.backtrace}") if $VERBOSE
  end
end

.optionsObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rib/runner.rb', line 6

def options
  @options ||=
  [['ruby options:'    , ''                                        ],
   ['-e, --eval LINE'                                               ,
    'Evaluate a LINE of code'                                      ],

   ['-d, --debug'                                                   ,
    'Set debugging flags (set $DEBUG to true)'                     ],

   ['-w, --warn'                                                    ,
     'Turn warnings on (set $-w and $VERBOSE to true)'             ],

   ['-I, --include PATH'                                            ,
     'Specify $LOAD_PATH (may be used more than once)'             ],

   ['-r, --require LIBRARY'                                         ,
     'Require the library, before executing your script'           ],

   ['rib options:'     , ''                                        ],
   ['-c, --config FILE', 'Load config from FILE'                   ],
   ['-n, --no-config'  , 'Suppress loading ~/.config/rib/config.rb'],
   ['-h, --help'       , 'Print this message'                      ],
   ['-v, --version'    , 'Print the version'                       ]] +

  [['rib commands:'    , '']] + commands
end

.parse(argv) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/rib/runner.rb', line 98

def parse argv
  unused = []
  until argv.empty?
    case arg = argv.shift
    when /^-e=?(.+)?/, /^--eval=?(.+)?/
      Rib.shell.eval_binding.eval(
        $1 || argv.shift || '', __FILE__, __LINE__)

    when /^-d/, '--debug'
      $DEBUG = true
      parse_next(argv, arg)

    when /^-w/, '--warn'
      $-w, $VERBOSE = true, true
      parse_next(argv, arg)

    when /^-I=?(.+)?/, /^--include=?(.+)?/
      paths = ($1 || argv.shift).split(':')
      $LOAD_PATH.unshift(*paths)

    when /^-r=?(.+)?/, /^--require=?(.+)?/
      require($1 || argv.shift)

    when /^-c=?(.+)?/, /^--config=?(.+)?/
      Rib.config[:config] = $1 || argv.shift

    when /^-n/, '--no-config'
      Rib.config.delete(:config)
      parse_next(argv, arg)

    when /^-h/, '--help'
      puts(help)
      exit

    when /^-v/, '--version'
      require 'rib/version'
      puts(Rib::VERSION)
      exit

    when /^[^-]/
      load_command(arg)

    else
      unused << arg
    end
  end
  unused
end

.parse_next(argv, arg) ⇒ Object



147
148
149
# File 'lib/rib/runner.rb', line 147

def parse_next argv, arg
  argv.unshift("-#{arg[2..-1]}") if arg.size > 2
end

.run(argv = ARGV) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/rib/runner.rb', line 67

def run argv=ARGV
  (@running_commands ||= []) << Rib.config[:name]
  unused = parse(argv)
  # if it's running a Rib command, the loop would be inside Rib itself
  # so here we only parse args for the command
  return if @running_commands.pop != 'rib'
  # by coming to this line, it means now we're running Rib main loop,
  # not any other Rib command
  Rib.warn("Unused arguments: #{unused.inspect}") unless unused.empty?
  require 'rib/core' if Rib.config.delete(:mimic_irb)
  loop
end

.which_bin(bin) ⇒ Object

handle windows here



180
181
182
183
184
# File 'lib/rib/runner.rb', line 180

def which_bin bin # handle windows here
  `which #{bin}`.strip
rescue Errno::ENOENT # probably a windows platform, try where
  `where #{bin}`.lines.first.strip
end