|
| 1 | +require 'tempfile' |
| 2 | + |
| 3 | +OUTPUT_DIR = 'references' |
| 4 | +CONFIGURATION_ERB = File.join(__dir__, 'references/configuration.erb') |
| 5 | +CONFIGURATION_MD = File.join(OUTPUT_DIR, 'configuration.md') |
| 6 | + |
| 7 | +def render_erb(erb_file, variables) |
| 8 | + # Create a binding so only the variables we specify will be visible |
| 9 | + template_binding = OpenStruct.new(variables).instance_eval {binding} |
| 10 | + ERB.new(File.read(erb_file), trim_mode: '-').result(template_binding) |
| 11 | +end |
| 12 | + |
| 13 | +def puppet_doc(reference) |
| 14 | + body = %x{bundle exec puppet doc -r #{reference}} |
| 15 | + # Remove the first H1 with the title, like "# Metaparameter Reference" |
| 16 | + body.sub!(/^# \w+ Reference *$/, '') |
| 17 | + body.chomp |
| 18 | +end |
| 19 | + |
| 20 | +# This is adapted from https://github.com/puppetlabs/puppet-docs/blob/1a13be3fc6981baa8a96ff832ab090abc986830e/lib/puppet_references/puppet/puppet_doc.rb#L22-L36 |
| 21 | +def generate_reference(reference, erb, body, output) |
| 22 | + sha = %x{git rev-parse HEAD}.chomp |
| 23 | + now = Time.now |
| 24 | + variables = { |
| 25 | + sha: sha, |
| 26 | + now: now, |
| 27 | + body: body |
| 28 | + } |
| 29 | + content = render_erb(erb, variables) |
| 30 | + File.write(output, content) |
| 31 | + puts "Generated #{output}" |
| 32 | +end |
| 33 | + |
| 34 | +namespace :references do |
| 35 | + desc "Generate configuration reference" |
| 36 | + task :configuration do |
| 37 | + ENV['PUPPET_REFERENCES_HOSTNAME'] = "(the system's fully qualified hostname)" |
| 38 | + ENV['PUPPET_REFERENCES_DOMAIN'] = "(the system's own domain)" |
| 39 | + |
| 40 | + body = puppet_doc('configuration') |
| 41 | + generate_reference('configuration', CONFIGURATION_ERB, body, CONFIGURATION_MD) |
| 42 | + end |
| 43 | +end |
0 commit comments