2016-09-20 11:18:20 +00:00
|
|
|
# Copyright:: Copyright (c) 2015 Chef Software, Inc.
|
|
|
|
# License:: Apache License, Version 2.0
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
#
|
|
|
|
|
2016-09-22 12:46:42 +00:00
|
|
|
require 'erb'
|
|
|
|
require 'ruby-progressbar'
|
|
|
|
require 'fileutils'
|
2018-08-02 15:43:46 +00:00
|
|
|
require 'yaml'
|
2017-05-19 21:49:41 +00:00
|
|
|
require_relative './shared'
|
2016-09-22 12:46:42 +00:00
|
|
|
|
2017-06-15 16:10:47 +00:00
|
|
|
WWW_DIR = File.expand_path(File.join(__dir__, '..', 'www')).freeze
|
|
|
|
DOCS_DIR = File.expand_path(File.join(__dir__, '..', 'docs')).freeze
|
2017-03-22 15:58:12 +00:00
|
|
|
|
2018-08-02 15:43:46 +00:00
|
|
|
begin
|
|
|
|
require 'git'
|
|
|
|
require_relative './contrib'
|
|
|
|
rescue LoadError
|
|
|
|
puts 'contrib tasks are unavailable because the git gem is not available.'
|
|
|
|
end
|
|
|
|
|
2016-09-20 11:22:10 +00:00
|
|
|
class Markdown
|
|
|
|
class << self
|
|
|
|
def h1(msg)
|
|
|
|
"# #{msg}\n\n"
|
|
|
|
end
|
|
|
|
|
|
|
|
def h2(msg)
|
|
|
|
"## #{msg}\n\n"
|
|
|
|
end
|
|
|
|
|
|
|
|
def h3(msg)
|
|
|
|
"### #{msg}\n\n"
|
|
|
|
end
|
|
|
|
|
|
|
|
def code(msg, syntax = nil)
|
|
|
|
"```#{syntax}\n"\
|
|
|
|
"#{msg}\n"\
|
|
|
|
"```\n\n"
|
|
|
|
end
|
|
|
|
|
|
|
|
def li(msg)
|
|
|
|
"* #{msg.gsub("\n", "\n ")}\n"
|
|
|
|
end
|
|
|
|
|
|
|
|
def ul(msg)
|
|
|
|
msg + "\n"
|
|
|
|
end
|
|
|
|
|
|
|
|
def p(msg)
|
|
|
|
"#{msg}\n\n"
|
|
|
|
end
|
|
|
|
|
2016-09-22 12:56:20 +00:00
|
|
|
def a(name, dst = nil)
|
|
|
|
dst ||= name
|
|
|
|
"[#{name}](#{dst})"
|
|
|
|
end
|
|
|
|
|
2016-09-20 11:22:10 +00:00
|
|
|
def suffix
|
|
|
|
'.md'
|
|
|
|
end
|
2016-09-20 11:44:44 +00:00
|
|
|
|
|
|
|
def meta(opts)
|
|
|
|
o = opts.map { |k, v| "#{k}: #{v}" }.join("\n")
|
|
|
|
"---\n#{o}\n---\n\n"
|
|
|
|
end
|
2016-09-20 11:22:10 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-09-20 11:18:20 +00:00
|
|
|
class RST
|
|
|
|
class << self
|
|
|
|
def h1(msg)
|
|
|
|
"=====================================================\n"\
|
|
|
|
"#{msg}\n"\
|
|
|
|
"=====================================================\n\n"\
|
|
|
|
end
|
|
|
|
|
|
|
|
def h2(msg)
|
|
|
|
"#{msg}\n"\
|
|
|
|
"=====================================================\n\n"\
|
|
|
|
end
|
|
|
|
|
|
|
|
def h3(msg)
|
|
|
|
"#{msg}\n"\
|
|
|
|
"-----------------------------------------------------\n\n"\
|
|
|
|
end
|
|
|
|
|
|
|
|
def code(msg, syntax = nil)
|
2016-09-20 11:22:10 +00:00
|
|
|
".. code-block:: #{syntax}\n\n"\
|
2016-09-20 11:18:20 +00:00
|
|
|
" #{msg.gsub("\n", "\n ")}\n\n"
|
|
|
|
end
|
|
|
|
|
2016-09-20 11:22:10 +00:00
|
|
|
def li(msg)
|
|
|
|
"#{msg.gsub("\n", "\n ")}\n\n"
|
|
|
|
end
|
|
|
|
|
|
|
|
def ul(msg)
|
|
|
|
msg
|
|
|
|
end
|
|
|
|
|
2016-09-20 11:18:20 +00:00
|
|
|
def p(msg)
|
|
|
|
"#{msg}\n\n"
|
|
|
|
end
|
2016-09-20 11:22:10 +00:00
|
|
|
|
2016-09-22 12:56:20 +00:00
|
|
|
def a(name, _dst = nil)
|
|
|
|
# FIXME: needs link handling
|
|
|
|
"`#{name}`_"
|
|
|
|
end
|
|
|
|
|
2016-09-20 11:22:10 +00:00
|
|
|
def suffix
|
|
|
|
'.rst'
|
|
|
|
end
|
2016-09-20 11:44:44 +00:00
|
|
|
|
|
|
|
def meta(_o)
|
|
|
|
'' # ignore for now
|
|
|
|
end
|
2016-09-20 11:18:20 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-09-22 12:46:42 +00:00
|
|
|
class ResourceDocs
|
|
|
|
def initialize(root)
|
|
|
|
@paths = {} # cache of paths
|
|
|
|
@root = root # relative root path for all docs
|
|
|
|
end
|
|
|
|
|
|
|
|
def render(path)
|
|
|
|
@paths[path] ||= render_path(path)
|
|
|
|
end
|
|
|
|
|
|
|
|
def partial(x)
|
|
|
|
render(x + '.md.erb')
|
|
|
|
end
|
|
|
|
|
2018-08-02 15:43:46 +00:00
|
|
|
def overview_page(resource_doc_files) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
|
|
|
|
renderer = Markdown
|
|
|
|
markdown = renderer.meta(title: 'InSpec Resources Reference')
|
|
|
|
markdown << renderer.h1('InSpec Resources Reference')
|
|
|
|
markdown << renderer.p('The following list of InSpec resources are available.')
|
|
|
|
|
|
|
|
contrib_config = YAML.load(File.read(File.join(CONTRIB_DIR, 'contrib.yaml')))
|
|
|
|
|
|
|
|
# Build a list of resources keyed on the group they are a part of.
|
|
|
|
# We'll determine the group using regexes.
|
|
|
|
group_regexes = [
|
|
|
|
# These are hardcoded present in the main repo. If they become resource
|
|
|
|
# packs, this should change.
|
|
|
|
{ group_name: 'AWS', regex: /^aws_/ },
|
|
|
|
{ group_name: 'Azure', regex: /^azure(rm)?_/ },
|
|
|
|
]
|
|
|
|
# Also pick up regexes and group names from contrib resource packs.
|
|
|
|
contrib_config['resource_packs'].values.each do |project_info|
|
|
|
|
group_regexes << { group_name: project_info['doc_group_title'], regex: Regexp.new(project_info['resource_file_regex']) }
|
2018-02-17 15:44:42 +00:00
|
|
|
end
|
|
|
|
|
2018-08-02 15:43:46 +00:00
|
|
|
# OK, apply the regexes we have to the resource doc file list we were passed.
|
|
|
|
# doc_file looks like /resources/foo.md.erb - trim off directory and file extension
|
|
|
|
trimmed_doc_files = resource_doc_files.dup.map { |file| File.basename(file).sub(/\.md(\.erb)?$/, '') }
|
|
|
|
resources_by_group = Hash[group_regexes.map { |info| [info[:group_name], []] }] # Initialize each group to an empty array
|
2019-04-11 20:17:04 +00:00
|
|
|
resources_by_group['OS'] = []
|
2018-08-02 15:43:46 +00:00
|
|
|
trimmed_doc_files.each do |doc_file|
|
|
|
|
matched = false
|
|
|
|
group_regexes.each do |group_info|
|
|
|
|
next if matched
|
|
|
|
if doc_file =~ group_info[:regex]
|
|
|
|
resources_by_group[group_info[:group_name]] << doc_file
|
|
|
|
matched = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
# Any resources that don't match a regex are assumed to be 'os' resources.
|
2019-04-11 20:17:04 +00:00
|
|
|
resources_by_group['OS'] << doc_file unless matched
|
2018-08-02 15:43:46 +00:00
|
|
|
end
|
2018-02-19 21:14:01 +00:00
|
|
|
|
2018-08-02 15:43:46 +00:00
|
|
|
# Now transform the resource lists into HTML
|
|
|
|
markdown_resource_links_by_group = {}
|
|
|
|
resources_by_group.each do |group_name, resource_list|
|
|
|
|
markdown_resource_links_by_group[group_name] = resource_list.map do |resource_name|
|
|
|
|
renderer.li(renderer.a(resource_name.gsub('_', '\\_'), 'resources/' + resource_name + '.html'))
|
|
|
|
end.join('')
|
|
|
|
end
|
|
|
|
|
|
|
|
# Remove any groups that have no resource docs.
|
|
|
|
resources_by_group.reject! { |_, resource_list| resource_list.empty? }
|
|
|
|
|
|
|
|
# Generate the big buttons that jump to the section of the page for each group.
|
|
|
|
markdown << '<div class="row columns align">'
|
|
|
|
# "Sorted, except OS is always in first place"
|
2019-04-11 20:17:04 +00:00
|
|
|
ordered_group_names = ['OS'] + resources_by_group.keys.sort.reject { |group_name| group_name == 'OS' }
|
2018-08-02 15:43:46 +00:00
|
|
|
button_template = '<a class="resources-button button btn-lg btn-purple-o shadow margin-right-xs" href="%s">%s</a>'
|
|
|
|
ordered_group_names.each do |group_name|
|
|
|
|
markdown << format(button_template, '#'+(group_name+'-resources').downcase, group_name)
|
|
|
|
markdown << "\n"
|
|
|
|
end
|
|
|
|
markdown << '</div>'
|
|
|
|
|
|
|
|
# Generate the actual long lists of links
|
|
|
|
group_section_header_template = '
|
2018-02-19 21:14:01 +00:00
|
|
|
<div class="brdr-left margin-top-sm margin-under-xs">
|
|
|
|
<h3 class="margin-left-xs"><a id="%s" class="a-purple"><h3 class="a-purple">%s</h3></a></h3>
|
|
|
|
</div>
|
|
|
|
'
|
2018-08-02 15:43:46 +00:00
|
|
|
ordered_group_names.each do |group_name|
|
|
|
|
markdown << format(group_section_header_template, (group_name + '-resources').downcase, group_name)
|
|
|
|
markdown << renderer.ul(markdown_resource_links_by_group[group_name])
|
2018-02-17 15:44:42 +00:00
|
|
|
end
|
|
|
|
|
2018-08-02 15:43:46 +00:00
|
|
|
markdown
|
2018-02-17 15:44:42 +00:00
|
|
|
end
|
|
|
|
|
2016-09-22 12:46:42 +00:00
|
|
|
private
|
|
|
|
|
2018-02-17 15:44:42 +00:00
|
|
|
def namify(n)
|
|
|
|
n.capitalize.gsub(/\baws\b/i, 'AWS')
|
|
|
|
end
|
|
|
|
|
2016-09-22 12:46:42 +00:00
|
|
|
def render_path(path)
|
|
|
|
abs = File.join(@root, path)
|
2017-02-08 22:49:16 +00:00
|
|
|
raise "Can't find file to render in #{abs}" unless File.file?(abs)
|
2016-09-22 12:46:42 +00:00
|
|
|
|
|
|
|
ERB.new(File.read(abs)).result(binding)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-11-21 07:49:41 +00:00
|
|
|
namespace :docs do # rubocop:disable Metrics/BlockLength
|
2016-09-20 11:18:20 +00:00
|
|
|
desc 'Create cli docs'
|
|
|
|
task :cli do
|
2016-09-22 11:44:09 +00:00
|
|
|
# formatter for the output file
|
2016-09-22 11:41:20 +00:00
|
|
|
f = Markdown
|
2016-09-22 11:44:09 +00:00
|
|
|
# list of subcommands we ignore; these are e.g. plugins
|
|
|
|
skip_commands = %w{scap}
|
|
|
|
|
2016-09-20 11:44:44 +00:00
|
|
|
res = f.meta(title: 'About the InSpec CLI')
|
|
|
|
res << f.h1('InSpec CLI')
|
|
|
|
res << f.p('Use the InSpec CLI to run tests and audits against targets '\
|
|
|
|
'using local, SSH, WinRM, or Docker connections.')
|
2016-09-20 11:18:20 +00:00
|
|
|
|
|
|
|
require 'inspec/cli'
|
|
|
|
cmds = Inspec::InspecCLI.all_commands
|
|
|
|
cmds.keys.sort.each do |key|
|
2016-09-22 11:44:09 +00:00
|
|
|
next if skip_commands.include? key
|
2016-09-20 11:18:20 +00:00
|
|
|
cmd = cmds[key]
|
|
|
|
|
|
|
|
res << f.h2(cmd.usage.split.first)
|
|
|
|
res << f.p(cmd.description.capitalize)
|
2018-06-29 12:43:34 +00:00
|
|
|
if cmd.long_description
|
|
|
|
res << f.p(cmd.long_description)
|
|
|
|
end
|
2016-09-20 11:18:20 +00:00
|
|
|
|
|
|
|
res << f.h3('Syntax')
|
|
|
|
res << f.p('This subcommand has the following syntax:')
|
|
|
|
res << f.code("$ inspec #{cmd.usage}", 'bash')
|
|
|
|
|
2017-11-21 07:49:41 +00:00
|
|
|
opts = cmd.options.reject { |_, o| o.hide }
|
2016-09-20 11:18:20 +00:00
|
|
|
unless opts.empty?
|
|
|
|
res << f.h3('Options') + f.p('This subcommand has additional options:')
|
|
|
|
|
2016-09-20 11:22:10 +00:00
|
|
|
list = ''
|
2016-09-20 11:18:20 +00:00
|
|
|
opts.keys.sort.each do |option|
|
|
|
|
opt = cmd.options[option]
|
|
|
|
# TODO: remove when UX of help is reworked 1.0
|
|
|
|
usage = opt.usage.split(', ')
|
|
|
|
.map { |x| x.tr('[]', '') }
|
|
|
|
.map { |x| x.start_with?('-') ? x : '-'+x }
|
|
|
|
.map { |x| '``' + x + '``' }
|
2016-09-22 11:44:09 +00:00
|
|
|
list << f.li("#{usage.join(', ')} \n#{opt.description}")
|
2016-09-20 11:22:10 +00:00
|
|
|
end.join
|
|
|
|
res << f.ul(list)
|
2016-09-20 11:18:20 +00:00
|
|
|
end
|
2016-09-20 11:22:10 +00:00
|
|
|
|
|
|
|
# FIXME: for some reason we have extra lines in our RST; needs investigation
|
|
|
|
res << "\n\n" if f == RST
|
2016-09-20 11:18:20 +00:00
|
|
|
end
|
|
|
|
|
2017-03-22 15:58:12 +00:00
|
|
|
dst = File.join(DOCS_DIR, "cli#{f.suffix}")
|
2016-09-20 11:18:20 +00:00
|
|
|
File.write(dst, res)
|
|
|
|
puts "Documentation generated in #{dst.inspect}"
|
|
|
|
end
|
2016-09-22 12:46:42 +00:00
|
|
|
|
|
|
|
desc 'Create resources docs'
|
2018-08-02 15:43:46 +00:00
|
|
|
# This task injects the contrib:cleanup_docs as a followup
|
|
|
|
# to the actual doc building.
|
|
|
|
task resources: [:resources_actual, :'contrib:cleanup_docs']
|
|
|
|
|
|
|
|
task resources_actual: [:clean, :'contrib:copy_docs'] do
|
2017-03-22 15:58:12 +00:00
|
|
|
src = DOCS_DIR
|
|
|
|
dst = File.join(WWW_DIR, 'source', 'docs', 'reference', 'resources')
|
2016-09-25 23:20:04 +00:00
|
|
|
FileUtils.mkdir_p(dst)
|
|
|
|
|
2016-09-22 12:46:42 +00:00
|
|
|
docs = ResourceDocs.new(src)
|
2018-08-02 15:43:46 +00:00
|
|
|
resources = Dir.glob([File.join(src, 'resources/*.md.erb'), File.join(src, 'resources/*.md')])
|
|
|
|
.map { |x| x.sub(/^#{src}/, '') }
|
|
|
|
.sort
|
2017-10-26 21:02:48 +00:00
|
|
|
puts "Found #{resources.length} resource docs"
|
2016-09-22 12:46:42 +00:00
|
|
|
puts "Rendering docs to #{dst}/"
|
|
|
|
|
2018-02-17 15:44:42 +00:00
|
|
|
# Render all resources
|
2016-09-22 12:46:42 +00:00
|
|
|
progressbar = ProgressBar.create(total: resources.length, title: 'Rendering')
|
|
|
|
resources.each do |file|
|
2016-09-25 23:20:04 +00:00
|
|
|
progressbar.log(' '+file)
|
2018-08-02 15:43:46 +00:00
|
|
|
dst_name = File.basename(file).sub(/\.md(\.erb)?$/, '.html.md')
|
2016-09-22 12:46:42 +00:00
|
|
|
res = docs.render(file)
|
|
|
|
File.write(File.join(dst, dst_name), res)
|
|
|
|
progressbar.increment
|
|
|
|
end
|
|
|
|
progressbar.finish
|
2016-09-22 12:56:20 +00:00
|
|
|
|
2018-02-17 15:44:42 +00:00
|
|
|
# Create a resource summary markdown doc
|
2016-09-22 12:56:20 +00:00
|
|
|
dst = File.join(src, 'resources.md')
|
|
|
|
puts "Create #{dst}"
|
2018-02-17 15:44:42 +00:00
|
|
|
File.write(dst, docs.overview_page(resources))
|
2016-09-25 23:20:04 +00:00
|
|
|
end
|
2016-09-22 12:56:20 +00:00
|
|
|
|
2016-09-25 23:20:04 +00:00
|
|
|
desc 'Clean all rendered docs from www/'
|
|
|
|
task :clean do
|
2017-03-22 15:58:12 +00:00
|
|
|
dst = File.join(WWW_DIR, 'source', 'docs', 'reference')
|
2016-09-25 23:20:04 +00:00
|
|
|
puts "Clean up #{dst}"
|
|
|
|
FileUtils.rm_rf(dst) if File.exist?(dst)
|
|
|
|
FileUtils.mkdir_p(dst)
|
|
|
|
end
|
|
|
|
|
|
|
|
desc 'Copy fixed doc files'
|
|
|
|
task copy: [:clean, :resources] do
|
2017-03-22 15:58:12 +00:00
|
|
|
src = DOCS_DIR
|
|
|
|
dst = File.join(WWW_DIR, 'source', 'docs', 'reference')
|
2016-09-25 23:20:04 +00:00
|
|
|
files = Dir[File.join(src, '*.md')]
|
|
|
|
|
|
|
|
progressbar = ProgressBar.create(total: files.length, title: 'Copying')
|
|
|
|
files.each do |path|
|
|
|
|
name = File.basename(path).sub(/\.md$/, '.html.md')
|
|
|
|
progressbar.log(' '+File.join(dst, name))
|
|
|
|
FileUtils.cp(path, File.join(dst, name))
|
|
|
|
progressbar.increment
|
|
|
|
end
|
|
|
|
progressbar.finish
|
2016-09-22 12:46:42 +00:00
|
|
|
end
|
2016-09-20 11:18:20 +00:00
|
|
|
end
|
2016-09-22 11:41:20 +00:00
|
|
|
|
|
|
|
def run_tasks_in_namespace(ns)
|
|
|
|
Rake.application.in_namespace(ns) do |x|
|
|
|
|
x.tasks.each do |task|
|
|
|
|
puts "----> #{task}"
|
|
|
|
task.invoke
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
desc 'Create all docs in docs/ from source code'
|
|
|
|
task :docs do
|
|
|
|
run_tasks_in_namespace :docs
|
2017-03-22 15:58:12 +00:00
|
|
|
Verify.file(File.join(WWW_DIR, 'source', 'docs', 'reference', 'README.html.md'))
|
|
|
|
Verify.file(File.join(WWW_DIR, 'source', 'docs', 'reference', 'cli.html.md'))
|
|
|
|
Verify.file(File.join(WWW_DIR, 'source', 'docs', 'reference', 'resources.html.md'))
|
2016-09-22 11:41:20 +00:00
|
|
|
end
|