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_errors: 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_errors (Boolean) (defaults to: true)

    If quit in case of fatal errors.

Returns:

  • (Boolean)

    true if operation succeeded, false otherwise.



424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
# File 'lib/bovem/shell.rb', line 424

def create_directories(*directories, mode: 0755, run: true, show_errors: false, fatal_errors: true)
  rv = true

  # Adjust directory
  directories = directories.ensure_array(no_duplicates: true, compact: true, flatten: true) { |d| File.expand_path(d.ensure_string) }

  if !run # Just print
    dry_run_directory_creation(directories)
  else
    directories.each do |directory|
      rv &&= try_create_directory(directory, mode, fatal_errors, directories, show_errors)
      break unless 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.



405
406
407
408
409
410
411
412
413
414
# File 'lib/bovem/shell.rb', line 405

def within_directory(directory, restore: true, show_messages: false)
  directory = File.expand_path(directory.ensure_string)
  original = Dir.pwd

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

  rv
end