diff options
Diffstat (limited to 'vendor/plugins/thinking-sphinx/contribute.rb')
| -rwxr-xr-x | vendor/plugins/thinking-sphinx/contribute.rb | 332 |
1 files changed, 0 insertions, 332 deletions
diff --git a/vendor/plugins/thinking-sphinx/contribute.rb b/vendor/plugins/thinking-sphinx/contribute.rb deleted file mode 100755 index 315b438..0000000 --- a/vendor/plugins/thinking-sphinx/contribute.rb +++ /dev/null | |||
| @@ -1,332 +0,0 @@ | |||
| 1 | #!/usr/bin/env ruby | ||
| 2 | |||
| 3 | require 'rubygems' | ||
| 4 | require 'yaml' | ||
| 5 | require 'pp' | ||
| 6 | |||
| 7 | module ContributeHelper; end | ||
| 8 | |||
| 9 | class Contribute | ||
| 10 | include ContributeHelper | ||
| 11 | |||
| 12 | def dependencies | ||
| 13 | [ | ||
| 14 | Dependencies::Sphinx, | ||
| 15 | Dependencies::Mysql, | ||
| 16 | Dependencies::AR, | ||
| 17 | Dependencies::Ginger | ||
| 18 | ] | ||
| 19 | end | ||
| 20 | |||
| 21 | def show | ||
| 22 | show_welcome_screen | ||
| 23 | |||
| 24 | ( | ||
| 25 | check_for_dependencies && | ||
| 26 | create_database_yaml && | ||
| 27 | check_mysql_is_working && | ||
| 28 | create_test_database | ||
| 29 | ) || exit(1) | ||
| 30 | |||
| 31 | show_done_screen | ||
| 32 | end | ||
| 33 | |||
| 34 | private | ||
| 35 | WELCOME_SCREEN = <<-EO_WELCOME | ||
| 36 | <banner>Thinking Sphinx Contribution</banner> | ||
| 37 | |||
| 38 | Thanks for contributing to Thinking Sphinx. | ||
| 39 | |||
| 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. | ||
| 44 | |||
| 45 | EO_WELCOME | ||
| 46 | |||
| 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 | |||
| 83 | def show_welcome_screen | ||
| 84 | colour_puts WELCOME_SCREEN | ||
| 85 | wait! | ||
| 86 | end | ||
| 87 | |||
| 88 | def show_done_screen | ||
| 89 | colour_puts DONE_SCREEN | ||
| 90 | end | ||
| 91 | |||
| 92 | # create database.yml | ||
| 93 | def create_database_yaml | ||
| 94 | colour_puts "<banner>creating database yaml</banner>" | ||
| 95 | puts | ||
| 96 | |||
| 97 | |||
| 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}" | ||
| 118 | end | ||
| 119 | |||
| 120 | puts | ||
| 121 | |||
| 122 | wait! | ||
| 123 | true | ||
| 124 | end | ||
| 125 | |||
| 126 | def check_mysql_is_working | ||
| 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 defined?(JRUBY_VERSION) ? Java::JavaSql::SQLException : Mysql::Error | ||
| 143 | colour_puts "<red>failed</red>" | ||
| 144 | |||
| 145 | puts MYSQL_FAILED | ||
| 146 | end | ||
| 147 | |||
| 148 | false | ||
| 149 | end | ||
| 150 | |||
| 151 | # create test db | ||
| 152 | def create_test_database | ||
| 153 | colour_puts "<banner>create test database</banner>" | ||
| 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 | |||
| 174 | false | ||
| 175 | end | ||
| 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 mysql_adapter | ||
| 191 | defined?(JRUBY_VERSION) ? 'jdbcmysql' : 'mysql' | ||
| 192 | end | ||
| 193 | |||
| 194 | def connect_to_db | ||
| 195 | config = YAML.load_file(db_yml) | ||
| 196 | config.update(:adapter => mysql_adapter, :database => 'test') | ||
| 197 | config.symbolize_keys! | ||
| 198 | |||
| 199 | ActiveRecord::Base.establish_connection(config) | ||
| 200 | end | ||
| 201 | end | ||
| 202 | |||
| 203 | |||
| 204 | |||
| 205 | |||
| 206 | |||
| 207 | |||
| 208 | |||
| 209 | class String | ||
| 210 | def /(other) | ||
| 211 | "#{self}/#{other}" | ||
| 212 | end | ||
| 213 | end | ||
| 214 | |||
| 215 | module ContributeHelper | ||
| 216 | class Dependency | ||
| 217 | def self.name(name=nil) | ||
| 218 | if name then @name = name else @name end | ||
| 219 | end | ||
| 220 | |||
| 221 | attr_reader :location | ||
| 222 | |||
| 223 | def initialize | ||
| 224 | @found = false | ||
| 225 | @location = nil | ||
| 226 | end | ||
| 227 | |||
| 228 | def name; self.class.name end | ||
| 229 | |||
| 230 | def check; false end | ||
| 231 | def check! | ||
| 232 | @found = check | ||
| 233 | end | ||
| 234 | |||
| 235 | def found? | ||
| 236 | @found | ||
| 237 | end | ||
| 238 | end | ||
| 239 | |||
| 240 | class Gem < Dependency | ||
| 241 | def gem_name; self.class.name end | ||
| 242 | def name; "#{super} gem" end | ||
| 243 | |||
| 244 | def check | ||
| 245 | ::Gem.available? self.gem_name | ||
| 246 | end | ||
| 247 | end | ||
| 248 | |||
| 249 | |||
| 250 | def check_for_dependencies | ||
| 251 | colour_puts "<banner>Checking for required software</banner>" | ||
| 252 | puts | ||
| 253 | |||
| 254 | all_found = true | ||
| 255 | |||
| 256 | dependencies.each do |klass| | ||
| 257 | dep = klass.new | ||
| 258 | print " * #{dep.name}... " | ||
| 259 | dep.check! | ||
| 260 | |||
| 261 | if dep.found? | ||
| 262 | if dep.location | ||
| 263 | colour_puts "<green>found at #{dep.location}</green>" | ||
| 264 | else | ||
| 265 | colour_puts "<green>found</green>" | ||
| 266 | end | ||
| 267 | else | ||
| 268 | all_found &= false | ||
| 269 | colour_puts "<red>not found</red>" | ||
| 270 | end | ||
| 271 | end | ||
| 272 | |||
| 273 | puts | ||
| 274 | |||
| 275 | all_found | ||
| 276 | end | ||
| 277 | |||
| 278 | |||
| 279 | |||
| 280 | DEFAULT_TERMINAL_COLORS = "\e[0m\e[37m\e[40m" | ||
| 281 | def subs_colour(data) | ||
| 282 | data = data.gsub(%r{<b>(.*?)</b>}m, "\e[1m\\1#{DEFAULT_TERMINAL_COLORS}") | ||
| 283 | data.gsub!(%r{<red>(.*?)</red>}m, "\e[1m\e[31m\\1#{DEFAULT_TERMINAL_COLORS}") | ||
| 284 | data.gsub!(%r{<green>(.*?)</green>}m, "\e[1m\e[32m\\1#{DEFAULT_TERMINAL_COLORS}") | ||
| 285 | data.gsub!(%r{<yellow>(.*?)</yellow>}m, "\e[1m\e[33m\\1#{DEFAULT_TERMINAL_COLORS}") | ||
| 286 | data.gsub!(%r{<banner>(.*?)</banner>}m, "\e[33m\e[44m\e[1m\\1#{DEFAULT_TERMINAL_COLORS}") | ||
| 287 | |||
| 288 | return data | ||
| 289 | end | ||
| 290 | |||
| 291 | def colour_puts(text) | ||
| 292 | puts subs_colour(text) | ||
| 293 | end | ||
| 294 | |||
| 295 | def colour_print(text) | ||
| 296 | print subs_colour(text) | ||
| 297 | end | ||
| 298 | |||
| 299 | |||
| 300 | def wait! | ||
| 301 | colour_puts "<b>Hit Enter to continue, or Ctrl-C to quit.</b>" | ||
| 302 | STDIN.readline | ||
| 303 | rescue Interrupt | ||
| 304 | exit! | ||
| 305 | end | ||
| 306 | end | ||
| 307 | |||
| 308 | module Dependencies | ||
| 309 | class Mysql < ContributeHelper::Gem | ||
| 310 | name(defined?(JRUBY_VERSION) ? 'jdbc-mysql' : 'mysql') | ||
| 311 | end | ||
| 312 | |||
| 313 | class AR < ContributeHelper::Gem | ||
| 314 | name 'activerecord' | ||
| 315 | end | ||
| 316 | |||
| 317 | class Ginger < ContributeHelper::Gem | ||
| 318 | name 'ginger' | ||
| 319 | end | ||
| 320 | |||
| 321 | class Sphinx < ContributeHelper::Dependency | ||
| 322 | name 'sphinx' | ||
| 323 | |||
| 324 | def check | ||
| 325 | output = `which searchd` | ||
| 326 | @location = output.chomp if $? == 0 | ||
| 327 | $? == 0 | ||
| 328 | end | ||
| 329 | end | ||
| 330 | end | ||
| 331 | |||
| 332 | Contribute.new.show \ No newline at end of file | ||
