Method: Hanami::Utils::Files.replace_first_line

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

.replace_first_line(path, target, replacement) ⇒ Object

Replace first 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

[View source]

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