man: refactor and fix code style
Split monolithic method into more manageable chunks and fix code style.
This commit is contained in:
parent
8e42077a8e
commit
291ff3cc42
@ -1,33 +1,45 @@
|
|||||||
require "formula"
|
require "formula"
|
||||||
|
|
||||||
module Homebrew
|
module Homebrew
|
||||||
SOURCE_PATH=HOMEBREW_REPOSITORY/"Library/Homebrew/manpages"
|
SOURCE_PATH = HOMEBREW_LIBRARY_PATH/"manpages"
|
||||||
TARGET_PATH=HOMEBREW_REPOSITORY/"share/man/man1"
|
TARGET_MAN_PATH = HOMEBREW_REPOSITORY/"share/man/man1"
|
||||||
DOC_PATH=HOMEBREW_REPOSITORY/"share/doc/homebrew"
|
TARGET_DOC_PATH = HOMEBREW_REPOSITORY/"share/doc/homebrew"
|
||||||
LINKED_PATH=HOMEBREW_PREFIX/"share/man/man1"
|
|
||||||
|
|
||||||
def man
|
def man
|
||||||
abort <<-EOS.undent unless ARGV.named.empty?
|
raise UsageError unless ARGV.named.empty?
|
||||||
This command updates the brew manpage and does not take formula names.
|
|
||||||
EOS
|
|
||||||
|
|
||||||
if ARGV.flag? "--link"
|
if ARGV.flag? "--link"
|
||||||
abort <<-EOS.undent if TARGET_PATH == LINKED_PATH
|
link_man_pages
|
||||||
The target path is the same as the linked one, aborting.
|
|
||||||
EOS
|
|
||||||
Dir["#{TARGET_PATH}/*.1"].each do |page|
|
|
||||||
FileUtils.ln_s page, LINKED_PATH
|
|
||||||
return
|
|
||||||
end
|
|
||||||
else
|
else
|
||||||
|
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"
|
Homebrew.install_gem_setup_path! "ronn"
|
||||||
|
|
||||||
puts "Writing HTML fragments to #{DOC_PATH}"
|
convert_man_page("brew.1", build_man_page)
|
||||||
puts "Writing manpages to #{TARGET_PATH}"
|
end
|
||||||
|
|
||||||
|
def build_man_page
|
||||||
header = (SOURCE_PATH/"header.1.md").read
|
header = (SOURCE_PATH/"header.1.md").read
|
||||||
footer = (SOURCE_PATH/"footer.1.md").read
|
footer = (SOURCE_PATH/"footer.1.md").read
|
||||||
sub_commands = Pathname.glob("#{HOMEBREW_LIBRARY_PATH}/cmd/*.{rb,sh}").
|
|
||||||
|
commands = Pathname.glob("#{HOMEBREW_LIBRARY_PATH}/cmd/*.{rb,sh}").
|
||||||
sort_by { |source_file| source_file.basename.sub(/\.(rb|sh)$/, "") }.
|
sort_by { |source_file| source_file.basename.sub(/\.(rb|sh)$/, "") }.
|
||||||
map { |source_file|
|
map { |source_file|
|
||||||
source_file.read.
|
source_file.read.
|
||||||
@ -39,21 +51,37 @@ module Homebrew
|
|||||||
reject { |s| s.strip.empty? }.
|
reject { |s| s.strip.empty? }.
|
||||||
join("\n\n")
|
join("\n\n")
|
||||||
|
|
||||||
target_md = SOURCE_PATH/"brew.1.md"
|
header + commands + footer
|
||||||
target_md.atomic_write(header + sub_commands + footer)
|
end
|
||||||
|
|
||||||
args = %W[
|
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
|
--pipe
|
||||||
--organization=Homebrew
|
--organization=Homebrew
|
||||||
--manual=brew
|
--manual=brew
|
||||||
#{SOURCE_PATH}/brew.1.md
|
#{source}
|
||||||
]
|
]
|
||||||
|
|
||||||
target_html = DOC_PATH/"brew.1.html"
|
format_flag, format_desc = target_path_to_format(target)
|
||||||
target_html.atomic_write Utils.popen_read("ronn", "--fragment", *args)
|
|
||||||
|
|
||||||
target_man = TARGET_PATH/"brew.1"
|
puts "Writing #{format_desc} to #{target}"
|
||||||
target_man.atomic_write Utils.popen_read("ronn", "--roff", *args)
|
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}'."
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user