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



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.



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.

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.



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.



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



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.



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.



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.



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

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

#glob(pattern) ⇒ Array<String>

Returns an array of file paths that match the given pattern.



319
320
321
322
323
# File 'lib/staticky/files/file_system.rb', line 319

def glob(pattern)
  with_error_handling do
    Dir.glob(pattern)
  end
end

#join(*path) ⇒ String

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



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)

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

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.

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.



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.

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



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



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



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.



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.

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