name: Build

on:
    push:
        branches:
            - master
        tags:
            - v*
    pull_request:
        branches:
            - master
concurrency:
    group: ${{ github.ref }}
    cancel-in-progress: true
jobs:
    build:
        name: Build Binary
        strategy:
            matrix:
                os:
                    - macos-latest
                    - macos-13
                    - ubuntu-latest
                    - windows-latest
                include:
                    - os: macos-latest
                      target: aarch64-apple-darwin
                      main_target: aarch64-apple-darwin
                      artifact_name: neocmakelsp
                    - os: macos-13
                      target: x86_64-apple-darwin
                      main_target: x86_64-apple-darwin
                      artifact_name: neocmakelsp
                    - os: ubuntu-latest
                      target: x86_64-unknown-linux-gnu x86_64-unknown-linux-musl
                      main_target: x86_64-unknown-linux-gnu
                      artifact_name: neocmakelsp
                    - os: windows-latest
                      target: x86_64-pc-windows-msvc
                      main_target: x86_64-pc-windows-msvc
                      artifact_name: neocmakelsp.exe

        runs-on: ${{ matrix.os }}
        steps:
            - uses: actions/checkout@v4
            - uses: dtolnay/rust-toolchain@stable
              with:
                  targets: ${{ matrix.target }}
            - name: Install gcc-musl
              if: ${{ matrix.os  == 'ubuntu-latest' }}
              run: sudo apt install -y musl-tools
            - name: Build
              run: cargo build --verbose --release
            - name: Build musl
              if: ${{ matrix.os == 'ubuntu-latest' }}
              run: cargo build --target x86_64-unknown-linux-musl --verbose --release
            - name: Run tests
              run: cargo test --verbose
            - name: Run tests for musl
              if: ${{ matrix.os == 'ubuntu-latest' }}
              run: cargo test --target x86_64-unknown-linux-musl --verbose
            - name: Upload artifacts
              uses: actions/upload-artifact@v4
              with:
                  path: target/release/${{ matrix.artifact_name }}
                  name: ${{ matrix.main_target }}
            - name: Upload artifacts musl
              if: ${{ matrix.os == 'ubuntu-latest' }}
              uses: actions/upload-artifact@v4
              with:
                  path: target/x86_64-unknown-linux-musl/release/${{ matrix.artifact_name }}
                  name: "x86_64-unknown-linux-musl"
    release:
        permissions:
            contents: write
        if: startsWith(github.ref, 'refs/tags/v')
        needs:
            - build
        runs-on: ubuntu-latest
        steps:
            - uses: actions/download-artifact@v4
            - name: Show files
              run: |
                  pwd
                  find
            - name: Copy files
              run: |
                  mkdir out
                  mv aarch64-apple-darwin/neocmakelsp out/neocmakelsp-aarch64-apple-darwin
                  mv x86_64-apple-darwin/neocmakelsp out/neocmakelsp-x86_64-apple-darwin
                  mv x86_64-pc-windows-msvc/neocmakelsp.exe out/neocmakelsp-x86_64-pc-windows-msvc.exe
                  mv x86_64-unknown-linux-gnu/neocmakelsp out/neocmakelsp-x86_64-unknown-linux-gnu
                  mv x86_64-unknown-linux-musl/neocmakelsp out/neocmakelsp-x86_64-unknown-linux-musl
                  cd out
                  sha256sum * > sha256sum
            - name: Release
              uses: softprops/action-gh-release@v2
              with:
                  files: out/*
                  draft: true
