name: graphql_ppx pipeline

on: [pull_request, push]

jobs:
  test_and_build_linux:
    name: ${{ matrix.os }}/node-${{ matrix.node-version }}
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        node-version: [12.x]
        os: [ubuntu-latest]

    container:
      # https://github.com/baransu/docker-esy
      image: cichocinski/docker-esy:alpine3.8

    steps:
      - uses: actions/checkout@v1
      - name: Use Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v1
        with:
          node-version: ${{ matrix.node-version }}

      - name: Get esy store path
        id: esy_cache_path
        shell: bash
        run: |
          # COMPUTE THE ESY INSTALL CACHE LOCATION AHEAD OF TIME
          DESIRED_LEN="86"
          HOME_ESY3="$HOME/.esy/3"
          HOME_ESY3_LEN=${#HOME_ESY3}
          NUM_UNDERS=$(echo "$(($DESIRED_LEN-$HOME_ESY3_LEN))")
          UNDERS=$(printf "%-${NUM_UNDERS}s" "_")
          UNDERS="${UNDERS// /_}"
          THE_ESY__CACHE_INSTALL_PATH=${HOME_ESY3}${UNDERS}/i
          echo "THE_ESY__CACHE_INSTALL_PATH: $THE_ESY__CACHE_INSTALL_PATH"
          echo "##[set-output name=path;]$THE_ESY__CACHE_INSTALL_PATH"
      - name: Restore esy cache
        uses: actions/cache@v1
        with:
          path: ${{ steps.esy_cache_path.outputs.path }}
          key: v1-esy-${{ matrix.os }}-${{ hashFiles('**/index.json') }}
          restore-keys: |
            v1-esy-${{ matrix.os }}-

      - name: install
        run: |
          esy install
      - name: test-native
        run: |
          esy b dune runtest -f
        env:
          CI: true
      - name: test-bucklescript
        run: |
          esy release-static
          cd tests_bucklescript
          npm ci --no-optional
          npm run test
        env:
          CI: true

      - name: (only on release) Upload artifacts ${{ matrix.os }}
        if: github.event_name != 'pull_request' && startsWith(github.ref, 'refs/tags/')
        uses: actions/upload-artifact@master
        with:
          name: ${{ matrix.os }}
          path: _build/default/src/bucklescript_bin/bin.exe

  test_and_build:
    name: ${{ matrix.os }}/node-${{ matrix.node-version }}
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        node-version: [12.x]
        os: [macOS-latest]

    steps:
      - uses: actions/checkout@v1
      - name: Use Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v1
        with:
          node-version: ${{ matrix.node-version }}
      - name: Install esy
        run: |
          npm install -g esy@0.6.0

      - name: Get esy store path
        id: esy_cache_path
        shell: bash
        run: |
          # COMPUTE THE ESY INSTALL CACHE LOCATION AHEAD OF TIME
          if [ "${{ matrix.os }}" == "windows-2016" ]; then
            THE_ESY__CACHE_INSTALL_PATH=$HOME/.esy/3_/i
            THE_ESY__CACHE_INSTALL_PATH=$( cygpath --mixed --absolute "$THE_ESY__CACHE_INSTALL_PATH")
          else
            DESIRED_LEN="86"
            HOME_ESY3="$HOME/.esy/3"
            HOME_ESY3_LEN=${#HOME_ESY3}
            NUM_UNDERS=$(echo "$(($DESIRED_LEN-$HOME_ESY3_LEN))")
            UNDERS=$(printf "%-${NUM_UNDERS}s" "_")
            UNDERS="${UNDERS// /_}"
            THE_ESY__CACHE_INSTALL_PATH=${HOME_ESY3}${UNDERS}/i
          fi
          echo "THE_ESY__CACHE_INSTALL_PATH: $THE_ESY__CACHE_INSTALL_PATH"
          echo "##[set-output name=path;]$THE_ESY__CACHE_INSTALL_PATH"
      - name: Restore esy cache
        uses: actions/cache@v1
        with:
          path: ${{ steps.esy_cache_path.outputs.path }}
          key: v1-esy-${{ matrix.os }}-${{ hashFiles('**/index.json') }}
          restore-keys: |
            v1-esy-${{ matrix.os }}-

      - name: install
        run: |
          esy install
      - name: test-native
        run: |
          esy b dune runtest -f
        env:
          CI: true
      - name: test-bucklescript
        run: |
          esy b
          cd tests_bucklescript
          npm ci --no-optional
          npm run test
        env:
          CI: true

      - name: (only on release) Upload artifacts ${{ matrix.os }}
        if: github.event_name != 'pull_request' && startsWith(github.ref, 'refs/tags/')
        uses: actions/upload-artifact@master
        with:
          name: ${{ matrix.os }}
          path: _build/default/src/bucklescript_bin/bin.exe

  publish:
    needs: test_and_build
    if: github.event_name != 'pull_request' && startsWith(github.ref, 'refs/tags/')
    name: (only on release) Publish
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v1
      - uses: actions/setup-node@v1
        with:
          node-version: "12.x"
          registry-url: "https://registry.npmjs.org"

      - name: Download linux artifacts
        if: success()
        uses: actions/download-artifact@master
        with:
          name: ubuntu-latest
          path: binaries/linux

      - name: Download macOS artifacts
        if: success()
        uses: actions/download-artifact@master
        with:
          name: macOS-latest
          path: binaries/darwin

      - name: Move artifacts
        if: success()
        run: |
          mkdir -p bin
          mv binaries/darwin/bin.exe bin/graphql_ppx-darwin-x64.exe
          mv binaries/linux/bin.exe bin/graphql_ppx-linux-x64.exe

      - name: Publish
        if: success()
        run: npm publish
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }}
