From aa46db87a7d460d9167810fdd66bfda117b8faab Mon Sep 17 00:00:00 2001 From: Adam Vandenberg Date: Wed, 2 Jun 2010 15:06:54 -0700 Subject: [PATCH] Convert server to external command and enhance. --- Library/Contributions/examples/brew-server | 173 +++++++++++++++++++++ website/formulas.rb | 31 ---- 2 files changed, 173 insertions(+), 31 deletions(-) create mode 100755 Library/Contributions/examples/brew-server delete mode 100644 website/formulas.rb diff --git a/Library/Contributions/examples/brew-server b/Library/Contributions/examples/brew-server new file mode 100755 index 0000000000..1d461db619 --- /dev/null +++ b/Library/Contributions/examples/brew-server @@ -0,0 +1,173 @@ +#!/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. + +puts "View our tasting menu at http://localhost:4567/\nUse \"Control-C\" to exit.\n\n" +$:.unshift(ENV['HOMEBREW_LIBRARY_PATH']) + +require 'rubygems' +require 'sinatra' + +require 'cgi' + +require 'global' +require 'formula' + +def link_to_formula name + "#{name}" +end + +def css_style + "" # No CSS defined yet. +end + +def search_form + <<-EOS +
+ Search: +
+ EOS +end + +def html_page + body = <<-HTML + + + Homebrew Menu + #{css_style} + + +
+ + +
+ HTML + yield body + body += <<-HTML +
+
+ + + HTML + return body +end + +get '/' do + return html_page do |s| + s << <<-HTML +
#{search_form}
+
+ +
+ HTML + end +end + +get '/search' do + q = params['q'] + + formulae = Formulary.names with_aliases=true + + if q =~ /^\/(.*)\/$/ + results = formulae.grep(Regexp.new($1)) + else + search_term = Regexp.escape(q || "") + results = formulae.grep(/.*#{search_term}.*/) + end + s = <<-HTML + + + Search Results + #{css_style} + + +

Results

+ #{search_form} +

Searched for “#{q}”

+ + + + HTML + + return s +end + +get '/formula/:name' do + # klass = Formulary.read params[:name] + klass = Formula.factory(params[:name]) + + installed = klass.installed? ? "Installed at" : "Not installed." + installed_dd = klass.installed? ? "#{klass.prefix}" : "" + + s = "" + s << <<-HTML + + + Formula: #{klass.name} + #{css_style} + + +
Back to menu
+

#{klass.name}

+
+
Version
+
#{klass.version}
+ +
Homepage
+
#{klass.homepage}
+ +
Download
+
#{klass.url}
+ +
#{installed}
+
#{installed_dd}
+ HTML + + unless klass.deps.count == 0 + s << <<-HTML +
Depends on + HTML + klass.deps.each do |name| + s << "
#{link_to_formula(name)}
" + end + end + + used_by = Formula.get_used_by()[klass.name] + unless used_by == nil + s << <<-HTML +
Used by + HTML + if used_by != nil + used_by.each do |name| + s << "
#{link_to_formula(name)}
" + end + end + end + + s += <<-HTML +
+ + + HTML + + return s +end diff --git a/website/formulas.rb b/website/formulas.rb deleted file mode 100644 index e6b938f22f..0000000000 --- a/website/formulas.rb +++ /dev/null @@ -1,31 +0,0 @@ -require 'rubygems' -require 'sinatra' - -$LOAD_PATH << File.join(File.dirname(__FILE__), "../Library/Homebrew") -$LOAD_PATH << File.join(File.dirname(__FILE__), "../Library/Formula") - -require 'global' -require 'formula' - -get '/' do - body = "" -end - -get '/formula/:name' do - klass = Formulary.read params[:name] - body = "

#{klass.to_s}

" - body << "
" - body << "
Version
" - body << "
#{klass.version}
" - body << "
Homepage
" - body << "
#{klass.homepage}
" - body << "
Download
" - body << "
#{klass.url}
" - body << "
MD5
" - body << "
#{klass.md5}
" - body << "
" -end