|
| 1 | +#!/bin/bash |
| 2 | +# vim: ft=ruby |
| 3 | +# Make sure this shim uses the same Ruby interpreter that is used by Homebrew. |
| 4 | +if [[ -z "${HOMEBREW_RUBY_PATH}" ]] |
| 5 | +then |
| 6 | + echo "${0##*/}: The build tool has reset ENV; --env=std required." >&2 |
| 7 | + exit 1 |
| 8 | +fi |
| 9 | +unset RUBYLIB |
| 10 | +exec "${HOMEBREW_RUBY_PATH}" --enable-frozen-string-literal --disable=gems,did_you_mean,rubyopt -x "$0" "$@" |
| 11 | +#!/usr/bin/env ruby -W0 |
| 12 | + |
| 13 | +require "open3" |
| 14 | +require "pathname" |
| 15 | + |
| 16 | +def split_args(args) |
| 17 | + options = [] |
| 18 | + positional_args = [] |
| 19 | + args.each do |arg| |
| 20 | + # `-` is stdin, so treat it as a positional argument |
| 21 | + if arg.start_with?("-") && arg != "-" |
| 22 | + options << arg |
| 23 | + else |
| 24 | + positional_args << arg |
| 25 | + end |
| 26 | + end |
| 27 | + [options, positional_args] |
| 28 | +end |
| 29 | + |
| 30 | +def remove_superbin_from_path(paths) |
| 31 | + superbin = Pathname.new(__FILE__).dirname.realpath |
| 32 | + paths.reject do |x| |
| 33 | + path = Pathname.new(x) |
| 34 | + path.directory? && path.realpath == superbin |
| 35 | + end |
| 36 | +end |
| 37 | + |
| 38 | +if __FILE__ == $PROGRAM_NAME |
| 39 | + dirname, tool = File.split($PROGRAM_NAME) |
| 40 | + separated_args = split_args(ARGV) |
| 41 | + options = separated_args[0] |
| 42 | + positional_args = separated_args[1] |
| 43 | + |
| 44 | + # pod2man will not accept an empty string for --release, so use a space instead. |
| 45 | + # Set this to avoid the perl version from being included in the generated docs. |
| 46 | + # By prepending, the caller may choose to override this value to something else (later |
| 47 | + # args take precedence). |
| 48 | + options.prepend "--release= " |
| 49 | + |
| 50 | + # pod2man accepts any provided positional arguments as input and output pairs. The first |
| 51 | + # argument is always assumed to be input. If there is an odd number of positional arguments, |
| 52 | + # the output of the last input will be emitted to stdout. |
| 53 | + outputs = [] |
| 54 | + positional_args.each_slice(2) { |_, output| outputs << output unless output.nil? } |
| 55 | + |
| 56 | + paths = ENV["PATH"].split(":") |
| 57 | + paths = remove_superbin_from_path(paths) |
| 58 | + paths.unshift "#{ENV["HOMEBREW_PREFIX"]}/bin" |
| 59 | + ENV["PATH"] = paths.join(":") |
| 60 | + |
| 61 | + # Wrap the execution of the real pod2man. Don't use exec, because we need to |
| 62 | + # clean non-deterministic contents from the output files afterwards. |
| 63 | + Open3.popen3(tool, *options, *positional_args) do |stdin, stdout, stderr, wait_thr| |
| 64 | + # Clean any non-deterministic contents from output emitted via stdout |
| 65 | + Thread.new do |
| 66 | + stdout.each do |line| |
| 67 | + $stdout.puts line.gsub(/^\.\\"\s*Automatically generated by .*\n/, "") |
| 68 | + end |
| 69 | + end |
| 70 | + |
| 71 | + Thread.new do |
| 72 | + stderr.each { |line| $stderr.puts line } |
| 73 | + end |
| 74 | + |
| 75 | + while !$stdin.tty? && $stdin.gets |
| 76 | + stdin.puts $_ |
| 77 | + end |
| 78 | + |
| 79 | + stdin.close |
| 80 | + wait_thr.value |
| 81 | + end |
| 82 | + |
| 83 | + outputs.each do |output| |
| 84 | + next if output == "-" |
| 85 | + |
| 86 | + str = File.binread(output).gsub(/^\.\\"\s*Automatically generated by .*\n/, "") |
| 87 | + File.write(output, str) |
| 88 | + end |
| 89 | +end |
0 commit comments