---
version: '3'

tasks:
  load:custom-engines:
    deps:
      - :install:software:docker
      - :install:software:jq
      - :install:software:yq
    env:
      CUSTOM_ENGINES:
        sh: yq e -o=j '.' .codeclimate.yml | jq -r '.plugins | keys[]'
    cmds:
      - |
        function dockerGet() {
          if ! docker images {{.DOCKERHUB_PROFILE}}/$1 | grep ' slim-codeclimate ' > /dev/null; then
            docker pull "{{.DOCKERHUB_PROFILE}}/$1:slim-codeclimate"
          fi
          docker tag "{{.DOCKERHUB_PROFILE}}/$1:slim-codeclimate" "codeclimate/codeclimate-$1:latest"
        }
        for ENGINE in $CUSTOM_ENGINES; do
          if [ "$(yq e -o=j '.' .codeclimate.yml | jq --arg engine $ENGINE -r '.plugins[$engine].enabled')" == 'true' ]; then
            .config/log info "Loading custom CodeClimate engine ($ENGINE)"
            dockerGet "$ENGINE"
          fi
        done

  taskfiles:add:
    cmds:
      - rm -rf .config
      - cp -rf ../../.config .config
      - cp ../../start.sh start.sh
      - cp ../../Taskfile.yml Taskfile.yml
      - cp ../../package.json package.json
    status:
      - '[ -z "$CODECLIMATE_INTEGRATION_TEST" ]'

  taskfiles:remove:
    cmds:
      - rm -rf .config start.sh Taskfile.yml package.json
    status:
      - '[ -z "$CODECLIMATE_INTEGRATION_TEST" ]'
