Module: CSD::Commands

Includes:
Process
Included in:
CommandsInstance
Defined in:
lib/csd/commands.rb

Overview

This module contains wrapper methods for standard file system operations. They are meant to be a little bit more robust (e.g. raising no exceptions) and return elaborate feedback on their operation. All of these methods, except for the run method, are platform independent.

Defined Under Namespace

Classes: CommandResult

Instance Method Summary collapse

Methods included from Process

daemon

Instance Method Details

#cd(target) ⇒ Object

Changes the current directory.

Returns

This method returns a CommandResult object with the following values:

success?

true if pwd is where it was requested to be after the operation, nil if not.



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/csd/commands.rb', line 82

def cd(target)
  target = target.pathnamify
  result = CommandResult.new
  if target.directory? or Options.reveal
    UI.info "cd #{target}".yellow
    if Options.reveal
      @pwd = target.to_s
    else
      Dir.chdir(target)
    end
  elsif target.exist?
    UI.error "Cannot change to directory because it exists but is not a directory: #{target}".red
  end
  result.success = (target.current_path? or Options.reveal)
  result
end

#copy(src, dest) ⇒ Object

Copies one or several files to the destination



101
102
103
# File 'lib/csd/commands.rb', line 101

def copy(src, dest)
  FileUtils.cp(src, dest) unless (Options.dry or Options.reveal)
end

#die_if_last_command_had_errorsObject



168
169
170
# File 'lib/csd/commands.rb', line 168

def die_if_last_command_had_errors
  UI.die "The last command was unsuccessful." unless $?.try(:success?)
end

#mkdir(target) ⇒ Object

Creates a directory recursively.

Returns

This method returns a CommandResult object with the following values:

success?

true if the directory exists after the operation, nil if not.

already_existed?

true if the directory existed before the operation, nil if not.

writable?

true if the directory is writable, false if not, nil if the directory doesn’t exist.

Examples

result = mkdir('foo')    # => #<CommandResult...>
result.success?          # => true
result.already_existed?  # => false

puts "I created a directory" if mkdir('bar').success?

mkdir('i/can/create/directories/recursively')


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/csd/commands.rb', line 53

def mkdir(target)
  target = target.pathnamify
  result = CommandResult.new
  if target.directory?
    # Don't do anything if the directory already exists
    result.already_existed = true
  else
    begin
      UI.info "Creating directory: #{target}".cyan
      # Try to create the directory
      target.mkpath unless (Options.dry or Options.reveal)
    rescue Errno::EACCES => e
       UI.error "Cannot create directory (no permission): #{target}"
       return result
    end
  end
  result.success  = (target.directory? or Options.reveal)
  result.writable = (target.writable? or Options.reveal)
  result
end

#pwdObject

This returns the current pwd. However, it will return a fake result if we are in reveal-commands-mode.



107
108
109
110
111
112
113
# File 'lib/csd/commands.rb', line 107

def pwd
  if Options.reveal
    @pwd ||= Dir.pwd
  else
    Dir.pwd
  end
end

#replace(filepath, pattern, substitution) ⇒ Object

Replaces all occurences of a pattern in a file

Returns

This method returns a CommandResult object with the following values:

success?

true if the replacement was successful, nil if not.



123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/csd/commands.rb', line 123

def replace(filepath, pattern, substitution)
  result = CommandResult.new
  begin
    UI.info "Modifying contents of `#{filepath}´ as follows:".blue
    UI.info "  (Replacing all occurences of `#{pattern}´ with `#{substitution}´)".blue
    new_file_content = File.read(filepath).gsub(pattern, substitution)
    File.open(filepath, 'w+') { |file| file << new_file_content } unless (Options.dry or Options.reveal)
  rescue Errno::ENOENT => e
    result.success = false
  end
  result.success = true if Options.reveal
  result
end

#run(cmd, params = {}) ⇒ Object

Runs a command on the system.

Returns

The command’s output as a String (with newline delimiters). Note that the exit code can be accessed via the global variable $?

Options

The following options can be passed as a hash.

:exit_on_failure

If the exit code of the command was not 0, exit the CSD application.



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/csd/commands.rb', line 149

def run(cmd, params={})
  default_params = { :die_on_failure => true, :silent => false }
  params = default_params.merge(params)
  unless params[:silent]
    UI.info "Running command in #{pwd}".yellow
    UI.info cmd.cyan
  end
  return '' if Options.reveal or Options.dry
  ret = ''
  IO.popen(cmd) do |stdout|
    stdout.each do |line|
      UI.info "       #{line}" unless params[:silent]
      ret << line
    end
  end
  die_if_last_command_had_errors if params[:exit_on_failure]
  ret
end