Instiki

From EggeWiki
Revision as of 10:49, 13 June 2006 by Brianegge (talk | contribs)

I created a setup script to help me get Instiki running from source. So far I have it working with SQLite, but I'd like to get mysql working as wel.

#!/usr/bin/env ruby

require 'stringio'
require 'ftools'
require 'find'

def get(prompt) 
  print prompt 
  res = readline.chomp 
  throw :quit_requested if res == "!" 
  res 
end

def choose(prompt, list)
  (1..list.length).each {|x| puts "#{x}) #{list[x-1]}"}
  i = get(prompt + " (1-#{list.length}): ")
  list[i.to_i - 1]
end

def show_configs(configs, io)
  configs.each do |name,settings|
    io.puts "#{name}:"
    settings.each {|k,v| io.puts "  #{k}: #{v}"}
    io.puts
  end
end

puts "This script will help you configure your database.yml file."

adapters = []

`which mysql`
if $? == 0 then
  adapters << "mysql"
end

# SQLite seems to return 1 for it's normal return code
`which sqlite3`
if $? == 0 then 
  adapters << "sqlite3"
else
  puts $?
end

adapter = choose('Select a database adapter', adapters)

configs = { 'development' => {}, 'test' => {}, 'production' => {}}

configs.each {|k,v| v['adapter'] = adapter}
case adapter
when "sqlite3"
  configs.each {|k,v| v['database'] = "db/#{k}.db.sqlite3"}
end

str = StringIO.new()

show_configs(configs, str)

puts "Created database config:"
puts str.string

if File.file?('config/database.yml') then
  if get("Would you like to replace database.yml with the above config? (Y/n) ").downcase == 'n' then
    puts "Cancelled"
    exit 1
  end
  # backup the svn version
  if !File.file?('config/database.yml.old')
    File.move("config/database.yml", "config/database.yml.old", true)
  end
end

File.open("config/database.yml", "w") do |file|
  show_configs(configs, file)
end

case adapter
when "sqlite3"
  Find.find('db') { |f| File.delete(f) if f =~ /.*\.db/ }
end

configs.each do |env,k|
  system("rake migrate RAILS_ENV=#{env}")
end