# TODO: support skip ci (https://github.community/t/github-actions-does-not-respect-skip-ci/17325/8)

name: CI
on: [push, pull_request]

jobs:
  windows64-build:
    runs-on: windows-latest
    env:
      ACTIONS_ALLOW_UNSECURE_COMMANDS: true
      PLATFORM: windows64
      ARCH: 64
      MINGW_ARCH: x86_64
      CYG_ROOT: D:\cygwin
    steps:
      - uses: actions/checkout@main
        with:
          submodules: recursive

      - name: Use GNU Tar from msys
        run: |
          echo "C:\msys64\usr\bin" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append
          rm C:\msys64\usr\bin\bash.exe

      @import install-nsis.yml
      @import install-neko-windows.yml
      @import install-ocaml-windows64.yml
      @import install-ocaml-libs-windows.yml
      @import build-windows.yml

  windows-build:
    runs-on: windows-latest
    env:
      ACTIONS_ALLOW_UNSECURE_COMMANDS: true
      PLATFORM: windows
      ARCH: 32
      MINGW_ARCH: i686
      CYG_ROOT: D:\cygwin
    steps:
      - uses: actions/checkout@main
        with:
          submodules: recursive

      - name: Use GNU Tar from msys
        run: |
          echo "C:\msys64\usr\bin" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append
          rm C:\msys64\usr\bin\bash.exe

      @import install-nsis.yml
      @import install-neko-windows.yml
      @import install-ocaml-windows.yml
      @import install-ocaml-libs-windows.yml
      @import build-windows.yml

  linux-build:
    runs-on: ubuntu-20.04
    env:
      PLATFORM: linux64
      OPAMYES: 1
    strategy:
      fail-fast: false
      matrix:
        ocaml: ["4.08.1", "5.0.0"]
    steps:
      - uses: actions/checkout@main
        with:
          submodules: recursive

      - name: Cache opam
        id: cache-opam
        uses: actions/cache@v3.0.11
        with:
          path: ~/.opam/
          key: ${{ runner.os }}-${{ matrix.ocaml }}-${{ hashFiles('./haxe.opam', './libs/') }}

      @import install-neko-unix.yml

      - name: Install dependencies
        run: |
          set -ex
          sudo add-apt-repository ppa:avsm/ppa -y # provides OPAM 2
          sudo add-apt-repository ppa:haxe/ocaml -y # provides newer version of mbedtls
          sudo apt-get update -qqy
          sudo apt-get install -qqy ocaml-nox camlp5 opam libpcre2-dev zlib1g-dev libgtk2.0-dev libmbedtls-dev ninja-build libstring-shellquote-perl libipc-system-simple-perl

      - name: Install OCaml libraries
        if: steps.cache-opam.outputs.cache-hit != 'true'
        run: |
          set -ex
          opam init # --disable-sandboxing
          opam update
          opam switch create ${{ matrix.ocaml }}
          opam pin add haxe . --no-action
          opam install haxe --deps-only --assume-depexts
          opam list
          ocamlopt -v

      - name: Set ADD_REVISION=1 for non-release
        if: ${{ !startsWith(github.ref, 'refs/tags/') }}
        run: echo "ADD_REVISION=1" >> $GITHUB_ENV

      - name: Build Haxe
        run: |
          set -ex
          eval $(opam env)
          opam config exec -- make -s -j`nproc` STATICLINK=1 haxe
          opam config exec -- make -s haxelib
          make -s package_unix
          ls -l out
          ldd -v ./haxe
          ldd -v ./haxelib

      # https://stackoverflow.com/questions/58033366/how-to-get-current-branch-within-github-actions
      - name: Extract branch name
        id: extract_branch
        shell: bash
        run: echo "branch=${GITHUB_REF#refs/heads/}" >> $GITHUB_OUTPUT

      - name: Build xmldoc
        run: |
          set -ex
          make -s xmldoc
          cat >extra/doc/info.json <<EOL
            {
              "commit": "$GITHUB_SHA",
              "branch": "${{ steps.extract_branch.outputs.branch }}"
            }
          EOL

      - name: Upload artifact
        uses: actions/upload-artifact@v3
        with:
          name: linuxBinaries${{ (matrix.ocaml == '5.0.0' && '_ocaml5') || '' }}
          path: out

      - name: Upload xmldoc artifact
        uses: actions/upload-artifact@v3
        if: matrix.ocaml == '4.08.1'
        with:
          name: xmldoc
          path: extra/doc

  linux-test:
    needs: linux-build
    runs-on: ubuntu-20.04
    env:
      PLATFORM: linux64
      TEST: ${{matrix.target}}
      HXCPP_COMPILE_CACHE: ~/hxcache
      HAXE_STD_PATH: /usr/local/share/haxe/std
    strategy:
      fail-fast: false
      matrix:
        ocaml: ["4.08.1", "5.0.0"]
        target: [macro, js, hl, cpp, 'java,jvm', cs, php, python, lua, flash, neko]
        include:
          - target: hl
            APT_PACKAGES: cmake ninja-build libturbojpeg-dev
          - target: cpp
            APT_PACKAGES: gcc-multilib g++-multilib
          - target: lua
            APT_PACKAGES: ncurses-dev
          - target: flash
            APT_PACKAGES: libglib2.0-0 libgtk2.0-0 libfreetype6 xvfb
    steps:
      - uses: actions/checkout@main
        with:
          submodules: recursive
      - uses: actions/download-artifact@v3
        with:
          name: linuxBinaries${{ (matrix.ocaml == '5.0.0' && '_ocaml5') || '' }}
          path: linuxBinaries

      @import install-neko-unix.yml

      - name: Setup Haxe
        run: |
          sudo apt install -qqy libmbedtls-dev

          set -ex
          tar -xf linuxBinaries/*_bin.tar.gz -C linuxBinaries --strip-components=1
          sudo mkdir -p /usr/local/bin/
          sudo mkdir -p /usr/local/share/haxe/
          sudo ln -s `pwd`/linuxBinaries/haxe /usr/local/bin/haxe
          sudo ln -s `pwd`/linuxBinaries/haxelib /usr/local/bin/haxelib
          sudo ln -s `pwd`/linuxBinaries/std /usr/local/share/haxe/std

      - name: Print Haxe version
        run: haxe -version

      - name: Setup haxelib
        run: |
          set -ex
          mkdir ~/haxelib
          haxelib setup ~/haxelib

      - name: Install apt packages
        if: matrix.APT_PACKAGES
        run: |
          set -ex
          sudo apt update -qqy
          sudo apt install -qqy ${{matrix.APT_PACKAGES}}

      - name: Flash setup
        if: matrix.target == 'flash'
        run: export DISPLAY=:99.0

      - name: Test
        run: haxe RunCi.hxml
        working-directory: ${{github.workspace}}/tests

  test-docgen:
    needs: linux-build
    runs-on: ubuntu-20.04
    env:
      PLATFORM: linux64
      HXCPP_COMPILE_CACHE: ~/hxcache
    steps:
      - uses: actions/checkout@main
        with:
          submodules: recursive

      - uses: actions/download-artifact@v3
        with:
          name: linuxBinaries
          path: linuxBinaries

      - name: Download xmldoc artifact
        uses: actions/download-artifact@v3
        with:
          name: xmldoc
          path: xmldoc

      @import install-neko-unix.yml

      - name: Setup Haxe
        run: |
          sudo apt install -qqy libmbedtls-dev

          set -ex
          tar -xf linuxBinaries/*_bin.tar.gz -C linuxBinaries --strip-components=1
          sudo mkdir -p /usr/local/bin/
          sudo mkdir -p /usr/local/share/haxe/
          sudo ln -s `pwd`/linuxBinaries/haxe /usr/local/bin/haxe
          sudo ln -s `pwd`/linuxBinaries/haxelib /usr/local/bin/haxelib
          sudo ln -s `pwd`/linuxBinaries/std /usr/local/share/haxe/std

      - name: Print Haxe version
        run: haxe -version

      - name: Setup haxelib
        run: |
          set -ex
          mkdir ~/haxelib
          haxelib setup ~/haxelib

      - name: Test documentation generation
        run: |
          set -ex
          haxelib git dox https://github.com/HaxeFoundation/dox.git
          haxelib git hxtemplo https://github.com/Simn/hxtemplo.git
          haxelib git hxargs https://github.com/Simn/hxargs.git
          haxelib git markdown https://github.com/dpeek/haxe-markdown.git
          haxelib git hxcpp https://github.com/HaxeFoundation/hxcpp.git
          cd $(haxelib libpath hxcpp)/tools/hxcpp
          haxe compile.hxml
          cd -
          haxe dox.hxml
          mkdir resources
          cp ../../src-json/* resources
          cpp/Dox -i ../../xmldoc -ex microsoft -ex javax -ex cs.internal -theme $(haxelib libpath dox)/themes/default
        working-directory: ${{github.workspace}}/tests/docgen

  linux-arm64:
    runs-on: ubuntu-20.04
    permissions:
      packages: write
    env:
      FORCE_COLOR: 1
    steps:
      - name: Login to GitHub Container Registry
        uses: docker/login-action@v2
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Install Earthly
        run: sudo /bin/sh -c 'wget https://github.com/earthly/earthly/releases/download/v0.6.13/earthly-linux-amd64 -O /usr/local/bin/earthly && chmod +x /usr/local/bin/earthly && /usr/local/bin/earthly bootstrap --with-autocomplete'

      - name: Set up QEMU
        id: qemu
        uses: docker/setup-qemu-action@v2
        with:
            image: tonistiigi/binfmt:latest
            platforms: all

      - uses: actions/checkout@main
        with:
          submodules: recursive

      - name: Set CONTAINER_ vars
        run: |
          echo "CONTAINER_REG=$(echo ${{ github.repository }} | tr '[:upper:]' '[:lower:]')" >> $GITHUB_ENV;
          echo "CONTAINER_TAG=$(echo ${{ github.ref_name }} | sed -e 's/[^A-Za-z0-9\.]/-/g')" >> $GITHUB_ENV;

      - name: Build devcontainer
        run: earthly --platform=linux/arm64 +devcontainer --IMAGE_NAME="ghcr.io/${CONTAINER_REG}_devcontainer" --IMAGE_TAG="${CONTAINER_TAG}-arm64" --IMAGE_CACHE="ghcr.io/haxefoundation/haxe_devcontainer:development-arm64"
        env:
          EARTHLY_PUSH: "${{ github.event_name == 'push' }}"
          EARTHLY_USE_INLINE_CACHE: true
          EARTHLY_SAVE_INLINE_CACHE: true

      - name: Set ADD_REVISION=1 for non-release
        if: ${{ !startsWith(github.ref, 'refs/tags/') }}
        run: echo "ADD_REVISION=1" >> $GITHUB_ENV

      - name: Build
        run: earthly --platform=linux/arm64 +build --ADD_REVISION="$ADD_REVISION" --SET_SAFE_DIRECTORY="true"
        env:
          EARTHLY_PUSH: "${{ github.event_name == 'push' }}"
          EARTHLY_REMOTE_CACHE: "ghcr.io/${{env.CONTAINER_REG}}_cache:build-${{env.CONTAINER_TAG}}-arm64"

      - name: Upload artifact
        uses: actions/upload-artifact@v3
        with:
          name: linuxArm64Binaries
          path: out/linux/arm64

  mac-build:
    runs-on: macos-latest
    env:
      PLATFORM: mac
      OPAMYES: 1
      MACOSX_DEPLOYMENT_TARGET: 10.13
    steps:
      - uses: actions/checkout@main
        with:
          submodules: recursive

      - name: Cache opam
        id: cache-opam
        uses: actions/cache@v3.0.11
        with:
          path: ~/.opam/
          key: ${{ runner.os }}-${{ hashFiles('./haxe.opam', './libs/') }}

      @import install-neko-unix.yml
      @import build-mac.yml

  windows64-test:
    needs: windows64-build
    runs-on: windows-latest
    env:
      ACTIONS_ALLOW_UNSECURE_COMMANDS: true
      PLATFORM: windows64
      TEST: ${{matrix.target}}
      HXCPP_COMPILE_CACHE: ~/hxcache
      ARCH: 64
    strategy:
      fail-fast: false
      matrix:
        # TODO enable lua after https://github.com/HaxeFoundation/haxe/issues/10919
        target: [macro, js, hl, cpp, 'java,jvm', cs, php, python, flash, neko]
    steps:
      - uses: actions/checkout@main
        with:
          submodules: recursive
      - uses: actions/download-artifact@v3
        with:
          name: win${{env.ARCH}}Binaries
          path: win${{env.ARCH}}Binaries

      @import install-neko-windows.yml
      @import test-windows.yml

  windows-test:
    needs: windows-build
    runs-on: windows-latest
    env:
      ACTIONS_ALLOW_UNSECURE_COMMANDS: true
      PLATFORM: windows
      TEST: ${{matrix.target}}
      HXCPP_COMPILE_CACHE: ~/hxcache
      ARCH: 32
    strategy:
      fail-fast: false
      matrix:
        # TODO jvm: https://github.com/HaxeFoundation/haxe/issues/8601
        # TODO enable lua after https://github.com/HaxeFoundation/haxe/issues/10919
        target: [macro, js, hl, cpp, java, cs, php, python, flash, neko]
    steps:
      - uses: actions/checkout@main
        with:
          submodules: recursive
      - uses: actions/download-artifact@v3
        with:
          name: win${{env.ARCH}}Binaries
          path: win${{env.ARCH}}Binaries

      @import install-neko-windows.yml
      @import test-windows.yml

  mac-test:
    needs: mac-build
    runs-on: macos-latest
    env:
      PLATFORM: mac
      TEST: ${{matrix.target}}
      HXCPP_COMPILE_CACHE: ~/hxcache
      HAXE_STD_PATH: /usr/local/share/haxe/std
    strategy:
      fail-fast: false
      matrix:
        target: [macro, js, hl, cpp, 'java,jvm', cs, php, python, flash, neko]
        include:
          - target: hl
            BREW_PACKAGES: ninja
    steps:
      - uses: actions/checkout@main
        with:
          submodules: recursive
      - uses: actions/download-artifact@v3
        with:
          name: macBinaries
          path: macBinaries

      @import install-neko-unix.yml
      @import test-mac.yml

  deploy:
    if: success() && github.repository_owner == 'HaxeFoundation' && github.event_name != 'pull_request'
    needs: [linux-test, linux-arm64, mac-test, windows-test, windows64-test]
    runs-on: ubuntu-20.04
    steps:
      # this is only needed for to get `COMMIT_DATE`...
      # maybe https://github.community/t/expose-commit-timestamp-in-the-github-context-data/16460/3
      # would be faster
      - name: Checkout the repository
        uses: actions/checkout@main

      - name: Download build artifacts
        uses: actions/download-artifact@v3

      - name: Install awscli
        run: |
          set -ex
          sudo apt-get update -qqy
          sudo apt-get install -qqy awscli

      # https://stackoverflow.com/questions/58033366/how-to-get-current-branch-within-github-actions
      - name: Extract branch name
        id: extract_branch
        shell: bash
        run: echo "branch=${GITHUB_REF#refs/heads/}" >> $GITHUB_OUTPUT

      - name: Upload binaries
        shell: bash
        env:
          AWS_ACCESS_KEY_ID: ${{ secrets.HXBUILDS_AWS_ACCESS_KEY_ID }}
          AWS_SECRET_ACCESS_KEY: ${{ secrets.HXBUILDS_AWS_SECRET_ACCESS_KEY }}
          HXBUILDS_S3ADDR: ${{ secrets.HXBUILDS_S3ADDR }}
          AWS_EC2_METADATA_DISABLED: true
        run: |
          set -ex
          COMMIT_HASH_SHORT=${GITHUB_SHA:0:7}
          COMMIT_DATE=`TZ=UTC git show --quiet --date='format-local:%Y-%m-%d' --format="%cd"`
          FILE_NAME=haxe_${COMMIT_DATE}_${{ steps.extract_branch.outputs.branch }}_${COMMIT_HASH_SHORT}
          aws s3 cp linuxBinaries/*_bin.tar.gz      ${HXBUILDS_S3ADDR}/haxe/linux64/${FILE_NAME}.tar.gz
          aws s3 cp linuxArm64Binaries/*_bin.tar.gz ${HXBUILDS_S3ADDR}/haxe/linux-arm64/${FILE_NAME}.tar.gz
          aws s3 cp macBinaries/*_bin.tar.gz        ${HXBUILDS_S3ADDR}/haxe/mac/${FILE_NAME}.tar.gz
          aws s3 cp macBinaries/*_installer.tar.gz  ${HXBUILDS_S3ADDR}/haxe/mac-installer/${FILE_NAME}.tar.gz
          aws s3 cp win64Binaries/*_bin.zip         ${HXBUILDS_S3ADDR}/haxe/windows64/${FILE_NAME}.zip
          aws s3 cp win64Binaries/*_installer.zip   ${HXBUILDS_S3ADDR}/haxe/windows64-installer/${FILE_NAME}.zip
          aws s3 cp win64Binaries/*.nupkg           ${HXBUILDS_S3ADDR}/haxe/windows64-choco/
          aws s3 cp win32Binaries/*_bin.zip         ${HXBUILDS_S3ADDR}/haxe/windows/${FILE_NAME}.zip
          aws s3 cp win32Binaries/*_installer.zip   ${HXBUILDS_S3ADDR}/haxe/windows-installer/${FILE_NAME}.zip
          aws s3 cp win32Binaries/*.nupkg           ${HXBUILDS_S3ADDR}/haxe/windows-choco/

      - name: Update "latest"
        if: github.ref == 'refs/heads/development'
        shell: bash
        env:
          AWS_ACCESS_KEY_ID: ${{ secrets.HXBUILDS_AWS_ACCESS_KEY_ID }}
          AWS_SECRET_ACCESS_KEY: ${{ secrets.HXBUILDS_AWS_SECRET_ACCESS_KEY }}
          HXBUILDS_S3ADDR: ${{ secrets.HXBUILDS_S3ADDR }}
          AWS_EC2_METADATA_DISABLED: true
        run: |
          set -ex
          aws s3 cp linuxBinaries/*_bin.tar.gz      ${HXBUILDS_S3ADDR}/haxe/linux64/haxe_latest.tar.gz
          aws s3 cp linuxArm64Binaries/*_bin.tar.gz ${HXBUILDS_S3ADDR}/haxe/linux-arm64/haxe_latest.tar.gz
          aws s3 cp macBinaries/*_bin.tar.gz        ${HXBUILDS_S3ADDR}/haxe/mac/haxe_latest.tar.gz
          aws s3 cp macBinaries/*_installer.tar.gz  ${HXBUILDS_S3ADDR}/haxe/mac-installer/haxe_latest.tar.gz
          aws s3 cp win64Binaries/*_bin.zip         ${HXBUILDS_S3ADDR}/haxe/windows64/haxe_latest.zip
          aws s3 cp win64Binaries/*_installer.zip   ${HXBUILDS_S3ADDR}/haxe/windows64-installer/haxe_latest.zip
          aws s3 cp win32Binaries/*_bin.zip         ${HXBUILDS_S3ADDR}/haxe/windows/haxe_latest.zip
          aws s3 cp win32Binaries/*_installer.zip   ${HXBUILDS_S3ADDR}/haxe/windows-installer/haxe_latest.zip

          # Chocolatey packages have to be named with version number,
          # so let's use web redirection to keep the original file name.
          [[ "$HXBUILDS_S3ADDR" =~ s3://([^/]+)(.*) ]] && HXBUILDS_S3BUCKET="${BASH_REMATCH[1]}" && HXBUILDS_S3PATH="${BASH_REMATCH[2]}"
          [[ `echo win64Binaries/*.nupkg` =~ win64Binaries/(.+) ]] && FILE_NAME="${BASH_REMATCH[1]}"
          aws s3 cp ${HXBUILDS_S3ADDR}/haxe/windows64-choco/${FILE_NAME} ${HXBUILDS_S3ADDR}/haxe/windows64-choco/haxe_latest.nupkg --acl public-read --website-redirect "${HXBUILDS_S3PATH}/haxe/windows64-choco/${FILE_NAME}"
          [[ `echo win32Binaries/*.nupkg` =~ win32Binaries/(.+) ]] && FILE_NAME="${BASH_REMATCH[1]}"
          aws s3 cp ${HXBUILDS_S3ADDR}/haxe/windows-choco/${FILE_NAME}   ${HXBUILDS_S3ADDR}/haxe/windows-choco/haxe_latest.nupkg   --acl public-read --website-redirect "${HXBUILDS_S3PATH}/haxe/windows-choco/${FILE_NAME}"

  deploy_apidoc:
    if: success() && github.repository_owner == 'HaxeFoundation' && github.event_name != 'pull_request'
    needs: [linux-test, linux-arm64, mac-test, windows-test, windows64-test]
    runs-on: ubuntu-20.04
    steps:
      - name: Install dependencies
        run: |
          set -ex
          sudo apt-get install -qqy libc6

      - name: Download Haxe
        uses: actions/download-artifact@v3
        with:
          name: linuxBinaries
          path: linuxBinaries

      - name: Setup Haxe
        run: |
          set -ex
          tar -xf linuxBinaries/*_bin.tar.gz -C linuxBinaries --strip-components=1
          sudo mkdir -p /usr/local/bin/
          sudo mkdir -p /usr/local/share/haxe/
          sudo ln -s `pwd`/linuxBinaries/haxe /usr/local/bin/haxe
          sudo ln -s `pwd`/linuxBinaries/haxelib /usr/local/bin/haxelib
          sudo ln -s `pwd`/linuxBinaries/std /usr/local/share/haxe/std

      - name: Download xmldoc artifact
        uses: actions/download-artifact@v3
        with:
          name: xmldoc
          path: xmldoc

      - name: Deploy to api.haxe.org
        env:
          GHP_EMAIL: haxe-ci@onthewings.net
          GHP_USERNAME: Haxe CI Bot
          GHP_REMOTE: ${{ secrets.GHP_REMOTE }}
        run: |
          set -ex
          LOCAL="`pwd`/extra/api.haxe.org"
          git clone "${GHP_REMOTE}" "${LOCAL}"
          haxe --cwd "${LOCAL}" --run ImportXml "`pwd`/xmldoc"
