code-smore 0.1.34

A morse code practice tool
name: Release

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

jobs:
  build:
    name: Build - ${{ matrix.platform.release_for }}
    runs-on: ${{ matrix.platform.os }}
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Install libasound2-dev (Linux only)
        if: matrix.platform.os == 'ubuntu-24.04'
        run: |
          sudo apt-get update
          sudo apt-get install -y libasound2-dev  libpipewire-0.3-dev pipewire-bin libclang-dev build-essential pkg-config libudev-dev

      - name: Update Rust stable
        run: rustup toolchain install stable

      # Preprocess Cargo.lock to ignore version fields for cache id hashing purposes:
      - name: Prepare Cargo.lock for caching (ignore code-smore version)
        run: |
          PROJECT_NAME=$(grep '^name' Cargo.toml | sed 's/name = "\(.*\)"/\1/')
          awk '/\[\[package\]\]/{p=0} /name = "'"${PROJECT_NAME}"'"/{p=1} p && /version = /{next} 1' Cargo.lock > Cargo.lock.no-version
      # Cache cargo registry (source code artifacts only):

      - name: Cache cargo registry
        uses: actions/cache@v3
        with:
          path: ~/.cargo/registry
          key: ${{ runner.os }}-cargo-registry-${{ hashFiles('**/Cargo.lock.no-version') }}
          restore-keys: |
            ${{ runner.os }}-cargo-registry-

      - name: Get version from cargo.toml
        uses: SebRollen/toml-action@v1.2.0
        id: cargo_version
        with:
          file: 'Cargo.toml'
          field: 'package.version'

      - name: Get the tag of current release
        id: get_version
        run: echo "VERSION=${GITHUB_REF#refs/tags/v}" >> ${GITHUB_OUTPUT}

      - name: Verify package version is equal to tag version
        if: ${{ steps.cargo_version.outputs.value != steps.get_version.outputs.VERSION }}
        run: |
          echo "${{ steps.cargo_version.outputs.value }}"
          echo "${{ steps.get_version.outputs.VERSION }}"
          exit 1

      # - name: Generate licenses.txt
      #   run: |
      #     curl -L --proto '=https' --tlsv1.2 -sSf https://raw.githubusercontent.com/cargo-bins/cargo-binstall/main/install-from-binstall-release.sh | bash
      #     cargo binstall --locked cargo-about
      #     source funcs.sh && build_license

      - name: Build binary
        uses: houseabsolute/actions-rust-cross@v0
        with:
          # cross-version: v0.2.5
          command: ${{ matrix.platform.command }}
          target: ${{ matrix.platform.target }}
          args: "--bin ${{ matrix.platform.bin }} --locked --release ${{ matrix.platform.features }}"
          strip: true

      - name: Move binary path
        run: |
          if [[ "${{ matrix.platform.target }}" == *"-windows-"* ]]; then
            mv target/${{ matrix.platform.target }}/release/${{ matrix.platform.bin }}.exe .
          else
            mv target/${{ matrix.platform.target }}/release/${{ matrix.platform.bin }} .
          fi
          
      - name: Package as archive
        run: |
          if [[ "${{ matrix.platform.target }}" == *"-windows-"* ]]; then
             zip ${{ matrix.platform.bin }}-v${{ steps.cargo_version.outputs.value }}-${{ matrix.platform.name }}.zip \
                ${{ matrix.platform.bin }}.exe
          else
              tar czvf ${{ matrix.platform.bin }}-v${{ steps.cargo_version.outputs.value }}-${{ matrix.platform.name }}.tar.gz \
                ${{ matrix.platform.bin }}
          fi

      # - name: Generate SHA-256
      #   run: shasum -a 256 ${{ matrix.platform.bin }}-v${{ steps.cargo_version.outputs.value }}-${{ matrix.platform.name }}.tar.gz > ${{ matrix.platform.bin }}-v${{ steps.cargo_version.outputs.value }}-${{ matrix.platform.name }}.tar.gz.sha256

      - name: Upload Linux artifacts
        if: matrix.platform.target == 'x86_64-unknown-linux-gnu'
        uses: actions/upload-artifact@v4
        with:
          name: ${{ matrix.platform.bin }}-v${{ steps.cargo_version.outputs.value }}-${{ matrix.platform.name }}.tar.gz
          path: ${{ matrix.platform.bin }}-v${{ steps.cargo_version.outputs.value }}-${{ matrix.platform.name }}.tar.gz

      - name: Upload Windows artifacts
        if: matrix.platform.target == 'x86_64-pc-windows-gnu'
        uses: actions/upload-artifact@v4
        with:
          name: ${{ matrix.platform.bin }}-v${{ steps.cargo_version.outputs.value }}-${{ matrix.platform.name }}.zip
          path: ${{ matrix.platform.bin }}-v${{ steps.cargo_version.outputs.value }}-${{ matrix.platform.name }}.zip

      # - name: Upload digests
      #   uses: actions/upload-artifact@v4
      #   with:
      #     name: ${{ matrix.platform.bin }}-v${{ steps.cargo_version.outputs.value }}-${{ matrix.platform.name }}.tar.gz.sha256
      #     path: ${{ matrix.platform.bin }}-v${{ steps.cargo_version.outputs.value }}-${{ matrix.platform.name }}.tar.gz.sha256

    strategy:
      matrix:
        platform:
          - release_for: Linux-x86_64
            os: ubuntu-24.04
            target: x86_64-unknown-linux-gnu
            bin: code-smore
            name: Linux-x86_64
            command: build
            features: ""
          # - release_for: Linux-aarch64
          #   os: ubuntu-24.04
          #   target: aarch64-unknown-linux-gnu
          #   bin: code-smore
          #   name: Linux-aarch64
          #   command: build
          # - release_for: macOS-x86_64
          #   os: macOS-latest
          #   target: x86_64-apple-darwin
          #   bin: code-smore
          #   name: Darwin-x86_64
          #   command: build
          # - release_for: macOS-arm64
          #   os: macOS-latest
          #   target: aarch64-apple-darwin
          #   bin: code-smore
          #   name: Darwin-arm64
          #   command: build
          - release_for: Windows-x86_64
            os: ubuntu-24.04
            target: x86_64-pc-windows-gnu
            bin: code-smore
            name: Windows-x86_64
            command: build
            features: "--no-default-features"
          # - release_for: FreeBSD-x86_64
          #   os: ubuntu-24.04
          #   target: x86_64-unknown-freebsd
          #   bin: code-smore
          #   name: FreeBSD-x86_64
          #   command: build

  release:
    name: Release - ${{ matrix.platform.release_for }}
    needs: build
    runs-on: ubuntu-24.04
    steps:

    - uses: actions/download-artifact@v4

    # Release to github:
    - name: Release
      uses: softprops/action-gh-release@v2
      if: startsWith(github.ref, 'refs/tags/v')
      with:
        files: |
          code-smore-v*.*/code-smore-v*.*

  publish:
    name: Publish crate
    needs: release
    runs-on: ubuntu-24.04
    steps:
    - name: Checkout
      uses: actions/checkout@v4
    - name: Update Rust stable
      run: rustup toolchain install stable
    # Preprocess Cargo.lock to ignore version fields for cache id hashing purposes:
    - name: Prepare Cargo.lock for caching (ignore code-smore version)
      run: |
        PROJECT_NAME=$(grep '^name' Cargo.toml | sed 's/name = "\(.*\)"/\1/')
        awk '/\[\[package\]\]/{p=0} /name = "'"${PROJECT_NAME}"'"/{p=1} p && /version = /{next} 1' Cargo.lock > /tmp/Cargo.lock.no-version

    # Cache cargo registry (source code artifacts only):
    - name: Cache cargo registry
      uses: actions/cache@v3
      with:
        path: ~/.cargo/registry
        key: ${{ runner.os }}-cargo-registry-${{ hashFiles('/tmp/Cargo.lock.no-version') }}
        restore-keys: |
          ${{ runner.os }}-cargo-registry-

    - name: Install deps
      run: |
        sudo apt-get update
        sudo apt-get install -y libasound2-dev  libpipewire-0.3-dev pipewire-bin libclang-dev build-essential pkg-config libudev-dev
          
    # # Verify if the crate is ready to be published:
    # - name: Check crate is ready for publishing
    #   run: cargo package
      
    # Publish the crate to crates.io
    - name: Publish to crates.io
      env:
        CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
      run: cargo publish --token $CARGO_REGISTRY_TOKEN