Module: Flowcation::FileWriter
Instance Method Summary collapse
- #add_generated_comment(content) ⇒ Object
- #check_if_generated_by_flowcation!(file) ⇒ Object
- #generated?(file) ⇒ Boolean
- #generated_comment ⇒ Object
- #within_comment(content) ⇒ Object
- #write_files ⇒ Object
Instance Method Details
#add_generated_comment(content) ⇒ Object
45 46 47 |
# 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) ⇒ Object
53 54 55 56 57 58 59 60 61 62 |
# 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 |
#generated?(file) ⇒ Boolean
49 50 51 |
# File 'lib/flowcation/file_writer.rb', line 49 def generated?(file) !!File.open(file).readlines.first&.scan(/#{generated_comment}/)&.send(:[],0) end |
#generated_comment ⇒ Object
41 42 43 |
# File 'lib/flowcation/file_writer.rb', line 41 def generated_comment Settings.get('generated-line') || DEFAULT_GENERATED_TEXT end |
#within_comment(content) ⇒ Object
36 37 38 39 |
# 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 ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# 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 |