name: Release
on:
  schedule:
    - cron: "0 0 * * *" # midnight UTC

  workflow_dispatch:

  push:
    branches:
      - release

jobs:
  check_date:
    runs-on: ubuntu-latest
    name: Check latest commit
    outputs:
      should_run: ${{ steps.should_run.outputs.should_run }}
    steps:
      - uses: actions/checkout@v4
      - name: print latest_commit
        run: echo ${{ github.sha }}
      - id: should_run
        continue-on-error: true
        name: check latest commit is less than a day
        if: ${{ github.event_name == 'schedule' }}
        run: test -z $(git rev-list  --after="24 hours"  ${{ github.sha }}) && echo "::set-output name=should_run::false"

  build_server:
    strategy:
      matrix:
        include:
          - os: ubuntu-latest
            code-target: linux-x64

          - os: ubuntu-latest
            code-target: no-server

          - os: macos-14
            code-target: darwin-arm64

          - os: macos-12
            code-target: darwin-x64

    name: dist (${{ matrix.code-target }})
    runs-on: ${{ matrix.os }}
    steps:
      - name: Set patch version
        run: |
          if [[ $GITHUB_REF == 'refs/heads/release' ]]; then
            echo "version=0.1" >> $GITHUB_ENV
          else
            echo "version=0.2" >> $GITHUB_ENV
          fi
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          fetch-depth: ${{ env.FETCH_DEPTH }}

      - name: Install Node.js
        uses: actions/setup-node@v4
        with:
          node-version: 16

      - name: Use OCaml
        with:
          ocaml-compiler: 4.14.0
          dune-cache: true
          cache-prefix: v1-${{ matrix.os }}
          allow-prerelease-opam: true
        uses: ocaml/setup-ocaml@v3

      - run: |
          opam install . --deps-only
          opam pin add why3 https://gitlab.inria.fr/why3/why3.git#1aff0e0debcc53ab

      - run: LINKING_MODE=static opam exec -- dune build --profile=release

      - run: npm ci
        working-directory: ide

      - name: Copy server to bundle
        run: |
          cp _build/default/bin/main.exe ide/whycode
          cp -R _opam/lib/why3 ide/why-lib
          cp -R _opam/share/why3 ide/why-data
        if: matrix.code_target != 'no-server'

      - run: mkdir dist

      - name: Set Version Number
        run: |
          echo "Preparing version: ${{env.version}}.${{github.run_number}}"
          node -e "var p = require('./package.json'); console.log(JSON.stringify({...p, version: '${{ env.version }}.${{ github.run_number }}' }))" > ./package.nightly.json
          mv package.nightly.json package.json
        working-directory: ide

      - name: Package
        run: npx vsce package -o "../dist/whycode-${{ matrix.code-target }}.vsix" --target ${{ matrix.code-target }}
        if: github.ref == 'refs/heads/release' && matrix.code-target != 'no-server'
        working-directory: ide

      - name: Package (Nightly)
        run: npx vsce package -o "../dist/whycode-${{ matrix.code-target }}.vsix" --target ${{ matrix.code-target }} --pre-release
        if: github.ref != 'refs/heads/release' && matrix.code-target != 'no-server'
        working-directory: ide

      # Publish fallback
      - if: matrix.code-target == 'no-server' && github.ref == 'refs/heads/release'
        run: npx vsce package -o ../dist/whycode-no-server.vsix
        working-directory: ide

      - if: matrix.code-target == 'no-server' && github.ref != 'refs/heads/release'
        run: npx vsce package -o ../dist/whycode-no-server.vsix --pre-release
        working-directory: ide

      - name: Upload artifacts
        uses: actions/upload-artifact@v4
        with:
          name: ${{ matrix.code-target }}
          path: ./dist

  publish:
    needs: [build_server, check_date]
    runs-on: ubuntu-latest
    steps:
      - name: Install Nodejs
        uses: actions/setup-node@v4
        with:
          node-version: 16

      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          fetch-depth: ${{ env.FETCH_DEPTH }}

      - uses: actions/download-artifact@v4
        with:
          name: darwin-arm64
          path: dist

      - uses: actions/download-artifact@v4
        with:
          name: darwin-x64
          path: dist

      - uses: actions/download-artifact@v4
        with:
          name: no-server
          path: dist

      - uses: actions/download-artifact@v4
        with:
          name: linux-x64
          path: dist

      - run: ls -al ./dist

      - run: npm ci
        working-directory: ./ide

      - name: Publish Extension (Code Marketplace, nightly)
        if: github.ref != 'refs/heads/release' && needs.check_date.outputs.should_run != 'true'
        working-directory: ./ide
        run: npx vsce publish --pat ${{ secrets.MARKETPLACE_TOKEN }} --packagePath ../dist/whycode-*.vsix

      - name: Publish Extension (OpenVSX, nightly)
        if: github.ref != 'refs/heads/release' && needs.check_date.outputs.should_run != 'true'
        working-directory: ./ide
        run: npx ovsx publish --pat ${{ secrets.OPENVSX_TOKEN }} --packagePath ../dist/whycode-*.vsix
        timeout-minutes: 2

      - name: Publish Extension (Code Marketplace)
        if: github.ref == 'refs/heads/release'
        working-directory: ./ide
        run: npx vsce publish --pat ${{ secrets.MARKETPLACE_TOKEN }} --packagePath ../dist/whycode-*.vsix

      - name: Publish Extension (OpenVSX)
        if: github.ref == 'refs/heads/release'
        working-directory: ./ide
        run: npx ovsx publish --pat ${{ secrets.OPENVSX_TOKEN }} --packagePath ../dist/whycode-*.vsix
        timeout-minutes: 2

      # - name: Release
      # - name: Release
      #   uses: softprops/action-gh-release@v1
