lib/chef_attrdoc.rb in chef_attrdoc-1.0.3 vs lib/chef_attrdoc.rb in chef_attrdoc-1.1.0
- old
+ new
@@ -131,12 +131,13 @@
strings.join
end
end
# open the :readme: Markdown file and replace the 'Attributes' section
- # with the contents of :parsed:
- def self.write_readme(readme, parsed)
+ # with the contents of :parsed: . If :dry_run: is `true`, the string
+ # will be returned instead of overwriting the :readme: file.
+ def self.write_readme(readme, parsed, dry_run=false)
File.open(readme, File::RDWR) do |f|
# TODO find a cleaner way and do this in one step
content = f.read
if content =~ /\nAttributes\s*=+\s*\n/
updated = content.sub(/(?<before>.*\nAttributes\s*=+\s*?\n)\n*(?m:.+?)(?<after>\n.+\s*\n=+.*)/,
@@ -154,13 +155,17 @@
'CHEF_ATTRDOC_UPDATING_TEMPLATE',
# need to sanitize the second parameter, otherwise it can repeat
# parts of the original README
"\n" + parsed.gsub(/\\/, "\\\\\\"))
- f.rewind
- f.write(updated)
- f.flush
- f.truncate(f.pos)
+ if dry_run
+ return updated
+ else
+ f.rewind
+ f.write(updated)
+ f.flush
+ f.truncate(f.pos)
+ end
end
end
# return a list of [filename, file-contents] for each ruby file in :dir:
def self.attrs_contents(dir)