Class: Dry::Files::FileSystem Private

Inherits:
Object
  • Object
show all
Defined in:
lib/dry/files/file_system.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

File System abstraction to support ‘Dry::Files`

Since:

  • 0.1.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file: File, file_utils: FileUtils) ⇒ Dry::Files::FileSystem

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Creates a new instance

Parameters:

  • file (Class) (defaults to: File)
  • file_utils (Class) (defaults to: FileUtils)

Since:

  • 0.1.0



28
29
30
31
# File 'lib/dry/files/file_system.rb', line 28

def initialize(file: File, file_utils: FileUtils)
  @file = file
  @file_utils = file_utils
end

Instance Attribute Details

#fileObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 0.1.0



14
15
16
# File 'lib/dry/files/file_system.rb', line 14

def file
  @file
end

#file_utilsObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 0.1.0



18
19
20
# File 'lib/dry/files/file_system.rb', line 18

def file_utils
  @file_utils
end

Instance Method Details

#chdir(path, &blk) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Temporary changes the current working directory of the process to the given path and yield the given block.

The argument ‘path` is intended to be a directory.

Parameters:

  • path (String, Pathname)

    the target directory

  • blk (Proc)

    the code to execute with the target directory

Raises:

See Also:

Since:

  • 0.1.0



207
208
209
210
211
# File 'lib/dry/files/file_system.rb', line 207

def chdir(path, &blk)
  with_error_handling do
    file_utils.chdir(path, &blk)
  end
end

#chmod(path, mode) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Sets UNIX permissions of the file at the given path.

Accepts permissions in numeric mode only, best provided as octal numbers matching the standard UNIX octal permission modes, such as ‘0o544` for a file writeable by its owner and readable by others, or `0o755` for a file writeable by its owner and executable by everyone.

Parameters:

  • path (String, Pathname)

    the path to the file

  • mode (Integer)

    the UNIX permissions mode

Raises:

Since:

  • 1.1.0



147
148
149
150
151
# File 'lib/dry/files/file_system.rb', line 147

def chmod(path, mode)
  with_error_handling do
    file_utils.chmod(mode, path)
  end
end

#cp(source, destination, **kwargs) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Copies file content from ‘source` to `destination` All the intermediate `destination` directories are created.

Parameters:

  • source (String)

    the file(s) or directory to copy

  • destination (String)

    the directory destination

Raises:

See Also:

Since:

  • 0.1.0



281
282
283
284
285
286
287
# File 'lib/dry/files/file_system.rb', line 281

def cp(source, destination, **kwargs)
  mkdir_p(destination)

  with_error_handling do
    file_utils.cp(source, destination, **kwargs)
  end
end

#directory?(path) ⇒ TrueClass, FalseClass

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Check if the given path is a directory.

Parameters:

  • path (String, Pathname)

    the directory to check

Returns:

  • (TrueClass, FalseClass)

    the result of the check

See Also:

Since:

  • 0.1.0



347
348
349
# File 'lib/dry/files/file_system.rb', line 347

def directory?(path)
  file.directory?(path)
end

#executable?(path) ⇒ TrueClass, FalseClass

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Check if the given path is an executable.

Parameters:

  • path (String, Pathname)

    the path to check

Returns:

  • (TrueClass, FalseClass)

    the result of the check

See Also:

Since:

  • 0.1.0



361
362
363
# File 'lib/dry/files/file_system.rb', line 361

def executable?(path)
  file.executable?(path)
end

#exist?(path) ⇒ TrueClass, FalseClass

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Check if the given path exist.

Parameters:

  • path (String, Pathname)

    the file to check

Returns:

  • (TrueClass, FalseClass)

    the result of the check

See Also:

Since:

  • 0.1.0



333
334
335
# File 'lib/dry/files/file_system.rb', line 333

def exist?(path)
  file.exist?(path)
end

#expand_path(path, dir) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Converts a path to an absolute path.

Parameters:

  • path (String, Pathname)

    the path to the file

  • dir (String, Pathname)

    the base directory

See Also:

Since:

  • 0.1.0



177
178
179
# File 'lib/dry/files/file_system.rb', line 177

def expand_path(path, dir)
  file.expand_path(path, dir)
end

#join(*path) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new string formed by joining the strings using Operating System path separator

Parameters:

  • path (Array<String,Pathname>)

    path tokens

Returns:

  • (String)

    the joined path

See Also:

Since:

  • 0.1.0



164
165
166
# File 'lib/dry/files/file_system.rb', line 164

def join(*path)
  file.join(*path)
end

#mkdir(path, **kwargs) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Creates a directory and all its parent directories.

