Module: Hanami::Utils::Files
- Defined in:
- lib/hanami/utils/files.rb
Overview
Files utilities
Class Method Summary collapse
-
.append(path, contents) ⇒ Object
Adds a new line at the bottom of the file.
-
.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.
-
.exist?(path) ⇒ TrueClass, FalseClass
Checks if ‘path` exist.
-
.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_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`.
-
.mkdir(path) ⇒ Object
Creates a directory for the given path.
-
.mkdir_p(path) ⇒ Object
Creates a directory for the given path.
-
.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 for the given path and content.
Class Method Details
.append(path, contents) ⇒ Object
Adds a new line at the bottom of the file
146 147 148 149 150 151 152 153 154 |
# File 'lib/hanami/utils/files.rb', line 146 def self.append(path, contents) mkdir_p(path) content = ::File.readlines(path) content << "\n" if _append_newline?(content) content << "#{contents}\n" write(path, content) end |
.cp(source, destination) ⇒ Object
Copies source into destination. All the intermediate directories are created. If the destination already exists, it overrides the contents.
45 46 47 48 |
# File 'lib/hanami/utils/files.rb', line 45 def self.cp(source, destination) mkdir_p(destination) FileUtils.cp(source, destination) end |
.delete(path) ⇒ Object
Deletes given path (file).
104 105 106 |
# File 'lib/hanami/utils/files.rb', line 104 def self.delete(path) FileUtils.rm(path) end |
.delete_directory(path) ⇒ Object
Deletes given path (directory).
115 116 117 |
# File 'lib/hanami/utils/files.rb', line 115 def self.delete_directory(path) FileUtils.remove_entry_secure(path) end |
.directory?(path) ⇒ TrueClass, FalseClass
Checks if ‘path` is a directory
359 360 361 |
# File 'lib/hanami/utils/files.rb', line 359 def self.directory?(path) File.directory?(path) end |
.exist?(path) ⇒ TrueClass, FalseClass
Checks if ‘path` exist
340 341 342 |
# File 'lib/hanami/utils/files.rb', line 340 def self.exist?(path) File.exist?(path) end |
.inject_line_after(path, target, contents) ⇒ Object
Inject ‘contents` in `path` after `target`.
244 245 246 |
# File 'lib/hanami/utils/files.rb', line 244 def self.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`.
263 264 265 |
# File 'lib/hanami/utils/files.rb', line 263 def self.inject_line_after_last(path, target, contents) _inject_line_after(path, target, contents, method(:rindex)) end |
.inject_line_before(path, target, contents) ⇒ Object
Inject ‘contents` in `path` before `target`.
208 209 210 |
# File 'lib/hanami/utils/files.rb', line 208 def self.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`.
226 227 228 |
# File 'lib/hanami/utils/files.rb', line 226 def self.inject_line_before_last(path, target, contents) _inject_line_before(path, target, contents, method(:rindex)) 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.
69 70 71 |
# File 'lib/hanami/utils/files.rb', line 69 def self.mkdir(path) FileUtils.mkdir_p(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.
93 94 95 |
# File 'lib/hanami/utils/files.rb', line 93 def self.mkdir_p(path) Pathname.new(path).dirname.mkpath end |
.remove_block(path, target) ⇒ Object
Removes ‘target` block from `path`
311 312 313 314 315 316 317 318 319 320 321 322 323 |
# File 'lib/hanami/utils/files.rb', line 311 def self.remove_block(path, target) content = ::File.readlines(path) starting = index(content, path, target) line = content[starting] size = line[/\A[[:space:]]*/].bytesize closing = (" " * size) + (/{/.match?(target) ? "}" : "end") ending = starting + index(content[starting..], 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`.
276 277 278 279 280 281 282 |
# File 'lib/hanami/utils/files.rb', line 276 def self.remove_line(path, target) content = ::File.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`.
168 169 170 171 172 173 |
# File 'lib/hanami/utils/files.rb', line 168 def self.replace_first_line(path, target, replacement) content = ::File.readlines(path) content[index(content, path, target)] = "#{replacement}\n" write(path, content) end |
.replace_last_line(path, target, replacement) ⇒ Object
Replace last line in ‘path` that contains `target` with `replacement`.
187 188 189 190 191 192 |
# File 'lib/hanami/utils/files.rb', line 187 def self.replace_last_line(path, target, replacement) content = ::File.readlines(path) content[-index(content.reverse, path, target) - 1] = "#{replacement}\n" 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
20 21 22 23 |
# File 'lib/hanami/utils/files.rb', line 20 def self.touch(path) mkdir_p(path) FileUtils.touch(path) end |
.unshift(path, line) ⇒ Object
Adds a new line at the top of the file
129 130 131 132 133 134 |
# File 'lib/hanami/utils/files.rb', line 129 def self.unshift(path, line) content = ::File.readlines(path) content.unshift("#{line}\n") write(path, content) end |
.write(path, *content) ⇒ Object
Creates a new file for the given path and content. All the intermediate directories are created.
32 33 34 35 |
# File 'lib/hanami/utils/files.rb', line 32 def self.write(path, *content) mkdir_p(path) open(path, ::File::CREAT | ::File::WRONLY | ::File::TRUNC, *content) # rubocop:disable Security/Open - this isn't a call to `::Kernel.open`, but to `self.open` end |