---
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: 'true'
      - :install:npm:semantic-release
      - :install:software:git
      - :install:software:poetry
    summary: |
      # Run Semantic Release

      Runs `semantic-release` using the `semantic-release-config` package. The package
      includes release instructions for:

      1. Python packages
      2. NPM packages
      3. Dockerfile projects
      4. Go projects
      5. Ansible roles

      It does its best to ship projects out in as many formats (that make sense) as possible.
      For instance, an NPM CLI gets published as:

      1. NPM package
      2. Binaries (including deb and rpm) available on GitHub/GitLab releases
      3. Brew formulae
      4. Snapcraft
      5. Probably more implemented after writing this

      ## Python Package Notes

      If `setup.cfg` is present, then the project is assumed to be a legacy Python
      project that needs to run in environment with access to the `python-semantic`
      extras defined in `pyproject.toml`.
    vars:
      RELEASE_PREFIX:
        sh: if [ -f setup.cfg ]; then echo 'poetry run '; else echo ''; fi
    log:
      error: Error while running `semantic-release`
      start: Releasing with `semantic-release`..
      success: Finished running `semantic-release`
    cmds:
      - 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
      - |
        export GIT_CREDENTIALS="gitlab-ci-token:$CI_BUILD_TOKEN"
        {{.RELEASE_PREFIX}}semantic-release -r "$(git remote get-url origin)"{{if .CLI_ARGS}} {{.CLI_ARGS}}{{end}}

  version:tag:
    deps:
      - :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
