class AutomateIt::ShellManager
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.
Public Instance Methods
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.
# File lib/automateit/shell_manager.rb, line 51 def backup(*sources) dispatch(*sources) end
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.
# File lib/automateit/shell_manager.rb, line 143 def cd(dir, opts={}, &block) dispatch(dir, opts, &block) end
Change the permission mode
of the targets
. Returns an array of targets modified or false
if all have the desired mode.
# File lib/automateit/shell_manager.rb, line 238 def chmod(mode, targets, opts={}) dispatch(mode, targets, opts) end
Change the permission mode
of the targets
recursively. Returns an array of targets modified or false
if all have the desired mode.
# File lib/automateit/shell_manager.rb, line 242 def chmod_R(mode, targets, opts={}) dispatch(mode, targets, opts) end
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.
# File lib/automateit/shell_manager.rb, line 248 def chown(user, group, targets, opts={}) dispatch(user, group, targets, opts) end
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.
# File lib/automateit/shell_manager.rb, line 254 def chown_R(user, group, targets, opts={}) dispatch(user, group, targets, opts) end
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.
# File lib/automateit/shell_manager.rb, line 127 def chperm(targets, opts={}) dispatch(targets, opts) end
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.
# File lib/automateit/shell_manager.rb, line 206 def cp(sources, target, opts={}) dispatch(sources, target, opts) end
Copy the sources
to the target
recursively. Returns an array of sources copied or false
if all are present.
# File lib/automateit/shell_manager.rb, line 214 def cp_R(sources, target, opts={}) dispatch(sources, target, opts) end
Copy the sources
to the target
recursively. Returns an array of sources copied or false
if all are present.
# File lib/automateit/shell_manager.rb, line 210 def cp_r(sources, target, opts={}) dispatch(sources, target, opts) end
Copy the source
to the target
and set its mode
. Returns true if the file was installed or false
if already present.
# File lib/automateit/shell_manager.rb, line 234 def install(source, target, mode) dispatch(source, target, mode) end
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.
# File lib/automateit/shell_manager.rb, line 184 def ln(source, target, opts={}) dispatch(source, target, opts) end
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.
# File lib/automateit/shell_manager.rb, line 189 def ln_s(sources, target, opts={}) dispatch(sources, target, opts) end
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.
# File lib/automateit/shell_manager.rb, line 195 def ln_sf(sources, target, opts={}) dispatch(sources, target, opts) end
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.
# File lib/automateit/shell_manager.rb, line 158 def mkdir(dirs, opts={}, &block) dispatch(dirs, &block) end
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.
# File lib/automateit/shell_manager.rb, line 174 def mkdir_p(dirs, opts={}, &block) dispatch(dirs, &block) end
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.
# File lib/automateit/shell_manager.rb, line 74 def mktemp(name=nil, &block) # :yields: path dispatch(name, &block) end
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!
# File lib/automateit/shell_manager.rb, line 91 def mktempdir(name=nil, &block) # :yields: path dispatch(name, &block) end
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
# File lib/automateit/shell_manager.rb, line 106 def mktempdircd(name=nil, &block) # :yields: path dispatch(name, &block) end
Move the sources
to the target
. Returns an array of sources copied or false
if none of the sources exist.
# File lib/automateit/shell_manager.rb, line 218 def mv(sources, target) dispatch(sources, target) end
See ShellManager#provides_mode?
# File lib/automateit/shell_manager.rb, line 25 def provides_link?() dispatch_safely end
See ShellManager#provides_mode?
# File lib/automateit/shell_manager.rb, line 16 def provides_mode?() dispatch_safely end
See ShellManager#provides_mode?
# File lib/automateit/shell_manager.rb, line 19 def provides_ownership?() dispatch_safely end
See ShellManager#provides_mode?
# File lib/automateit/shell_manager.rb, line 22 def provides_symlink?() dispatch_safely end
Returns the current directory.
# File lib/automateit/shell_manager.rb, line 146 def pwd() dispatch() end
Remove the targets
. Returns a list of targets removed or false
if none of them exist.
# File lib/automateit/shell_manager.rb, line 222 def rm(targets, opts={}) dispatch(targets, opts) end
Remove the targets
recursively. Returns a list of targets removed or false
if none of them exist.
# File lib/automateit/shell_manager.rb, line 226 def rm_r(targets, opts={}) dispatch(targets, opts) end
Remove the targets
recursively and forcefully. Returns a list of targets removed or false
if none of them exist.
# File lib/automateit/shell_manager.rb, line 230 def rm_rf(targets, opts={}) dispatch(targets, opts) end
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.
# File lib/automateit/shell_manager.rb, line 179 def rmdir(dirs) dispatch(dirs) end
Execute a shell command.
# File lib/automateit/shell_manager.rb, line 54 def sh(*commands) dispatch(*commands) end
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.
# File lib/automateit/shell_manager.rb, line 263 def touch(targets, opts={}) dispatch(targets, opts) end
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.
# File lib/automateit/shell_manager.rb, line 132 def umask(mode=nil, &block) dispatch(mode, &block) end
What is the path for this command? Returns nil
if command isn’t found.
Example:
which("ls") # => "/bin/ls"
# File lib/automateit/shell_manager.rb, line 60 def which(command) dispatch(command) end
Same as which
but throws an ArgumentError if command isn’t found.
# File lib/automateit/shell_manager.rb, line 63 def which!(command) dispatch(command) end