---
version: '3'

tasks:
  app:beta:
    deps:
      - :publish:android:beta
      - :publish:chrome:private
      - :publish:firefox:private
      - :publish:ios:beta
      - :publish:opera:private
    desc: Publish a beta release of all the release targets
    hide: '{{ne (print .REPOSITORY_TYPE "-" .REPOSITORY_SUBTYPE) "angular-app"}}'
    log:
      error: Error publishing beta release
      start: Publishing beta release
      success: Finished publishing beta release

  app:release:
    deps:
      - :publish:android:release
      - :publish:brew:cask
      - :publish:chrome:release
      - :publish:firefox:release
      - :publish:ios:release
      - :publish:opera:release
      - :publish:menubar:release
    desc: Publish a production release of all the targets
    hide: '{{ne (print .REPOSITORY_TYPE "-" .REPOSITORY_SUBTYPE) "angular-app"}}'
    summary: |
      This task is used to release Angular applications.
    log:
      error: Error publishing `production` release
      start: Publishing `production` release
      success: Successfully published `production` release

  semantic-release:
    deps:
      - task: :install:modules:local
        vars:
          SEMANTIC_RELEASE:
            sh: echo true
      - :install:npm:semantic-release
      - :install:software:git
    vars:
      RELEASE_PREFIX:
        sh: if [ -f setup.cfg ]; then echo 'poetry run '; else echo ''; fi
      RUN_OPTIONS: '{{if .CLI_ARGS}} {{.CLI_ARGS}}{{else}} --ci false --debug --dry-run false{{end}}'
    env:
      GIT_CREDENTIALS:
        sh: |
          if [ -n "$CI" ]; then
            echo "gitlab-ci-token:$CI_BUILD_TOKEN"
          fi
      HUSKY: '0'
      PUBLISHING: 'true'
      SEMANTIC_RELEASE: 'true'
    log:
      error: Error while running `semantic-release`
      start: Releasing with `semantic-release`..
      success: Finished running `semantic-release`
    cmds:
      - git fetch --tags -f
      - task: semantic-release:prepare
      - |
        {{.RELEASE_PREFIX}}semantic-release -r "$(git remote get-url origin)"{{.RUN_OPTIONS}}
      - git push all master || git push origin master

  semantic-release:build:
    cmds:
      - rm -rf artifacts build dist
      - task: :release:build

  semantic-release:poetry:
    deps:
      - :install:software:poetry
    cmds:
      - poetry install -E semantic
    status:
      - '[ ! -f setup.cfg ]'

  semantic-release:prepare:
    deps:
      - task: :install:modules:local:sync
        vars:
          NPM_KEEP_UPDATED: '{{.SEMANTIC_CONFIG}}'
      - :common:update:variables
      - :npm:config
      - semantic-release:build
      - semantic-release:poetry
      - version:tag

  version:tag:
    deps:
      - :ci:commit:config
      - :install:software:jq
    summary: |
      This command is intended to be used before the `semantic-release` task to ensure
      that the latest git tag is up-to-date with the latest `package.json` version.
    cmds:
      - git fetch --all --tags -f
      - git tag "v$(jq -r '.version' package.json)"
    status:
      - git tag | grep "v$(jq -r '.version' package.json)"
    preconditions:
      - sh: '[ -f package.json ]'
        msg: A package.json file must be present!
      - sh: '[ "$(jq -r \".version\" package.json)" != "null" ]'
        msg: Version must be defined in package.json
