CONTAINER_USERID = %x(id -u).freeze
VERSION = '42.3'.freeze

namespace :docker do
  desc 'Build our development environment'
  task :build do
    begin
      sh 'echo "# This file is generated by our Rakefile. Do not change it!" > docker-compose.override.yml'
      # rubocop:disable Metrics/LineLength
      sh "echo \"version: \'2\'\nservices:\n  frontend:\n    build:\n      args:\n        CONTAINER_USERID: #{CONTAINER_USERID}\" >> docker-compose.override.yml"
      # rubocop:enable Metrics/LineLength
      # Build the frontend container
      sh 'docker-compose build frontend'
      # Bootstrap the app
      sh 'docker-compose up -d db'
      sh 'docker-compose run --no-deps --rm frontend bundle exec rake dev:bootstrap RAILS_ENV=development'
    ensure
      sh 'docker-compose stop'
    end
  end

  namespace :test do
    desc 'Run our frontend tests in the docker container'
    task :frontend do
      begin
        sh 'docker-compose -f docker-compose.ci.yml up --abort-on-container-exit'
      ensure
        sh 'docker-compose -f docker-compose.ci.yml stop'
      end
    end

    desc 'Run our backend tests in the docker container'
    task :backend do
      begin
        sh 'docker-compose run --rm -w /obs backend make -C src/backend test'
      ensure
        sh 'docker-compose stop'
      end
    end

    desc 'Scan the code base for syntax/code problems'
    task :lint do
      begin
        sh 'docker-compose -f docker-compose.ci.yml run --rm rspec bundle exec rake dev:bootstrap dev:lint'
      ensure
        sh 'docker-compose -f docker-compose.ci.yml stop'
      end
    end

    namespace :old do
      desc 'Run our frontend api old test suite in the docker container'
      task :api, :test do |_t, args|
        begin
          if args[:test]
            sh "docker-compose -f docker-compose.ci_old.yml run --rm --entrypoint '/obs/contrib/start_old_tests #{args[:test]}' old-test-suite"
          else
            sh 'docker-compose -f docker-compose.ci_old.yml up --no-recreate --abort-on-container-exit'
          end
        ensure
          sh 'docker-compose -f docker-compose.ci_old.yml stop'
        end
      end
    end
  end

  namespace :maintainer do
    desc 'Rebuild all our static containers'
    task rebuild: ['rebuild:base', 'rebuild:backend', 'rebuild:frontend-base', 'rebuild:mariadb', 'rebuild:memcached', 'rebuild:old-test-suite'] do
    end
    namespace :rebuild do
      task :base do
        sh "docker build . -t openbuildservice/base:#{VERSION} -t openbuildservice/base -f Dockerfile.#{VERSION}"
      end
      task :mariadb do
        sh "docker build . -t openbuildservice/mariadb:#{VERSION} -t openbuildservice/mariadb -f Dockerfile.mariadb"
      end
      task :memcached do
        sh "docker build . -t openbuildservice/memcached:#{VERSION} -t openbuildservice/memcached -f Dockerfile.memcached"
      end
      task 'frontend-base' do
        sh "docker build . -t openbuildservice/frontend-base:#{VERSION} -t openbuildservice/frontend-base -f Dockerfile.frontend-base"
      end
      task :backend do
        sh "docker build . -t openbuildservice/backend:#{VERSION} -t openbuildservice/backend -f Dockerfile.backend"
      end
      task 'old-test-suite' do
        sh "docker build . -t openbuildservice/old-test-suite:#{VERSION} -t openbuildservice/old-test-suite -f Dockerfile.old-test-suite"
      end
    end

    desc 'Rebuild and publish all our static containers'
    task publish: [:rebuild, 'publish:base', 'publish:mariadb', 'publish:memcached', 'publish:backend', 'publish:frontend-base', \
                   'publish:old-test-suite'] do
    end
    namespace :publish do
      task :base do
        sh "docker push openbuildservice/base:#{VERSION}"
        sh 'docker push openbuildservice/base'
      end
      task :mariadb do
        sh "docker push openbuildservice/mariadb:#{VERSION}"
        sh 'docker push openbuildservice/mariadb'
      end
      task :memcached do
        sh "docker push openbuildservice/memcached:#{VERSION}"
        sh 'docker push openbuildservice/memcached'
      end
      task :backend do
        sh "docker push openbuildservice/backend:#{VERSION}"
        sh 'docker push openbuildservice/backend'
      end
      task 'frontend-base' do
        sh "docker push openbuildservice/frontend-base:#{VERSION}"
        sh 'docker push openbuildservice/frontend-base'
      end
      task 'old-test-suite' do
        sh "docker push openbuildservice/old-test-suite:#{VERSION}"
        sh 'docker push openbuildservice/old-test-suite'
      end
    end
  end
end
