| 
									
										
										
										
											2016-09-08 09:05:00 +01:00
										 |  |  | #:  * `man`: | 
					
						
							|  |  |  | #:    Generate Homebrew's manpages. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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-08-24 10:08:54 +01:00
										 |  |  |       odie "`brew man --link` is now done automatically by `brew update`." | 
					
						
							| 
									
										
										
										
											2015-06-15 12:41:12 +02:00
										 |  |  |     else | 
					
						
							| 
									
										
										
										
											2016-04-17 02:28:58 +02:00
										 |  |  |       regenerate_man_pages | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   private | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   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-08-20 16:36:34 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-22 01:49:12 +02:00
										 |  |  |     cask_markup = (HOMEBREW_LIBRARY/"Homebrew/manpages/brew-cask.1.md").read | 
					
						
							| 
									
										
										
										
											2016-08-20 16:36:34 +01:00
										 |  |  |     convert_man_page(cask_markup, TARGET_MAN_PATH/"brew-cask.1") | 
					
						
							| 
									
										
										
										
											2016-04-17 02:28:58 +02:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-05 21:46:40 +01:00
										 |  |  |   def path_glob_commands(glob) | 
					
						
							| 
									
										
										
										
											2016-09-11 17:41:51 +01:00
										 |  |  |     Pathname.glob(glob) | 
					
						
							|  |  |  |             .sort_by { |source_file| sort_key_for_path(source_file) } | 
					
						
							|  |  |  |             .map do |source_file| | 
					
						
							|  |  |  |       source_file.read.lines | 
					
						
							|  |  |  |                  .grep(/^#:/) | 
					
						
							|  |  |  |                  .map { |line| line.slice(2..-1) } | 
					
						
							|  |  |  |                  .join | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |             .reject { |s| s.strip.empty? || s.include?("@hide_from_man_page") } | 
					
						
							| 
									
										
										
										
											2016-09-05 21:46:40 +01:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def build_man_page | 
					
						
							|  |  |  |     template = (SOURCE_PATH/"brew.1.md.erb").read | 
					
						
							|  |  |  |     variables = OpenStruct.new | 
					
						
							| 
									
										
										
										
											2016-04-17 02:28:58 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-05 21:46:40 +01:00
										 |  |  |     variables[:commands] = path_glob_commands("#{HOMEBREW_LIBRARY_PATH}/cmd/*.{rb,sh}") | 
					
						
							|  |  |  |     variables[:developer_commands] = path_glob_commands("#{HOMEBREW_LIBRARY_PATH}/dev-cmd/*.{rb,sh}") | 
					
						
							| 
									
										
										
										
											2016-09-11 17:41:51 +01:00
										 |  |  |     variables[:maintainers] = (HOMEBREW_REPOSITORY/"README.md") | 
					
						
							|  |  |  |                               .read[/Homebrew's current maintainers are (.*)\./, 1] | 
					
						
							|  |  |  |                               .scan(/\[([^\]]*)\]/).flatten | 
					
						
							| 
									
										
										
										
											2016-06-14 21:01:50 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-11 17:41:51 +01: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 | 
					
						
							| 
									
										
										
										
											2016-08-20 16:36:34 +01:00
										 |  |  |       --manual=#{target.basename(".1")} | 
					
						
							| 
									
										
										
										
											2016-04-17 02:28:58 +02:00
										 |  |  |     ] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     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 |