Created
August 2, 2023 03:52
-
-
Save bradgessler/c73eafe8168ea3ddca30a5d579241f7f to your computer and use it in GitHub Desktop.
Dumps the parts of a RubyGem docs into a folder... almost
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "open-uri" | |
require "rubygems/package" | |
require "fileutils" | |
require "pathname" | |
require "yard" | |
require "net/http" | |
class Gemomatic | |
attr_reader :code_objects | |
def initialize(name:, version:) | |
@name = name | |
@version = Gem::Version.new(version) | |
end | |
def url | |
URI("https://rubygems.org/downloads/#{@name}-#{@version}.gem") | |
end | |
def download | |
response = Net::HTTP.get_response(url) | |
case response | |
when Net::HTTPSuccess | |
File.write(package_path, response.body) | |
end | |
end | |
def package_path | |
path.join("package.gem") | |
end | |
def package | |
@package ||= if package_path.exist? | |
Gem::Package.new package_path.to_s | |
else | |
FileUtils.mkdir_p path | |
download | |
package | |
end | |
end | |
def download_and_extract | |
package.extract_files path | |
end | |
def glob | |
path.join("**/*.rb") | |
end | |
def parse | |
# No idea what I'm doing here, but I do know that this thing is a threaded situation. | |
# Do I need a Mutex? Who knows ... deal with it later. | |
YARD::Registry.clear | |
YARD::Parser::SourceParser.parse glob.to_s | |
puts "Loaded objects: #{YARD::Registry.all.count}" | |
YARD::Registry.all.each do |code| | |
pp code | |
case code | |
when YARD::CodeObjects::MethodObject | |
pp code.docstring | |
pp code.name | |
pp code.scope | |
pp code.sep | |
pp code.parameters | |
pp code.path | |
pp code.source | |
pp [code.file, code.line] | |
pp code | |
# pp code.public_methods | |
end | |
end | |
@code_objects = YARD::Registry.all.clone | |
end | |
private | |
def path | |
self.class.root_path.join(@name, @version.to_s) | |
end | |
def self.root_path | |
Rails.root.join("tmp", "cache", "gems") | |
end | |
end | |
gemomatic = Gemomatic.new(name: params[:gem_id], version: params[:id]) | |
gemomatic.download_and_extract | |
gemomatic.parse | |
genomatic.code_objects.each do |co| | |
p co # TODO: Dump these into a Frontmatter/markdown format | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment