CONTAINER_USERID = %x(id -u).freeze

namespace :docker do
  desc 'Build our development environment'
  task :build do
    # Setup the git commit message template
    sh 'git config commit.template .gitmessage'
    sh 'touch docker-files/home/.bash_history docker-files/home/.irb_history docker-files/home/.pry_history'
    sh "cat << EOF > docker-compose.override.yml
# This file is generated by our Rakefile. Do not change it!
services:
  frontend:
    build:
      args:
        CONTAINER_USERID: #{CONTAINER_USERID}
    volumes:
      - ./docker-files/home/.bash_history:/home/frontend/.bash_history:Z
      - ./docker-files/home/.irb_history:/home/frontend/.irb_history:Z
      - ./docker-files/home/.pry_history:/home/frontend/.pry_history:Z
      - ./docker-files/home/.irbrc:/home/frontend/.irbrc:Z
      - ./docker-files/home/.pryrc:/home/frontend/.pryrc:Z
EOF"
    # Build the frontend container and pull newer version of the image if available
    sh 'docker compose build --pull 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
