#!/usr/bin/env ruby # frozen_string_literal: true require 'fileutils' require 'optparse' require 'pathname' def die(message) abort "\n== #{message.strip} ==" end def log(message) puts "\n== #{message.strip} ==" end def prompt(message) print "#{message.strip} " gets.strip end def confirm(message) prompt("#{message.strip} [y/N]").casecmp('y') == 0 end def system!(*args) system(*args) || die("Command #{args} failed") end APP_ROOT = Pathname.new File.expand_path('..', __dir__) options = {} OptionParser.new do |opt| opt.banner = 'Usage: bin/setup [-a] [--root URL] [--seed] [--boot]' opt.on('-a', 'automated setup (do not prompt for information not given on the command line)') { |o| options[:auto] = o } opt.on('--root URL', 'the URL root ("localhost" in development environments, your domain in staging environments)') { |o| options[:root] = o } opt.on('--seed', 'Seed the database with sample users, images, comments, and posts') { |o| options[:seed] = o } opt.on('--boot', 'Boot the application server and background processing') { |o| options[:boot] = o } end.parse! log 'Beginning setup' Dir.chdir APP_ROOT do log 'Installing gem dependencies' system! 'bin/bundle', 'install' files = [] files << %w[config/nginx.conf.example config/nginx.local.conf] unless File.exist? 'config/nginx.local.conf' files << %w[config/booru/settings.yml.sample config/settings.yml] unless File.exist? 'config/booru/settings.yml' files << %w[config/booru/footer.yml.sample config/booru/footer.yml] unless File.exist? 'config/booru/footer.yml' unless files.empty? log 'Creating local configuration' options[:root] ||= prompt 'Enter the URL root (your domain in staging environments, empty/default is "localhost"):' unless options[:auto] options[:root] ||= 'localhost' files.each { |old, new| File.write new, File.read(old).gsub(/\bderpibooru\.org\b|\b(?:camo\.)?derpicdn\.net\b/, options[:root]) } end log 'Preparing database' options[:seed] ||= confirm 'Seed the database with sample users, images, comments, and posts?' unless options[:auto] ENV['DEV_SEED'] = 'y' if options[:seed] system! 'bin/rails', 'db:setup', out: '/dev/null' system! 'bin/rails', 'db:environment:set', 'RAILS_ENV=development' log 'Initializing system flags (Flipper)' system! 'bin/rails', 'booru:features:enable_defaults' log 'Removing old logs and temporary files' system! 'bin/rails', 'log:clear', 'tmp:clear' log 'Done' options[:boot] ||= confirm 'Boot the application server and background processing?' unless options[:auto] system! 'bin/bundle', 'exec', 'foreman', 'start' if options[:boot] end