{% render "scripts/helper/templates/copyright-liquid.yml" %}

{% if platforms == "all" -%}
  {% assign platforms = "linux-x64,linux-arm64,linux-arm,darwin-x64,darwin-arm64,win32-x64" -%}
{% endif -%}
{% assign platforms = platforms | split: "," -%}

name: 'Build xPack {{ APP_NAME }} xbbma'

on:
  workflow_dispatch:
    inputs:
      version:
        description: 'The semver of the release'
        required: false
        default: 'current'

jobs:
{% for platform in platforms -%}
{% if platform == "darwin-arm64" %}
  macos-arm64:
    name: 'macOS Arm - {{ APP_NAME }} {% raw %}${{ github.event.inputs.version }}{% endraw %} build'
    timeout-minutes: 2880 # 2 days
{% if MACOS_ARM_VERSION %}
    runs-on: [self-hosted, macos, apple, {{ MACOS_ARM_VERSION }}]
{% else %}
    runs-on: [self-hosted, macos, apple]
{% endif %}
    defaults:
      run:
        shell: "/usr/bin/arch -arch arm64e /bin/bash -l {0}"

    steps:
      - name: 'Machine'
        run: |
          uname -a
          node --version
          npm --version

      - name: 'Checkout'
        uses: actions/checkout@v2
        with:
          fetch-depth: 3
          submodules: true

      - name: 'Clean working area'
        run: rm -rf ~/Work/{{ APP_LC_NAME }}-{% raw %}${{ github.event.inputs.version }}{% endraw %}*
{% for platform in platforms -%}
  {% case platform -%}
    {% when "darwin-arm64" %}
      - name: 'Build macOS arm64 binary'
        timeout-minutes: 1440
        run: bash scripts/helper/build.sh --version "{% raw %}${{ github.event.inputs.version }}{% endraw %}" --macos
  {% endcase -%}
{% endfor %}
      - name: 'Publish pre-release'
        # https://github.com/ncipollo/release-action
        uses: ncipollo/release-action@v1
        with:
          allowUpdates: true
          artifacts: '~/Work/{{ APP_LC_NAME }}-{% raw %}${{ github.event.inputs.version }}{% endraw %}/deploy/*'
          bodyFile: '.github/workflows/body-github-pre-releases-test.md'
          commit: 'master'
          draft: false
          name: 'Test binaries'
          omitBodyDuringUpdate: true
          omitDraftDuringUpdate: true
          omitNameDuringUpdate: true
          owner: 'xpack-dev-tools'
          prerelease: true
          replacesArtifacts: true
          repo: 'pre-releases'
          tag: 'test'
          token: {% raw %}${{ secrets.PUBLISH_TOKEN }}{% endraw %}
{% break -%}
{% endif -%}
{% endfor %}
