Method: Hanami::Utils::Files.replace_last_line

Defined in:
lib/hanami/utils/files.rb

.replace_last_line(path, target, replacement) ⇒ Object

Replace last line in path that contains target with replacement.

Parameters:

  • path (String, Pathname)

    the path to file

  • target (String, Regexp)

    the target to replace

  • replacement (String)

    the replacement

Raises:

  • (Errno::ENOENT)

    if the path doesn’t exist

  • (ArgumentError)

    if target cannot be found in path

See Also:

Since:

  • 1.1.0



188
189
190
191
192
193
# File 'lib/hanami/utils/files.rb', line 188

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