Skip to content
Browse files

improved bootstrap process

  • Loading branch information...
1 parent 47dc09f commit b0d97ca798a8ddb8b8bc929e31a3a5e6e7c613f9 @schof schof committed Apr 16, 2008
Showing with 318 additions and 27 deletions.
  1. +4 −3 Rakefile
  2. +242 −0 lib/spree/setup.rb
  3. +35 −0 lib/tasks/database.rake
  4. +11 −0 lib/tasks/environments.rake
  5. +26 −0 lib/tasks/instance.rake
  6. +0 −24 lib/tasks/spree_bootstrap.rake
View
7 Rakefile
@@ -1,10 +1,11 @@
-# Add your own tasks in files placed in lib/tasks ending in .rake,
-# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
-
require(File.join(File.dirname(__FILE__), 'config', 'boot'))
require 'rake'
require 'rake/testtask'
require 'rake/rdoctask'
require 'tasks/rails'
+
+unless Rake::Task.task_defined? "spree:release"
+ Dir["#{SPREE_ROOT}/lib/tasks/**/*.rake"].sort.each { |ext| load ext }
+end
View
242 lib/spree/setup.rb
@@ -0,0 +1,242 @@
+#######################################################################################################
+# Substantial portions of this code were adapted from the Radiant CMS project (http://radiantcms.org) #
+#######################################################################################################
+require "highline"
+require "forwardable"
+require 'active_record/fixtures'
+require 'custom_fixtures'
+require 'find'
+
+module Spree
+ class Setup
+
+ class << self
+ def bootstrap(config)
+ setup = new
+ setup.bootstrap(config)
+ setup
+ end
+ end
+
+ attr_accessor :config
+
+ def bootstrap(config)
+ @config = config
+ @admin = create_admin_user(config[:admin_name], config[:admin_username], config[:admin_password], config[:admin_email])
+ load_default_tax_treatments
+ load_sample_data if sample_data?
+ announce "Finished.\n\n"
+ end
+
+ def create_admin_user(name, username, password, email)
+ unless name and username and password
+ announce "Create the admin user (press enter for defaults)."
+ #name = prompt_for_admin_name unless name
+ username = prompt_for_admin_username unless username
+ password = prompt_for_admin_password unless password
+ email = prompt_for_admin_email unless email
+ end
+ attributes = {
+ #:name => name,
+ :login => username,
+ :password => password,
+ :password_confirmation => password,
+ :email => email
+ }
+ admin = User.create(attributes)
+
+ # create an admin role and and assign the admin user to that role
+ role = Role.create(:name => 'admin')
+ admin.roles << role
+ admin.save
+ admin
+ end
+
+ # This is a temporary workaround. Remove this once tax treatments are supported via extensions.
+ def load_default_tax_treatments
+ TaxTreatment.create(:name => "Non taxable")
+ TaxTreatment.create(:name => "U.S. Sales Tax")
+ end
+
+ # Uses a special set of fixtures to load sample data
+ def load_sample_data
+ # load initial database fixtures (in db/sample/*.yml) into the current environment's database
+ ActiveRecord::Base.establish_connection(RAILS_ENV.to_sym)
+ Dir.glob(File.join(SPREE_ROOT, 'db', 'sample', '*.{yml,csv}')).each do |fixture_file|
+ Fixtures.create_fixtures('db/sample', File.basename(fixture_file, '.*'))
+ end
+
+ # make product images available to the app
+ target = "#{RAILS_ROOT}/public/images/products/"
+ source = "#{SPREE_ROOT}/lib/tasks/sample/products/"
+
+ Find.find(source) do |f|
+ # omit hidden directories (SVN, etc.)
+ if File.basename(f) =~ /^[.]/
+ Find.prune
+ next
+ end
+
+ src_path = source + f.sub(source, '')
+ target_path = target + f.sub(source, '')
+
+ if File.directory?(f)
+ FileUtils.mkdir_p target_path
+ else
+ FileUtils.cp src_path, target_path
+ end
+ end
+
+ announce "Sample products have been loaded into to the store"
+ end
+
+ private
+=begin
+ def prompt_for_admin_name
+ username = ask('Name (Administrator): ', String) do |q|
+ q.validate = /^.{0,100}$/
+ q.responses[:not_valid] = "Invalid name. Must be at less than 100 characters long."
+ q.whitespace = :strip
+ end
+ username = "Administrator" if username.blank?
+ username
+ end
+=end
+ def prompt_for_admin_username
+ username = ask('Username [admin]: ', String) do |q|
+ q.validate = /^(|.{3,40})$/
+ q.responses[:not_valid] = "Invalid username. Must be at least 3 characters long."
+ q.whitespace = :strip
+ end
+ username = "admin" if username.blank?
+ username
+ end
+
+ def prompt_for_admin_password
+ password = ask('Password [spree]: ', String) do |q|
+ q.echo = false
+ q.validate = /^(|.{5,40})$/
+ q.responses[:not_valid] = "Invalid password. Must be at least 5 characters long."
+ q.whitespace = :strip
+ end
+ password = "spree" if password.blank?
+ password
+ end
+
+ def prompt_for_admin_email
+ email = ask('Email [spree@example.com]: ', String) do |q|
+ q.echo = false
+ q.whitespace = :strip
+ end
+ email = "spree@example.com" if email.blank?
+ email
+ end
+
+ # ask user if we should generate some sample data
+ def sample_data?
+ sample = ask('Load Sample Data? [y]: ', String) do |q|
+ q.echo = false
+ q.whitespace = :strip
+ end
+ sample == "y" or sample == "yes" or sample == "true"
+ end
+=begin
+ def find_template_in_path(filename)
+ [
+ filename,
+ "#{RADIANT_ROOT}/#{filename}",
+ "#{RADIANT_ROOT}/db/templates/#{filename}",
+ "#{RAILS_ROOT}/#{filename}",
+ "#{RAILS_ROOT}/db/templates/#{filename}",
+ "#{Dir.pwd}/#{filename}",
+ "#{Dir.pwd}/db/templates/#{filename}",
+ ].find { |name| File.file?(name) }
+ end
+
+ def find_and_load_templates(glob)
+ templates = Dir[glob]
+ templates.map! { |template| load_template_file(template) }
+ templates.sort_by { |template| template['name'] }
+ end
+
+ def load_template_file(filename)
+ YAML.load_file(filename)
+ end
+
+ def create_records(template)
+ records = template['records']
+ if records
+ puts
+ records.keys.each do |key|
+ feedback "Creating #{key.to_s.underscore.humanize}" do
+ model = model(key)
+ model.reset_column_information
+ record_pairs = order_by_id(records[key])
+ step do
+ record_pairs.each do |id, record|
+ model.new(record).save
+ end
+ end
+ end
+ end
+ end
+ end
+
+ def model(model_name)
+ model_name.to_s.singularize.constantize
+ end
+
+ def order_by_id(records)
+ records.map { |name, record| [record['id'], record] }.sort { |a, b| a[0] <=> b[0] }
+ end
+
+ extend Forwardable
+ def_delegators :terminal, :agree, :ask, :choose, :say
+
+ def terminal
+ @terminal ||= HighLine.new
+ end
+
+ def output
+ terminal.instance_variable_get("@output")
+ end
+
+ def wrap(string)
+ string = terminal.send(:wrap, string) unless terminal.wrap_at.nil?
+ string
+ end
+
+ def print(string)
+ output.print(wrap(string))
+ output.flush
+ end
+
+ def puts(string = "\n")
+ say string
+ end
+=end
+ def announce(string)
+ puts "\n#{string}"
+ end
+=begin
+ def feedback(process, &block)
+ print "#{process}..."
+ if yield
+ puts "OK"
+ true
+ else
+ puts "FAILED"
+ false
+ end
+ rescue Exception => e
+ puts "FAILED"
+ raise e
+ end
+
+ def step
+ yield if block_given?
+ print '.'
+ end
+=end
+ end
+end
View
35 lib/tasks/database.rake
@@ -0,0 +1,35 @@
+#######################################################################################################
+# Substantial portions of this code were adapted from the Radiant CMS project (http://radiantcms.org) #
+#######################################################################################################
+namespace :db do
+ desc "Migrate schema to version 0 and back up again. WARNING: Destroys all data in tables!!"
+ task :remigrate => :environment do
+ require 'highline/import'
+ if ENV['OVERWRITE'].to_s.downcase == 'true' or agree("This task will destroy any data in the database. Are you sure you want to \ncontinue? [yn] ")
+
+ # Migrate downward
+ ActiveRecord::Migrator.migrate("#{SPREE_ROOT}/db/migrate/", 0)
+
+ # Migrate upward
+ Rake::Task["db:migrate"].invoke
+
+ # Dump the schema
+ Rake::Task["db:schema:dump"].invoke
+ else
+ say "Task cancelled."
+ exit
+ end
+ end
+
+ desc "Bootstrap your database for Spree."
+ task :bootstrap => :remigrate do
+ require 'spree/setup'
+ Spree::Setup.bootstrap(
+ :admin_name => ENV['ADMIN_NAME'],
+ :admin_username => ENV['ADMIN_USERNAME'],
+ :admin_password => ENV['ADMIN_PASSWORD'],
+ :admin_email => ENV['ADMIN_EMAIL'],
+ :database_template => ENV['DATABASE_TEMPLATE']
+ )
+ end
+end
View
11 lib/tasks/environments.rake
@@ -0,0 +1,11 @@
+desc "Set the environment variable RAILS_ENV='development'."
+task :development do
+ ENV['RAILS_ENV'] = RAILS_ENV = 'development'
+ Rake::Task[:environment].invoke
+end
+
+desc "Set the environment variable RAILS_ENV='production'."
+task :production do
+ ENV['RAILS_ENV'] = RAILS_ENV = 'production'
+ Rake::Task[:environment].invoke
+end
View
26 lib/tasks/instance.rake
@@ -0,0 +1,26 @@
+# Redefined standard Rails tasks only in instance mode
+unless File.directory? "#{RAILS_ROOT}/app"
+ require 'rake/testtask'
+
+ ENV['SPREE_ENV_FILE'] = File.join(RAILS_ROOT, 'config', 'environment')
+
+ [Dir["#{SPREE_ROOT}/vendor/rails/railties/lib/tasks/*.rake"], Dir["#{SPREE_ROOT}/vendor/plugins/rspec_on_rails/tasks/*.rake"]].flatten.each do |rake|
+ lines = IO.readlines(rake)
+ lines.map! do |line|
+ line.gsub!('RAILS_ROOT', 'SPREE_ROOT') unless rake =~ /(misc|rspec)\.rake$/
+ case rake
+ when /testing\.rake$/
+ line.gsub!(/t.libs << (["'])/, 't.libs << \1' + SPREE_ROOT + '/')
+ line.gsub!(/t\.pattern = (["'])/, 't.pattern = \1' + SPREE_ROOT + '/')
+ when /databases\.rake$/
+ line.gsub!(/migrate\((["'])/, 'migrate(\1' + SPREE_ROOT + '/')
+ line.sub!("db/schema.rb", "#{RAILS_ROOT}/db/schema.rb")
+ when /rspec\.rake$/
+ line.gsub!('RAILS_ROOT', 'SPREE_ROOT') unless line =~ /:noop/
+ line.gsub!(/FileList\[(["'])/, "FileList[\\1#{SPREE_ROOT}/")
+ end
+ line
+ end
+ eval(lines.join("\n"), binding, rake)
+ end
+end
View
24 lib/tasks/spree_bootstrap.rake
@@ -1,24 +0,0 @@
-namespace :spree do
- desc "Loads admin user and other structural data required by RC. You must run this task before using RC."
- task :bootstrap => :environment do
- # create the default admin user
- User.create(:login => 'admin',
- :email => 'admin@changeme.com',
- :salt => '7e3041ebc2fc05a40c60028e2c4901a81035d3cd',
- :crypted_password => '00742970dc9e6319f8019fd54864d3ea740f04b1',
- :password => 'test',
- :password_confirmation => 'test')
-
- # create an admin role and and assign the default admin user to the role
- role = Role.create(:name => 'admin')
- user = User.find(1)
- user.roles << role
- user.save!
-
- # create some built-in tax treatments to choose from
- TaxTreatment.create(:name => "Non taxable")
- TaxTreatment.create(:name => "U.S. Sales Tax")
-
- puts "Spree bootstrap process completed."
- end
-end

0 comments on commit b0d97ca

Please sign in to comment.
Something went wrong with that request. Please try again.