module Mina::Dotenv::Utils

Public Class Methods

add_to_file(file, string, before: true, force: false) click to toggle source
# File lib/mina/dotenv/utils.rb, line 17
def self.add_to_file(file, string, before: true, force: false)
  return unless File.exist?(file)
  return if !force && file_contains?(file, string)

  file_content = File.read(file)

  File.open(file, 'w') do |f|
    f.puts(string) if before
    f.puts file_content
    f.puts(string) unless before
  end
end
add_to_gitignore(path) click to toggle source
# File lib/mina/dotenv/utils.rb, line 34
def self.add_to_gitignore(path)
  gitignore_path = '.gitignore'
  return if file_contains?(gitignore_path, path)
  append_to_file(gitignore_path, path)
end
append_to_file(file, string, force: false) click to toggle source
# File lib/mina/dotenv/utils.rb, line 9
def self.append_to_file(file, string, force: false)
  add_to_file(file, string, before: true, force: force)
end
create_file(path, content = '') click to toggle source
# File lib/mina/dotenv/utils.rb, line 40
def self.create_file(path, content = '')
  return if File.exist?(path)
  File.open(path, 'w') do |f|
    f.puts(content)
  end
end
file_contains?(file, regex) click to toggle source
# File lib/mina/dotenv/utils.rb, line 30
def self.file_contains?(file, regex)
  File.foreach(file).grep(Regexp.new(regex)).any?
end
prepend_to_file(file, string, force: false) click to toggle source
# File lib/mina/dotenv/utils.rb, line 13
def self.prepend_to_file(file, string, force: false)
  add_to_file(file, string, force: force)
end
read_file(file_path) click to toggle source
# File lib/mina/dotenv/utils.rb, line 4
def self.read_file(file_path)
  return '' unless File.exist?(file_path)
  File.open(file_path, 'r').read
end