2015-08-03 13:09:07 +01:00
|
|
|
require "formula"
|
2014-09-20 15:30:44 +01:00
|
|
|
|
|
|
|
module Homebrew
|
2016-04-17 02:28:58 +02:00
|
|
|
SOURCE_PATH = HOMEBREW_LIBRARY_PATH/"manpages"
|
|
|
|
TARGET_MAN_PATH = HOMEBREW_REPOSITORY/"share/man/man1"
|
|
|
|
TARGET_DOC_PATH = HOMEBREW_REPOSITORY/"share/doc/homebrew"
|
2014-09-20 15:30:44 +01:00
|
|
|
|
|
|
|
def man
|
2016-04-17 02:28:58 +02:00
|
|
|
raise UsageError unless ARGV.named.empty?
|
2015-06-15 12:41:12 +02:00
|
|
|
|
|
|
|
if ARGV.flag? "--link"
|
2016-04-17 02:28:58 +02:00
|
|
|
link_man_pages
|
2015-06-15 12:41:12 +02:00
|
|
|
else
|
2016-04-17 02:28:58 +02:00
|
|
|
regenerate_man_pages
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def link_man_pages
|
|
|
|
linked_path = HOMEBREW_PREFIX/"share/man/man1"
|
|
|
|
|
|
|
|
if TARGET_MAN_PATH == linked_path
|
|
|
|
odie "The target path is the same as the linked one."
|
|
|
|
end
|
|
|
|
|
|
|
|
Dir["#{TARGET_MAN_PATH}/*.1"].each do |page|
|
|
|
|
FileUtils.ln_s page, linked_path
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def regenerate_man_pages
|
|
|
|
Homebrew.install_gem_setup_path! "ronn"
|
|
|
|
|
|
|
|
convert_man_page("brew.1", build_man_page)
|
|
|
|
end
|
|
|
|
|
|
|
|
def build_man_page
|
|
|
|
header = (SOURCE_PATH/"header.1.md").read
|
|
|
|
footer = (SOURCE_PATH/"footer.1.md").read
|
|
|
|
|
|
|
|
commands = Pathname.glob("#{HOMEBREW_LIBRARY_PATH}/cmd/*.{rb,sh}").
|
|
|
|
sort_by { |source_file| source_file.basename.sub(/\.(rb|sh)$/, "") }.
|
|
|
|
map { |source_file|
|
|
|
|
source_file.read.
|
|
|
|
split("\n").
|
|
|
|
grep(/^#:/).
|
|
|
|
map { |line| line.slice(2..-1) }.
|
|
|
|
join("\n")
|
|
|
|
}.
|
|
|
|
reject { |s| s.strip.empty? }.
|
|
|
|
join("\n\n")
|
|
|
|
|
|
|
|
header + commands + footer
|
|
|
|
end
|
|
|
|
|
|
|
|
def convert_man_page(page, contents)
|
|
|
|
source = SOURCE_PATH/"#{page}.md"
|
|
|
|
source.atomic_write(contents)
|
|
|
|
|
|
|
|
convert_with_ronn(source, TARGET_DOC_PATH/"#{page}.html")
|
|
|
|
convert_with_ronn(source, TARGET_MAN_PATH/page)
|
|
|
|
end
|
|
|
|
|
|
|
|
def convert_with_ronn(source, target)
|
|
|
|
shared_args = %W[
|
|
|
|
--pipe
|
|
|
|
--organization=Homebrew
|
|
|
|
--manual=brew
|
|
|
|
#{source}
|
|
|
|
]
|
|
|
|
|
|
|
|
format_flag, format_desc = target_path_to_format(target)
|
|
|
|
|
|
|
|
puts "Writing #{format_desc} to #{target}"
|
|
|
|
target.atomic_write Utils.popen_read("ronn", format_flag, *shared_args)
|
|
|
|
end
|
|
|
|
|
|
|
|
def target_path_to_format(target)
|
|
|
|
case target.basename
|
|
|
|
when /\.html?$/ then ["--fragment", "HTML fragment"]
|
|
|
|
when /\.\d$/ then ["--roff", "man page"]
|
|
|
|
else
|
|
|
|
odie "Failed to infer output format from '#{target.basename}'."
|
2014-09-20 15:30:44 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|