---
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
      - :install:software:poetry
    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:
      PUBLISHING: 'true'
    log:
      error: Error while running `semantic-release`
      start: Releasing with `semantic-release`..
      success: Finished running `semantic-release`
    cmds:
      - rm -rf build dist
      - task: :install:modules:local:sync
        vars:
          NPM_KEEP_UPDATED: '{{.SEMANTIC_CONFIG}}'
      - task: version:tag
      - task: :common:update:variables
      - |
        if [ -f setup.cfg ]; then
          poetry install -E semantic
        fi
      - |
        if [ -n "$CI" ]; then
          export GIT_CREDENTIALS="gitlab-ci-token:$CI_BUILD_TOKEN"
        fi
        unset
        {{.RELEASE_PREFIX}}semantic-release -r "$(git remote get-url origin)"{{.RUN_OPTIONS}}

  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
      - |
        if ! git tag | grep "v$(jq -r '.version' package.json)"; then
          git tag -a "v$(jq -r '.version' package.json )" -m "Latest package.json version tagged."
          git push origin --tags
        fi
    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
