---
name: "Tag Release Build"

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

jobs:
  # Check the rust formatting
  format:
    name: "Rust Format Check"
    runs-on: ubuntu-latest
    steps:
      - name: "Checkout"
        uses: actions/checkout@v2

      - name: "Rust Toolchain setup"
        uses: actions-rs/toolchain@v1
        with:
            toolchain: stable
            components: rustfmt
            override: true

      - name: "Format Check"
        uses: mbrobbel/rustfmt-check@master
        with:
          token: ${{ secrets.GITHUB_TOKEN }}

  # Build the Linux release artifacts
  tag-release-build-linux:
    name: "Tag Release Build - Linux"
    runs-on: "ubuntu-latest"
    needs: format
    strategy:
      matrix:
        RUST: [stable]
        TARGET:
          - x86_64-unknown-linux-gnu
          - armv7-unknown-linux-gnueabihf
          - aarch64-unknown-linux-gnu

    steps:
      - name: "Checkout"
        uses: actions/checkout@v2

      - name: "Toolchain setup"
        uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: ${{ matrix.RUST }}
          target: ${{ matrix.TARGET }}
          override: true

      - name: "Test"
        uses: actions-rs/cargo@v1
        with:
          use-cross: true
          command: test
          args: --target=${{ matrix.TARGET }}

      - name: "Build release"
        uses: actions-rs/cargo@v1
        with:
          use-cross: true
          command: build
          args: --release --target=${{ matrix.TARGET }}

      - name: "Package Release"
        run: tar cvzf bcd-${{ matrix.TARGET }}.tar.gz --directory=target/${{ matrix.TARGET }}/release bookmark-cd

      - name: "Generate SHA checksum"
        run: shasum -a 256 -U bcd-${{ matrix.TARGET }}.tar.gz > bcd-${{ matrix.TARGET }}.sha

      - name: "Generate MD5 checksum"
        run: md5sum --tag bcd-${{ matrix.TARGET }}.tar.gz > bcd-${{ matrix.TARGET }}.md5

      - name: "Upload the artifact with the workflow run"
        uses: actions/upload-artifact@v3
        with:
          name: bcd-${{ matrix.TARGET }}-${{ github.ref_name }}
          path: bcd-${{ matrix.TARGET }}.*
          retention-days: 1

  # Build the MacOS release artifacts
  tag-release-build-macos:
    name: "Tag Release Build - MacOS"
    runs-on: "macos-latest"
    needs: format
    strategy:
      matrix:
        RUST: [stable]
        TARGET:
          - x86_64-apple-darwin
          - aarch64-apple-darwin

    steps:
      - name: "Checkout"
        uses: actions/checkout@v2

      - name: "Toolchain setup"
        uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: ${{ matrix.RUST }}
          target: ${{ matrix.TARGET }}
          override: true

      - name: "Test"
        uses: actions-rs/cargo@v1
        with:
          command: test

      - name: "Build release"
        uses: actions-rs/cargo@v1
        with:
          use-cross: true
          command: build
          args: --release --target=${{ matrix.TARGET }}

      - name: "Package Release"
        run: tar cvzf bcd-${{ matrix.TARGET }}.tar.gz --directory=target/${{ matrix.TARGET }}/release bookmark-cd

      - name: "Generate SHA checksum"
        run: shasum -a 256 -U bcd-${{ matrix.TARGET }}.tar.gz > bcd-${{ matrix.TARGET }}.sha

      - name: "Generate MD5 checksum"
        run: md5 bcd-${{ matrix.TARGET }}.tar.gz > bcd-${{ matrix.TARGET }}.md5

      - name: "Upload the artifact with the workflow run"
        uses: actions/upload-artifact@v3
        with:
          name: bcd-${{ matrix.TARGET }}-${{ github.ref_name }}
          path: bcd-${{ matrix.TARGET }}.*
          retention-days: 1

  # Publishing to crates.io - this will be skipped if the tag as a '-' in it for testing purposes
  tag-release-publish:
    name: "Tag Release Publish"
    runs-on: "ubuntu-latest"
    needs: [tag-release-build-linux, tag-release-build-macos]
    if: github.ref_type == 'tag' && ( contains(github.ref_name, '-') == false ) 

    steps:
      - name: "Checkout"
        uses: actions/checkout@v2

      - name: "Publish Application to crates.io"
        env:
          CARGO_REGISTRY_TOKEN: "${{ secrets.CRATES_TOKEN }}"
        uses: actions-rs/cargo@v1
        with:
          command: publish

  # Package the artifacts as a release package
  tag-release-package:
    name: "Tag Release Package"
    runs-on: "ubuntu-latest"
    needs: [tag-release-build-linux, tag-release-build-macos]

    steps:
      - name: "Checkout"
        uses: actions/checkout@v2
        
      - name: "Download all release artifacts from the workflow"
        uses: actions/download-artifact@v3

      - name: "Create CLI Release"
        uses: "marvinpinto/action-automatic-releases@latest"
        with:
          automatic_release_tag: "${{ github.ref_name }}"
          title: "Release ${{ github.ref_name }}"
          repo_token: "${{ secrets.GITHUB_TOKEN }}"
          prerelease: false
          files: |
            LICENSE
            **/bcd-*.*

  # Kick off the workflow in the tap repo (a1ecbr0wn/homebrew-bcd) that will generate the homebrew formula 
  generate-formula:
    name: "Initiate the generation of homebrew formula"
    runs-on: ubuntu-latest
    needs: [tag-release-build-linux, tag-release-build-macos]
    steps:
      - name: "Repository Dispatch"
        uses: benc-uk/workflow-dispatch@v1
        with:
          token: ${{ secrets.PAT }}
          repo: a1ecbr0wn/homebrew-bcd
          ref: refs/heads/main
          workflow: tap.yml
          inputs: '{ "tap_version": "${{ github.ref_name }}" }'

  # Kick off the workflow in the snap repo (a1ecbr0wn/snapcraft-bcd) that will generate the snapcraft yaml file 
  generate-snap:
    name: "Initiate the generation of the snapcraft yaml file"
    runs-on: ubuntu-latest
    needs: [tag-release-build-linux, tag-release-build-macos]
    steps:
      - name: "Repository Dispatch"
        uses: benc-uk/workflow-dispatch@v1
        with:
          token: ${{ secrets.PAT }}
          repo: a1ecbr0wn/snapcraft-bcd
          ref: refs/heads/main
          workflow: snap.yml
          inputs: '{ "snap_version": "${{ github.ref_name }}" }'
