Module: Bovem::ShellMethods::Directories

Included in:
Bovem::Shell
Defined in:
lib/bovem/shell.rb

Overview

Methods to interact with directories.

Instance Method Summary (collapse)

Instance Method Details

- (Boolean) create_directories(directories, mode = 0755, run = true, show_errors = false, fatal = true)

Creates a list of directories, included missing parent directories.

Parameters:

  • directories (Array)

    The list of directories to create.

  • mode (Fixnum) (defaults to: 0755)

    Initial permissions for the new directories.

  • run (Boolean) (defaults to: true)

    If false, it will just print a list of directories that would be created.

  • show_errors (Boolean) (defaults to: false)

    If show errors.

  • fatal (Boolean) (defaults to: true)

    If quit in case of fatal errors.

Returns:

  • (Boolean)

    true if operation succeeded, false otherwise.



455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
# File 'lib/bovem/shell.rb', line 455

def create_directories(directories, mode = 0755, run = true, show_errors = false, fatal = true)
  rv = true

  # Adjust directory
  directories = directories.ensure_array.compact {|d| File.expand_path(d.ensure_string) }

  if !run then # Just print
    dry_run_directory_creation(directories)
  else
    directories.each do |directory|
      rv = rv && try_create_directory(directory, mode, fatal, directories, show_errors)
      break if !rv
    end
  end

  rv
end

- (Boolean) within_directory(directory, restore = true, show_messages = false)

Executes a block of code in another directory.

Parameters:

  • directory (String)

    The new working directory.

  • restore (Boolean) (defaults to: true)

    If to restore the original working directory.

  • show_messages (Boolean) (defaults to: false)

    Show informative messages about working directory changes.

Returns:

  • (Boolean)

    true if the directory was valid and the code executed, false otherwise.



433
434
435
436
437
438
439
440
441
442
443
444
445
# File 'lib/bovem/shell.rb', line 433

def within_directory(directory, restore = true, show_messages = false)
  rv = false
  locale = self.i18n.shell

  directory = File.expand_path(directory.ensure_string)
  original = Dir.pwd

  rv = enter_directory(directory, show_messages, locale.move_in(directory))
  yield if rv && block_given?
  rv = enter_directory(original, show_messages, locale.move_out(directory)) if rv && restore

  rv
end