The argument ‘path` is intended to be a directory that you want to explicitly create.

Examples:

require "dry/cli/utils/files/file_system"

fs = Dry::Files::FileSystem.new
fs.mkdir("/usr/var/project")
# creates all the directory structure (/usr/var/project)

Parameters:

  • path (String)

    the directory to create

Raises:

See Also:

Since:

  • 0.1.0



234
235
236
237
238
# File 'lib/dry/files/file_system.rb', line 234

def mkdir(path, **kwargs)
  with_error_handling do
    file_utils.mkdir_p(path, **kwargs)
  end
end

#mkdir_p(path, **kwargs) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Creates a directory and all its parent directories.

The argument ‘path` is intended to be a file, where its directory ancestors will be implicitly created.

Examples:

require "dry/cli/utils/files/file_system"

fs = Dry::Files::FileSystem.new
fs.mkdir("/usr/var/project/file.rb")
# creates all the directory structure (/usr/var/project)
# where file.rb will eventually live

Parameters:

  • path (String)

    the file that will be in the directories that this method creates

Raises:

See Also:

Since:

  • 0.1.0



263
264
265
266
267
# File 'lib/dry/files/file_system.rb', line 263

def mkdir_p(path, **kwargs)
  mkdir(
    file.dirname(path), **kwargs
  )
end

#open(path, mode, *args, &blk) {|the| ... } ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Opens (or creates) a new file for both read/write operations.

If the file doesn’t exist, it creates a new one.

Parameters:

  • path (String)

    the target file

  • mode (String, Integer)

    Ruby file open mode

  • args (Array<Object>)

    ::File.open args

  • blk (Proc)

    the block to yield

Yield Parameters:

  • the (::File)

    opened file

Raises:

See Also:

Since:

  • 0.1.0



49
50
51
52
53
54
55
# File 'lib/dry/files/file_system.rb', line 49

def open(path, mode, *args, &blk)
  touch(path)

  with_error_handling do
    file.open(path, mode, *args, &blk)
  end
end

#pwdString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the name of the current working directory.

Returns:

  • (String)

    the current working directory.

See Also:

Since:

  • 0.1.0



189
190
191
# File 'lib/dry/files/file_system.rb', line 189

def pwd
  file_utils.pwd
end

#read(path, *args, **kwargs) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Opens the file, optionally seeks to the given offset, then returns length bytes (defaulting to the rest of the file).

Read ensures the file is closed before returning.

Parameters:

  • path (String, Array<String>)

    the target path

Raises:

See Also:

Since:

  • 0.1.0



70
71
72
73
74
# File 'lib/dry/files/file_system.rb', line 70

def read(path, *args, **kwargs)
  with_error_handling do
    file.read(path, *args, **kwargs)
  end
end

#readlines(path, *args) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Reads the entire file specified by name as individual lines, and returns those lines in an array

Parameters:

  • path (String)

    the file to read

Raises:

See Also:

Since:

  • 0.1.0



87
88
89
90
91
# File 'lib/dry/files/file_system.rb', line 87

def readlines(path, *args)
  with_error_handling do
    file.readlines(path, *args)
  end
end

#rm(path, **kwargs) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Removes (deletes) a file

Parameters:

  • path (String)

    the file to remove

Raises:

See Also:

Since:

  • 0.1.0



301
302
303
304
305
# File 'lib/dry/files/file_system.rb', line 301

def rm(path, **kwargs)
  with_error_handling do
    file_utils.rm(path, **kwargs)
  end
end

#rm_rf(path, *args) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Removes (deletes) a directory

Parameters:

  • path (String)

    the directory to remove

Raises:

See Also:

Since:

  • 0.1.0



317
318
319
320
321
# File 'lib/dry/files/file_system.rb', line 317

def rm_rf(path, *args)
  with_error_handling do
    file_utils.remove_entry_secure(path, *args)
  end
end

#touch(path, **kwargs) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Updates modification time (mtime) and access time (atime) of file(s) in list.

Files are created if they don’t exist.

Parameters:

  • path (String, Array<String>)

    the target path

Raises:

See Also:

Since:

  • 0.1.0



106
107
108
109
110
111
112
113
# File 'lib/dry/files/file_system.rb', line 106

def touch(path, **kwargs)
  raise IOError, Errno::EISDIR.new(path.to_s) if directory?(path)

  with_error_handling do
    mkdir_p(path)
    file_utils.touch(path, **kwargs)
  end
end

#write(path, *content) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Creates a new file or rewrites the contents of an existing file for the given path and content All the intermediate directories are created.

Parameters:

  • path (String, Pathname)

    the path to file

  • content (String, Array<String>)

    the content to write

Raises:

Since:

  • 0.1.0



126
127
128
129
130
131
132
# File 'lib/dry/files/file_system.rb', line 126

def write(path, *content)
  mkdir_p(path)

  self.open(path, WRITE_MODE) do |f|
    f.write(Array(content).flatten.join)
  end
end