on:
  push:
    tags:
      - "v*" # Run when tag matches v*, i.e. v1.0, v20.15.10

name: Release

env:
  RELEASE_BIN: editorconfiger
  RELEASE_DIR: artifacts
  GITHUB_REF: "${{ github.ref }}"
  RELEASER_VER: 0.16.0

jobs:
  build:
    name: Build artifacts
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        include:
          - build: linux-x64
            os: ubuntu-latest
            rust: stable
            target: x86_64-unknown-linux-musl
          - build: linux-aarch64
            os: ubuntu-latest
            rust: stable
            target: aarch64-unknown-linux-musl
          - build: macos-x64
            os: macos-latest
            rust: stable
            target: x86_64-apple-darwin
          - build: macos-arm64
            os: macos-latest
            rust: stable
            target: aarch64-apple-darwin
          - build: windows
            os: windows-latest
            rust: stable
            target: x86_64-pc-windows-msvc

    steps:
      - uses: actions/checkout@v6

      - name: Query version number
        id: get_version
        shell: bash
        run: |
          echo "using version tag ${GITHUB_REF:11}"
          echo "version=${GITHUB_REF:11}" >> $GITHUB_OUTPUT

      - name: Build
        uses: houseabsolute/actions-rust-cross@v1
        with:
          command: "build"
          target: ${{ matrix.target }}
          toolchain: ${{ matrix.rust }}
          args: '--release --bin editorconfiger --features="build-binary"'
          strip: true

      - name: Create artifact directory
        run: |
          mkdir ${{ env.RELEASE_DIR }}
          mkdir dist

      - name: Create tarball (Linux)
        if: matrix.build == 'linux-x64' || matrix.build == 'linux-aarch64'
        run: |
          cp ./target/${{ matrix.target }}/release/${{ env.RELEASE_BIN }} ./dist/${{ env.RELEASE_BIN }}
          cp ./LICENSE.txt ./dist/
          7z a -ttar -so -an ./dist/* | 7z a -si ./${{ env.RELEASE_DIR }}/${{ env.RELEASE_BIN }}-${{ steps.get_version.outputs.VERSION }}-${{ matrix.target }}.tar.gz

      - name: Build Debian release
        if: matrix.build == 'linux-x64' || matrix.build == 'linux-aarch64'
        run: |
          cargo install cargo-deb
          [[ -d ./target/release/ ]] || mkdir ./target/release/
          [[ -f ./target/release/${{ env.RELEASE_BIN }} ]] || cp ./target/${{ matrix.target }}/release/${{ env.RELEASE_BIN }} ./target/release/
          cp ./target/${{ matrix.target }}/release/${{ env.RELEASE_BIN }} ./target/release/
          cargo deb --no-strip --no-build --target ${{ matrix.target }}
          cp ./target/${{ matrix.target }}/debian/${{ env.RELEASE_BIN }}_*.deb ./${{ env.RELEASE_DIR }}/

      - name: Build RPM release
        if: matrix.build == 'linux-x64' || matrix.build == 'linux-aarch64'
        run: |
          cargo install cargo-generate-rpm
          cargo generate-rpm --target ${{ matrix.target }}
          cp ./target/${{ matrix.target }}/generate-rpm/${{ env.RELEASE_BIN }}-*.rpm ./${{ env.RELEASE_DIR }}/

      - name: Create tarball (Windows)
        if: matrix.build == 'windows'
        shell: bash
        run: |
          mv ./target/${{ matrix.target }}/release/${{ env.RELEASE_BIN }}.exe ./dist/${{ env.RELEASE_BIN }}.exe
          7z a -ttar -so -an ./dist/* | 7z a -si ./${{ env.RELEASE_DIR }}/${{ env.RELEASE_BIN }}-${{ steps.get_version.outputs.VERSION }}-${{ matrix.target }}.tar.gz

      - name: Create tarball (MacOS)
        if: matrix.build == 'macos-x64' || matrix.build == 'macos-arm64'
        run: |
          mv ./target/${{ matrix.target }}/release/${{ env.RELEASE_BIN }} ./dist/${{ env.RELEASE_BIN }}
          7z a -ttar -so -an ./dist/* | 7z a -si ./${{ env.RELEASE_DIR }}/${{ env.RELEASE_BIN }}-${{ steps.get_version.outputs.VERSION }}-${{ matrix.target }}.tar.gz

      - name: Upload Zip
        uses: actions/upload-artifact@v7
        with:
          name: ${{ matrix.build }}
          path: ./${{ env.RELEASE_DIR }}

  generate-changelog:
    name: Generate changelog
    runs-on: ubuntu-latest
    outputs:
      release_body: ${{ steps.git-cliff.outputs.content }}
    steps:
      - name: Checkout
        uses: actions/checkout@v6
        with:
          fetch-depth: 0
      - name: Generate a changelog
        uses: orhun/git-cliff-action@main
        id: git-cliff
        with:
          config: cliff.toml
          args: --latest --strip header
        env:
          OUTPUT: CHANGES.md

  release:
    name: GitHub Release
    needs: [build, generate-changelog]
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - name: Query version number
        id: get_version
        shell: bash
        run: |
          echo "using version tag ${GITHUB_REF:11}"
          echo "version=${GITHUB_REF:11}" >> $GITHUB_OUTPUT

      - name: Download artefacts
        uses: actions/download-artifact@v8
        with:
          path: release

      - name: Download Releaser
        run: |
          curl -L  -o releaser.tar.gz https://github.com/aegoroff/releaser/releases/download/${{ env.RELEASER_VER }}/releaser-${{ env.RELEASER_VER }}-x86_64-unknown-linux-musl.tar.gz
          tar -xf releaser.tar.gz
      - name: Create brew formula
        run: ${{ github.workspace }}/releaser b -c . -b https://github.com/${{ github.repository }}/releases/download/${{ steps.get_version.outputs.VERSION }}/ -l './release/linux-x64/' -m './release/macos-x64/' -a './release/macos-arm64/' -u ${{ env.RELEASE_BIN }}.rb
      - name: Publish brew formula
        uses: dmnemec/copy_file_to_another_repo_action@main
        env:
          API_TOKEN_GITHUB: ${{ secrets.GH_TOKEN }}
        with:
          source_file: ${{ env.RELEASE_BIN }}.rb
          destination_repo: "aegoroff/homebrew-tap"
          user_email: "egoroff@email.com"
          user_name: "aegoroff"
          commit_message: "release: ${{ steps.get_version.outputs.VERSION }}"

      - name: Create scoop json
        run: ${{ github.workspace }}/releaser s -c . -b https://github.com/${{ github.repository }}/releases/download/${{ steps.get_version.outputs.VERSION }}/ -i './release/windows/' -e ${{ env.RELEASE_BIN }}.exe -u ${{ env.RELEASE_BIN }}.json
      - name: Publish scoop json
        uses: dmnemec/copy_file_to_another_repo_action@main
        env:
          API_TOKEN_GITHUB: ${{ secrets.GH_TOKEN }}
        with:
          source_file: ${{ env.RELEASE_BIN }}.json
          destination_repo: "aegoroff/scoop-bucket"
          user_email: "egoroff@email.com"
          user_name: "aegoroff"
          commit_message: "release: ${{ steps.get_version.outputs.VERSION }}"

      - name: Create Release
        uses: softprops/action-gh-release@v3
        with:
          tag_name: ${{ steps.get_version.outputs.VERSION }}
          body: "${{ needs.generate-changelog.outputs.release_body }}"
          files: |
            ./release/**/*.{tar.gz,rpm,deb}
      - name: Publish editorconfiger-bin to AUR
        uses: zokugun/github-actions-aur-releaser@v1
        with:
          package_name: editorconfiger-bin
          aur_private_key: ${{ secrets.AUR_PRIVATE_KEY }}
          aur_username: ${{ secrets.AUR_USERNAME }}
          aur_email: ${{ secrets.AUR_EMAIL }}
          package_version: ${{ steps.get_version.outputs.VERSION }}