name: "Build And Upload"

on:
  push:
    tags: [ "*" ]

jobs:
  # job 1
  build-and-upload:
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os: [
          windows-latest,
          ubuntu-latest,
          macos-latest
        ]
        node: [
          16
        ]
        lerna: [
          6
        ]
    steps:
      - name: "Checkout"
        uses: actions/checkout@v4.1.7

      - name: "Setup Node.js environment"
        uses: actions/setup-node@v4.0.3
        with:
          node-version: ${{ matrix.node }}
          registry-url: https://npm.pkg.github.com/

      - name: "Get package info"
        id: package-info
        uses: luizfelipelaviola/get-package-info@v1
        with:
          path: ./packages/mitmproxy

      - name: "Print"
        run: |
          echo "version = ${{ steps.package-info.outputs.version }}";
          echo "github.ref_type = ${{ github.ref_type }}";
          echo "github.ref = ${{ github.ref }}";
          echo "github.ref_name = ${{ github.ref_name }}";

      - name: "npm install lerna/yarn"
        run: |
          echo "======================================================================";
          dir || ls;

          echo "======================================================================";
          echo "npm install lerna@${{ matrix.lerna }} -g";
          echo "----------";
          npm install lerna@${{ matrix.lerna }} -g;

          echo "======================================================================";
          echo "npm install yarn";
          echo "----------";
          npm install yarn;

      - name: "npm -v | lerna -v | yarn -v"
        run: |
          echo "======================================================================";
          echo "lerna -v";
          echo "----------";
          lerna -v;

          echo "======================================================================";
          echo "npm -v";
          echo "----------";
          npm -v;

          echo "======================================================================";
          echo "yarn -v";
          echo "----------";
          yarn -v;

      - name: "lerna bootstrap"
        run: |
          echo "======================================================================";
          dir || ls;

          echo "======================================================================";
          echo "lerna bootstrap --npm-client=yarn";
          echo "----------";
          lerna bootstrap --npm-client=yarn;

      - name: "npm run electron:build"
        run: |
          echo "======================================================================";
          dir || ls;

          echo "======================================================================";
          echo "cd packages/gui";
          echo "----------";
          cd packages/gui;

          echo "======================================================================";
          dir || ls;

          echo "======================================================================";
          echo "npm run electron:build";
          echo "----------";
          npm run electron:build;

          echo "======================================================================";
          cd dist_electron;
          dir || ls;

          cd ../../../;

      - name: "Upload DevSidecar-${{ steps.package-info.outputs.version }}.exe"
        uses: actions/upload-artifact@v3
        if: ${{ matrix.os == 'windows-latest' }}
        with:
          path: packages/gui/dist_electron/DevSidecar-${{ steps.package-info.outputs.version }}.exe
          name: "DevSidecar-${{ steps.package-info.outputs.version }}.exe"
          if-no-files-found: error
      - name: "Upload DevSidecar-${{ steps.package-info.outputs.version }}.deb"
        uses: actions/upload-artifact@v3
        if: ${{ matrix.os == 'ubuntu-latest' }}
        with:
          path: packages/gui/dist_electron/DevSidecar-${{ steps.package-info.outputs.version }}.deb
          name: "DevSidecar-${{ steps.package-info.outputs.version }}.deb"
          if-no-files-found: error
      - name: "Upload DevSidecar-${{ steps.package-info.outputs.version }}.AppImage"
        uses: actions/upload-artifact@v3
        if: ${{ matrix.os == 'ubuntu-latest' }}
        with:
          path: packages/gui/dist_electron/DevSidecar-${{ steps.package-info.outputs.version }}.AppImage
          name: "DevSidecar-${{ steps.package-info.outputs.version }}.AppImage"
          if-no-files-found: error
      - name: "Upload DevSidecar-${{ steps.package-info.outputs.version }}.dmg "
        uses: actions/upload-artifact@v3
        if: ${{ (matrix.os == 'macos-11' || matrix.os == 'macos-12' || matrix.os == 'macos-13' || matrix.os == 'macos-latest') }}
        with:
          path: packages/gui/dist_electron/DevSidecar-${{ steps.package-info.outputs.version }}.dmg
          name: "DevSidecar-${{ steps.package-info.outputs.version }}.dmg"
          if-no-files-found: error

  # job 2
  download-and-release:
    runs-on: ubuntu-latest
    needs:
      - build-and-upload
    steps:
      - name: "Checkout"
        uses: actions/checkout@v4.1.7

      - name: "Get package info"
        id: package-info
        uses: luizfelipelaviola/get-package-info@v1
        with:
          path: ./packages/mitmproxy

      - name: "Make './release' dir"
        run: mkdir release

      - name: "Download DevSidecar-${{ steps.package-info.outputs.version }}.exe"
        uses: actions/download-artifact@v3
        with:
          name: DevSidecar-${{ steps.package-info.outputs.version }}.exe
          path: release
      - name: "Download DevSidecar-${{ steps.package-info.outputs.version }}.deb"
        uses: actions/download-artifact@v3
        with:
          name: DevSidecar-${{ steps.package-info.outputs.version }}.deb
          path: release
      - name: "Download DevSidecar-${{ steps.package-info.outputs.version }}.AppImage"
        uses: actions/download-artifact@v3
        with:
          name: DevSidecar-${{ steps.package-info.outputs.version }}.AppImage
          path: release
      - name: "Download DevSidecar-${{ steps.package-info.outputs.version }}.dmg"
        uses: actions/download-artifact@v3
        with:
          name: DevSidecar-${{ steps.package-info.outputs.version }}.dmg
          path: release

      - name: "Print"
        run: |
          ls -lah release;

      - name: "Create a draft release"
        uses: fnkr/github-action-ghr@ghr-options
        env:
          GHR_PATH: release/
          GITHUB_TOKEN: ${{ github.token }}
          GHR_REPLACE: true
          GHR_TITLE: ${{ steps.package-info.outputs.version }}
          GHR_DRAFT: true