# SPDX-FileCopyrightText: 2023 - 2024 Ali Sajid Imami
#
# SPDX-License-Identifier: Apache-2.0
# SPDX-License-Identifier: MIT

---
on:
    workflow_call:

name: Semantic Release
env:
    RUST_BACKTRACE: 1
    SEMREL_RUST_VERSION: 2.3.39
concurrency:
    group: ${{ github.workflow }}
jobs:
    build_application:
        name: Build CLI ${{ matrix.build.target }}
        runs-on: ${{ matrix.build.os }}
        env:
            CARGO: cargo
        strategy:
            matrix:
                bin:
                    - bfkview
                    - bfkrun
                build:
                    # alphabatized by target
                    - os: macOS-latest
                      target: aarch64-apple-darwin
                      cross: true
                    - os: ubuntu-latest
                      target: aarch64-unknown-linux-gnu
                      cross: true
                    - os: ubuntu-latest
                      target: aarch64-unknown-linux-musl
                      cross: true
                    - os: ubuntu-latest
                      target: i686-unknown-linux-gnu
                      cross: true
                    - os: ubuntu-latest
                      target: i686-unknown-linux-musl
                      cross: true
                    - os: macOS-latest
                      target: x86_64-apple-darwin
                      cross: false
                    - os: ubuntu-latest
                      target: x86_64-unknown-linux-gnu
                      cross: false
                    - os: ubuntu-latest
                      target: x86_64-unknown-linux-musl
                      cross: false
                    - os: ubuntu-latest
                      target: x86_64-pc-windows-gnu
                      cross: true
                    - os: ubuntu-latest
                      target: i686-pc-windows-gnu
                      cross: true
        steps:
            - name: Checkout
              uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
            - name: Install tree
              if: runner.os == 'Linux'
              run: sudo apt install tree
            - name: Install build inputs
              if: runner.os == 'Linux' && !matrix.build.cross
              run: sudo apt install musl-tools mingw-w64
            - name: Install Rust toolchain
              uses: dtolnay/rust-toolchain@master
              id: rust-toolchain
              with:
                toolchain: stable
                target: ${{ matrix.build.target }}
            - name: Cache dependencies
              uses: Swatinem/rust-cache@23bce251a8cd2ffc3c1075eaa2367cf899916d84 # v2.7.3
            - name: Install cross
              uses: taiki-e/install-action@37461a1de4134bec919a737ee9ba018e72011b7c # v2.42.4
              with:
                tool: cross
            - name: Configure cross
              if: matrix.build.cross
              run: echo "CARGO=cross" >> "$GITHUB_ENV"
            - name: Compile release binary
              run: ${{ env.CARGO }} build --bin ${{ matrix.bin }} --release --target ${{ matrix.build.target }} --verbose
            - name: Show generated binary
              if: runner.os == 'Linux'
              run: tree target/
            - name: Create release archive - windows
              if: matrix.build.target == 'x86_64-pc-windows-gnu' || matrix.build.target == 'i686-pc-windows-gnu'
              run: |
                mkdir dist
                cp target/${{ matrix.build.target }}/release/${{ matrix.bin }}.exe dist/${{ matrix.bin }}-${{ matrix.build.target }}
            - name: Create release archive - non-windows
              if: matrix.build.target != 'x86_64-pc-windows-gnu' && matrix.build.target != 'i686-pc-windows-gnu'
              run: |
                mkdir dist
                cp target/${{ matrix.build.target }}/release/${{ matrix.bin }} dist/${{ matrix.bin }}-${{ matrix.build.target }}
            - name: Upload release artifacts
              uses: actions/upload-artifact@0b2256b8c012f0828dc542b3febcab082c67f72b # v4.3.4
              with:
                name: ${{ matrix.bin }}-${{ matrix.build.target }}
                path: |
                    dist/${{ matrix.bin }}-${{ matrix.build.target }}
                if-no-files-found: error
                retention-days: 1
    release:
        name: Semantic Release
        runs-on: ubuntu-latest
        needs: build_application
        outputs:
            new_release_version: ${{steps.semantic.outputs.new_release_version}}
            new_release_published: ${{steps.semantic.outputs.new_release_published}}
            new_release_notes: ${{steps.semantic.outputs.new_release_notes}}
            new_release_channel: ${{steps.semantic.outputs.new_release_channel}}
        steps:
            - name: Checkout
              uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
              with:
                fetch-depth: 0
                persist-credentials: false
            - name: Import GPG key
              uses: crazy-max/ghaction-import-gpg@01dd5d3ca463c7f10f7f4f7b4f177225ac661ee4 # v6.1.0
              with:
                gpg_private_key: ${{ secrets.GPG_PRIVATE_KEY }}
                passphrase: ${{ secrets.GPG_KEY_PASSPHRASE }}
                fingerprint: ${{ secrets.GPG_SUBKEY_FINGERPRINT }}
                trust_level: 5
                git_user_signingkey: true
                git_commit_gpgsign: true
                git_tag_gpgsign: false
                git_committer_name: ${{ vars.GIT_AUTHOR_NAME }}
                git_committer_email: ${{ vars.GIT_AUTHOR_EMAIL }}
            - name: Test GPG Key Import
              run: gpg --list-keys --keyid-format LONG
            - name: Install dependencies
              run: sudo apt install tree
            - name: Install semantic-release-cargo
              uses: taiki-e/install-action@37461a1de4134bec919a737ee9ba018e72011b7c # v2.42.4
              with:
                tool: semantic-release-cargo@${{env.SEMREL_RUST_VERSION}}
            - name: Download release artifacts
              uses: actions/download-artifact@fa0a91b85d4f404e444e00e005971372dc801d16 # v4.1.8
              with:
                path: artifacts
            - name: ls artifacts
              run: tree ./artifacts
            - name: Prepare GitHub Release artifacts
              run: ./scripts/build.sh
            - name: Install Conventional Commit preset
              run: npm install conventional-changelog-conventionalcommits
            - name: Semantic Release
              uses: cycjimmy/semantic-release-action@cb425203a562475bca039ba4dbf90c7f9ac790f4 # v4.1.0
              id: semantic
              with:
                semantic_version: 24.0.0
                extra_plugins: |
                    @semantic-release/exec@6
                    @semantic-release/git@10
              env:
                GITHUB_TOKEN: ${{secrets.ACTIONS_PAT_GITHUB}}
                CARGO_REGISTRY_TOKEN: ${{secrets.SEMREL_CRATES_IO}}
                GIT_AUTHOR_NAME: ${{vars.GIT_AUTHOR_NAME}}
                GIT_AUTHOR_EMAIL: ${{vars.GIT_AUTHOR_EMAIL}}
                GIT_COMMITTER_NAME: ${{vars.GIT_AUTHOR_NAME}}
                GIT_COMMITTER_EMAIL: ${{vars.GIT_AUTHOR_EMAIL}}
