From aa46db87a7d460d9167810fdd66bfda117b8faab Mon Sep 17 00:00:00 2001
From: Adam Vandenberg
Date: Wed, 2 Jun 2010 15:06:54 -0700
Subject: Convert server to external command and enhance.
---
Library/Contributions/examples/brew-server | 173 +++++++++++++++++++++++++++++
1 file changed, 173 insertions(+)
create mode 100755 Library/Contributions/examples/brew-server
(limited to 'Library')
diff --git a/Library/Contributions/examples/brew-server b/Library/Contributions/examples/brew-server
new file mode 100755
index 000000000..1d461db61
--- /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
+
+ 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
+ Formulary.read_all do |name, klass|
+ s << "- #{link_to_formula(name)}
"
+ end
+ s << <<-HTML
+
+
+ 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
+
+ results.each do |name|
+ s << "- #{link_to_formula(name)}
"
+ end
+
+ s += <<-HTML
+
+
+
+ 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}
+
+
+
+ #{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
--
cgit v1.2.3