2014-05-16 17:54:34 +00:00
|
|
|
require 'puppet/face'
|
2014-09-19 17:59:37 +00:00
|
|
|
|
2016-09-09 20:44:05 +00:00
|
|
|
# Implements the 'puppet strings' interface.
|
2014-09-11 00:07:33 +00:00
|
|
|
Puppet::Face.define(:strings, '0.0.1') do
|
2016-09-09 20:44:05 +00:00
|
|
|
summary 'Generate Puppet documentation with YARD.'
|
2014-05-16 17:54:34 +00:00
|
|
|
|
2016-09-09 20:44:05 +00:00
|
|
|
action(:generate) do
|
|
|
|
default
|
2014-09-22 20:41:08 +00:00
|
|
|
|
2016-09-09 20:44:05 +00:00
|
|
|
option '--emit-json-stdout' do
|
|
|
|
summary 'Print JSON representation of the documentation to stdout.'
|
2014-09-22 20:41:08 +00:00
|
|
|
end
|
2016-09-09 20:44:05 +00:00
|
|
|
option '--emit-json FILE' do
|
|
|
|
summary 'Write JSON representation of the documentation to the given file.'
|
|
|
|
end
|
|
|
|
option '--markup FORMAT' do
|
|
|
|
summary "The markup format to use for docstring text (defaults to 'markdown')."
|
2014-09-22 20:41:08 +00:00
|
|
|
end
|
|
|
|
|
2016-09-09 20:44:05 +00:00
|
|
|
summary 'Generate documentation from files.'
|
|
|
|
arguments '[[search_pattern] ...]'
|
2014-05-16 17:54:34 +00:00
|
|
|
|
2016-09-09 20:44:05 +00:00
|
|
|
when_invoked do |*args|
|
|
|
|
check_required_features
|
|
|
|
require 'puppet-strings'
|
|
|
|
|
|
|
|
PuppetStrings::generate(
|
|
|
|
args.count > 1 ? args[0..-2] : PuppetStrings::DEFAULT_SEARCH_PATTERNS,
|
|
|
|
build_generate_options(args.last)
|
|
|
|
)
|
|
|
|
nil
|
2015-10-08 01:22:29 +00:00
|
|
|
end
|
2016-09-09 20:44:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
action(:server) do
|
|
|
|
option '--markup FORMAT' do
|
|
|
|
summary "The markup format to use for docstring text (defaults to 'markdown')."
|
2015-10-08 01:22:29 +00:00
|
|
|
end
|
|
|
|
|
2016-09-09 20:44:05 +00:00
|
|
|
summary 'Runs a local documentation server for the modules in the current Puppet environment.'
|
|
|
|
arguments '[[module_name] ...]'
|
2014-06-01 06:24:27 +00:00
|
|
|
|
2014-05-26 19:02:59 +00:00
|
|
|
when_invoked do |*args|
|
2014-06-01 19:33:34 +00:00
|
|
|
check_required_features
|
2016-09-09 20:44:05 +00:00
|
|
|
require 'puppet-strings'
|
|
|
|
|
|
|
|
modules = args.count > 1 ? args[0..-2] : []
|
2015-07-02 01:47:54 +00:00
|
|
|
|
2016-09-09 20:44:05 +00:00
|
|
|
# Generate documentation for all (or the given) modules
|
|
|
|
module_docs = []
|
|
|
|
environment = Puppet.lookup(:current_environment)
|
|
|
|
environment.modules.each do |mod|
|
|
|
|
next unless modules.empty? || modules.include?(mod.name)
|
|
|
|
db = File.join(mod.path, '.yardoc')
|
|
|
|
patterns = PuppetStrings::DEFAULT_SEARCH_PATTERNS.map do |p|
|
|
|
|
File.join(mod.path, p)
|
|
|
|
end
|
|
|
|
puts "Generating documentation for Puppet module '#{mod.name}'."
|
|
|
|
PuppetStrings.generate(patterns, build_generate_options(args.last, '--db', db))
|
2015-08-04 22:34:13 +00:00
|
|
|
|
2016-09-09 20:44:05 +00:00
|
|
|
# Clear the registry so that the next call to generate has a clean database
|
|
|
|
YARD::Registry.clear
|
|
|
|
|
|
|
|
module_docs << mod.name
|
|
|
|
module_docs << db
|
|
|
|
end
|
|
|
|
|
|
|
|
if module_docs.empty?
|
|
|
|
puts 'No Puppet modules were found to serve documentation for.'
|
|
|
|
return
|
|
|
|
end
|
|
|
|
puts 'Starting YARD documentation server.'
|
|
|
|
PuppetStrings::run_server('-m', *module_docs)
|
|
|
|
nil
|
2014-05-16 17:54:34 +00:00
|
|
|
end
|
|
|
|
end
|
2014-06-01 19:33:34 +00:00
|
|
|
|
2016-09-09 20:44:05 +00:00
|
|
|
# Checks that the required features are installed.
|
|
|
|
# @return [void]
|
|
|
|
def check_required_features
|
|
|
|
raise RuntimeError, "The 'yard' gem must be installed in order to use this face." unless Puppet.features.yard?
|
|
|
|
raise RuntimeError, "The 'rgen' gem must be installed in order to use this face." unless Puppet.features.rgen?
|
|
|
|
raise RuntimeError, 'This face requires Ruby 1.9 or greater.' if RUBY_VERSION.match(/^1\.8/)
|
|
|
|
end
|
2014-06-02 00:37:18 +00:00
|
|
|
|
2016-09-09 20:44:05 +00:00
|
|
|
# Builds the options to PuppetStrings.generate.
|
|
|
|
# @param [Hash] options The Puppet face options hash.
|
|
|
|
# @param [Array] yard_args The additional arguments to pass to YARD.
|
|
|
|
# @return [Hash] Returns the PuppetStrings.generate options hash.
|
|
|
|
def build_generate_options(options = nil, *yard_args)
|
|
|
|
generate_options = {}
|
|
|
|
generate_options[:debug] = Puppet[:debug]
|
|
|
|
generate_options[:backtrace] = Puppet[:trace]
|
|
|
|
generate_options[:yard_args] = yard_args unless yard_args.empty?
|
2014-06-02 00:37:18 +00:00
|
|
|
|
2016-09-09 20:44:05 +00:00
|
|
|
if options
|
|
|
|
markup = options[:markup]
|
|
|
|
generate_options[:markup] = markup if markup
|
|
|
|
json_file = options[:emit_json]
|
|
|
|
generate_options[:json] = json_file if json_file
|
|
|
|
generate_options[:json] = nil if options[:emit_json_stdout]
|
2014-06-02 00:37:18 +00:00
|
|
|
end
|
2016-09-09 20:44:05 +00:00
|
|
|
generate_options
|
2014-06-02 00:37:18 +00:00
|
|
|
end
|
2014-05-16 17:54:34 +00:00
|
|
|
end
|