/
app.rb
170 lines (143 loc) · 4.58 KB
/
app.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
require File.join(File.dirname(__FILE__), 'vendor', 'gems', 'environment')
require "sinatra"
require "tokyocabinet"
require "yajl/json_gem"
require "grit"
include TokyoCabinet
include Grit
HOMEBREW_LOCATION = `brew --prefix`.chomp!
DB = BDB::new
DB.open("repo.bdb", BDB::OWRITER | BDB::OCREAT)
DBC = BDBCUR::new(DB)
REPO = Repo.new(HOMEBREW_LOCATION)
helpers do
def search_for(pkg)
results =[]
DBC.first
while key = DBC.key
results << [DBC.key, DBC.val].join(':') if DBC.val =~ Regexp.new(pkg)
DBC.next
end
return results
end
def in_homebrew
Dir.chdir(HOMEBREW_LOCATION) do |hb|
yield
end
end
end
get '/' do
erb :index
end
get '/search' do
content_type :json
results = search_for(params['query'])
{
:query => params['query'],
:suggestions => results,
:data => results
}.to_json
end
get '/preview' do
tree = params['tree']
formula = params['formula']
(REPO.tree(tree, ["Library/Formula"]).contents.first/"#{formula}.rb").data
end
get '/install' do
tree = params['tree']
formula = params['formula']
puts "HIT"
in_homebrew {
`git checkout #{tree} Library/Formula/#{formula}.rb`
`osascript -e 'tell app "Terminal" to do script "brew install #{formula}"'`
}
end
# FIXME: lol, sinatra won't call this
trap("INT") {
#DB.close
}
__END__
@@ layout
<html>
<head>
<title>Homebrew</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
<script src="http://www.devbridge.com/projects/autocomplete/jquery/local/scripts/jquery.autocomplete.js" type="text/javascript"></script>
<script type="text/javascript" charset="utf-8">
$(document).ready(function(){
var preview_formula = function(path){
var tree = path.split(':')[0];
var formula = path.split(':')[1];
$.get('/preview',
{"tree": tree,
"formula": formula},
function(data){
$("#preview_box").text(data);
$("#formula_preview").show();
},
"text"
);
}
var install_formula = function(path){
var tree = path.split(':')[0];
var formula = path.split(':')[1];
$.get('/install',
{"tree": tree,
"formula": formula},
function(data){},
"text"
);
}
var a = $('#search').autocomplete({
serviceUrl:'/search',
minChars:2,
maxHeight:500,
width:400,
zIndex: 9999,
deferRequestBy: 0, //miliseconds
onSelect: function(value, data){
$("input#current_formula").val(data);
preview_formula(data);
},
});
$("input#install").click(function(){
alert("Installing " + $("#current_formula").val());
install_formula($("#current_formula").val());
});
});
</script>
<style type="text/css" media="screen">
* { padding:0; margin:0; }
body { background:#efefef; }
#wrapper { padding:2em; border:1px solid #666; }
input { border:1px solid #999; font-size:2em;}
.control { padding:1em; }
.button_controls { padding:0 5px 0 5px;}
#preview_box { border:1px solid #999; }
/* Autocomplete: */
.autocomplete-w1 { position:absolute; top:0px; left:0px; margin:8px 0 0 6px;}
.autocomplete { border:1px solid #999; background:#FFF; cursor:default; text-align:left; max-height:350px; overflow:auto; margin:-6px 6px 6px -6px; /* IE6 specific: */ _height:350px; _margin:0; _overflow-x:hidden; }
.autocomplete .selected { background:#F0F0F0; }
.autocomplete div { padding:2px 5px; white-space:nowrap; }
.autocomplete strong { font-weight:normal; color:#3399FF; }
.autocomplete .selected { background:red; }
</style>
</head>
<body>
<%= yield %>
</body>
</html>
@@ index
<div id="wrapper">
<div class="control">
<h1 id="search_for_formulas">Search for Formulas</h1>
<input type="text" name="search" value="" id="search">
</div>
<div id="formula_preview" style="display:none;">
<textarea id="preview_box" rows='30' cols='80'></textarea>
<div class="control">
<input type="button" name="install" value="Install" id="install" class='button_controls'>
<input type="hidden" name="current_formula" value="" id="current_formula">
</div>
</div>
</div>