# yaml-language-server: $schema=https://taskfile.dev/schema.json

# Copyright 2024 Google LLC. This software is provided as-is, without
# warranty or representation for any use or purpose. Your use of it is
# subject to your agreement with Google.
# 
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
#    http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


version: '3'
dotenv: [taskfile.env]

includes:
  gcloud:
    taskfile: ./.task/gcloud.yaml
    optional: true
    dir: ./.task
  
  docker:
    taskfile: ./.task/docker.yaml
    optional: true
    dir: ./.task

  infra:
    taskfile: ./.task/infra.yaml
    optional: true
    dir: ./.task

  deploy:
    taskfile: ./.task/deploy.yaml
    optional: true
    dir: ./.task

tasks:
  default:
    desc: List all available tasks
    cmds:
      - task --list

  venv-sync:
    desc: Sync your UV environment
    silent: true
    cmds:
      - task: uv-create
      - task: uv-sync

  uv-create:
    internal: true
    silent: true
    desc: Create a new UV environment if one doesn't exist  
    cmds:
      - uv sync --all-groups --link-mode=copy
    status:
      - ls uv.lock

  uv-sync:
    internal: true
    silent: true
    desc: Create a new UV environment if one doesn't exist  
    cmds:
      - uv sync --all-groups --link-mode=copy
      # - uv sync --frozen --all-groups --link-mode=copy
  
  create-coverage-folder:
    desc: Create a coverage folder and include a .gitignore file in that folder
    internal: true
    status:
      - ls ./.coverage/.gitignore
    cmds:
      - cmd: mkdir ./.coverage
        ignore_error: true
      - echo "# Added by 'create-coverage-folder' task" >> ./.coverage/.gitignore
      - echo "*" >> ./.coverage/.gitignore
    
  test:
    desc: Run tests
    summary: |
      Examples:
        task test -- src/backend/tests/api/test_quality_assessment_evaluation.py
    deps: [venv-sync, create-coverage-folder]
    cmds:
      - uv run pytest -s {{.CLI_ARGS}}

  install-keyring:
    desc: Install keyring tools for Google Artifact Registry
    cmds:
      - uv tool install keyring --with keyrings.google-artifactregistry-auth

  watch-logs-*:
    interactive: true
    desc: Watch the logs of a given container pass container name after '-', e.g. task docker:watch-logs-backend
    preconditions:
      - bash ./.task/is_container_status.sh --running {{ .CONTAINER_NAME }}
    vars:
      CONTAINER_NAME: '{{index .MATCH 0}}'
    cmds: 
      - while true; do docker logs -f {{ .CONTAINER_NAME }}; sleep 5;done

  ###########################################################################
  #             Google Cloud Secret Manager References                      #
  ###########################################################################
  # If you want to use a secret stored in Google Secret Manager for         #
  # configuration or secrets, you can inject them as environment variables  #
  # a specific command doing something like the below 'show-secret' task    #
  #                                                                         #
  # In this case, in the taskfile.env file, EXAMPLE_SECRET_NAME is set to   #
  # 'super-duper-secret' if we created a secret in Secret Manager named #
  # 'super-duper-secret' and set the value to "password123", then running   #
  # this task would look like this (assuming you have access to the secret  #
  # in Google Cloud Secret Manager):                                        #
  #                                                                         #
  #     > task show-secret                                                  #
  #     The secret is 'password123'                                         #
  ###########################################################################

  show-secret:
    desc: Echo the value of the secret {{ .EXAMPLE_SECRET_NAME }}
    cmds:
      - echo "The secret is '${SECRET_VALUE}'"
    env:
      SECRET_VALUE:
        sh: gcloud secrets versions access latest --secret={{ .EXAMPLE_SECRET_NAME }}
