class Caco::FileWriter

Constants

DifferentMD5
SameMD5
UseCustomRoot

Public Instance Methods

calculate_md5(ctx, path:, file_exist:, content:, **) click to toggle source
# File lib/caco/file_writer.rb, line 31
def calculate_md5(ctx, path:, file_exist:, content:, **)
  ctx[:current_md5] = (file_exist ? Digest::MD5.hexdigest(File.read(path)) : "")
  ctx[:content_md5] = Digest::MD5.hexdigest(content)
end
compare_md5(ctx, content_md5:, current_md5:, **) click to toggle source
# File lib/caco/file_writer.rb, line 36
def compare_md5(ctx, content_md5:, current_md5:, **)
  different_md5 = (content_md5 != current_md5)
  ctx[:file_changed] = different_md5 ? true : false
  different_md5 ? DifferentMD5 : SameMD5
end
file_exist(ctx, path:, **) click to toggle source
# File lib/caco/file_writer.rb, line 25
def file_exist(ctx, path:, **)
  ctx[:file_exist] = File.exist?(path)
  ctx[:file_created] = !ctx[:file_exist]
  ctx[:file_exist]
end
mkdir_p(ctx, path:, **) click to toggle source
# File lib/caco/file_writer.rb, line 42
def mkdir_p(ctx, path:, **)
  dirname = File.dirname(path)
  if Caco.config.write_files
    FileUtils.mkdir_p(dirname) unless File.exist?(dirname)
  end
  true
end
use_custom_root(ctx, path:, **) click to toggle source
# File lib/caco/file_writer.rb, line 17
def use_custom_root(ctx, path:, **)
  return true unless Caco.config.write_files_root
  unless ctx[:path].start_with?(Caco.config.write_files_root.to_s)
    ctx[:path] = "#{Caco.config.write_files_root}#{ctx[:path]}"
  end
  UseCustomRoot
end
write_file(ctx, path:, content:, file_exist:, **) click to toggle source
# File lib/caco/file_writer.rb, line 50
def write_file(ctx, path:, content:, file_exist:, **)
  if Caco.config.write_files
    File.write(path, content)
  end
  ctx[:file_created] = !file_exist
  ctx[:file_changed] = true
end