2015-08-03 13:09:07 +01:00
|
|
|
require "formula"
|
2016-04-17 05:33:21 +02:00
|
|
|
require "erb"
|
2016-04-19 19:43:32 +08:00
|
|
|
require "ostruct"
|
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"
|
|
|
|
|
2016-04-17 03:04:27 +02:00
|
|
|
markup = build_man_page
|
|
|
|
convert_man_page(markup, TARGET_DOC_PATH/"brew.1.html")
|
|
|
|
convert_man_page(markup, TARGET_MAN_PATH/"brew.1")
|
2016-04-17 02:28:58 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def build_man_page
|
2016-04-17 05:33:21 +02:00
|
|
|
template = (SOURCE_PATH/"brew.1.md.erb").read
|
2016-04-19 19:43:32 +08:00
|
|
|
variables = OpenStruct.new
|
2016-04-17 02:28:58 +02:00
|
|
|
|
2016-04-19 19:43:32 +08:00
|
|
|
variables[:commands] = Pathname.glob("#{HOMEBREW_LIBRARY_PATH}/cmd/*.{rb,sh}").
|
2016-04-17 09:36:48 +02:00
|
|
|
sort_by { |source_file| sort_key_for_path(source_file) }.
|
2016-04-17 02:28:58 +02:00
|
|
|
map { |source_file|
|
2016-04-17 05:33:21 +02:00
|
|
|
source_file.read.lines.
|
2016-04-17 02:28:58 +02:00
|
|
|
grep(/^#:/).
|
|
|
|
map { |line| line.slice(2..-1) }.
|
2016-04-17 05:33:21 +02:00
|
|
|
join
|
2016-04-17 02:28:58 +02:00
|
|
|
}.
|
2016-06-28 08:08:12 -07:00
|
|
|
reject { |s| s.strip.empty? || s.include?("@hide_from_man_page") }
|
2016-04-17 02:28:58 +02:00
|
|
|
|
2016-06-14 21:01:50 +08:00
|
|
|
variables[:maintainers] = (HOMEBREW_REPOSITORY/"README.md").
|
|
|
|
read[/Homebrew's current maintainers are (.*)\./, 1].
|
|
|
|
scan(/\[([^\]]*)\]/).flatten
|
|
|
|
|
2016-04-19 19:43:32 +08:00
|
|
|
ERB.new(template, nil, ">").result(variables.instance_eval{ binding })
|
2016-04-17 02:28:58 +02:00
|
|
|
end
|
|
|
|
|
2016-04-17 09:36:48 +02:00
|
|
|
def sort_key_for_path(path)
|
|
|
|
# Options after regular commands (`~` comes after `z` in ASCII table).
|
|
|
|
path.basename.to_s.sub(/\.(rb|sh)$/, "").sub(/^--/, "~~")
|
|
|
|
end
|
|
|
|
|
2016-04-17 03:04:27 +02:00
|
|
|
def convert_man_page(markup, target)
|
2016-04-17 02:28:58 +02:00
|
|
|
shared_args = %W[
|
|
|
|
--pipe
|
|
|
|
--organization=Homebrew
|
|
|
|
--manual=brew
|
|
|
|
]
|
|
|
|
|
|
|
|
format_flag, format_desc = target_path_to_format(target)
|
|
|
|
|
|
|
|
puts "Writing #{format_desc} to #{target}"
|
2016-04-17 03:04:27 +02:00
|
|
|
Utils.popen(["ronn", format_flag] + shared_args, "rb+") do |ronn|
|
|
|
|
ronn.write markup
|
|
|
|
ronn.close_write
|
|
|
|
target.atomic_write ronn.read
|
|
|
|
end
|
2016-04-17 02:28:58 +02:00
|
|
|
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
|