name: Release

on:
  push:
    tags:
      - 'v*'

env:
  CARGO_TERM_COLOR: always

jobs:
  create-release:
    name: Create Release
    runs-on: ubuntu-latest
    outputs:
      upload_url: ${{ steps.create_release.outputs.upload_url }}
      release_version: ${{ env.RELEASE_VERSION }}
    steps:
    - name: Get the release version from the tag
      shell: bash
      if: env.RELEASE_VERSION == ''
      run: |
        echo "RELEASE_VERSION=${GITHUB_REF#refs/tags/}" >> $GITHUB_ENV
        echo "version is: ${{ env.RELEASE_VERSION }}"
    - name: Create GitHub release
      id: create_release
      uses: actions/create-release@v1
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
        tag_name: ${{ env.RELEASE_VERSION }}
        release_name: ${{ env.RELEASE_VERSION }}
        draft: false
        prerelease: false

  build-release:
    name: Build Release
    needs: create-release
    strategy:
      fail-fast: false
      matrix:
        build: [linux, macos, win-msvc]
        include:
        - build: linux
          os: ubuntu-latest
          rust: stable
          target: x86_64-unknown-linux-gnu
          archive-name: omni-dev-linux.tar.gz
        - build: macos
          os: macos-latest
          rust: stable
          target: x86_64-apple-darwin
          archive-name: omni-dev-macos.tar.gz
        - build: win-msvc
          os: windows-latest
          rust: stable
          target: x86_64-pc-windows-msvc
          archive-name: omni-dev-windows.zip
    runs-on: ${{ matrix.os }}
    steps:
    - name: Checkout repository
      uses: actions/checkout@v4

    - name: Install Rust
      uses: dtolnay/rust-toolchain@master
      with:
        toolchain: ${{ matrix.rust }}
        target: ${{ matrix.target }}

    - name: Build release binary
      run: cargo build --verbose --release --target ${{ matrix.target }}

    - name: Strip release binary (linux and macos)
      if: matrix.build == 'linux' || matrix.build == 'macos'
      run: strip "target/${{ matrix.target }}/release/omni-dev"

    - name: Build archive
      shell: bash
      run: |
        mkdir archive
        cp LICENSE README.md archive/
        if [ "${{ matrix.build }}" = "win-msvc" ]; then
          cp "target/${{ matrix.target }}/release/omni-dev.exe" archive/
          cd archive
          7z a "${{ matrix.archive-name }}" *
        else
          cp "target/${{ matrix.target }}/release/omni-dev" archive/
          cd archive
          tar czf "${{ matrix.archive-name }}" *
        fi

    - name: Upload release archive
      uses: actions/upload-release-asset@v1.0.1
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
        upload_url: ${{ needs.create-release.outputs.upload_url }}
        asset_path: archive/${{ matrix.archive-name }}
        asset_name: ${{ matrix.archive-name }}
        asset_content_type: application/octet-stream

  publish-crates:
    name: Publish to crates.io
    runs-on: ubuntu-latest
    needs: create-release
    steps:
    - uses: actions/checkout@v4
    - uses: dtolnay/rust-toolchain@stable
    - name: Publish
      run: cargo publish --token ${{ secrets.CARGO_REGISTRY_TOKEN }}