Skip to content

Instantly share code, notes, and snippets.

@sixtus
Created October 14, 2009 09:01
Show Gist options
  • Save sixtus/209905 to your computer and use it in GitHub Desktop.
Save sixtus/209905 to your computer and use it in GitHub Desktop.
task :populate_db do
begin
require 'rubygems'
require 'json'
require 'couchrest'
rescue
puts "Ruby gems missing, trying to install with sudo rights"
sh 'sudo gem install json couchrest'
retry
end
couch = CouchRest.new
db = Hash.new do |hash, key|
puts "(Re-)initializing #{key}"
begin
couch.database!(key).delete!
rescue
puts "Sleeping 2secs to wait for couch bootup"
sleep 2 # wait and see if couch is starting
couch.database!(key).delete!
end
hash[key] = couch.create_db(key)
end
FileList.new('examples/**/*.json') do |list|
list.each do |name|
begin
json = JSON.parse(File.read(name))
json['_id'] ||= File.basename(name, '.json')
js_file = name[0...-5] + '.js'
if File.exist? js_file
json['content'] = {
'application/javascript' => File.read(js_file)
}
end
FileList.new("#{File.dirname(name)}/#{json['_id']}.template.*") do |templates|
templates.each do |template|
(json['templates'] ||= {})[template.slice(/\w+\z/)] = File.read(template)
end
end
db[name.split('/')[1]].save_doc(json)
rescue => exception
puts "Failed on #{name}"
throw exception
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment