Class: Staticky::Files::FileSystem

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

Overview

File System abstraction to support ‘Staticky::Files`

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file: File, file_utils: FileUtils, dir: Dir) ⇒ Staticky::Files::FileSystem

Creates a new instance

Parameters:

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


17
18
19
20
21
# File 'lib/staticky/files/file_system.rb', line 17

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

Instance Attribute Details

#dirObject (readonly)

Returns the value of attribute dir.



9
10
11
# File 'lib/staticky/files/file_system.rb', line 9

def dir
  @dir
end

#fileObject (readonly)

Returns the value of attribute file.



9
10
11
# File 'lib/staticky/files/file_system.rb', line 9

def file
  @file
end

#file_utilsObject (readonly)

Returns the value of attribute file_utils.



9
10
11
# File 'lib/staticky/files/file_system.rb', line 9

def file_utils
  @file_utils
end

Instance Method Details

#chdir(path, &blk) ⇒ Object

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:



168
169
170
171
172
# File 'lib/staticky/files/file_system.rb', line 168

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

#chmod(path, mode) ⇒ Object

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:



120
121
122
123
124
# File 'lib/staticky/files/file_system.rb', line 120

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

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

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:



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

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

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

#directory?(path) ⇒ TrueClass, FalseClass

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:



287
288
289
# File 'lib/staticky/files/file_system.rb', line 287

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

#entries(path) ⇒ Object

Get entries from a directory

Parameters:

  • the (String, Pathname)

    path to list entries for

Raises:

See Also:



309
310
311
312
313
# File 'lib/staticky/files/file_system.rb', line 309

def entries(path)
  with_error_handling do
    dir.entries(path)
  end
end

#executable?(path) ⇒ TrueClass, FalseClass

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:



298
299
300
# File 'lib/staticky/files/file_system.rb', line 298

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

#exist?(path) ⇒ TrueClass, FalseClass

Check if the given path exist.

Parameters:

  • path (String, Pathname)

    the file to check

Returns:

  • (TrueClass, FalseClass)

    the result of the check

See Also:



276
277
278
# File 'lib/staticky/files/file_system.rb', line 276

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

#expand_path(path, dir) ⇒ Object

Converts a path to an absolute path.

Parameters:

  • path (String, Pathname)

    the path to the file

  • dir (String, Pathname)

    the base directory

See Also:



144
145
146
# File 'lib/staticky/files/file_system.rb', line 144

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

#join(*path) ⇒ String

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:



134
135
136
# File 'lib/staticky/files/file_system.rb', line 134

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

#mkdir(path, **kwargs) ⇒ Object

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 = Staticky::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:



192
193
194
195
196
# File 'lib/staticky/files/file_system.rb', line 192

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

#mkdir_p(path, **kwargs) ⇒ Object

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 = Staticky::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:



218
219
220
221
222
# File 'lib/staticky/files/file_system.rb', line 218

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

#open(path, mode) {|the| ... } ⇒ Object

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:



37
38
39
40
41
42
43
# File 'lib/staticky/files/file_system.rb', line 37

def open(path, mode, ...)
  touch(path)

  with_error_handling do
    file.open(path, mode, ...)
  end
end

#pwdString

Returns the name of the current working directory.

Returns:

  • (String)

    the current working directory.

See Also:



153
154
155
# File 'lib/staticky/files/file_system.rb', line 153

def pwd
  file_utils.pwd
end

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

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:



55
56
57
58
59
# File 'lib/staticky/files/file_system.rb', line 55

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

#readlines(path, *args) ⇒ Object

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:



69
70
71
72
73
# File 'lib/staticky/files/file_system.rb', line 69

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

#rm(path, **kwargs) ⇒ Object

Removes (deletes) a file

Parameters:

  • path (String)

    the file to remove

Raises:

See Also:



250
251
252
253
254
# File 'lib/staticky/files/file_system.rb', line 250

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

#rm_rf(path, *args) ⇒ Object

Removes (deletes) a directory

Parameters:

  • path (String)

    the directory to remove

Raises:

See Also:



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

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

#touch(path, **kwargs) ⇒ Object

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:



85
86
87
88
89
90
91
92
# File 'lib/staticky/files/file_system.rb', line 85

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

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:



102
103
104
105
106
107
108
# File 'lib/staticky/files/file_system.rb', line 102

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

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