module Flowcation::FileWriter

Public Instance Methods

add_generated_comment(content) click to toggle source
# File lib/flowcation/file_writer.rb, line 45
def add_generated_comment(content)
  within_comment(generated_comment) + "\n" + content
end
check_if_generated_by_flowcation!(file) click to toggle source
# File lib/flowcation/file_writer.rb, line 53
def check_if_generated_by_flowcation!(file)
  return true unless File.exist? file
  return true if Settings.get('force-overwrite')
  generated? file
  # if generated? file
  #   return true
  # else
  #   raise OverwriteException.for_file(file)
  # end
end
file_writer_collections() click to toggle source
# File lib/flowcation/file_writer.rb, line 9
def file_writer_collections
  @file_writer_collections
end
generated?(file) click to toggle source
# File lib/flowcation/file_writer.rb, line 49
def generated?(file)
  !!File.open(file).readlines.first&.scan(/#{generated_comment}/)&.send(:[],0)
end
generated_comment() click to toggle source
# File lib/flowcation/file_writer.rb, line 41
def generated_comment
  Settings.get('generated-line') || DEFAULT_GENERATED_TEXT
end
within_comment(content) click to toggle source
# File lib/flowcation/file_writer.rb, line 36
def within_comment(content)
  comment = Settings.get('comment') || DEFAULT_COMMENT
  comment.sub("::comment::", content)
end
write_files() click to toggle source
# File lib/flowcation/file_writer.rb, line 17
def write_files
  # todo rescue/finally close file
  self.class.file_writer_collections.each do |writables|
    send(writables).each do |writeable|
      file_name = writeable.path
      path = File.dirname file_name
      FileUtils.mkdir_p(path)
      if check_if_generated_by_flowcation! file_name
        file = File.new(file_name, 'w')
        c = writeable.content
        c = add_generated_comment c
        File.write(file, c.encode(cr_newline: true))
      else
        STDERR.puts "File #{file_name} not generated by flowcation. Use -f to overwrite or add Setting 'force-overwrite: true' to configuration yaml file entry 'flowcation:'"
      end
    end
  end
end
writeables(file_writer_collections) click to toggle source
# File lib/flowcation/file_writer.rb, line 12
def writeables(file_writer_collections)
  @file_writer_collections = file_writer_collections
end