---
version: '3'

tasks:
  build:
    deps:
      - :install:software:jq
    vars:
      BUILD_COMMAND:
        sh: jq -r '.blueprint.build_command' package.json
      BUILD_OUTPUT:
        sh: jq -r '.blueprint.build_command_output' package.json
    cmds:
      - task: build:bin
        vars:
          BUILD_COMMAND: '{{.BUILD_COMMAND}}'
          BUILD_OUTPUT: '{{.BUILD_OUTPUT}}'
    status:
      - '[[ "{{.BUILD_COMMAND}}" == "null" ]] || [[ "{{.BUILD_OUTPUT}}" == "null" ]]'

  build:bin:
    deps:
      - :install:software:go
    log:
      error: Failed to build binary
      start: Running build command specified in `package.json` (defined under the `build_command` key in the `blueprint` section)
      success: Successfully built the binary
    cmds:
      - go mod tidy
      - '{{.BUILD_COMMAND}}'
    sources:
      - '**/*.go'
    generates:
      - '{{.BUILD_OUTPUT}}'

  help:
    deps:
      - build
      - :install:software:jq
    vars:
      BUILD_COMMAND:
        sh: jq -r '.blueprint.build_command' package.json
      BUILD_OUTPUT:
        sh: jq -r '.blueprint.build_command_output' package.json
    log:
      error: Failed to query binary help menu output
      start: Querying binary help menu output
      success: Successfully injected binary help menu output into `.variables.json`
    cmds:
      - |
        TMP_HELP="$(mktemp)"
        {{.BUILD_OUTPUT}} --help 2> "$TMP_HELP"
        TMP_VARS="$(mktemp)"
        jq --arg output "$(cat "$TMP_HELP")" '.help_menu_output = $output' .variables.json > "$TMP_VARS"
        mv "$TMP_VARS" .variables.json
    status:
      - '[[ "{{.BUILD_COMMAND}}" == "null" ]] || [[ "{{.BUILD_OUTPUT}}" == "null" ]]'

  prepare:
    cmds:
      - task: :go:goreleaser:build

  publish:
    cmds:
      - task: :go:goreleaser:release
      - task: publish:after
      - rm -rf dist

  publish:after:
    deps:
      - publish:after:docker
      - publish:after:fury

  publish:after:docker:
    summary: |
      Unsure why this is here and how it is useful.
    cmds:
      - .config/log warn 'Unused step go:publish:after:docker - pay attention to this only for debugging purposes'
    # cmds:
    #   - |
    #     for FILE in build/goreleaserdocker*; do
    #       [ -e "$FILE" ] && task --dir "$FILE" docker:verify docker:build docker:publish
    #       break
    #     done
    #   - rm -rf build/goreleaserdocker*

  publish:after:fury:
    deps:
      - :install:software:fury
    cmds:
      - fury push build/*.deb --account=$FURY_ACCOUNT --api-token=$FURY_DEPLOY_TOKEN
      - fury push build/*.rpm --account=$FURY_ACCOUNT --api-token=$FURY_DEPLOY_TOKEN
    status:
      - '[ -z "$FURY_ACCOUNT" ] || [ -z "$FURY_DEPLOY_TOKEN" ]'

  test:
    deps:
      - :install:software:go
    log:
      error: Failed to test Go sources
      start: Testing Go sources
      success: Successfully finished testing Go sources
    cmds:
      - go test ./...

  verify:
    cmds:
      - task: :go:goreleaser:check
      - task: :publish:snap:register
