Skip to content

Instantly share code, notes, and snippets.

@arika
Last active February 4, 2017 06:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arika/96a27689ed0c9f0988ae1ac811e31fa2 to your computer and use it in GitHub Desktop.
Save arika/96a27689ed0c9f0988ae1ac811e31fa2 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'rbconfig'
require 'shellwords'
require 'yaml'
require 'tmpdir'
def get_output(cmd)
IO.popen(cmd, 'r', &:read)
end
def gem_list
out = get_output %w(gem list --local --no-versions)
end
def gem_dependencies(gem_name)
yaml = get_output %W(gem specification #{gem_name})
YAML.load(yaml).dependencies.map(&:name)
end
def gem_editor_cmd(gem_name, *args)
[RbConfig.ruby, File.expand_path(__FILE__), *args].map(&:shellescape).join(' ')
end
unless ARGV.first
($stdin.tty? ? gem_list : $stdin).each_line do |line|
gem_name = line.chomp
$stderr.puts gem_name
Dir.mktmpdir do |td|
out_name = "#{td}/l"
type = gem_dependencies(gem_name).include?('yard') ? 'y' : 't'
ENV['GEM_EDITOR'] = gem_editor_cmd(gem_name, type, out_name)
get_output(%W(gem open #{gem_name}))
print File.read(out_name) if File.exist?(out_name)
end
end
exit
end
def get_list_from_yard
out = get_output %w(yard doc --list --quiet)
out.each_line.map {|line| line.chomp.split(/\s/).last }
end
def get_list_from_rdoc
require 'json'
Dir.mktmpdir do |td|
dir = "#{td}/t"
get_output %W(rdoc --output #{dir} --format darkfish --quiet)
json_text = File.read("#{dir}/js/search_index.js")
.sub(/\A[^{]*/, '').sub(/[^}]*\z/, '')
index = JSON.parse(json_text)['index']
index['longSearchIndex'].each_with_index.map do |name, i|
next if name.empty? # pages
info = index['info'][i]
if info[3].empty? # classes
info[0]
else # methods
[info[1], info[0]].join('.')
end
end.compact
end
end
Dir.chdir(ARGV.last)
open(ARGV[1], 'a') do |io|
list = if ARGV[0] == 'y' || File.exist?('.yardopts')
get_list_from_yard
else
get_list_from_rdoc
end
io.puts list
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment