name: deploy

env:
  GRAAL_VERSION: "20.0.0.java11"
# on:
#   push:
#     tags:
#       - "*.*.*"
on: [push, pull_request]

jobs:
  build-jar:
    runs-on: ubuntu-latest
    steps:
      - name: Git checkout
        uses: actions/checkout@v1
        with:
          fetch-depth: 1

      - uses: actions/cache@v1
        with:
          path: ~/.m2/repository
          key: ${{ runner.os }}-maven-${{ hashFiles('**/deps.edn') }}-${{ hashFiles('**/boot.properties') }}
          restore-keys: |
            ${{ runner.os }}-maven-

      - uses: actions/cache@v1
        with:
          path: ~/.gitlibs
          key: ${{ runner.os }}-deps-${{ hashFiles('**/deps.edn') }}
          restore-keys: |
            ${{ runner.os }}-deps-

      - uses: actions/cache@v1
        with:
          path: /opt/hostedtoolcache/Boot
          key: ${{ runner.os }}-boot-2.8.3
          restore-keys: |
            ${{ runner.os }}-boot-

      - name: Parse Ref
        id: parse-ref
        run: |
          echo ::set-output name=version::${GITHUB_REF#refs/tags/}

      - name: Install Node.js 9.x
        uses: actions/setup-node@v1
        with:
          node-version: 9.x

      - name: Install Boot-clj etc (https://github.com/marketplace/actions/setup-clojure)
        uses: DeLaGuardo/setup-clojure@2.0
        with:
          boot: 2.8.3

      - name: Check boot version
        run: boot -V

      # - name: Install shadow-cljs
      #   run: |
      #     npm install shadow-cljs
      #     npm install
      #
      # - name: Install npm packages
      #   run: |
      #     npm install

      - name: Build into uberjar via boot
        run: |
          npm run pkg-java && \
          mv target/closh-zero.jar closh-zero.jar

      - uses: actions/upload-artifact@v1
        with:
          path: closh-zero.jar
          name: closh-zero-jar

  # Initial task to compile a JAR, store as a pipeline artifact to be used by
  # downstream builders.
  build-jar-sci:
    runs-on: ubuntu-latest
    steps:
      - name: Git checkout
        uses: actions/checkout@v1
        with:
          fetch-depth: 1

      - uses: actions/cache@v1
        with:
          path: ~/.m2/repository
          key: ${{ runner.os }}-maven-${{ hashFiles('**/deps.edn') }}-${{ hashFiles('**/boot.properties') }}
          restore-keys: |
            ${{ runner.os }}-maven-

      - uses: actions/cache@v1
        with:
          path: ~/.gitlibs
          key: ${{ runner.os }}-deps-${{ hashFiles('**/deps.edn') }}
          restore-keys: |
            ${{ runner.os }}-deps-

      - uses: actions/cache@v1
        with:
          path: /opt/hostedtoolcache/Boot
          key: ${{ runner.os }}-boot-2.8.3
          restore-keys: |
            ${{ runner.os }}-boot-

      - name: Parse Ref
        id: parse-ref
        run: |
          echo ::set-output name=version::${GITHUB_REF#refs/tags/}

      - name: Install Node.js 9.x
        uses: actions/setup-node@v1
        with:
          node-version: 9.x

      - name: Install Boot-clj etc (https://github.com/marketplace/actions/setup-clojure)
        uses: DeLaGuardo/setup-clojure@2.0
        with:
          boot: 2.8.3

      - name: Check boot version
        run: boot -V

      - run: |
          which boot

      # - name: Install shadow-cljs
      #   run: |
      #     npm install shadow-cljs
      #     npm install
      #
      # - name: Install npm packages
      #   run: |
      #     npm install

      - name: Build into uberjar via boot
        run: |
          npm run compile-sci-boot && \
          mv target/closh-zero-sci.jar closh-zero-sci.jar

      - uses: actions/upload-artifact@v1
        with:
          path: closh-zero-sci.jar
          name: closh-zero-sci-jar

  # Use GraalVM on macOS to convert JAR to a native macOS binary
  native-image-mac:
    needs: [build-jar-sci]
    runs-on: macOS-latest
    steps:
      - name: Parse Ref
        id: parse-ref
        run: |
          echo ::set-output name=version::${GITHUB_REF#refs/tags/}

      # === used for running tests, maybe extract into separate job

      - uses: actions/cache@v1
        with:
          path: /Users/runner/hostedtoolcache/GraalVM
          key: ${{ runner.os }}-graal-${{ env.GRAAL_VERSION }}
          restore-keys: |
            ${{ runner.os }}-graal-

      - name: Setup Clojure (https://github.com/marketplace/actions/setup-clojure)
        uses: DeLaGuardo/setup-clojure@2.0
        with:
          tools-deps: '1.10.1.469'

      - name: Check clojure version
        run: clojure -Sdescribe

      - name: Git checkout
        uses: actions/checkout@v1
        with:
          fetch-depth: 1

      # ===

      - uses: actions/download-artifact@v1
        with:
          name: closh-zero-sci-jar
          path: .

      - name: Prepare GraalVM
        uses: DeLaGuardo/setup-graalvm@3
        with:
          graalvm-version: ${{ env.GRAAL_VERSION }}

      - name: Install native-image
        run: gu install native-image

      - run: |
          which native-image

      - name: Build macOS native image
        run: |
          native-image \
              -Dclojure.compiler.direct-linking=true \
              --initialize-at-build-time \
              --report-unsupported-elements-at-runtime \
              --no-server \
              --no-fallback \
              --enable-url-protocols=http,https \
              --allow-incomplete-classpath \
              -jar closh-zero-sci.jar \
              closh-zero-sci

      - uses: actions/upload-artifact@v1
        with:
          path: closh-zero-sci
          name: closh-zero-sci-macos

      - run: brew install expect

      - name: Test binary
        run: ./scripts/test-sci-binary.sh

      # - name: Run tests with binary
      #   run: env CI_ENV=1 __CLOSH_USE_SCI_EVAL__=1 clojure -A:sci:test -m cognitect.test-runner

  native-image-linux:
    needs: [build-jar-sci]
    runs-on: ubuntu-latest
    steps:
      - name: Parse Ref
        id: parse-ref
        run: |
          echo ::set-output name=version::${GITHUB_REF#refs/tags/}

      # === used for running tests, maybe extract into separate job

      - uses: actions/cache@v1
        with:
          path: /opt/hostedtoolcache/GraalVM
          key: ${{ runner.os }}-graal-${{ env.GRAAL_VERSION }}
          restore-keys: |
            ${{ runner.os }}-graal-

      - name: Setup Clojure (https://github.com/marketplace/actions/setup-clojure)
        uses: DeLaGuardo/setup-clojure@2.0
        with:
          tools-deps: '1.10.1.469'

      - name: Check clojure version
        run: clojure -Sdescribe

      - name: Git checkout
        uses: actions/checkout@v1
        with:
          fetch-depth: 1

      # ===

      - uses: actions/download-artifact@v1
        with:
          name: closh-zero-sci-jar
          path: .

      - name: Prepare GraalVM
        uses: DeLaGuardo/setup-graalvm@3
        with:
          graalvm-version: ${{ env.GRAAL_VERSION }}

      - name: Install native-image
        run: gu install native-image

      - run: |
          which native-image

      - name: Build Linux native image
        run: |
          native-image \
              -Dclojure.compiler.direct-linking=true \
              --initialize-at-build-time \
              --report-unsupported-elements-at-runtime \
              --no-server \
              --no-fallback \
              --enable-url-protocols=http,https \
              --allow-incomplete-classpath \
              -jar closh-zero-sci.jar \
              closh-zero-sci

      - uses: actions/upload-artifact@v1
        with:
          path: closh-zero-sci
          name: closh-zero-sci-linux

      - run: sudo apt-get install -y expect

      - name: Test binary
        run: ./scripts/test-sci-binary.sh

      # - name: Run tests with binary
      #   run: env CI_ENV=1 __CLOSH_USE_SCI_EVAL__=1 clojure -A:sci:test -m cognitect.test-runner

  # Collate artifacts, upload to GitHub Releases on semver tags

  # releaser:
  #   needs: [native-image-mac]
  #   runs-on: ubuntu-latest
  #   steps:
  #     - name: Parse Ref
  #       id: parse-ref
  #       run: |
  #         echo ::set-output name=version::${GITHUB_REF#refs/tags/}

  #     - uses: actions/download-artifact@v1
  #       with:
  #         name: closh-zero-sci
  #         path: .

  #     - name: Create Release
  #       id: create-release
  #       uses: actions/create-release@v1.0.0
  #       env:
  #         GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  #       with:
  #         tag_name: ${{ github.ref }}
  #         release_name: ${{ github.ref }}
  #         draft: false
  #         prerelease: false

  #     - name: Upload Release Asset
  #       id: upload-release-asset
  #       uses: actions/upload-release-asset@v1.0.1
  #       env:
  #         GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  #       with:
  #         upload_url: ${{ steps.create-release.outputs.upload_url }}
  #         asset_path: ./closh-zero-sci.tgz
  #         asset_name: closh-zero-sci.tgz
  #         asset_content_type: application/tar+gzip
