Class: AutomateIt::ShellManager
- Inherits:
-
Plugin::Manager
- Object
- Common
- Plugin::Base
- Plugin::Manager
- AutomateIt::ShellManager
- Defined in:
- lib/automateit/shell_manager.rb
Overview
ShellManager
The ShellManager provides Unix-like shell commands for manipulating files and executing commands.
WARNING: Previewing code can be dangerous. Read previews.txt for instructions on how to write code that can be safely previewed.
Defined Under Namespace
Classes: BaseDriver, BaseLink, Link, Portable, Symlink, WhichBase, WhichUnix, WhichWindows
Constant Summary
Constants included from Constants
Constants::HELPERS_DIR, Constants::INSTALL_DIR, Constants::PERROR, Constants::PEXEC, Constants::PNOTE, Constants::WARNING_BOILERPLATE
Instance Attribute Summary
Attributes inherited from Plugin::Manager
Attributes inherited from Common
Instance Method Summary collapse
-
#backup(*sources) ⇒ Object
Backup
sources
if they exist. -
#cd(dir, opts = {}, &block) ⇒ Object
Changes the directory into the specified
dir
. -
#chmod(mode, targets, opts = {}) ⇒ Object
Change the permission
mode
of thetargets
. -
#chmod_R(mode, targets, opts = {}) ⇒ Object
Change the permission
mode
of thetargets
recursively. -
#chown(user, group, targets, opts = {}) ⇒ Object
Change the
user
andgroup
ownership of thetargets
. -
#chown_R(user, group, targets, opts = {}) ⇒ Object
Change the
user
andgroup
ownership of thetargets
recursively. -
#chperm(targets, opts = {}) ⇒ Object
Change the permissions of the
targets
. -
#cp(sources, target, opts = {}) ⇒ Object
Copy the
sources
to thetarget
. -
#cp_r(sources, target, opts = {}) ⇒ Object
Copy the
sources
to thetarget
recursively. -
#cp_R(sources, target, opts = {}) ⇒ Object
Copy the
sources
to thetarget
recursively. -
#install(source, target, mode) ⇒ Object
Copy the
source
to thetarget
and set itsmode
. -
#ln(source, target, opts = {}) ⇒ Object
Create a hard link between the
source
andtarget
. -
#ln_s(sources, target, opts = {}) ⇒ Object
Create a symbolic link between the
sources
andtarget
. -
#ln_sf(sources, target, opts = {}) ⇒ Object
Create a symbolic link between the
sources
andtarget
. -
#mkdir(dirs, opts = {}, &block) ⇒ Object
Create a directory or directories.
-
#mkdir_p(dirs, opts = {}, &block) ⇒ Object
Create a directory or directories with their parents.
-
#mktemp(name = nil, &block) ⇒ Object
Creates a temporary file.
-
#mktempdir(name = nil, &block) ⇒ Object
Creates a temporary directory.
-
#mktempdircd(name = nil, &block) ⇒ Object
Same as #mktempdir but performs a #cd into the directory for the duration of the block.
-
#mv(sources, target) ⇒ Object
Move the
sources
to thetarget
. -
#provides_link? ⇒ Boolean
See ShellManager#provides_mode?.
-
#provides_mode? ⇒ Boolean
See ShellManager#provides_mode?.
-
#provides_ownership? ⇒ Boolean
See ShellManager#provides_mode?.
-
#provides_symlink? ⇒ Boolean
See ShellManager#provides_mode?.
-
#pwd ⇒ Object
Returns the current directory.
-
#rm(targets, opts = {}) ⇒ Object
Remove the
targets
. -
#rm_r(targets, opts = {}) ⇒ Object
Remove the
targets
recursively. -
#rm_rf(targets, opts = {}) ⇒ Object
Remove the
targets
recursively and forcefully. -
#rmdir(dirs) ⇒ Object
Remove a directory or directories.
-
#sh(*commands) ⇒ Object
Execute a shell command.
-
#touch(targets, opts = {}) ⇒ Object
Create the
targets
as files if needed and update their modification time. -
#umask(mode = nil, &block) ⇒ Object
Set the umask to
mode
. -
#which(command) ⇒ Object
What is the path for this command? Returns
nil
if command isn’t found. -
#which!(command) ⇒ Object
Same as #which but throws an ArgumentError if command isn’t found.
Methods inherited from Plugin::Manager
#[], abstract_manager, alias_methods, #available?, #default, #default=, #dispatch, #dispatch_safely, #dispatch_safely_to, #dispatch_to, driver_classes, #driver_for, #driver_suitability_levels_for, inherited, #instantiate_drivers, #setup
Methods inherited from Plugin::Base
Methods inherited from Common
#initialize, #log, #nitpick, #noop, #noop=, #noop?, #preview, #preview=, #preview?, #preview_for, #setup, #superuser?, #writing, #writing=, #writing?
Constructor Details
This class inherits a constructor from AutomateIt::Common
Instance Method Details
#backup(*sources) ⇒ Object
Backup sources
if they exist. Returns the names of the backups created.
Options:
-
:quiet – Don’t display output? Default is false.
These backups are copies of the original sources saved into the same directories as the originals. The pathnames of these copies are timestamped and guaranteed to be unique, so you can have multiple backups of the same sources.
WARNING: This method is not conditional. It will make a backup every time it’s called if the sources exist. Therefore, only execute this method when its needed.
For example, backup a file:
backup("/tmp/myfile") # => "/tmp/myfile.1190994237_M2xhLrC6Sj.bak
In the above example, the backup’s name contains two special strings. The “1190994237” is the time the backup was made in seconds since the Epoch. The “M2xhLrC6Sj” is a random string used to guarantee the uniqueness of this backup in case two are made at exactly the same time.
51 |
# File 'lib/automateit/shell_manager.rb', line 51 def backup(*sources) dispatch(*sources) end |
#cd(dir, opts = {}, &block) ⇒ Object
Changes the directory into the specified dir
. If called with a block, changes to the directory for the duration of the block, and then changes back to the previous directory at the end.
WARNING: Previewing code can be dangerous. Read previews.txt for instructions on how to write code that can be safely previewed.
143 |
# File 'lib/automateit/shell_manager.rb', line 143 def cd(dir, opts={}, &block) dispatch(dir, opts, &block) end |
#chmod(mode, targets, opts = {}) ⇒ Object
Change the permission mode
of the targets
. Returns an array of targets modified or false
if all have the desired mode.
238 |
# File 'lib/automateit/shell_manager.rb', line 238 def chmod(mode, targets, opts={}) dispatch(mode, targets, opts) end |
#chmod_R(mode, targets, opts = {}) ⇒ Object
Change the permission mode
of the targets
recursively. Returns an array of targets modified or false
if all have the desired mode.
242 |
# File 'lib/automateit/shell_manager.rb', line 242 def chmod_R(mode, targets, opts={}) dispatch(mode, targets, opts) end |
#chown(user, group, targets, opts = {}) ⇒ Object
Change the user
and group
ownership of the targets
. You can leave either the user or group as nil if you don’t want to change it. Returns an array of targets modified or false
if all have the desired ownership.
248 |
# File 'lib/automateit/shell_manager.rb', line 248 def chown(user, group, targets, opts={}) dispatch(user, group, targets, opts) end |
#chown_R(user, group, targets, opts = {}) ⇒ Object
Change the user
and group
ownership of the targets
recursively. You can leave either the user or group as nil if you don’t want to change it. Returns an array of targets modified or false
if all have the desired ownership.
254 |
# File 'lib/automateit/shell_manager.rb', line 254 def chown_R(user, group, targets, opts={}) dispatch(user, group, targets, opts) end |
#chperm(targets, opts = {}) ⇒ Object
Change the permissions of the targets
. This command is like the #chmod and #chown in a single command.
Options:
-
:recursive – Change files and directories recursively. Defaults to false.
-
:user – User name to change ownership to.
-
:group – Group name to change ownership to.
-
:mode – Mode to use as octal, e.g.,
0400
to make a file readable only to its owner. -
:details – Reports the files modified, rather than the arguments modified. An argument might be a single directory, but this may result in modifications to many files within that directory. Use :details for situations when there’s a need to see all files actually changed. The reason :details is off by default is that it will flood the screen with a list of all files modified in a large directory, which is overwhelming and probably unnecessary unless you actually need to see these details. Defaults to false.
127 |
# File 'lib/automateit/shell_manager.rb', line 127 def chperm(targets, opts={}) dispatch(targets, opts) end |
#cp(sources, target, opts = {}) ⇒ Object
Copy the sources
to the target
. Returns an array of sources copied or false
if all are present.
Options:
-
:preserve – Preserve file modification time and ownership. Defaults to false. Can be
true
,false
, or :try. If :try, the properties will be preserved if possible on the platform, whereastrue
will raise an exception if not available. -
:recursive – Copy files and directories recursively, boolean.
206 |
# File 'lib/automateit/shell_manager.rb', line 206 def cp(sources, target, opts={}) dispatch(sources, target, opts) end |
#cp_r(sources, target, opts = {}) ⇒ Object
Copy the sources
to the target
recursively. Returns an array of sources copied or false
if all are present.
210 |
# File 'lib/automateit/shell_manager.rb', line 210 def cp_r(sources, target, opts={}) dispatch(sources, target, opts) end |
#cp_R(sources, target, opts = {}) ⇒ Object
Copy the sources
to the target
recursively. Returns an array of sources copied or false
if all are present.
214 |
# File 'lib/automateit/shell_manager.rb', line 214 def cp_R(sources, target, opts={}) dispatch(sources, target, opts) end |
#install(source, target, mode) ⇒ Object
Copy the source
to the target
and set its mode
. Returns true if the file was installed or false
if already present.
234 |
# File 'lib/automateit/shell_manager.rb', line 234 def install(source, target, mode) dispatch(source, target, mode) end |
#ln(source, target, opts = {}) ⇒ Object
Create a hard link between the source
and target
. Your platform must support hard links to use this. Returns the target created or false
if the link is already present.
184 |
# File 'lib/automateit/shell_manager.rb', line 184 def ln(source, target, opts={}) dispatch(source, target, opts) end |
#ln_s(sources, target, opts = {}) ⇒ Object
Create a symbolic link between the sources
and target
. Your platform must support symbolic links to use this. Returns an array of sources linked or false
if all are already present.
189 |
# File 'lib/automateit/shell_manager.rb', line 189 def ln_s(sources, target, opts={}) dispatch(sources, target, opts) end |
#ln_sf(sources, target, opts = {}) ⇒ Object
Create a symbolic link between the sources
and target
. If the target
already exists, will remove it and recreate it. Your platform must support symbolic links to use this. Returns an array of sources linked or false
if all are already present.
195 |
# File 'lib/automateit/shell_manager.rb', line 195 def ln_sf(sources, target, opts={}) dispatch(sources, target, opts) end |
#mkdir(dirs, opts = {}, &block) ⇒ Object
Create a directory or directories. Returns an array of directories created or false
if all directories are already present.
Options:
-
:parents – Create parents, like “mkdir -p”. Boolean.
-
:mode, :user, :group – See #chperm
WARNING: Previewing code can be dangerous. Read previews.txt for instructions on how to write code that can be safely previewed.
158 |
# File 'lib/automateit/shell_manager.rb', line 158 def mkdir(dirs, opts={}, &block) dispatch(dirs, &block) end |
#mkdir_p(dirs, opts = {}, &block) ⇒ Object
Create a directory or directories with their parents. Returns an array of directories created or false
if all directories are already present.
Options same as #mkdir.
Example:
File.exists?("/tmp/foo") # => false
mkdir_p("/tmp/foo/bar")
File.exists?("/tmp/foo") # => true
File.exists?("/tmp/foo/bar") # => true
WARNING: Previewing code can be dangerous. Read previews.txt for instructions on how to write code that can be safely previewed.
174 |
# File 'lib/automateit/shell_manager.rb', line 174 def mkdir_p(dirs, opts={}, &block) dispatch(dirs, &block) end |
#mktemp(name = nil, &block) ⇒ Object
Creates a temporary file. Optionally takes a name
argument which is purely cosmetic, e.g., if the name
is “foo”, the routine may create a temporary file named /tmp/foo_qeKo7nJk1s
.
When called with a block, invokes the block with the path of the temporary file and deletes the file at the end of the block.
Without a block, returns the path of the temporary file and you’re responsible for removing it when done.
74 75 76 |
# File 'lib/automateit/shell_manager.rb', line 74 def mktemp(name=nil, &block) # :yields: path dispatch(name, &block) end |
#mktempdir(name = nil, &block) ⇒ Object
Creates a temporary directory. See #mktemp for details on the name
argument.
When called with a block, invokes the block with the path of the temporary directory and recursively deletes the directory and its contents at the end of the block.
Without a block, returns the path of the temporary directory and you’re responsible for removing it when done.
CAUTION: Read notes at the top of ShellManager for potentially problematic situations that may be encountered if using this command in preview mode!
91 92 93 |
# File 'lib/automateit/shell_manager.rb', line 91 def mktempdir(name=nil, &block) # :yields: path dispatch(name, &block) end |
#mktempdircd(name = nil, &block) ⇒ Object
Same as #mktempdir but performs a #cd into the directory for the duration of the block.
Example:
puts pwd # => "/home/bubba"
mktempdircd do |path|
puts path # => "/tmp/tempster_qeKo7nJk1s"
puts pwd # => "/tmp/tempster_qeKo7nJk1s"
end
puts File.exists?("/tmp/tempster_qeKo7nJk1s") # => false
106 107 108 |
# File 'lib/automateit/shell_manager.rb', line 106 def mktempdircd(name=nil, &block) # :yields: path dispatch(name, &block) end |
#mv(sources, target) ⇒ Object
Move the sources
to the target
. Returns an array of sources copied or false
if none of the sources exist.
218 |
# File 'lib/automateit/shell_manager.rb', line 218 def mv(sources, target) dispatch(sources, target) end |
#provides_link? ⇒ Boolean
See ShellManager#provides_mode?
25 |
# File 'lib/automateit/shell_manager.rb', line 25 def provides_link?() dispatch_safely end |
#provides_mode? ⇒ Boolean
See ShellManager#provides_mode?
16 |
# File 'lib/automateit/shell_manager.rb', line 16 def provides_mode?() dispatch_safely end |
#provides_ownership? ⇒ Boolean
See ShellManager#provides_mode?
19 |
# File 'lib/automateit/shell_manager.rb', line 19 def provides_ownership?() dispatch_safely end |
#provides_symlink? ⇒ Boolean
See ShellManager#provides_mode?
22 |
# File 'lib/automateit/shell_manager.rb', line 22 def provides_symlink?() dispatch_safely end |
#pwd ⇒ Object
Returns the current directory.
146 |
# File 'lib/automateit/shell_manager.rb', line 146 def pwd() dispatch() end |
#rm(targets, opts = {}) ⇒ Object
Remove the targets
. Returns a list of targets removed or false
if none of them exist.
222 |
# File 'lib/automateit/shell_manager.rb', line 222 def rm(targets, opts={}) dispatch(targets, opts) end |
#rm_r(targets, opts = {}) ⇒ Object
Remove the targets
recursively. Returns a list of targets removed or false
if none of them exist.
226 |
# File 'lib/automateit/shell_manager.rb', line 226 def rm_r(targets, opts={}) dispatch(targets, opts) end |
#rm_rf(targets, opts = {}) ⇒ Object
Remove the targets
recursively and forcefully. Returns a list of targets removed or false
if none of them exist.
230 |
# File 'lib/automateit/shell_manager.rb', line 230 def rm_rf(targets, opts={}) dispatch(targets, opts) end |
#rmdir(dirs) ⇒ Object
Remove a directory or directories. The directories must be empty or an exception is thrown. Returns the directories removed or false
if none of the directories exist.
179 |
# File 'lib/automateit/shell_manager.rb', line 179 def rmdir(dirs) dispatch(dirs) end |
#sh(*commands) ⇒ Object
Execute a shell command.
54 |
# File 'lib/automateit/shell_manager.rb', line 54 def sh(*commands) dispatch(*commands) end |
#touch(targets, opts = {}) ⇒ Object
Create the targets
as files if needed and update their modification time. Unlike most other commands provided by ShellManager, this one will always modify the targets. Returns an array of targets modified.
Options:
-
:like – Touch the targets like this file. Defaults to none.
-
:stamp – Set the targets to the specified timestamp. Defaults to Time.now.
263 |
# File 'lib/automateit/shell_manager.rb', line 263 def touch(targets, opts={}) dispatch(targets, opts) end |
#umask(mode = nil, &block) ⇒ Object
Set the umask to mode
. If given a block, changes the umask only for the duration of the block and changes it back to its previous setting at the end.
132 |
# File 'lib/automateit/shell_manager.rb', line 132 def umask(mode=nil, &block) dispatch(mode, &block) end |
#which(command) ⇒ Object
What is the path for this command? Returns nil
if command isn’t found.
Example:
which("ls") # => "/bin/ls"
60 |
# File 'lib/automateit/shell_manager.rb', line 60 def which(command) dispatch(command) end |
#which!(command) ⇒ Object
Same as #which but throws an ArgumentError if command isn’t found.
63 |
# File 'lib/automateit/shell_manager.rb', line 63 def which!(command) dispatch(command) end |