Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 328 lines (233 sloc) 6.291 kb
23e4b03 Lachie Cox added first bits of contrib script
lachie authored
1 #!/usr/bin/env ruby
2
d723846 Lachie Cox continued hax on contrib script
lachie authored
3 require 'rubygems'
63739b5 Lachie Cox finished basic contribute script
lachie authored
4 require 'yaml'
5 require 'pp'
6
23e4b03 Lachie Cox added first bits of contrib script
lachie authored
7 module ContributeHelper; end
8
9 class Contribute
10 include ContributeHelper
d723846 Lachie Cox continued hax on contrib script
lachie authored
11
12 def dependencies
13 [
14 Dependencies::Sphinx,
15 Dependencies::Mysql,
63739b5 Lachie Cox finished basic contribute script
lachie authored
16 Dependencies::AR,
d723846 Lachie Cox continued hax on contrib script
lachie authored
17 Dependencies::Ginger
18 ]
19 end
23e4b03 Lachie Cox added first bits of contrib script
lachie authored
20
21 def show
22 show_welcome_screen
23
24 (
63739b5 Lachie Cox finished basic contribute script
lachie authored
25 check_for_dependencies &&
26 create_database_yaml &&
27 check_mysql_is_working &&
23e4b03 Lachie Cox added first bits of contrib script
lachie authored
28 create_test_database
29 ) || exit(1)
30
31 show_done_screen
32 end
33
34 private
35 WELCOME_SCREEN = <<-EO_WELCOME
d723846 Lachie Cox continued hax on contrib script
lachie authored
36 <banner>Thinking Sphinx Contribution</banner>
23e4b03 Lachie Cox added first bits of contrib script
lachie authored
37
38 Thanks for contributing to Thinking Sphinx.
39
63739b5 Lachie Cox finished basic contribute script
lachie authored
40 In this script we'll help you get setup to hack:
41
42 <b>1.</b> We'll check that you have the right software installed and running.
43 <b>2.</b> We'll set up the test database for specs to run against.
d723846 Lachie Cox continued hax on contrib script
lachie authored
44
23e4b03 Lachie Cox added first bits of contrib script
lachie authored
45 EO_WELCOME
46
63739b5 Lachie Cox finished basic contribute script
lachie authored
47 DONE_SCREEN = <<-EO_DONE
48 <banner>Setup done!</banner>
49
50 All done! Now you can start hacking by running
51
52 <b>rake spec</b>
53
54 EO_DONE
55
56 REVIEW_YAML = <<-EO_REVIEW_YAML
57
58 Please review the database details in the yaml file details before continuing.
59
60 This file is used by the specs to connect to the database.
61
62 Current details:
63 EO_REVIEW_YAML
64
65
66
67 MYSQL_FAILED = <<-EO_MYSQL_FAILED
68
69 Looks like we couldn't successfully talk to the mysql database.
70
71 Don't worry though...
72
73 EO_MYSQL_FAILED
74
75 CREATE_DATABASE_FAILED = <<-EO_CREATE_DATABASE_FAILED
76
77 Looks like we couldn't create a test database to work against.
78
79 Don't worry though...
80
81 EO_CREATE_DATABASE_FAILED
82
23e4b03 Lachie Cox added first bits of contrib script
lachie authored
83 def show_welcome_screen
84 colour_puts WELCOME_SCREEN
63739b5 Lachie Cox finished basic contribute script
lachie authored
85 wait!
23e4b03 Lachie Cox added first bits of contrib script
lachie authored
86 end
87
88 def show_done_screen
63739b5 Lachie Cox finished basic contribute script
lachie authored
89 colour_puts DONE_SCREEN
23e4b03 Lachie Cox added first bits of contrib script
lachie authored
90 end
63739b5 Lachie Cox finished basic contribute script
lachie authored
91
92 # create database.yml
93 def create_database_yaml
94 colour_puts "<banner>creating database yaml</banner>"
d723846 Lachie Cox continued hax on contrib script
lachie authored
95 puts
96
97
63739b5 Lachie Cox finished basic contribute script
lachie authored
98 config = {
99 'username' => 'root',
100 'password' => nil,
101 'host' => 'localhost'
102 }
103
104
105 colour_print " * <b>#{db_yml}</b>... "
106 unless File.exist?(db_yml)
107 open(db_yml,'w') {|f| f << config.to_yaml}
108 colour_puts "<green>created</green>"
109 else
110 config = YAML.load_file(db_yml)
111 colour_puts "<green>already exists</green>"
112 end
113
114 colour_puts REVIEW_YAML
115
116 config.each do |(k,v)|
117 colour_puts " * <b>#{k}</b>: #{v}"
d723846 Lachie Cox continued hax on contrib script
lachie authored
118 end
119
120 puts
121
63739b5 Lachie Cox finished basic contribute script
lachie authored
122 wait!
123 true
23e4b03 Lachie Cox added first bits of contrib script
lachie authored
124 end
63739b5 Lachie Cox finished basic contribute script
lachie authored
125
d723846 Lachie Cox continued hax on contrib script
lachie authored
126 def check_mysql_is_working
63739b5 Lachie Cox finished basic contribute script
lachie authored
127 require 'activerecord'
128 colour_puts "<banner>check mysql is working</banner>"
129 puts
130
131 connect_to_db
132
133 print " * connecting to mysql... "
134
135 begin
136 ActiveRecord::Base.connection.select_value('select sysdate() from dual')
137
138 colour_puts "<green>successful</green>"
139 puts
140
141 return true
142 rescue Mysql::Error
143 colour_puts "<red>failed</red>"
144
145 puts MYSQL_FAILED
146 end
147
23e4b03 Lachie Cox added first bits of contrib script
lachie authored
148 false
149 end
150
151 # create test db
152 def create_test_database
d723846 Lachie Cox continued hax on contrib script
lachie authored
153 colour_puts "<banner>create test database</banner>"
63739b5 Lachie Cox finished basic contribute script
lachie authored
154 puts
155
156 connect_to_db
157
158 colour_print " * <b>creating thinking_sphinx database</b>... "
159 begin
160 ActiveRecord::Base.connection.create_database('thinking_sphinx')
161 colour_puts "<green>successful</green>"
162 rescue ActiveRecord::StatementInvalid
163 if $!.message[/database exists/]
164 colour_puts "<green>successful</green> (database already existed)"
165 puts
166 return true
167 else
168 colour_puts "<red>failed</red>"
169 end
170 end
171
172 colour_puts CREATE_DATABASE_FAILED
173
23e4b03 Lachie Cox added first bits of contrib script
lachie authored
174 false
175 end
63739b5 Lachie Cox finished basic contribute script
lachie authored
176
177 # project
178 def ts_root
179 File.expand_path(File.dirname(__FILE__))
180 end
181
182 def specs
183 ts_root / 'spec'
184 end
185
186 def db_yml
187 specs / 'fixtures' / 'database.yml'
188 end
189
190 def connect_to_db
191 config = YAML.load_file(db_yml)
192 config.update(:adapter => 'mysql', :database => 'test')
193 config.symbolize_keys!
194
195 ActiveRecord::Base.establish_connection(config)
196 end
197 end
198
199
200
201
202
203
204
205 class String
206 def /(other)
207 "#{self}/#{other}"
208 end
23e4b03 Lachie Cox added first bits of contrib script
lachie authored
209 end
210
211 module ContributeHelper
d723846 Lachie Cox continued hax on contrib script
lachie authored
212 class Dependency
213 def self.name(name=nil)
214 if name then @name = name else @name end
215 end
216
217 attr_reader :location
218
219 def initialize
220 @found = false
221 @location = nil
222 end
223
224 def name; self.class.name end
225
226 def check; false end
227 def check!
228 @found = check
229 end
230
231 def found?
232 @found
233 end
234 end
235
236 class Gem < Dependency
237 def gem_name; self.class.name end
238 def name; "#{super} gem" end
239
240 def check
241 ::Gem.available? self.gem_name
242 end
243 end
244
245
63739b5 Lachie Cox finished basic contribute script
lachie authored
246 def check_for_dependencies
247 colour_puts "<banner>Checking for required software</banner>"
248 puts
249
250 all_found = true
251
252 dependencies.each do |klass|
253 dep = klass.new
254 print " * #{dep.name}... "
255 dep.check!
256
257 if dep.found?
258 if dep.location
259 colour_puts "<green>found at #{dep.location}</green>"
260 else
261 colour_puts "<green>found</green>"
262 end
263 else
264 all_found &= false
265 colour_puts "<red>not found</red>"
266 end
267 end
268
269 puts
270
271 all_found
272 end
273
274
d723846 Lachie Cox continued hax on contrib script
lachie authored
275
23e4b03 Lachie Cox added first bits of contrib script
lachie authored
276 DEFAULT_TERMINAL_COLORS = "\e[0m\e[37m\e[40m"
277 def subs_colour(data)
278 data = data.gsub(%r{<b>(.*?)</b>}m, "\e[1m\\1#{DEFAULT_TERMINAL_COLORS}")
279 data.gsub!(%r{<red>(.*?)</red>}m, "\e[1m\e[31m\\1#{DEFAULT_TERMINAL_COLORS}")
280 data.gsub!(%r{<green>(.*?)</green>}m, "\e[1m\e[32m\\1#{DEFAULT_TERMINAL_COLORS}")
281 data.gsub!(%r{<yellow>(.*?)</yellow>}m, "\e[1m\e[33m\\1#{DEFAULT_TERMINAL_COLORS}")
282 data.gsub!(%r{<banner>(.*?)</banner>}m, "\e[33m\e[44m\e[1m\\1#{DEFAULT_TERMINAL_COLORS}")
283
284 return data
285 end
286
287 def colour_puts(text)
288 puts subs_colour(text)
289 end
63739b5 Lachie Cox finished basic contribute script
lachie authored
290
291 def colour_print(text)
292 print subs_colour(text)
293 end
294
295
296 def wait!
297 colour_puts "<b>Hit Enter to continue, or Ctrl-C to quit.</b>"
298 STDIN.readline
299 rescue Interrupt
300 exit!
301 end
23e4b03 Lachie Cox added first bits of contrib script
lachie authored
302 end
303
d723846 Lachie Cox continued hax on contrib script
lachie authored
304 module Dependencies
305 class Mysql < ContributeHelper::Gem
306 name 'mysql'
307 end
308
63739b5 Lachie Cox finished basic contribute script
lachie authored
309 class AR < ContributeHelper::Gem
310 name 'activerecord'
311 end
312
d723846 Lachie Cox continued hax on contrib script
lachie authored
313 class Ginger < ContributeHelper::Gem
314 name 'ginger'
315 end
316
317 class Sphinx < ContributeHelper::Dependency
318 name 'sphinx'
319
320 def check
321 output = `which searchd`
322 @location = output.chomp if $? == 0
323 $? == 0
324 end
325 end
326 end
327
23e4b03 Lachie Cox added first bits of contrib script
lachie authored
328 Contribute.new.show
Something went wrong with that request. Please try again.