name: Publish
on: 
  push:
    branches:
      - bw
  pull_request:
    branches:
      - bw

jobs:
    publish:
        runs-on: ubuntu-latest
        steps:
        - uses: actions/checkout@v2
        - name: Install Rust
          uses: actions-rs/toolchain@v1
          with:
              toolchain: stable
        - name: Add embedded target
          run: |
            rustup target add thumbv6m-none-eabi
        - name: Install dependencies
          run: |
            sudo apt install libudev-dev && cargo install elf2uf2-rs && cargo install flip-link
        # Required or else cargo pkid in the next step will complain and the tag_name will be empty
        - name: Generate Cargo.lock file
          run: |
            cargo generate-lockfile
        - name: Get package version
          id: get_version
          run: |
            echo "version=$(cargo pkgid | cut -d# -f2 | cut -d: -f2 | cut -d@ -f2)" >> $GITHUB_OUTPUT
        - name: Cargo build example music
          uses: actions-rs/cargo@v1
          with:
            command: build
            args: --target thumbv6m-none-eabi --release --example music
        - name: Convert to UF2
          run: |
            elf2uf2-rs ./target/thumbv6m-none-eabi/release/examples/music
        - name: Create release
          id: create_release
          uses: actions/create-release@v1
          env:
              GITHUB_TOKEN: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
          with:
              tag_name: ${{ steps.get_version.outputs.version }}
              release_name: thumby-rs v${{ steps.get_version.outputs.version }}
              body: Example from library that plays Twinkle, Twinkle, Little Star on Thumby
              draft: false
              prerelease: false
        - name: Upload release asset
          uses: actions/upload-release-asset@v1
          with:
            upload_url: ${{ steps.create_release.outputs.upload_url }}
            asset_name: music.uf2
            asset_path: ./target/thumbv6m-none-eabi/release/examples/music.uf2
            asset_content_type: application/octet-stream
          env:
              GITHUB_TOKEN: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
        - name: Publish to crates.io
          uses: katyo/publish-crates@v1
          with:
            registry-token: ${{ secrets.CARGO_REGISTRY_TOKEN }}
          
          