module Ripl::ShellCommands
Allows for executing shell commands prefixed by a ‘!`.
Constants
- BLACKLIST
Blacklist of known commands that conflict with Ruby keywords.
- BUILTIN
Builtin commands
- EXECUTABLES
Names and statuses of executables.
- PATHS
The directories of ‘$PATH`.
- PATTERN
Regexp to recognize ‘!commands`.
Public Class Methods
Equivalent of the ‘cd` command, using `Dir.chdir`.
@param [Array<String>] arguments
The arguments of the command.
@return [Boolean]
Specifies whether the directory change was successful.
# File lib/ripl/shell_commands.rb, line 115 def self.cd(*arguments) old_pwd = Dir.pwd new_cwd = if arguments.empty? unless ENV['HOME'] warn "cd: HOME not set" return false end ENV['HOME'] elsif arguments.first == '-' unless ENV['OLDPWD'] warn 'cd: OLDPWD not set' return false end ENV['OLDPWD'] else arguments.first end Dir.chdir(new_cwd) ENV['OLDPWD'] = old_pwd return true end
Default command which executes a command in the shell.
@param [Array<String>] arguments
The arguments of the command.
@return [Boolean]
Specifies whether the command exited successfully.
# File lib/ripl/shell_commands.rb, line 102 def self.exec(*arguments) system(arguments.join(' ')) end
Determines if an executable exists on the system.
@param [String] path
The program path.
@return [Boolean]
Specifies whether the executable exists.
# File lib/ripl/shell_commands.rb, line 89 def self.executable?(path) File.file?(path) && File.executable?(path) end
Equivalent of the ‘export` or `set` commands.
@param [Array<String>] arguments
The arguments of the command.
@return [true]
# File lib/ripl/shell_commands.rb, line 149 def self.export(*arguments) arguments.each do |pair| name, value = pair.split('=',2) ENV[name] = value end end
Parses a Console command.
@param [String] command
The Console command to parse.
@return [String, Array<String>]
The command name and additional arguments.
# File lib/ripl/shell_commands.rb, line 68 def self.parse(command) # evaluate embedded Ruby expressions command = command.gsub(/\#\{[^\}]*\}/) do |expression| eval(expression[2..-2],Ripl.shell.binding).to_s.dump end arguments = Shellwords.shellsplit(command) name = arguments.shift return name, arguments end
Public Instance Methods
Dynamically execute shell commands, instead of Ruby.
@param [String] input
The input from the console.
# File lib/ripl/shell_commands.rb, line 40 def loop_eval(input) if (@buffer.nil? && input =~ PATTERN) command = input[1..-1] name, arguments = ShellCommands.parse(command) unless BLACKLIST.include?(name) if BUILTIN.include?(name) arguments ||= [] return ShellCommands.send(name,*arguments) elsif EXECUTABLES[name] return ShellCommands.exec(name,*arguments) end end end super(input) end