Class: Dry::Files
- Inherits:
-
Object
- Object
- Dry::Files
- Defined in:
- lib/dry/files.rb,
lib/dry/files/path.rb,
lib/dry/files/error.rb,
lib/dry/files/adapter.rb,
lib/dry/files/version.rb,
lib/dry/files/file_system.rb,
lib/dry/files/memory_file_system.rb,
lib/dry/files/memory_file_system/node.rb
Overview
File manipulations
Defined Under Namespace
Modules: Path Classes: Adapter, Delimiter, Error, FileSystem, IOError, MemoryFileSystem, MissingTargetError, NotMemoryFileError, UnknownMemoryNodeError
Constant Summary collapse
- OPEN_MODE =
::File::RDWR
- WRITE_MODE =
(::File::CREAT | ::File::WRONLY | ::File::TRUNC).freeze
- VERSION =
"1.0.1"
Instance Method Summary collapse
-
#append(path, contents) ⇒ Object
Adds a new line at the bottom of the file.
-
#chdir(path, &blk) ⇒ Object
Temporary changes the current working directory of the process to the given path and yield the given block.
-
#cp(source, destination) ⇒ Object
Copies source into destination.
-
#delete(path) ⇒ Object
Deletes given path (file).
-
#delete_directory(path) ⇒ Object
Deletes given path (directory).
-
#directory?(path) ⇒ TrueClass, FalseClass
Checks if ‘path` is a directory.
-
#executable?(path) ⇒ TrueClass, FalseClass
Checks if ‘path` is an executable.
-
#exist?(path) ⇒ TrueClass, FalseClass
Checks if ‘path` exist.
-
#expand_path(path, dir = pwd) ⇒ String
Converts a path to an absolute path.
-
#initialize(memory: false, adapter: Adapter.call(memory: memory)) ⇒ Dry::Files
constructor
Creates a new instance.
-
#inject_line_after(path, target, contents) ⇒ Object
Inject ‘contents` in `path` after `target`.
-
#inject_line_after_last(path, target, contents) ⇒ Object
Inject ‘contents` in `path` after last `target`.
-
#inject_line_at_block_bottom(path, target, *contents) ⇒ Object
Inject ‘contents` in `path` within the first Ruby block that matches `target`.
-
#inject_line_at_block_top(path, target, *contents) ⇒ Object
Inject ‘contents` in `path` within the first Ruby block that matches `target`.
-
#inject_line_at_class_bottom(path, target, *contents) ⇒ Object
Inject ‘contents` in `path` at the bottom of the Ruby class that matches `target`.
-
#inject_line_before(path, target, contents) ⇒ Object
Inject ‘contents` in `path` before `target`.
-
#inject_line_before_last(path, target, contents) ⇒ Object
Inject ‘contents` in `path` after last `target`.
-
#join(*path) ⇒ String
Returns a new string formed by joining the strings using Operating System path separator.
-
#mkdir(path) ⇒ Object
Creates a directory for the given path.
-
#mkdir_p(path) ⇒ Object
Creates a directory for the given path.
-
#open(path, mode = OPEN_MODE, *args, &blk) {|the| ... } ⇒ File, Dry::Files::MemoryFileSystem::Node
Opens (or creates) a new file for both read/write operations.
-
#pwd ⇒ String
Returns the name of the current working directory.
-
#read(path) ⇒ String
Read file content.
-
#remove_block(path, target) ⇒ Object
Removes ‘target` block from `path`.
-
#remove_line(path, target) ⇒ Object
Removes line from ‘path`, matching `target`.
-
#replace_first_line(path, target, replacement) ⇒ Object
Replace first line in ‘path` that contains `target` with `replacement`.
-
#replace_last_line(path, target, replacement) ⇒ Object
Replace last line in ‘path` that contains `target` with `replacement`.
-
#touch(path) ⇒ Object
Creates an empty file for the given path.
-
#unshift(path, line) ⇒ Object
Adds a new line at the top of the file.
-
#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.
Constructor Details
#initialize(memory: false, adapter: Adapter.call(memory: memory)) ⇒ Dry::Files
Creates a new instance
Memory file system is experimental
36 37 38 |
# File 'lib/dry/files.rb', line 36 def initialize(memory: false, adapter: Adapter.call(memory: memory)) @adapter = adapter end |
Instance Method Details
#append(path, contents) ⇒ Object
Adds a new line at the bottom of the file
332 333 334 335 336 337 338 339 340 341 |
# File 'lib/dry/files.rb', line 332 def append(path, contents) mkdir_p(path) touch(path) content = adapter.readlines(path) content << newline unless newline?(content.last) content << newline(contents) write(path, content) end |
#chdir(path, &blk) ⇒ Object
Temporary changes the current working directory of the process to the given path and yield the given block.
147 148 149 |
# File 'lib/dry/files.rb', line 147 def chdir(path, &blk) adapter.chdir(path, &blk) end |
#cp(source, destination) ⇒ Object
Copies source into destination. All the intermediate directories are created. If the destination already exists, it overrides the contents.
215 216 217 |
# File 'lib/dry/files.rb', line 215 def cp(source, destination) adapter.cp(source, destination) end |
#delete(path) ⇒ Object
Deletes given path (file).
227 228 229 |
# File 'lib/dry/files.rb', line 227 def delete(path) adapter.rm(path) end |
#delete_directory(path) ⇒ Object
Deletes given path (directory).
239 240 241 |
# File 'lib/dry/files.rb', line 239 def delete_directory(path) adapter.rm_rf(path) end |
#directory?(path) ⇒ TrueClass, FalseClass
Checks if ‘path` is a directory
279 280 281 |
# File 'lib/dry/files.rb', line 279 def directory?(path) adapter.directory?(path) end |
#executable?(path) ⇒ TrueClass, FalseClass
Checks if ‘path` is an executable
299 300 301 |
# File 'lib/dry/files.rb', line 299 def executable?(path) adapter.executable?(path) end |
#exist?(path) ⇒ TrueClass, FalseClass
Checks if ‘path` exist
259 260 261 |
# File 'lib/dry/files.rb', line 259 def exist?(path) adapter.exist?(path) end |
#expand_path(path, dir = pwd) ⇒ String
Converts a path to an absolute path.
Relative paths are referenced from the current working directory of the process unless ‘dir` is given.
109 110 111 |
# File 'lib/dry/files.rb', line 109 def (path, dir = pwd) adapter.(path, dir) end |
#inject_line_after(path, target, contents) ⇒ Object
Inject ‘contents` in `path` after `target`.
436 437 438 |
# File 'lib/dry/files.rb', line 436 def inject_line_after(path, target, contents) _inject_line_after(path, target, contents, method(:index)) end |
#inject_line_after_last(path, target, contents) ⇒ Object
Inject ‘contents` in `path` after last `target`.
455 456 457 |
# File 'lib/dry/files.rb', line 455 def inject_line_after_last(path, target, contents) _inject_line_after(path, target, contents, method(:rindex)) end |
#inject_line_at_block_bottom(path, target, *contents) ⇒ Object
Inject ‘contents` in `path` within the first Ruby block that matches `target`. The given `contents` will appear at the BOTTOM of the Ruby block.
683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 |
# File 'lib/dry/files.rb', line 683 def inject_line_at_block_bottom(path, target, *contents) content = adapter.readlines(path) starting = index(content, path, target) line = content[starting] delimiter = if line.match?(INLINE_OPEN_BLOCK_MATCHER) INLINE_BLOCK_DELIMITER else BLOCK_DELIMITER end target = content[starting..] ending = closing_block_index(target, starting, path, line, delimiter) offset = SPACE * (content[ending][SPACE_MATCHER].bytesize + INDENTATION) contents = Array(contents).flatten contents = _offset_block_lines(contents, offset) content.insert(ending, contents) write(path, content) end |
#inject_line_at_block_top(path, target, *contents) ⇒ Object
Inject ‘contents` in `path` within the first Ruby block that matches `target`. The given `contents` will appear at the TOP of the Ruby block.
565 566 567 568 569 570 571 572 573 574 575 |
# File 'lib/dry/files.rb', line 565 def inject_line_at_block_top(path, target, *contents) content = adapter.readlines(path) starting = index(content, path, target) offset = SPACE * (content[starting][SPACE_MATCHER].bytesize + INDENTATION) contents = Array(contents).flatten contents = _offset_block_lines(contents, offset) content.insert(starting + CONTENT_OFFSET, contents) write(path, content) end |
#inject_line_at_class_bottom(path, target, *contents) ⇒ Object
Inject ‘contents` in `path` at the bottom of the Ruby class that matches `target`. The given `contents` will appear at the BOTTOM of the Ruby class.
763 764 765 766 767 768 769 770 771 772 773 774 775 776 |
# File 'lib/dry/files.rb', line 763 def inject_line_at_class_bottom(path, target, *contents) content = adapter.readlines(path) starting = index(content, path, target) line = content[starting] target = content[starting..] ending = closing_class_index(target, starting, path, line, BLOCK_DELIMITER) offset = SPACE * (content[ending][SPACE_MATCHER].bytesize + INDENTATION) contents = Array(contents).flatten contents = _offset_block_lines(contents, offset) content.insert(ending, contents) write(path, content) end |
#inject_line_before(path, target, contents) ⇒ Object
Inject ‘contents` in `path` before `target`.
398 399 400 |
# File 'lib/dry/files.rb', line 398 def inject_line_before(path, target, contents) _inject_line_before(path, target, contents, method(:index)) end |
#inject_line_before_last(path, target, contents) ⇒ Object
Inject ‘contents` in `path` after last `target`.
417 418 419 |
# File 'lib/dry/files.rb', line 417 def inject_line_before_last(path, target, contents) _inject_line_before(path, target, contents, method(:rindex)) end |
#join(*path) ⇒ String
Returns a new string formed by joining the strings using Operating System path separator
94 95 96 |
# File 'lib/dry/files.rb', line 94 def join(*path) adapter.join(*path) end |
#mkdir(path) ⇒ Object
Creates a directory for the given path. It assumes that all the tokens in ‘path` are meant to be a directory. All the intermediate directories are created.
173 174 175 |
# File 'lib/dry/files.rb', line 173 def mkdir(path) adapter.mkdir(path) end |
#mkdir_p(path) ⇒ Object
Creates a directory for the given path. It assumes that all the tokens, but the last, in ‘path` are meant to be a directory, whereas the last is meant to be a file. All the intermediate directories are created.
200 201 202 |
# File 'lib/dry/files.rb', line 200 def mkdir_p(path) adapter.mkdir_p(path) end |
#open(path, mode = OPEN_MODE, *args, &blk) {|the| ... } ⇒ File, Dry::Files::MemoryFileSystem::Node
Opens (or creates) a new file for both read/write operations
134 135 136 |
# File 'lib/dry/files.rb', line 134 def open(path, mode = OPEN_MODE, *args, &blk) adapter.open(path, mode, *args, &blk) end |
#pwd ⇒ String
Returns the name of the current working directory.
118 119 120 |
# File 'lib/dry/files.rb', line 118 def pwd adapter.pwd end |
#read(path) ⇒ String
Read file content
TODO: allow buffered read
52 53 54 |
# File 'lib/dry/files.rb', line 52 def read(path) adapter.read(path) end |
#remove_block(path, target) ⇒ Object
Removes ‘target` block from `path`
824 825 826 827 828 829 830 831 832 833 834 835 836 837 |
# File 'lib/dry/files.rb', line 824 def remove_block(path, target) content = adapter.readlines(path) starting = index(content, path, target) line = content[starting] size = line[SPACE_MATCHER].bytesize closing = (SPACE * size) + (target.match?(INLINE_OPEN_BLOCK_MATCHER) ? INLINE_CLOSE_BLOCK : CLOSE_BLOCK) ending = starting + index(content[starting..-CONTENT_OFFSET], path, closing) content.slice!(starting..ending) write(path, content) remove_block(path, target) if match?(content, target) end |
#remove_line(path, target) ⇒ Object
Removes line from ‘path`, matching `target`.
788 789 790 791 792 793 794 |
# File 'lib/dry/files.rb', line 788 def remove_line(path, target) content = adapter.readlines(path) i = index(content, path, target) content.delete_at(i) write(path, content) end |
#replace_first_line(path, target, replacement) ⇒ Object
Replace first line in ‘path` that contains `target` with `replacement`.
356 357 358 359 360 361 |
# File 'lib/dry/files.rb', line 356 def replace_first_line(path, target, replacement) content = adapter.readlines(path) content[index(content, path, target)] = newline(replacement) write(path, content) end |
#replace_last_line(path, target, replacement) ⇒ Object
Replace last line in ‘path` that contains `target` with `replacement`.
376 377 378 379 380 381 |
# File 'lib/dry/files.rb', line 376 def replace_last_line(path, target, replacement) content = adapter.readlines(path) content[-index(content.reverse, path, target) - CONTENT_OFFSET] = newline(replacement) write(path, content) end |
#touch(path) ⇒ Object
Creates an empty file for the given path. All the intermediate directories are created. If the path already exists, it doesn’t change the contents
66 67 68 |
# File 'lib/dry/files.rb', line 66 def touch(path) adapter.touch(path) end |
#unshift(path, line) ⇒ Object
Adds a new line at the top of the file
314 315 316 317 318 319 |
# File 'lib/dry/files.rb', line 314 def unshift(path, line) content = adapter.readlines(path) content.unshift(newline(line)) write(path, content) 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.
81 82 83 |
# File 'lib/dry/files.rb', line 81 def write(path, *content) adapter.write(path, *content) end |