Module: ChefAttrdoc

Defined in:
lib/chef_attrdoc.rb,
lib/chef_attrdoc/version.rb

Defined Under Namespace

Classes: AttributesFile

Constant Summary collapse

VERSION =
"1.1.0"

Class Method Summary collapse

Class Method Details

.attrs_contents(dir) ⇒ Object

return a list of [filename, file-contents] for each ruby file in :dir:



172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/chef_attrdoc.rb', line 172

def self.attrs_contents(dir)
  files_contents = []
  filenames = Dir.glob(File.join(dir, '*.rb')).sort

  filenames.each do |f|
    begin
      files_contents << [File.basename(f), IO.read(f)]
    rescue Exception => e
      abort e.message
    end
  end

  files_contents
end

.process_attributes(dir) ⇒ Object

takes a directory of attributes files and returns a string with Markdown content with the output of processing all the attributes



189
190
191
192
193
194
195
196
197
198
199
# File 'lib/chef_attrdoc.rb', line 189

def self.process_attributes(dir)
  files_contents = ChefAttrdoc.attrs_contents dir

  output = []
  files_contents.each do |filename, contents|
    attrs = ChefAttrdoc::AttributesFile.new contents
    output << "## #{filename}\n\n#{attrs}"
  end

  output.join("\n")
end

.write_readme(readme, parsed, dry_run = false) ⇒ Object

open the :readme: Markdown file and replace the ‘Attributes’ section with the contents of :parsed: . If :dry_run: is true, the string will be returned instead of overwriting the :readme: file.



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/chef_attrdoc.rb', line 138

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=+.*)/,
        '\k<before>CHEF_ATTRDOC_UPDATING_TEMPLATE\k<after>')
    elsif content =~ /\n\#+\s*Attributes\s*\n/
      updated = content.sub(/(?<before>.*\n\#+\s*Attributes\s*?\n)\n*(.+?)(?<after>\n\#+.*)/m,
        '\k<before>CHEF_ATTRDOC_UPDATING_TEMPLATE\k<after>')
    else
      raise StandardError, "Could not find Attributes heading in #{readme}. "+
        "Please make sure your README file has proper markdown formatting "+
        "and includes an Attributes heading."
    end

    updated.sub!(
      'CHEF_ATTRDOC_UPDATING_TEMPLATE',
      # need to sanitize the second parameter, otherwise it can repeat
      # parts of the original README
      "\n" + parsed.gsub(/\\/, "\\\\\\"))

    if dry_run
      return updated
    else
      f.rewind
      f.write(updated)
      f.flush
      f.truncate(f.pos)
    end
  end
end