Repository URL to install this package:
|
Version:
3.2.0 ▾
|
# frozen_string_literal: true
require 'rake/tasklib'
require 'yaml'
module Faculty
module Rake
# Functions for running common ruby tasks using docker compose
class DockerTasks < ::Rake::TaskLib
TASKS = %w[audit build bundle bundle_install bundle_update db_refresh db_reset down rubocop rubocop_auto_correct
spec up].freeze
def initialize(app: :app, builder: :builder, test: :test, up_services: 'web')
super()
@app = app
@builder = builder
@test = test
@up_services = up_services
TASKS.each do |task|
send task
end
end
def audit
desc 'Run audit check'
task :audit do
self.class.exec_in @builder, 'bundle exec bundler-audit check --update'
end
end
def build # rubocop:disable Metrics/MethodLength
desc 'Run docker compose build with the correct config values'
task :build do
config_file = YAML.load_file('config.yaml')
env = {}
if config_file['web_library']
env['WEB_LIBRARY_ROOT'] = config_file['web_library']['root']
env['WEB_LIBRARY_VERSION'] = config_file['web_library']['version'].to_s
end
system env, 'docker compose build --pull'
system 'docker compose pull'
end
end
def bundle
desc 'Run bundle commands in docker'
task :bundle, [:cmd] do |_, args|
self.class.exec_in @builder, "bundle #{args.cmd}"
end
end
def bundle_install
namespace :bundle do
desc 'Install gems and fix ownership of Gemfile.lock'
task :install, [:args] do |_, args|
self.class.exec_in @builder, "bundle install #{args.args}"
self.class.exec_in @builder, "chown #{Process.uid}:#{Process.gid} Gemfile.lock"
end
end
end
def bundle_update
namespace :bundle do
desc 'Update gems and fix ownership of Gemfile.lock'
task :update, [:args] do |_, args|
self.class.exec_in @builder, "bundle update #{args.args}"
self.class.exec_in @builder, "chown #{Process.uid}:#{Process.gid} Gemfile.lock"
end
end
end
def db_refresh
desc 'Refresh the database and restart the app'
task db_refresh: %i[down db_reset up]
end
def db_reset
desc 'Reset the database'
task :db_reset do
sh "docker volume rm -f #{File.basename(Dir.getwd)}_db"
end
end
def down
desc 'Take down app'
task :down do
system 'docker compose down'
end
end
def self.exec_in(service, command)
system "docker compose run --rm #{service} #{command}"
end
def rubocop
desc 'Run rubocop'
task :rubocop, [:args] do |_, args|
self.class.exec_in @builder, "bundle exec rubocop #{args.args}"
end
end
def rubocop_auto_correct
namespace :rubocop do
desc 'Run rubocop -a'
task :auto_correct do
self.class.exec_in @builder, 'bundle exec rubocop -a'
end
end
end
def spec
desc 'Run rspec tests'
task :spec, [:args] do |_, args|
self.class.exec_in @test, "bundle exec rspec #{args.args}"
end
end
def up
desc 'Bring up app'
task :up do
system "docker compose up -d #{@up_services}"
end
end
end
end
end