#!/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby ## brew server: Run a local webserver for browsing available and installed brews. # Note: this external command is ruby, but set up as a shell script, so that it gets exec'd. # This is required for sinatra's run-loop to take over. $:.unshift(ENV['HOMEBREW_LIBRARY_PATH']) require 'global' require 'formula' require 'cmd/search' require 'rubygems' begin require 'sinatra' rescue LoadError onoe 'Sinatra required but not found' puts 'To install: /usr/bin/gem install sinatra' exit 1 end require 'cgi' def link_to_formula name "#{name}" end def css_style; <<-CSS CSS end def search_form; <<-EOS EOS end def html_page(title) body = <<-HTML #{title} #{css_style}
HTML yield body body << <<-HTML
HTML body end get '/' do html_page("Homebrew Menu") do |s| s << <<-HTML
#{search_form}

Show installed packages

HTML end end get '/search' do q = params['q'] results = Homebrew.search_formulae(q) html_page("Results") do |s| s << <<-HTML
#{search_form}

Searched for “#{q}”:

HTML results.each do |name| s << "" end s << <<-HTML
Name
#{link_to_formula(name)}
HTML end end get '/formula/:name' do f = Formula.factory(params[:name]) installed = <<-EOS
Installed at
#{f.prefix}
EOS html_page("Formula: #{f.name}") do |s| s << <<-HTML

#{f.name}

Version
#{f.version}
Homepage
#{f.homepage}
Download
#{f.url}
#{installed if f.installed?} HTML unless f.deps.empty? s << <<-HTML
Depends on HTML f.deps.each do |dep| s << "
#{link_to_formula(dep.name)}
" end end used_by = Formula.select { |ff| ff.deps.include?(f) }.map(&:name).flatten.uniq.sort unless used_by.empty? s << <<-HTML
Used by HTML used_by.each do |name| s << "
#{link_to_formula(name)}
" end end s << <<-HTML
HTML end end get '/installed' do html_page("Installed Formulae") do |s| s << <<-HTML

Installed Formulae:

HTML Formula.installed.each do |f| s << "" end s << <<-HTML
NameVersion
#{link_to_formula(f.name)}#{f.version}
HTML end end puts "View our tasting menu at http://localhost:4567/\nUse \"Control-C\" to exit.\n\n"