#!/usr/bin/env ruby require 'fileutils' REFINERY_ROOT = File.expand_path(File.dirname(__FILE__) << "/..") unless (app_path = ARGV.shift).nil? or app_path.length == 0 # if "" or "." or "./" is specified then get the current directory otherwise accept the specified app_path. RAILS_ROOT = (app_path.length <= 2 and ((is_current_dir = app_path =~ /(\.(\/)?)/).nil? or is_current_dir < 2)) ? Dir.getwd : app_path if File.exists? RAILS_ROOT if ARGV.include?("--force") # remove the contents of the current directory Dir[File.join(RAILS_ROOT, "*")].each {|dir| FileUtils::rm_rf(dir, :secure => true) } else puts "The path you specified already exists. If you want to override this directory (i.e. delete all the current contents) run this again with --force" end end unless File.exists? RAILS_ROOT and !ARGV.include?("--force") # make the application path directory FileUtils::makedirs RAILS_ROOT # copy in all of the relevant directories and root files. to_copy = %w(app config db lib public script CONTRIBUTORS LICENSE Rakefile README).map do |dir| File.join REFINERY_ROOT, dir end FileUtils::cp_r to_copy, RAILS_ROOT, :verbose => false # ensure lib/refinery_initializer.rb doesn't make it in. FileUtils::rm File.join(RAILS_ROOT, %w(lib refinery_initializer.rb)) if File.exists?(File.join(RAILS_ROOT, %w(lib refinery_initializer.rb))) # add in the database config file. FileUtils::move File.join(RAILS_ROOT, %w(config database.yml.example)), File.join(RAILS_ROOT, %w(config database.yml)) # figure out the database name from the install path and swap out your_local_database with this name. unless RAILS_ROOT == "/" or RUBY_PLATFORM =~ /mswin/ database_name = RAILS_ROOT.split(File::SEPARATOR).last # read in the file and split up the lines lines = File.open(File.join(RAILS_ROOT, %w(config database.yml)), "r").read.split("\n") lines.each do |line| line.gsub!("your_local_database", database_name) line.gsub!("your_test_database", "#{database_name}_test") line.gsub!("your_production_database", "#{database_name}_production") end # write the new content into the file. File.open(File.join(RAILS_ROOT, %w(config database.yml)), "w").puts(lines.join("\n")) else database_name = "your_local_database" end # update the environment file with a new secret key. require 'digest/sha1' new_digest = "" 3.times { new_digest << Digest::SHA1.hexdigest("--refinery--#{Time.now.to_s}--#{rand(10000000)}--") } # read in the file and split up the lines lines = File.open(File.join(RAILS_ROOT, %w(config environment.rb)), "r").read.split("\n") lines.each do |line| match = line.scan(/(:secret)([^']*)([\'])([^\']*)/).flatten.last line.gsub!(match, new_digest) unless match.nil? end # write the new content into the file. File.open(File.join(RAILS_ROOT, %w(config environment.rb)), "w").puts(lines.join("\n")) puts "\n---------" puts "Refinery successfully installed in '#{RAILS_ROOT}'!\n\n" # update core script files with symlinks. system "refinery-update-core #{RAILS_ROOT} --from-refinery-installer" puts "=== ACTION REQUIRED ===" puts "\nNow run these commands:" puts "\ncd #{RAILS_ROOT}" puts "rake gems:install" puts "rake db:setup" puts "\nruby script/server" puts "\nThis will install all the required gems, set up your database (named '#{database_name}') and launches the built-in webserver." puts "You can now see your site running in your browser at http://localhost:3000" puts "\nThanks for installing Refinery, enjoy creating your new application!" puts "---------\n\n" end else puts "Please specify the path where you want to install Refinery. i.e. refinery /path/to/project" end