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

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

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.



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

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

  # Adjust directory
  directories = directories.ensure_array(nil, true, true) {|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

#within_directory(directory, restore = true, show_messages = false) ⇒ Boolean

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
# File 'lib/bovem/shell.rb', line 433

def within_directory(directory, restore = true, show_messages = false)
  locale = 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