2014-05-16 17:54:34 +00:00
|
|
|
require 'puppet/face'
|
|
|
|
|
|
|
|
Puppet::Face.define(:yardoc, '0.0.1') do
|
2014-06-01 06:24:27 +00:00
|
|
|
summary "Generate Puppet documentation with YARD."
|
2014-05-16 17:54:34 +00:00
|
|
|
|
2014-06-01 19:33:34 +00:00
|
|
|
def check_required_features
|
|
|
|
unless Puppet.features.yard?
|
|
|
|
raise RuntimeError, "The 'yard' gem must be installed in order to use this face."
|
|
|
|
end
|
|
|
|
|
|
|
|
unless Puppet.features.rgen?
|
|
|
|
raise RuntimeError, "The 'rgen' gem must be installed in order to use this face."
|
|
|
|
end
|
|
|
|
|
|
|
|
if RUBY_VERSION < '1.9' && !Puppet.features.require_relative?
|
|
|
|
raise RuntimeError, "The 'backports' gem must be installed in order to use this face under Ruby 1.8.7."
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-06-03 02:38:19 +00:00
|
|
|
# Maps things like the Puppet `--debug` flag to YARD options.
|
|
|
|
def merge_puppet_args!(yard_args)
|
|
|
|
yard_args.unshift '--debug' if Puppet[:debug]
|
|
|
|
yard_args.unshift '--backtrace' if Puppet[:trace]
|
|
|
|
|
|
|
|
yard_args
|
|
|
|
end
|
|
|
|
|
2014-06-01 19:33:34 +00:00
|
|
|
# A list of globs that generates the default list of module files from which
|
|
|
|
# documentation can be extracted.
|
|
|
|
#
|
|
|
|
# TODO: It would be awesome if we could somehow override/append to the
|
|
|
|
# default file list that YARD uses. Consider an upstream PR for this.
|
2014-06-03 04:55:27 +00:00
|
|
|
MODULE_SOURCEFILES = ['manifests/**/*.pp', 'lib/**/*.rb']
|
2014-06-01 19:33:34 +00:00
|
|
|
|
2014-05-16 17:54:34 +00:00
|
|
|
action(:yardoc) do
|
|
|
|
default
|
|
|
|
|
2014-06-01 19:33:34 +00:00
|
|
|
summary "Generate YARD documentation from files."
|
2014-06-01 06:24:27 +00:00
|
|
|
arguments "[manifest_file.pp ...]"
|
|
|
|
|
2014-05-26 19:02:59 +00:00
|
|
|
when_invoked do |*args|
|
2014-06-01 19:33:34 +00:00
|
|
|
check_required_features
|
2014-05-30 16:59:55 +00:00
|
|
|
|
2014-05-26 19:02:59 +00:00
|
|
|
# The last element of the argument array should be the options hash.
|
|
|
|
#
|
|
|
|
# NOTE: The Puppet Face will throw 'unrecognized option' errors if any
|
|
|
|
# YARD options are passed to it. The best way to approach this problem is
|
|
|
|
# by using the `.yardopts` file. YARD will autoload any options placed in
|
|
|
|
# that file.
|
|
|
|
opts = args.pop
|
|
|
|
|
|
|
|
# For now, assume the remaining positional args are a list of manifest
|
2014-06-03 04:55:27 +00:00
|
|
|
# and ruby files to parse.
|
2014-06-03 02:38:19 +00:00
|
|
|
yard_args = (args.empty? ? MODULE_SOURCEFILES : args)
|
|
|
|
merge_puppet_args!(yard_args)
|
2014-05-26 19:02:59 +00:00
|
|
|
|
2014-05-24 19:54:22 +00:00
|
|
|
require 'puppetx/yardoc/yard/plugin'
|
|
|
|
|
2014-05-26 02:14:52 +00:00
|
|
|
# Hand off to YARD for further processing.
|
2014-06-03 02:38:19 +00:00
|
|
|
YARD::CLI::Yardoc.run(*yard_args)
|
2014-05-16 17:54:34 +00:00
|
|
|
end
|
|
|
|
end
|
2014-06-01 19:33:34 +00:00
|
|
|
|
|
|
|
# NOTE: Modeled after the `yard gems` command which builds doc indicies
|
|
|
|
# (.yardoc directories) for Ruby Gems. Currently lacks the fine-grained
|
|
|
|
# control over where these indicies are created and just dumps them in the
|
|
|
|
# module roots.
|
|
|
|
action(:modules) do
|
|
|
|
summary "Generate YARD indices for a list of modules."
|
|
|
|
arguments "[module-name ...]"
|
|
|
|
|
|
|
|
when_invoked do |*args|
|
|
|
|
check_required_features
|
|
|
|
require 'puppetx/yardoc/yard/plugin'
|
|
|
|
opts = args.pop
|
|
|
|
|
2014-06-10 04:20:56 +00:00
|
|
|
# NOTE: The retrun value of the `module` Face seems to have changed in
|
|
|
|
# 3.6.x. This part of the code will blow up if run under an earlier
|
|
|
|
# version of Puppet.
|
2014-06-01 19:33:34 +00:00
|
|
|
modules = Puppet::Face[:module, :current].list
|
|
|
|
module_list = modules[:modules_by_path].values.flatten
|
|
|
|
|
|
|
|
# TODO: Can use select! if Ruby 1.8.7 support is dropped.
|
|
|
|
module_list = module_list.select {|m| args.include? m.name} unless args.empty?
|
|
|
|
|
2014-06-03 02:38:19 +00:00
|
|
|
# Invoke `yardoc` with -n so that it doesn't generate any HTML output but
|
|
|
|
# does build a `.yardoc` index that other tools can generate output from.
|
|
|
|
yard_args = %w[--no-stats -n] + MODULE_SOURCEFILES
|
|
|
|
merge_puppet_args!(yard_args)
|
|
|
|
|
2014-06-01 19:33:34 +00:00
|
|
|
module_list.each do |m|
|
|
|
|
Dir.chdir(m.path) do
|
2014-06-03 02:38:19 +00:00
|
|
|
YARD::CLI::Yardoc.run(*yard_args)
|
2014-06-01 19:33:34 +00:00
|
|
|
|
|
|
|
# Cear the global Registry so that objects from one module don't
|
|
|
|
# bleed into the next.
|
|
|
|
YARD::Registry.clear
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2014-06-02 00:37:18 +00:00
|
|
|
|
|
|
|
action(:server) do
|
|
|
|
summary "Serve YARD documentation for modules."
|
|
|
|
|
|
|
|
when_invoked do |*args|
|
|
|
|
check_required_features
|
|
|
|
require 'puppetx/yardoc/yard/plugin'
|
|
|
|
opts = args.pop
|
|
|
|
|
|
|
|
# FIXME: This is pretty inefficient as it forcibly re-generates the YARD
|
|
|
|
# indicies each time the server is started. However, it ensures things are
|
|
|
|
# generated properly.
|
|
|
|
module_list = Puppet::Face[:yardoc, :current].modules
|
|
|
|
|
|
|
|
module_tuples = module_list.map do |mod|
|
|
|
|
name = (mod.forge_name || mod.name).gsub('/', '-')
|
|
|
|
yard_index = File.join(mod.path, '.yardoc')
|
|
|
|
|
|
|
|
[name, yard_index]
|
|
|
|
end
|
|
|
|
|
2014-06-03 02:38:19 +00:00
|
|
|
# The `-m` flag means a list of name/path pairs will follow. The name is
|
|
|
|
# used as the module name and the path indicates which `.yardoc` index to
|
|
|
|
# generate documentation from.
|
|
|
|
yard_args = %w[-m] + module_tuples.flatten
|
|
|
|
merge_puppet_args!(yard_args)
|
|
|
|
|
|
|
|
YARD::CLI::Server.run(*yard_args)
|
2014-06-02 00:37:18 +00:00
|
|
|
end
|
|
|
|
end
|
2014-05-16 17:54:34 +00:00
|
|
|
end
|