Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
uoy-faculty-rake / lib / faculty / rake / docker_tasks.rb
Size: Mime:
# 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 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

      # This is a little bit hacky - rake would normally accept parameters individually and assume each is a separate
      # task, but we can capture them and pass them all to `bundle`. We then have to `exit 0` otherwise rake will
      # try to execute the parameters as tasks.
      def bundle
        desc 'Run bundle commands in docker'
        task :bundle do
          self.class.exec_in @builder, "bundle #{ARGV[1..-1].join ' '}"
          exit 0
        end
      end

      def bundle_install
        namespace :bundle do
          desc 'Install gems and fix ownership of Gemfile.lock'
          task :install do
            self.class.exec_in @builder, 'bundle install'
            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 do
          self.class.exec_in @builder, 'bundle exec rubocop'
        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 do
          self.class.exec_in @test, 'bundle exec rspec'
        end
      end

      def up
        desc 'Bring up app'
        task :up do
          system "docker-compose up -d #{@up_services}"
        end
      end
    end
  end
end