Class: Mercurial::Shell
- Inherits:
-
Object
- Object
- Mercurial::Shell
- Defined in:
- lib/mercurial-ruby/shell.rb
Overview
This class makes it easy for you to execute shell commands. Use it to execute hg commands that don’t have proper wrappers yet.
Instance Attribute Summary collapse
-
#repository ⇒ Object
readonly
Returns the value of attribute repository.
Class Method Summary collapse
-
.run(cmd, options = {}) ⇒ Object
Creates a Command instance and executes it.
Instance Method Summary collapse
-
#hg(cmd, options = {}) ⇒ Object
This method is a shortcut to Shell.run(cmd, :append_hg => true, :in => repository.path).
-
#initialize(repository) ⇒ Shell
constructor
A new instance of Shell.
- #run(cmd, options = {}) ⇒ Object
Constructor Details
#initialize(repository) ⇒ Shell
Returns a new instance of Shell.
79 80 81 |
# File 'lib/mercurial-ruby/shell.rb', line 79 def initialize(repository) @repository = repository end |
Instance Attribute Details
#repository ⇒ Object (readonly)
Returns the value of attribute repository.
9 10 11 |
# File 'lib/mercurial-ruby/shell.rb', line 9 def repository @repository end |
Class Method Details
.run(cmd, options = {}) ⇒ Object
Creates a Command instance and executes it. Supports a few neat options:
:append_hg
You don’t have to worry about specifying a correct path to your hg binary every time you want to execute a command:
Shell.run("help", :append_hg => true)
Arguments interpolation
Interpolation make your commands secure by wrapping everything in single quotes and sanitizing them:
Shell.run(["clone ? ?", url, destination], :append_hg => true)
:in
Allows you to execute commands in a specific directory:
Shell.run('log', :in => repository_path)
:pipe
Gives you piping flexibility:
Shell.run('log', :pipe => "grep '9:0f41dd2ec166' -wc", :in => repository_path, :append_hg => true)
Same as running hg log | grep ‘9:0f41dd2ec166’ -wc
:timeout
Specify execution timeout in seconds for your command:
Shell.run("/usr/bin/long-running-task", :timeout => 5)
Timeout::Error
will be raised on timeout.
:cache
Caching is enabled by default for all commands. Use this setting to avoid it:
Shell.run("init", :append_hg => true, :cache => false)
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/mercurial-ruby/shell.rb', line 54 def self.run(cmd, ={}) build = [] if [:append_hg] cmd = append_command_with(hg_binary_path, cmd) end if dir = .delete(:in) build << interpolate_arguments(["cd ?", dir]) end if cmd.kind_of?(Array) cmd = interpolate_arguments(cmd) end build << cmd to_run = build.join(' && ') if pipe_cmd = [:pipe] to_run << " | #{ pipe_cmd }" end Mercurial::Command.new(to_run, ).execute end |
Instance Method Details
#hg(cmd, options = {}) ⇒ Object
This method is a shortcut to Shell.run(cmd, :append_hg => true, :in => repository.path).
86 87 88 89 90 91 |
# File 'lib/mercurial-ruby/shell.rb', line 86 def hg(cmd, ={}) [:in] ||= repository.path [:repository] = repository [:append_hg] = true run(cmd, ) end |
#run(cmd, options = {}) ⇒ Object
93 94 95 |
# File 'lib/mercurial-ruby/shell.rb', line 93 def run(cmd, ={}) self.class.run(cmd, ) end |