| 
									
										
										
										
											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" | 
					
						
							| 
									
										
										
										
											2018-03-24 21:26:16 +05:30
										 |  |  | require "cli_parser" | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							| 
									
										
										
										
											2018-05-05 18:40:01 +05:30
										 |  |  |     Homebrew::CLI::Parser.parse do | 
					
						
							| 
									
										
										
										
											2018-03-24 21:26:16 +05:30
										 |  |  |       switch "--fail-if-changed" | 
					
						
							|  |  |  |       switch "--link" | 
					
						
							| 
									
										
										
										
											2018-03-25 17:48:22 +05:30
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2018-03-24 21:26:16 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-17 02:28:58 +02:00
										 |  |  |     raise UsageError unless ARGV.named.empty? | 
					
						
							| 
									
										
										
										
											2015-06-15 12:41:12 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-05 18:40:01 +05:30
										 |  |  |     if args.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 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-22 21:37:09 +00:00
										 |  |  |     if system "git", "-C", HOMEBREW_REPOSITORY, "diff", "--quiet", "docs/Manpage.md", "manpages" | 
					
						
							| 
									
										
										
										
											2016-09-28 03:28:20 -07:00
										 |  |  |       puts "No changes to manpage output detected." | 
					
						
							| 
									
										
										
										
											2018-05-05 18:40:01 +05:30
										 |  |  |     elsif args.fail_if_changed? | 
					
						
							| 
									
										
										
										
											2016-09-28 03:28:20 -07:00
										 |  |  |       Homebrew.failed = true | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2016-04-17 02:28:58 +02:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def regenerate_man_pages | 
					
						
							| 
									
										
										
										
											2017-05-07 17:28:39 +01:00
										 |  |  |     Homebrew.install_gem_setup_path! "ronn" | 
					
						
							| 
									
										
										
										
											2016-04-17 02:28:58 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-17 03:04:27 +02:00
										 |  |  |     markup = build_man_page | 
					
						
							| 
									
										
										
										
											2017-03-22 21:37:09 +00:00
										 |  |  |     convert_man_page(markup, TARGET_DOC_PATH/"Manpage.md") | 
					
						
							| 
									
										
										
										
											2016-04-17 03:04:27 +02:00
										 |  |  |     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) } | 
					
						
							| 
									
										
										
										
											2017-10-07 00:31:28 +02:00
										 |  |  |             .map(&:read).map(&:lines) | 
					
						
							|  |  |  |             .map { |lines| lines.grep(/^#:/).map { |line| line.slice(2..-1) }.join } | 
					
						
							|  |  |  |             .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') | 
					
						
							| 
									
										
										
										
											2018-08-02 13:26:02 +01:00
										 |  |  |     variables[:leadership] = readme.read[/(Homebrew's project leadership committee .*\.)/, 1] | 
					
						
							|  |  |  |                                    .gsub(/\[([^\]]+)\]\([^)]+\)/, '\1') | 
					
						
							| 
									
										
										
										
											2017-07-15 12:57:34 +01:00
										 |  |  |     variables[:core_maintainer] = readme.read[%r{(Homebrew/homebrew-core's lead maintainer .*\.)}, 1] | 
					
						
							|  |  |  |                                         .gsub(/\[([^\]]+)\]\([^)]+\)/, '\1') | 
					
						
							| 
									
										
										
										
											2018-01-18 15:45:51 +00:00
										 |  |  |     variables[:brew_maintainers] = readme.read[%r{(Homebrew/brew's other current maintainers .*\.)}, 1] | 
					
						
							|  |  |  |                                          .gsub(/\[([^\]]+)\]\([^)]+\)/, '\1') | 
					
						
							|  |  |  |     variables[:core_maintainers] = readme.read[%r{(Homebrew/homebrew-core's other current maintainers .*\.)}, 1] | 
					
						
							|  |  |  |                                          .gsub(/\[([^\]]+)\]\([^)]+\)/, '\1') | 
					
						
							| 
									
										
										
										
											2016-09-20 11:50:02 +02:00
										 |  |  |     variables[:former_maintainers] = readme.read[/(Former maintainers .*\.)/, 1] | 
					
						
							|  |  |  |                                            .gsub(/\[([^\]]+)\]\([^)]+\)/, '\1') | 
					
						
							| 
									
										
										
										
											2016-06-14 21:01:50 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-15 16:25:14 +00:00
										 |  |  |     variables[:homebrew_bundle] = help_output(:bundle) | 
					
						
							|  |  |  |     variables[:homebrew_services] = help_output(:services) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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. | 
					
						
							| 
									
										
										
										
											2018-05-05 18:40:01 +05:30
										 |  |  |     date = if args.fail_if_changed? && | 
					
						
							| 
									
										
										
										
											2016-10-01 11:49:39 +01:00
										 |  |  |               target.extname == ".1" && target.exist? | 
					
						
							|  |  |  |       /"(\d{1,2})" "([A-Z][a-z]+) (\d{4})" "#{organisation}" "#{manual}"/ =~ target.read | 
					
						
							| 
									
										
										
										
											2017-06-10 20:23:20 +03:00
										 |  |  |       Date.parse("#{Regexp.last_match(1)} #{Regexp.last_match(2)} #{Regexp.last_match(3)}") | 
					
						
							| 
									
										
										
										
											2016-10-01 11:49:39 +01:00
										 |  |  |     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 | 
					
						
							| 
									
										
										
										
											2017-04-02 10:13:38 +01:00
										 |  |  |       ronn_output = ronn.read | 
					
						
							| 
									
										
										
										
											2018-07-20 09:47:28 +01:00
										 |  |  |       odie "Got no output from ronn!" unless ronn_output | 
					
						
							| 
									
										
										
										
											2018-02-01 17:17:09 -05:00
										 |  |  |       ronn_output.gsub!(%r{</var>`(?=[.!?,;:]?\s)}, "").gsub!(%r{</?var>}, "`") if format_flag == "--markdown" | 
					
						
							| 
									
										
										
										
											2017-04-02 10:13:38 +01:00
										 |  |  |       target.atomic_write ronn_output | 
					
						
							| 
									
										
										
										
											2016-04-17 03:04:27 +02:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2016-04-17 02:28:58 +02:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-15 16:25:14 +00:00
										 |  |  |   def help_output(command) | 
					
						
							|  |  |  |     tap = Tap.fetch("Homebrew/homebrew-#{command}") | 
					
						
							|  |  |  |     tap.install unless tap.installed? | 
					
						
							|  |  |  |     command_help_lines(which("brew-#{command}.rb", Tap.cmd_directories)) | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-17 02:28:58 +02:00
										 |  |  |   def target_path_to_format(target) | 
					
						
							|  |  |  |     case target.basename | 
					
						
							| 
									
										
										
										
											2017-03-22 21:37:09 +00:00
										 |  |  |     when /\.md$/    then ["--markdown", "markdown"] | 
					
						
							| 
									
										
										
										
											2016-04-17 02:28:58 +02:00
										 |  |  |     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 |