2014-01-04 20:12:01 +01:00
|
|
|
require 'stringio'
|
|
|
|
|
2013-05-26 14:02:16 +02:00
|
|
|
# put in here all foodsoft tasks
|
|
|
|
# => :environment loads the environment an gives easy access to the application
|
|
|
|
|
|
|
|
module Colors
|
|
|
|
def colorize(text, color_code)
|
|
|
|
"\033[#{color_code}m#{text}\033[0m"
|
|
|
|
end
|
2021-03-01 15:27:26 +01:00
|
|
|
|
2013-05-26 14:02:16 +02:00
|
|
|
{
|
2021-03-01 15:27:26 +01:00
|
|
|
:black => 30,
|
|
|
|
:red => 31,
|
|
|
|
:green => 32,
|
|
|
|
:yellow => 33,
|
|
|
|
:blue => 34,
|
|
|
|
:magenta => 35,
|
|
|
|
:cyan => 36,
|
|
|
|
:white => 37
|
2013-05-26 14:02:16 +02:00
|
|
|
}.each do |key, color_code|
|
|
|
|
define_method key do |text|
|
|
|
|
colorize(text, color_code)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
include Colors
|
|
|
|
|
|
|
|
namespace :foodsoft do
|
|
|
|
desc "Setup foodsoft"
|
|
|
|
task :setup_development do
|
|
|
|
puts yellow "This task will help you get your foodcoop running in development."
|
|
|
|
setup_bundler
|
|
|
|
setup_app_config
|
|
|
|
setup_development
|
|
|
|
setup_database
|
2022-01-31 08:44:53 +01:00
|
|
|
setup_storage
|
2013-05-29 12:35:16 +02:00
|
|
|
start_mailcatcher
|
2021-03-02 19:18:57 +01:00
|
|
|
puts yellow "All done! Your foodsoft setup should be running smoothly."
|
2013-05-26 14:02:16 +02:00
|
|
|
start_server
|
|
|
|
end
|
2013-07-24 13:21:52 +02:00
|
|
|
|
2021-03-02 19:18:57 +01:00
|
|
|
desc "Setup foodsoft"
|
|
|
|
task :setup_development_docker do
|
|
|
|
puts yellow "This task will help you get your foodcoop running in development via docker."
|
|
|
|
setup_app_config
|
|
|
|
setup_development
|
2022-01-31 08:44:53 +01:00
|
|
|
setup_storage
|
2021-03-02 19:18:57 +01:00
|
|
|
setup_run_rake_db_setup
|
|
|
|
puts yellow "All done! Your foodsoft setup should be running smoothly via docker."
|
|
|
|
end
|
|
|
|
|
2013-07-24 13:21:52 +02:00
|
|
|
namespace :setup do
|
|
|
|
desc "Initialize stock configuration"
|
|
|
|
task :stock_config do
|
|
|
|
setup_app_config
|
|
|
|
setup_development
|
|
|
|
end
|
|
|
|
end
|
2013-05-26 14:02:16 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def setup_bundler
|
2013-05-29 12:35:16 +02:00
|
|
|
puts yellow "Installing bundler if not installed..."
|
2021-03-01 15:27:26 +01:00
|
|
|
%x(if [ -z `which bundle` ]; then gem install bundler --no-rdoc --no-ri; fi)
|
2013-05-29 12:35:16 +02:00
|
|
|
puts yellow "Executing bundle install..."
|
2021-03-01 15:27:26 +01:00
|
|
|
%x(bundle install)
|
2013-05-26 14:02:16 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def setup_database
|
|
|
|
file = 'config/database.yml'
|
2015-05-02 13:47:52 +02:00
|
|
|
if ENV['DATABASE_URL']
|
|
|
|
puts blue "DATABASE_URL found, please remember to also set it when running Foodsoft"
|
|
|
|
return nil
|
|
|
|
end
|
2013-05-26 14:02:16 +02:00
|
|
|
return nil if skip?(file)
|
2021-03-01 15:27:26 +01:00
|
|
|
|
|
|
|
database = ask("What kind of database do you use?\nOptions:\n(1) MySQL\n(2) SQLite", ["1", "2"])
|
2013-05-26 14:02:16 +02:00
|
|
|
if database == "1"
|
|
|
|
puts yellow "Using MySQL..."
|
2021-03-02 19:18:57 +01:00
|
|
|
%x(cp -p #{Rails.root.join("#{file}.MySQL_SAMPLE")} #{Rails.root.join(file)})
|
2013-05-26 14:02:16 +02:00
|
|
|
elsif database == "2"
|
|
|
|
puts yellow "Using SQLite..."
|
2021-03-02 19:18:57 +01:00
|
|
|
%x(cp -p #{Rails.root.join("#{file}.SQLite_SAMPLE")} #{Rails.root.join(file)})
|
2013-05-26 14:02:16 +02:00
|
|
|
end
|
2021-03-01 15:27:26 +01:00
|
|
|
|
2013-05-26 14:02:16 +02:00
|
|
|
reminder(file)
|
2021-03-01 15:27:26 +01:00
|
|
|
|
2015-08-12 04:18:43 +02:00
|
|
|
puts blue "IMPORTANT: Edit (rake-generated) config/database.yml with valid username and password for EACH env before continuing!"
|
2013-05-26 14:02:16 +02:00
|
|
|
finished = ask("Finished?\nOptions:\n(y) Yes", ["y"])
|
2021-03-02 19:18:57 +01:00
|
|
|
setup_run_rake_db_setup if finished
|
|
|
|
end
|
|
|
|
|
|
|
|
def setup_run_rake_db_setup
|
|
|
|
Rake::Task["db:setup"].reenable
|
|
|
|
db_setup = capture_stdout { Rake::Task["db:setup"].invoke }
|
|
|
|
puts db_setup
|
2013-05-26 14:02:16 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def setup_app_config
|
|
|
|
file = 'config/app_config.yml'
|
2014-01-04 20:12:01 +01:00
|
|
|
sample = Rails.root.join("#{file}.SAMPLE")
|
2013-05-26 14:02:16 +02:00
|
|
|
return nil if skip?(file)
|
2021-03-01 15:27:26 +01:00
|
|
|
|
2013-05-26 14:02:16 +02:00
|
|
|
puts yellow "Copying #{file}..."
|
2021-03-02 19:18:57 +01:00
|
|
|
%x(cp -p #{sample} #{Rails.root.join(file)})
|
2013-05-26 14:02:16 +02:00
|
|
|
reminder(file)
|
|
|
|
end
|
|
|
|
|
|
|
|
def setup_development
|
|
|
|
file = 'config/environments/development.rb'
|
|
|
|
return nil if skip?(file)
|
2021-03-01 15:27:26 +01:00
|
|
|
|
2013-05-26 14:02:16 +02:00
|
|
|
puts yellow "Copying #{file}..."
|
2021-03-02 19:18:57 +01:00
|
|
|
%x(cp -p #{Rails.root.join("#{file}.SAMPLE")} #{Rails.root.join(file)})
|
2013-05-26 14:02:16 +02:00
|
|
|
reminder(file)
|
|
|
|
end
|
|
|
|
|
2022-01-31 08:44:53 +01:00
|
|
|
def setup_storage
|
|
|
|
file = 'config/storage.yml'
|
|
|
|
return nil if skip?(file)
|
|
|
|
|
|
|
|
puts yellow "Copying #{file}..."
|
|
|
|
%x(cp -p #{Rails.root.join("#{file}.SAMPLE")} #{Rails.root.join(file)})
|
|
|
|
reminder(file)
|
|
|
|
end
|
|
|
|
|
2013-05-29 12:35:16 +02:00
|
|
|
def start_mailcatcher
|
2015-05-02 13:47:52 +02:00
|
|
|
return nil if ENV['MAILCATCHER_PORT'] # skip when it has an existing Docker container
|
2021-03-01 15:27:26 +01:00
|
|
|
|
|
|
|
mailcatcher = ask("Do you want to start mailcatcher?\nOptions:\n(y) Yes\n(n) No", ["y", "n"])
|
2013-05-29 12:35:16 +02:00
|
|
|
if mailcatcher === "y"
|
|
|
|
puts yellow "Starting mailcatcher at http://localhost:1080..."
|
2021-03-01 15:27:26 +01:00
|
|
|
%x(mailcatcher)
|
2013-05-26 14:02:16 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-05-29 12:35:16 +02:00
|
|
|
def start_server
|
|
|
|
puts blue "Start your server running 'bundle exec rails s' and visit http://localhost:3000"
|
|
|
|
end
|
|
|
|
|
2014-01-04 20:12:01 +01:00
|
|
|
# Helper Methods
|
|
|
|
|
2013-05-26 14:02:16 +02:00
|
|
|
def ask(question, answers = false)
|
|
|
|
puts question
|
|
|
|
input = STDIN.gets.chomp
|
2015-01-14 21:15:08 +01:00
|
|
|
if input.blank? || (answers && !answers.include?(input))
|
2013-05-26 14:02:16 +02:00
|
|
|
puts red "Your Input is not valid. Try again!"
|
|
|
|
input = ask(question, answers)
|
|
|
|
end
|
|
|
|
input
|
|
|
|
end
|
|
|
|
|
|
|
|
def skip?(file)
|
|
|
|
output = false
|
2021-03-01 15:27:26 +01:00
|
|
|
skip = ask(cyan("We found #{file}!\nOptions:\n(1) Skip step\n(2) Force rewrite"), ["1", "2"]) if File.exists?(Rails.root.join(file))
|
2013-05-26 14:02:16 +02:00
|
|
|
output = true if skip == "1"
|
|
|
|
output
|
|
|
|
end
|
|
|
|
|
|
|
|
def reminder(file)
|
|
|
|
puts blue "don't forget to edit #{file}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def capture_stdout
|
|
|
|
s = StringIO.new
|
|
|
|
$stdout = s
|
|
|
|
yield
|
|
|
|
s.string
|
|
|
|
ensure
|
|
|
|
$stdout = STDOUT
|
|
|
|
end
|