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
|
|
|
{
|
2023-05-12 13:01:12 +02: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
|
2023-05-12 13:01:12 +02:00
|
|
|
desc 'Setup foodsoft'
|
2013-05-26 14:02:16 +02:00
|
|
|
task :setup_development do
|
2023-05-12 13:01:12 +02:00
|
|
|
puts yellow 'This task will help you get your foodcoop running in development.'
|
2013-05-26 14:02:16 +02:00
|
|
|
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
|
2023-05-12 13:01:12 +02: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
|
|
|
|
2023-05-12 13:01:12 +02:00
|
|
|
desc 'Setup foodsoft'
|
2021-03-02 19:18:57 +01:00
|
|
|
task :setup_development_docker do
|
2023-05-12 13:01:12 +02:00
|
|
|
puts yellow 'This task will help you get your foodcoop running in development via docker.'
|
2021-03-02 19:18:57 +01:00
|
|
|
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
|
2023-05-12 13:01:12 +02:00
|
|
|
puts yellow 'All done! Your foodsoft setup should be running smoothly via docker.'
|
2021-03-02 19:18:57 +01:00
|
|
|
end
|
|
|
|
|
2013-07-24 13:21:52 +02:00
|
|
|
namespace :setup do
|
2023-05-12 13:01:12 +02:00
|
|
|
desc 'Initialize stock configuration'
|
2013-07-24 13:21:52 +02:00
|
|
|
task :stock_config do
|
|
|
|
setup_app_config
|
2023-05-06 11:46:45 +02:00
|
|
|
setup_storage
|
2013-07-24 13:21:52 +02:00
|
|
|
setup_development
|
|
|
|
end
|
|
|
|
end
|
2013-05-26 14:02:16 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def setup_bundler
|
2023-05-12 13:01:12 +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)
|
2023-05-12 13:01:12 +02:00
|
|
|
puts yellow 'Executing bundle install...'
|
|
|
|
`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']
|
2023-05-12 13:01:12 +02:00
|
|
|
puts blue 'DATABASE_URL found, please remember to also set it when running Foodsoft'
|
2015-05-02 13:47:52 +02:00
|
|
|
return nil
|
|
|
|
end
|
2013-05-26 14:02:16 +02:00
|
|
|
return nil if skip?(file)
|
2021-03-01 15:27:26 +01:00
|
|
|
|
2023-05-12 13:01:12 +02:00
|
|
|
database = ask("What kind of database do you use?\nOptions:\n(1) MySQL\n(2) SQLite", %w[1 2])
|
|
|
|
if database == '1'
|
|
|
|
puts yellow 'Using MySQL...'
|
|
|
|
`cp -p #{Rails.root.join("#{file}.MySQL_SAMPLE")} #{Rails.root.join(file)}`
|
|
|
|
elsif database == '2'
|
|
|
|
puts yellow 'Using SQLite...'
|
|
|
|
`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
|
|
|
|
2023-05-12 13:01:12 +02:00
|
|
|
puts blue 'IMPORTANT: Edit (rake-generated) config/database.yml with valid username and password for EACH env before continuing!'
|
|
|
|
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
|
2023-05-12 13:01:12 +02:00
|
|
|
Rake::Task['db:setup'].reenable
|
|
|
|
db_setup = capture_stdout { Rake::Task['db:setup'].invoke }
|
2021-03-02 19:18:57 +01:00
|
|
|
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}..."
|
2023-05-12 13:01:12 +02:00
|
|
|
`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}..."
|
2023-05-12 13:01:12 +02:00
|
|
|
`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}..."
|
2023-05-12 13:01:12 +02:00
|
|
|
`cp -p #{Rails.root.join("#{file}.SAMPLE")} #{Rails.root.join(file)}`
|
2022-01-31 08:44:53 +01:00
|
|
|
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
|
|
|
|
2023-05-12 13:01:12 +02:00
|
|
|
mailcatcher = ask("Do you want to start mailcatcher?\nOptions:\n(y) Yes\n(n) No", %w[y n])
|
|
|
|
return unless mailcatcher === 'y'
|
|
|
|
|
|
|
|
puts yellow 'Starting mailcatcher at http://localhost:1080...'
|
|
|
|
`mailcatcher`
|
2013-05-26 14:02:16 +02:00
|
|
|
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))
|
2023-05-12 13:01:12 +02:00
|
|
|
puts red 'Your Input is not valid. Try again!'
|
2013-05-26 14:02:16 +02:00
|
|
|
input = ask(question, answers)
|
|
|
|
end
|
|
|
|
input
|
|
|
|
end
|
|
|
|
|
|
|
|
def skip?(file)
|
|
|
|
output = false
|
2023-05-12 13:01:12 +02:00
|
|
|
if File.exist?(Rails.root.join(file))
|
|
|
|
skip = ask(cyan("We found #{file}!\nOptions:\n(1) Skip step\n(2) Force rewrite"),
|
|
|
|
%w[1 2])
|
|
|
|
end
|
|
|
|
output = true if skip == '1'
|
2013-05-26 14:02:16 +02:00
|
|
|
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
|