Skip to content

Commit

Permalink
Added dropdown for all gems, plus /gemlist.js
Browse files Browse the repository at this point in the history
Signed-off-by: Jesse Newland <jnewland@gmail.com>
  • Loading branch information
jseifer authored and jnewland committed Feb 22, 2009
1 parent bd6fd73 commit 2e57fea
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 2 deletions.
11 changes: 9 additions & 2 deletions lib/gems_and_rdocs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,17 @@ def call(env)
can_serve = @urls.any? { |url| path.index(url) == 0 }

if can_serve
@file_server.call(env)
status, headers, response = @file_server.call(env)

if (path =~ /doc\// && !(path =~ /index\.html$/))
body = ""; response.each { |s| body << s }
response = body.gsub(/(<body.*\>)/,'\1<script src="/gemlist.js"></script>')
headers["Content-Length"] = response.size.to_s
end
else
env["PATH_INFO"] = old_path_info
@app.call(env)
status, headers, response = @app.call(env)
end
[status, headers, response]
end
end
76 changes: 76 additions & 0 deletions lib/rack_rubygems.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,82 @@ class RackRubygems < Sinatra::Base
Gem.deflate(quick(params[:splat].first).to_yaml)
end

get '/gemlist.js' do
specs = []
total_file_count = 0

source_index.each do |path, spec|
total_file_count += spec.files.size
deps = spec.dependencies.map do |dep|
{ "name" => dep.name,
"type" => dep.type,
"version" => dep.version_requirements.to_s, }
end

deps = deps.sort_by { |dep| [dep["name"].downcase, dep["version"]] }
deps.last["is_last"] = true unless deps.empty?

# executables
executables = spec.executables.sort.collect { |exec| {"executable" => exec} }
executables = nil if executables.empty?
executables.last["is_last"] = true if executables

specs << {
"authors" => spec.authors.sort.join(", "),
"date" => spec.date.to_s,
"dependencies" => deps,
"doc_path" => "/doc_root/#{spec.full_name}/rdoc/index.html",
"executables" => executables,
"only_one_executable" => (executables && executables.size == 1),
"full_name" => spec.full_name,
"has_deps" => !deps.empty?,
"homepage" => spec.homepage,
"name" => spec.name,
"rdoc_installed" => Gem::DocManager.new(spec).rdoc_installed?,
"summary" => spec.summary,
"version" => spec.version.to_s,
}
end

specs << {
"authors" => "Chad Fowler, Rich Kilmer, Jim Weirich, Eric Hodel and others",
"dependencies" => [],
"doc_path" => "/doc_root/rubygems-#{Gem::RubyGemsVersion}/rdoc/index.html",
"executables" => [{"executable" => 'gem', "is_last" => true}],
"only_one_executable" => true,
"full_name" => "rubygems-#{Gem::RubyGemsVersion}",
"has_deps" => false,
"homepage" => "http://rubygems.org/",
"name" => 'rubygems',
"rdoc_installed" => true,
"summary" => "RubyGems itself",
"version" => Gem::RubyGemsVersion,
}

specs = specs.sort_by { |spec| [spec["name"].downcase, spec["version"]] }
specs.last["is_last"] = true

# tag all specs with first_name_entry
last_spec = nil
specs.each do |spec|
is_first = last_spec.nil? || (last_spec["name"].downcase != spec["name"].downcase)
spec["first_name_entry"] = is_first
last_spec = spec
end

@values = { "gem_count" => specs.size.to_s, "specs" => specs,
"total_file_count" => total_file_count.to_s }

body = "document.writeln('<select style=\"float:right;margin: 10px 10px 0 0 \" onchange=\"window.parent.location=this.value\">');"
body << "document.writeln('<option value=\"/\">Gems:</option>');"
specs.each do |spec|
body << "document.writeln(\"<option value='#{spec['doc_path']}'>#{spec['name']} - #{spec['version']}</option>\");"
end
body << "document.writeln('</select>');"
body
end


def source_index
@gem_dir = Gem.dir
@spec_dir = File.join @gem_dir, 'specifications'
Expand Down

0 comments on commit 2e57fea

Please sign in to comment.