Module: Ripl::ShellCommands
- Defined in:
- lib/ripl/shell_commands.rb
Overview
Allows for executing shell commands prefixed by a !
.
Constant Summary collapse
- PATHS =
The directories of
$PATH
. ENV['PATH'].split(File::PATH_SEPARATOR)
- EXECUTABLES =
Names and statuses of executables.
Hash.new do |hash,key| hash[key] = executable?(key) || PATHS.any? do |dir| executable?(File.join(dir,key)) end end
- BUILTIN =
Builtin commands
- BLACKLIST =
Blacklist of known commands that conflict with Ruby keywords.
- PATTERN =
Regexp to recognize
!commands
. /^![a-zA-Z][a-zA-Z0-9\._-]*/
Class Method Summary collapse
-
.cd(*arguments) ⇒ Boolean
Equivalent of the
cd
command, usingDir.chdir
. -
.exec(*arguments) ⇒ Boolean
Default command which executes a command in the shell.
-
.executable?(path) ⇒ Boolean
Determines if an executable exists on the system.
-
.export(*arguments) ⇒ true
Equivalent of the
export
orset
commands. -
.parse(command) ⇒ String+
Parses a Console command.
Instance Method Summary collapse
-
#loop_eval(input) ⇒ Object
Dynamically execute shell commands, instead of Ruby.
Class Method Details
.cd(*arguments) ⇒ Boolean
Equivalent of the cd
command, using Dir.chdir
.
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 |
# 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 |
.exec(*arguments) ⇒ Boolean
Default command which executes a command in the shell.
102 103 104 |
# File 'lib/ripl/shell_commands.rb', line 102 def self.exec(*arguments) system(arguments.join(' ')) end |
.executable?(path) ⇒ Boolean
Determines if an executable exists on the system.
89 90 91 |
# File 'lib/ripl/shell_commands.rb', line 89 def self.executable?(path) File.file?(path) && File.executable?(path) end |
.export(*arguments) ⇒ true
Equivalent of the export
or set
commands.
149 150 151 152 153 154 155 |
# 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 |
.parse(command) ⇒ String+
Parses a Console command.
68 69 70 71 72 73 74 75 76 77 78 |
# 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 |
Instance Method Details
#loop_eval(input) ⇒ Object
Dynamically execute shell commands, instead of Ruby.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# 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 |