Method: Doing::Util#write_to_file

Defined in:
lib/doing/util.rb

#write_to_file(file, content, backup: true) ⇒ Object

Write content to a file

Parameters:

  • file (String)

    The path to the file to (over)write

  • content (String)

    The content to write to the file

  • backup (Boolean) (defaults to: true)

    create a ~ backup



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/doing/util.rb', line 117

def write_to_file(file, content, backup: true)
  unless file
    puts content
    return
  end
  Doing.logger.benchmark(:write_file, :start)
  file = File.expand_path(file)

  Backup.write_backup(file) if backup

  File.open(file, 'w+') do |f|
    f.puts content
    Doing.logger.debug('Write:', "File written: #{file}")
  end
  Doing.logger.benchmark(:_post_write_hook, :start)
  Hooks.trigger :post_write, file
  Doing.logger.benchmark(:_post_write_hook, :finish)
  Doing.logger.benchmark(:write_file, :finish)
end