# build-linux.yml
# Reusable workflow that builds the Linux and MacOS (x64 and arm64) versions of Libation.
---
name: build

on:
  workflow_call:
    inputs:
      version_override:
        type: string
        description: 'Version number override'
        required: false
      run_unit_tests:
        type: boolean
        description: 'Skip running unit tests'
        required: false
        default: true

env:
  DOTNET_CONFIGURATION: 'Release'
  DOTNET_VERSION: '7.0.x'
  RELEASE_NAME: 'chardonnay'

jobs:
  build:
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest, macos-latest]
        arch: [x64, arm64]
    steps:
      - uses: actions/checkout@v3
      - name: Setup .NET
        uses: actions/setup-dotnet@v3
        with:
          dotnet-version: ${{ env.DOTNET_VERSION }}
        env:
          NUGET_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Get version
        id: get_version
        run: |
          inputVersion="${{ inputs.version_override }}"
          if [[ "${#inputVersion}" -gt 0 ]]
          then
            version="${inputVersion}"
          else
            version="$(grep -Eio -m 1 '<Version>.*</Version>' ./Source/AppScaffolding/AppScaffolding.csproj | sed -r 's/<\/?Version>//g')"
          fi
          echo "version=${version}" >> "${GITHUB_OUTPUT}"
      - name: Unit test
        if: ${{ inputs.run_unit_tests }}
        working-directory: ./Source
        run: dotnet test

      - name: Publish
        id: publish
        working-directory: ./Source
        run: |
          os=${{ matrix.os }}
          target_os="$(echo ${os/-latest/} | sed 's/ubuntu/linux/')"
          display_os="$(echo ${target_os/macos/macOS} | sed 's/linux/Linux/')"
          echo "display_os=${display_os}" >> $GITHUB_OUTPUT
          RUNTIME_IDENTIFIER="$(echo ${target_os/macos/osx})-${{ matrix.arch }}"
          echo "$RUNTIME_IDENTIFIER"
          dotnet publish \
              LibationAvalonia/LibationAvalonia.csproj \
              --runtime "$RUNTIME_IDENTIFIER" \
              --configuration ${{ env.DOTNET_CONFIGURATION }} \
              --output bin/Publish/${display_os}-${{ matrix.arch }}-${{ env.RELEASE_NAME }} \
              -p:PublishProfile=LibationAvalonia/Properties/PublishProfiles/${display_os}Profile.pubxml
          dotnet publish \
              LoadByOS/${display_os}ConfigApp/${display_os}ConfigApp.csproj \
              --runtime "$RUNTIME_IDENTIFIER" \
              --configuration ${{ env.DOTNET_CONFIGURATION }} \
              --output bin/Publish/${display_os}-${{ matrix.arch }}-${{ env.RELEASE_NAME }} \
              -p:PublishProfile=LoadByOS/Properties/${display_os}ConfigApp/PublishProfiles/${display_os}Profile.pubxml
          dotnet publish \
              LibationCli/LibationCli.csproj \
              --runtime "$RUNTIME_IDENTIFIER" \
              --configuration ${{ env.DOTNET_CONFIGURATION }} \
              --output bin/Publish/${display_os}-${{ matrix.arch }}-${{ env.RELEASE_NAME }} \
              -p:PublishProfile=LibationCli/Properties/PublishProfiles/${display_os}Profile.pubxml
          dotnet publish \
              HangoverAvalonia/HangoverAvalonia.csproj \
              --runtime "$RUNTIME_IDENTIFIER" \
              --configuration ${{ env.DOTNET_CONFIGURATION }} \
              --output bin/Publish/${display_os}-${{ matrix.arch }}-${{ env.RELEASE_NAME }} \
              -p:PublishProfile=HangoverAvalonia/Properties/PublishProfiles/${display_os}Profile.pubxml
      - name: Build bundle
        id: bundle
        working-directory: ./Source/bin/Publish/${{ steps.publish.outputs.display_os }}-${{ matrix.arch }}-${{ env.RELEASE_NAME }}
        run: |
          BUNDLE_DIR=$(pwd)
          echo "Bundle dir: ${BUNDLE_DIR}"
          cd ..
          SCRIPT=../../../Scripts/Bundle_${{ steps.publish.outputs.display_os }}.sh
          chmod +rx ${SCRIPT}
          ${SCRIPT} "${BUNDLE_DIR}" "${{ steps.get_version.outputs.version }}" "${{ matrix.arch }}"
          artifact=$(ls ./bundle)
          echo "artifact=${artifact}" >> "${GITHUB_OUTPUT}"
      - name: Publish bundle
        uses: actions/upload-artifact@v3
        with:
          name: ${{ steps.bundle.outputs.artifact }}
          path: ./Source/bin/Publish/bundle/${{ steps.bundle.outputs.artifact }}
          if-no-files-found: error