Class: Cuprum::Cli::Dependencies::FileSystem

Inherits:
Object
  • Object
show all
Defined in:
lib/cuprum/cli/dependencies/file_system.rb

Overview

Utility wrapping filesystem operations.

Direct Known Subclasses

Mock

Defined Under Namespace

Classes: DirectoryIsAFileError, DirectoryNotFoundError, FileError, FileIsADirectoryError, FileNotFoundError, Mock

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root_path: Dir.pwd) ⇒ FileSystem

Returns a new instance of FileSystem.

Parameters:

  • root_path (String) (defaults to: Dir.pwd)

    the path to the root directory. Defaults to the value of Dir.pwd.



31
32
33
# File 'lib/cuprum/cli/dependencies/file_system.rb', line 31

def initialize(root_path: Dir.pwd)
  @root_path = root_path
end

Instance Attribute Details

#root_pathString (readonly)

Returns the path to the root directory.

Returns:

  • (String)

    the path to the root directory.



36
37
38
# File 'lib/cuprum/cli/dependencies/file_system.rb', line 36

def root_path
  @root_path
end

Instance Method Details

#create_directory(path, recursive: false) ⇒ String Also known as: make_directory

Creates a directory at the requested path.

Parameters:

  • path (String)

    the path to the directory.

  • recursive (true, false) (defaults to: false)

    if true, creates any required intermediate directories, equivalent to the -p flag on mkdir. Defaults to false.

Returns:

  • (String)

    the path to the created directory.



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/cuprum/cli/dependencies/file_system.rb', line 45

def create_directory(path, recursive: false)
  tools.assertions.validate_name(path, as: 'path')

  resolved = resolve_path(path)

  return path if directory?(resolved)

  handle_create_errors(path) do
    recursive ? FileUtils.mkdir_p(resolved) : FileUtils.mkdir(resolved)
  end

  path
end

#directory?(path) ⇒ true, false Also known as: directory_exists?

Checks if the requested directory exists.

Parameters:

  • path (String)

    the path to the requested directory.

Returns:

  • (true, false)

    true if the directory exists and is a directory, otherwise false.



66
67
68
69
70
71
72
# File 'lib/cuprum/cli/dependencies/file_system.rb', line 66

def directory?(path)
  tools.assertions.validate_name(path, as: 'path')

  path = resolve_path(path)

  File.exist?(path) && File.directory?(path)
end

#each_fileEnumerator<String> #each_file({ |file| }) {|the| ... } ⇒ nil

Overloads:

  • #each_fileEnumerator<String>

    Iterates over file names matching the given pattern.

    Parameters:

    • pattern (String)

      the file pattern to match.

    Returns:

    • (Enumerator<String>)

      an enumerator over the matching file names.

  • #each_file({ |file| }) {|the| ... } ⇒ nil

    Yields each file name matching the given pattern.

    Parameters:

    • pattern (String)

      the file pattern to match.

    Yield Parameters:

    • the (String)

      matching file name.

    Returns:

    • (nil)


90
91
92
93
94
95
96
97
98
# File 'lib/cuprum/cli/dependencies/file_system.rb', line 90

def each_file(pattern, &)
  return enum_for(:each_file, pattern) unless block_given?

  path = resolve_path(pattern)

  Dir[path].each(&)

  nil
end

#file?(path) ⇒ true, false Also known as: file_exists?

Checks if the requested file exists.

Parameters:

  • path (String)

    the path to the requested file.

Returns:

  • (true, false)

    true if the file exists and is a file, otherwise false.



106
107
108
109
110
111
112
# File 'lib/cuprum/cli/dependencies/file_system.rb', line 106

def file?(path)
  tools.assertions.validate_name(path, as: 'path')

  path = resolve_path(path)

  File.exist?(path) && File.file?(path)
end

#read_file(file) ⇒ String #read_file(path) ⇒ String Also known as: read

Overloads:

  • #read_file(file) ⇒ String

    Reads the contents of the given file or IO stream.

    Parameters:

    • file (IO)

      the file to read.

    Returns:

    • (String)

      the file contents.

  • #read_file(path) ⇒ String

    Reads the contents of the file at the given path.

    Parameters:

    • path (String)

      the file path to read.

    Returns:

    • (String)

      the file contents.

    Raises:



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/cuprum/cli/dependencies/file_system.rb', line 131

def read_file(file_or_path)
  validate_file(file_or_path, as: 'file')

  return file_or_path.read if io_stream?(file_or_path)

  path = resolve_path(file_or_path)

  File.read(path)
rescue Errno::EISDIR
  raise FileIsADirectoryError,
    "unable to read file #{file_or_path} - file is a directory"
rescue Errno::ENOENT
  raise FileNotFoundError,
    "unable to read file #{file_or_path} - file not found"
end

#with_tempfile {|the| ... } ⇒ Object

Creates a tempfile and passes it to the block.

Yield Parameters:

  • the (File)

    generated tempfile.

Returns:

  • (Object)

    the value returned by the block.



153
# File 'lib/cuprum/cli/dependencies/file_system.rb', line 153

def with_tempfile(&) = Tempfile.create(&)

#write_file(file, data) ⇒ Integer #write_file(path, data) ⇒ Integer Also known as: write

Overloads:

  • #write_file(file, data) ⇒ Integer

    Writes the data to the given file or IO stream.

    Parameters:

    • file (IO)

      the file to write.

    • data (String)

      the data to write.

    Returns:

    • (Integer)

      the number of bytes written.

  • #write_file(path, data) ⇒ Integer

    Writes the data to the file at the given path.

    Parameters:

    • path (String)

      the file path to write.

    • data (String)

      the data to write.

    Returns:

    • (Integer)

      the number of bytes written.

    Raises:



176
177
178
179
180
181
182
183
184
# File 'lib/cuprum/cli/dependencies/file_system.rb', line 176

def write_file(file_or_path, data)
  validate_file(file_or_path, as: 'file')

  return file_or_path.write(data) if io_stream?(file_or_path)

  path = resolve_path(file_or_path)

  handle_write_errors(file_or_path) { File.write(path, data) }
end