# Workflow for CI tests
name: CI-documentation-and-format

# Controls when the action will run.
on:
  # Triggers the workflow on push or pull request events but only for the master branch
  push:
    branches: [ master, maint_*, release* ]
  pull_request:
    branches: [ master, maint_*, release* ]

# Allows you to run this workflow manually from the Actions tab
#  workflow_dispatch:

defaults:
  run:
    shell: bash
    working-directory: Buildings

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:

  test-formatting:
    # Skip if the commit message contains "ci skip"
    if: "!contains(github.event.head_commit.message, 'ci skip')"

    runs-on: ubuntu-24.04

    steps:
      # Checks-out repository under $GITHUB_WORKSPACE, so job can access it
      - name: Checkout code
        uses: actions/checkout@v3

      - uses: actions/setup-python@v4
        with:
          python-version: '3.10.11' # Version range or exact version of a Python version to use, using SemVer's version range syntax
          architecture: 'x64'      # optional x64 or x86. Defaults to x64 if not specified

      - name: "Install pip"
        run: pip3 install --upgrade pip wheel

      - name: "Install BuildingsPy"
        run: pip3 install git+https://github.com/lbl-srg/BuildingsPy@v5.2.0

      - name: Check for UTF-8 BOM
        run : |
              for ff in `grep -rlI --exclude="ffd.vcxproj" --exclude-dir="\.git" $'^\xEF\xBB\xBF' .`; do
                echo "::error file=${ff}:: Found BOM in ${ff}."
              done;
              test -z "${ff}"

      - name: "Test for old Text annotations (see https://github.com/lbl-srg/modelica-buildings/issues/4456). If it fails, correct library with sed command below."
        run : |
              for FILE in `find . -name '*.mo'`; do
                sed -i -z -E \
                    -e 's/(Text[^)]*)(lineThickness[^,)]*[,]*[[:space:]]*)([^)]*)\)/\1\3)/g' \
                    -e 's/(Text[^)]*)(fillColor[^})]*[},]*[[:space:]]*)([^)]*)\)/\1\3)/g' \
                    -e 's/(Text[^)]*)(fillPattern[^,)]*[,]*[[:space:]]*)([^)]*)\)/\1\3)/g' \
                    -e 's/(Text[^)]*)(pattern[^,)]*[,]*[[:space:]]*)([^)]*)\)/\1\3)/g' \
                    -e 's/(Text\([^)]*),[[:space:]]*\)/\1)/g' \
                    "$FILE"
              done;
              git diff --exit-code --quiet .

      - name: "Test html syntax"
        run : ../bin/runUnitTests.py --validate-html-only

      - name: "Test experiment setup"
        run : ../bin/runUnitTests.py --validate-experiment-setup

      - name: "Test if package.order are correct, and revert order in UsersGuide"
        run : |
              python -c 'import buildingspy.development.refactor as r; r.write_package_order(".", recursive=True)'
              git checkout UsersGuide/ReleaseNotes/package.order
              git checkout UsersGuide/package.order
              git diff --exit-code .

      - name: "Verify files for invalid strings"
        run: ${GITHUB_WORKSPACE}/bin/verifyFiles.py

      - name: "Verify that all OpenModelica scripts are up to date, and no old scripts exist"
        run : |
              rm Resources/Scripts/OpenModelica/compareVars/*.mos
              python -c 'import buildingspy.development.regressiontest as u; u.Tester().writeOpenModelicaResultDictionary()'
              git status --porcelain Resources/Scripts/OpenModelica
              test -z "$(git status --porcelain Resources/Scripts/OpenModelica)"

      - name: "Test user guide"
        run : (cd Resources/Documentation/userGuide; make regressiontest)

      - name: "Test heat pump documentation"
        run : (cd Resources/src/fluid/heatpumps/calibration/doc; make regressiontest)

      - name: "Check for UnitConversion package to be consistent with the script that generates it"
        run : |
              cd ..
              python Buildings/Resources/src/Controls/OBC/UnitConversions/unit_converters.py
              git diff --exit-code Buildings

      - name: Check for file permissions
        run : |
              set -o noglob
              LIST="*.mo *.mos *.fmu *.json *.txt *.jar *.c *.h *.def *.cc *.vcxproj \
                *.so *.exe \
                *.java package-list element-list \
                *.csv *.dat *.TMY *.mat \
                *.cfd *.ffd *.mcr \
                *.rst *.svg *.png *.gif *.jpg *.tiff *.xcf *.ico *.psd *.pdf *.odp \
                *.tex *.md *.html *.css \
                *.bat *.linux *.darwin \
                *.js *.map *.eot *.ttf *.woff *.woff2 *.inv \
                *.idd *.idf *.epw \
                *.dll *.lib \
                *.zip
                package.order Makefile makefile RootMakefile"
              for tt in ${LIST}; do
                for ff in `find . -type f -name ${tt}`; do
                   chmod u=rw,og=r $ff
                done
              done

              LIST="spawn dymola *.sh"
              for tt in ${LIST}; do
                for ff in `find . -type f -name ${tt}`; do
                   chmod u=rwx,og=rx $ff
                done
              done
              git diff --exit-code

              # The Windows compiler generates files with u=rw,og=rw permission
              LIST="*.dll"
              for tt in ${LIST}; do
                for ff in `find . -type f -name ${tt}`; do
                   chmod u=rw,og=rw $ff
                done
              done

  test-checksum:
    # Skip if the commit message contains "ci skip"
    if: "!contains(github.event.head_commit.message, 'ci skip')"

    runs-on: ubuntu-22.04

    steps:
      # Checks-out repository under $GITHUB_WORKSPACE, so job can access it
      - name: Checkout code
        uses: actions/checkout@v3

      - name: Compute the checksum and check that it matches the value from the last commit
        run: ./Resources/Scripts/travis/templates/checkandrun.sh --checksum --skip --diffhead
