---
version: '3'

tasks:
  build:
    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:
      - '{{.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" ]]'
