| 
									
										
										
										
											2016-09-28 03:28:20 -07:00
										 |  |  | #:  * `man` [`--fail-if-changed`]: | 
					
						
							| 
									
										
										
										
											2016-09-08 09:05:00 +01:00
										 |  |  | #:    Generate Homebrew's manpages. | 
					
						
							| 
									
										
										
										
											2016-09-28 03:28:20 -07:00
										 |  |  | #: | 
					
						
							|  |  |  | #:    If `--fail-if-changed` is passed, the command will return a failing | 
					
						
							|  |  |  | #:    status code if changes are detected in the manpage outputs. | 
					
						
							|  |  |  | #:    This can be used for CI to be notified when the manpages are out of date. | 
					
						
							| 
									
										
										
										
											2016-10-01 12:19:51 +01:00
										 |  |  | #:    Additionally, the date used in new manpages will match those in the existing | 
					
						
							|  |  |  | #:    manpages (to allow comparison without factoring in the date). | 
					
						
							| 
									
										
										
										
											2016-09-08 09:05:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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-09-26 01:44:51 +02:00
										 |  |  |   module_function | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-17 02:28:58 +02:00
										 |  |  |   SOURCE_PATH = HOMEBREW_LIBRARY_PATH/"manpages" | 
					
						
							| 
									
										
										
										
											2016-09-19 19:58:39 +01:00
										 |  |  |   TARGET_MAN_PATH = HOMEBREW_REPOSITORY/"manpages" | 
					
						
							|  |  |  |   TARGET_DOC_PATH = HOMEBREW_REPOSITORY/"docs" | 
					
						
							| 
									
										
										
										
											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`." | 
					
						
							| 
									
										
										
										
											2016-04-17 02:28:58 +02:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2016-09-28 03:28:20 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-23 18:48:51 +00:00
										 |  |  |     regenerate_man_pages | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-28 03:28:20 -07:00
										 |  |  |     if system "git", "-C", HOMEBREW_REPOSITORY, "diff", "--quiet", "docs/brew.1.html", "manpages" | 
					
						
							|  |  |  |       puts "No changes to manpage output detected." | 
					
						
							|  |  |  |     elsif ARGV.include?("--fail-if-changed") | 
					
						
							|  |  |  |       Homebrew.failed = true | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2016-04-17 02:28:58 +02:00
										 |  |  |   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-08-20 16:36:34 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-23 01:22:54 -04:00
										 |  |  |     cask_markup = (SOURCE_PATH/"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-20 09:31:06 +01:00
										 |  |  |     readme = HOMEBREW_REPOSITORY/"README.md" | 
					
						
							| 
									
										
										
										
											2016-09-20 11:50:02 +02:00
										 |  |  |     variables[:lead_maintainer] = readme.read[/(Homebrew's lead maintainer .*\.)/, 1] | 
					
						
							|  |  |  |                                         .gsub(/\[([^\]]+)\]\([^)]+\)/, '\1') | 
					
						
							|  |  |  |     variables[:maintainers] = readme.read[/(Homebrew's current maintainers .*\.)/, 1] | 
					
						
							|  |  |  |                                     .gsub(/\[([^\]]+)\]\([^)]+\)/, '\1') | 
					
						
							|  |  |  |     variables[:former_maintainers] = readme.read[/(Former maintainers .*\.)/, 1] | 
					
						
							|  |  |  |                                            .gsub(/\[([^\]]+)\]\([^)]+\)/, '\1') | 
					
						
							| 
									
										
										
										
											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-10-01 11:49:39 +01:00
										 |  |  |     manual = target.basename(".1") | 
					
						
							|  |  |  |     organisation = "Homebrew" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Set the manpage date to the existing one if we're checking for changes. | 
					
						
							|  |  |  |     # This avoids the only change being e.g. a new date. | 
					
						
							|  |  |  |     date = if ARGV.include?("--fail-if-changed") && | 
					
						
							|  |  |  |               target.extname == ".1" && target.exist? | 
					
						
							|  |  |  |       /"(\d{1,2})" "([A-Z][a-z]+) (\d{4})" "#{organisation}" "#{manual}"/ =~ target.read | 
					
						
							|  |  |  |       Date.parse("#{$1} #{$2} #{$3}") | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |       Date.today | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |     date = date.strftime("%Y-%m-%d") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-17 02:28:58 +02:00
										 |  |  |     shared_args = %W[
 | 
					
						
							|  |  |  |       --pipe | 
					
						
							| 
									
										
										
										
											2016-10-01 11:49:39 +01:00
										 |  |  |       --organization=#{organisation} | 
					
						
							| 
									
										
										
										
											2016-08-20 16:36:34 +01:00
										 |  |  |       --manual=#{target.basename(".1")} | 
					
						
							| 
									
										
										
										
											2016-10-01 11:49:39 +01:00
										 |  |  |       --date=#{date} | 
					
						
							| 
									
										
										
										
											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 |