name: CD

on:
  push:
    tags:
      - "v*.*.*"

jobs:
  build-for-linux-x64:
    name: Build and upload for Linux x86_64
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Set the release version
        run: echo "RELEASE_VERSION=${GITHUB_REF:11}" >> $GITHUB_ENV

      - name: Install `musl` libc
        run: sudo apt-get install -y musl

      - name: Setup Rust toolchain
        uses: dtolnay/rust-toolchain@stable
        with:
          toolchain: stable
          targets: x86_64-unknown-linux-musl

      - name: Build binary
        run: RUSTFLAGS='-C linker=ld -C relocation-model=static -C strip=symbols' cargo build --release --target x86_64-unknown-linux-musl

      - name: Build Changelog
        id: github_release
        uses: mikepenz/release-changelog-builder-action@v5
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Upload binaries to release
        uses: svenstaro/upload-release-action@v2
        with:
          repo_token: ${{ secrets.GITHUB_TOKEN }}
          file: target/x86_64-unknown-linux-musl/release/srt-linter
          asset_name: srt-linter.linux.x86_64.musl
          tag: ${{ github.ref }}
          release_name: "Release v${{ env.RELEASE_VERSION }}"
          body: ${{steps.github_release.outputs.changelog}}

  build-for-windows-x64:
    name: Build and upload for Windows x86_64
    runs-on: windows-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Set the release version
        run: echo "RELEASE_VERSION=${GITHUB_REF:11}" >> $GITHUB_ENV

      - name: Setup Rust toolchain
        uses: dtolnay/rust-toolchain@stable
        with:
          toolchain: stable
          targets: x86_64-pc-windows-msvc

      - name: Build binary
        run: cargo build --release --target x86_64-pc-windows-msvc

      - name: Build Changelog
        id: github_release
        uses: mikepenz/release-changelog-builder-action@v5
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Upload binaries to release
        uses: svenstaro/upload-release-action@v2
        with:
          repo_token: ${{ secrets.GITHUB_TOKEN }}
          file: target\x86_64-pc-windows-msvc\release\srt-linter.exe
          asset_name: srt-linter.windows.x86_64.msvc
          tag: ${{ github.ref }}
          release_name: "Release v${{ env.RELEASE_VERSION }}"
          body: ${{steps.github_release.outputs.changelog}}

  build-for-macos-x64:
    name: Build and upload for MacOS Aarch64
    runs-on: macos-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Set the release version
        run: echo "RELEASE_VERSION=${GITHUB_REF:11}" >> $GITHUB_ENV

      - name: Setup Rust toolchain
        uses: dtolnay/rust-toolchain@stable
        with:
          toolchain: stable
          targets: aarch64-apple-darwin

      - name: Build binary
        run: cargo build --release --target aarch64-apple-darwin

      - name: Build Changelog
        id: github_release
        uses: mikepenz/release-changelog-builder-action@v5
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Upload binaries to release
        uses: svenstaro/upload-release-action@v2
        with:
          repo_token: ${{ secrets.GITHUB_TOKEN }}
          file: target/aarch64-apple-darwin/release/srt-linter
          asset_name: srt-linter.darwin.aarch64
          tag: ${{ github.ref }}
          release_name: "Release v${{ env.RELEASE_VERSION }}"
          body: ${{steps.github_release.outputs.changelog}}

  publish-on-crates-io:
    name: Publish on crates.io
    needs: [build-for-macos-x64, build-for-windows-x64, build-for-linux-x64]
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable

      - name: Publish
        run: cargo publish --locked --token ${{ secrets.CARGO_REGISTRY_TOKEN }}