Module: Flowcation::FileWriter

Extended by:
ActiveSupport::Concern
Included in:
Runtime
Defined in:
lib/flowcation/file_writer.rb

Instance Method Summary collapse

Instance Method Details

#add_generated_comment(content) ⇒ Object



44
45
46
# File 'lib/flowcation/file_writer.rb', line 44

def add_generated_comment(content)
  within_comment(generated_comment) + "\n" + content
end

#check_if_generated_by_flowcation!(file) ⇒ Object



52
53
54
55
56
57
58
59
60
61
# File 'lib/flowcation/file_writer.rb', line 52

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

#generated?(file) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/flowcation/file_writer.rb', line 48

def generated?(file)
  !!File.open(file).readlines.first&.scan(/#{generated_comment}/)&.send(:[],0)
end

#generated_commentObject



40
41
42
# File 'lib/flowcation/file_writer.rb', line 40

def generated_comment
  Settings.get('generated-line') || DEFAULT_GENERATED_TEXT
end

#within_comment(content) ⇒ Object



35
36
37
38
# File 'lib/flowcation/file_writer.rb', line 35

def within_comment(content)
  comment = Settings.get('comment') || DEFAULT_COMMENT
  comment.sub("::comment::", content)
end

#write_filesObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/flowcation/file_writer.rb', line 17

def write_files
  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 #{path} not generated by flowcation. Use -f to overwrite or add Setting 'force-overwrite: true' to configuration yaml file entry 'flowcation:'"
      end
    end
  end
end