name: V Apps and Modules

on:
  push:
    paths-ignore:
      - '**.md'
      - '**.yml'
      - '!**/v_apps_and_modules_compile_ci.yml'
      - 'examples/**'
      - 'cmd/tools/vrepl.v'
  pull_request:
    paths-ignore:
      - '**.md'
      - '**.yml'
      - '!**/v_apps_and_modules_compile_ci.yml'
      - 'examples/**'
      - 'cmd/tools/vrepl.v'

concurrency:
  group: v_apps-${{ github.workflow }}-${{ github.ref == 'refs/heads/master' && github.sha || github.ref }}
  cancel-in-progress: true

jobs:
  v-apps-compile:
    strategy:
      matrix:
        os: [ubuntu-latest, macos-14]
      fail-fast: false
    runs-on: ${{ matrix.os }}
    timeout-minutes: 121
    steps:
      - uses: actions/checkout@v4

      - name: Build V
        id: build
        run: make && sudo ./v symlink

      - name: Install dependencies
        run: |
          if [ "$RUNNER_OS" == 'Linux' ]; then
            v retry -- sudo apt -qq update
            v retry -- sudo apt -qq install libgc-dev libsodium-dev libssl-dev sqlite3 libsqlite3-dev
            v retry -- sudo apt -qq install libfreetype6-dev libxi-dev libxcursor-dev libgl-dev libxrandr-dev libasound2-dev xfonts-75dpi xfonts-base
            v retry -- sudo apt -qq install sassc libgit2-dev # needed by gitly
          else
            v retry brew install sassc libgit2
          fi

      - name: Build docs generator
        run: |
          v retry -- v install markdown
          v retry -- git clone https://github.com/vlang/docs --branch generator --depth 1
          cd docs
          v .

      - name: Test vtcc
        if: runner.os == 'Linux'
        run: .github/workflows/compile_v_with_vtcc.sh

      - name: Test vsql compilation and examples
        if: ${{ !cancelled() && steps.build.outcome == 'success' }}
        run: |
          echo "Install vsql"
          v retry -- v install elliotchance.vsql
          cd ~/.vmodules/elliotchance/vsql
          echo "Compile vsql"
          make bin/vsql
          echo "Run examples"
          make examples
          echo "Run vsql/connection_test.v"
          v vsql/connection_test.v

      - name: Test discord.v
        if: ${{ !cancelled() && steps.build.outcome == 'success' }}
        run: .github/workflows/compile_discordv.sh
      - name: Build herolib
        if: ${{ !cancelled() && steps.build.outcome == 'success' }}
        run: .github/workflows/compile_herolib.sh
      - name: Build vlang/vab
        if: ${{ !cancelled() && steps.build.outcome == 'success' }}
        run: |
          echo "Install VAB"
          v retry -- v install vab
          echo "Build vab"
          v ~/.vmodules/vab
          echo "Build vab with -g"
          v -g ~/.vmodules/vab

      - name: Build vlang/ved
        if: ${{ !cancelled() && steps.build.outcome == 'success' }}
        run: |
          v retry -- git clone --depth 1 https://github.com/vlang/ved
          cd ved && v -o ved .
          echo "Build"
          v .
          echo "Build with -autofree ..."
          v -autofree .
          echo "Build with -prod ..."
          v -prod .
          cd ..

      - name: Build vlang/pdf
        if: ${{ !cancelled() && steps.build.outcome == 'success' }}
        run: |
          v retry -- v install pdf
          echo "PDF examples should compile"
          v should-compile-all ~/.vmodules/pdf/examples

      - name: Build vlang/libsodium
        if: ${{ !cancelled() && steps.build.outcome == 'success' }}
        run: |
          echo "Install the libsodium wrapper"
          v retry -- v install libsodium
          echo "Test libsodium"
          VJOBS=1 v test ~/.vmodules/libsodium

      - name: Build vlang/coreutils
        if: ${{ !cancelled() && steps.build.outcome == 'success' }}
        run: |
          echo "Clone Coreutils"
          v retry -- git clone --depth 1 https://github.com/vlang/coreutils /tmp/coreutils
          echo "Build Coreutils"
          cd /tmp/coreutils; make

      - name: Build vlang/gitly
        if: ${{ !cancelled() && steps.build.outcome == 'success' }}
        run: |
          echo "Install markdown"
          v retry -- v install markdown
          echo "Install pcre"
          v retry -- v install pcre
          echo "Clone Gitly"
          v retry -- git clone https://github.com/vlang/gitly /tmp/gitly
          echo "Build Gitly"
          v -cc gcc /tmp/gitly
          ## echo "Build Gitly with -autofree"
          ## v -cc gcc -autofree /tmp/gitly
          echo "Compile gitly.css from gitly.scss"
          sassc /tmp/gitly/src/static/css/gitly.scss > /tmp/gitly/src/static/css/gitly.css
          # echo "Run first_run.v"
          # v -cc gcc run /tmp/gitly/tests/first_run.v
          # # /tmp/gitly/gitly -ci_run

      - name: Build V UI examples
        if: ${{ !cancelled() && steps.build.outcome == 'success' }}
        run: |
          v retry -- v install ui
          v -no-parallel ~/.vmodules/ui/examples/rectangles.v
          v -no-parallel ~/.vmodules/ui/examples/users.v
          # v run ~/.vmodules/ui/examples/build_examples.vsh

      - name: Build vlang/v-analyzer
        if: ${{ !cancelled() && steps.build.outcome == 'success' }}
        run: |
          echo "Clone v-analyzer"
          v retry -- git clone --depth=1 --filter=blob:none --recursive --shallow-submodules https://github.com/vlang/v-analyzer /tmp/v-analyzer
          cd /tmp/v-analyzer
          echo "Installing dependencies"
          v install
          echo "Build v-analyzer debug"
          v build.vsh debug
          echo "Build v-analyzer release"
          v build.vsh release

      - name: Format vlang/v-analyzer
        if: ${{ !cancelled() && steps.build.outcome == 'success' }}
        run: |
          cd /tmp/v-analyzer
          set +e
          v fmt -c .
          exit_code=$?
          if [[ $exit_code -ne 0 && $exit_code -ne 5 ]]; then
            # Don't fail if there are only internal errors (exit code 5).
            v fmt -diff .
            exit 1
          fi

      - name: Build vlang/go2v
        if: ${{ !cancelled() && steps.build.outcome == 'success' && matrix.os != 'macos-14' }}
        run: |
          echo "Clone Go2V"
          v retry -- git clone --depth=1 https://github.com/vlang/go2v /tmp/go2v/
          echo "Build Go2V"
          v /tmp/go2v/
          echo "Run Go2V tests"
          VJOBS=1 v test /tmp/go2v/

      - name: Install UI through VPM and make sure its examples compile
        if: ${{ !cancelled() && steps.build.outcome == 'success' }}
        run: |
          echo "Official VPM modules should be installable"
          v retry -- v install ui
          echo "Examples of UI should compile"
          v ~/.vmodules/ui/examples/build_examples.vsh

      - name: Build vlang/adventofcode
        if: ${{ !cancelled() && steps.build.outcome == 'success' }}
        run: |
          echo "Clone the AdventOfCode repo"
          v retry -- git clone --depth 1 https://github.com/vlang/adventofcode  /tmp/adventofcode
          echo "Install dependencies"
          v retry -- v install pcre
          echo "Execute Tests"
          cd /tmp/adventofcode && v run verify.v

      - name: Build vlang/msgpack
        if: ${{ !cancelled() && steps.build.outcome == 'success' }}
        run: |
          echo "Install msgpack"
          v retry -- v install msgpack
          echo "Build msgpack"
          v -shared ~/.vmodules/msgpack/
          echo "Run msgpack tests"
          v test ~/.vmodules/msgpack/
          echo "MessagePack examples should compile"
          v should-compile-all ~/.vmodules/msgpack/examples

      # - name: Build VEX
      #   if: ${{ !cancelled() }} && steps.build.outcome == 'success'
      #   run: |
      #     echo "Install Vex"
      #     v retry -- v install nedpals.vex
      #     echo "Compile all of the Vex examples"
      #     v should-compile-all ~/.vmodules/nedpals/vex/examples
      #     echo "Compile the simple Vex example"
      #     v ~/.vmodules/nedpals/vex/examples/simple_example.v
      #     echo "Run Vex Tests"
      #     v test ~/.vmodules/nedpals/vex

  vpm-site-compile:
    strategy:
      matrix:
        os: [ubuntu-24.04, macos-14]
      fail-fast: false
    runs-on: ${{ matrix.os }}
    steps:
      - uses: actions/checkout@v4
      - name: Build V
        run: make && ./v symlink
      - name: Build vpm
        run: |
          v retry -- v install markdown
          v retry -- git clone --depth 1 https://github.com/vlang/vpm
          cd vpm && v .
