-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
Copy pathgenerate_references.rake
59 lines (51 loc) · 2 KB
/
generate_references.rake
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
require 'tempfile'
OUTPUT_DIR = 'references'
CONFIGURATION_ERB = File.join(__dir__, 'references/configuration.erb')
CONFIGURATION_MD = File.join(OUTPUT_DIR, 'configuration.md')
METAPARAMETER_ERB = File.join(__dir__, 'references/metaparameter.erb')
METAPARAMETER_MD = File.join(OUTPUT_DIR, 'metaparameter.md')
REPORT_ERB = File.join(__dir__, 'references/report.erb')
REPORT_MD = File.join(OUTPUT_DIR, 'report.md')
def render_erb(erb_file, variables)
# Create a binding so only the variables we specify will be visible
template_binding = OpenStruct.new(variables).instance_eval {binding}
ERB.new(File.read(erb_file), trim_mode: '-').result(template_binding)
end
def puppet_doc(reference)
body = %x{bundle exec puppet doc -r #{reference}}
# Remove the first H1 with the title, like "# Metaparameter Reference"
body.sub!(/^# \w+ Reference *$/, '')
body.chomp
end
# This is adapted from https://github.com/puppetlabs/puppet-docs/blob/1a13be3fc6981baa8a96ff832ab090abc986830e/lib/puppet_references/puppet/puppet_doc.rb#L22-L36
def generate_reference(reference, erb, body, output)
sha = %x{git rev-parse HEAD}.chomp
now = Time.now
variables = {
sha: sha,
now: now,
body: body
}
content = render_erb(erb, variables)
File.write(output, content)
puts "Generated #{output}"
end
namespace :references do
desc "Generate configuration reference"
task :configuration do
ENV['PUPPET_REFERENCES_HOSTNAME'] = "(the system's fully qualified hostname)"
ENV['PUPPET_REFERENCES_DOMAIN'] = "(the system's own domain)"
body = puppet_doc('configuration')
generate_reference('configuration', CONFIGURATION_ERB, body, CONFIGURATION_MD)
end
desc "Generate metaparameter reference"
task :metaparameter do
body = puppet_doc('metaparameter')
generate_reference('metaparameter', METAPARAMETER_ERB, body, METAPARAMETER_MD)
end
desc "Generate report reference"
task :report do
body = puppet_doc('report')
generate_reference('report', REPORT_ERB, body, REPORT_MD)
end
end