summaryrefslogtreecommitdiffstats
path: root/pkgs/kde/lib/qmllint-hook.sh
blob: e42dff126edc9d1f4401364231ec9ec341101124 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# shellcheck shell=bash
if [[ -z "${__nix_qmllintHook-}" ]]; then
  __nix_qmllintHook=1 # Don't run this hook more than once.

  qmlHostPathSeen=()
  qmlIncludeDirs=()

  qmlUnseenHostPath() {
      for pkg in "${qmlHostPathSeen[@]}"; do
          if [ "${pkg:?}" == "$1" ]; then
              return 1
          fi
      done

      qtHostPathSeen+=("$1")
      return 0
  }

  qmlHostPathHook() {
      qmlUnseenHostPath "$1" || return 0

      if ! [ -v qtQmlPrefix ]; then
          echo "qmlLintHook: qtQmlPrefix is unset. hint: add qt6.qtbase to buildInputs"
      fi

      local qmlDir="$1/${qtQmlPrefix:?}"
      if [ -d "$qmlDir" ]; then
          qmlIncludeDirs+=("-I" "$qmlDir")
      fi
  }
  addEnvHooks "$targetOffset" qmlHostPathHook

  doQmlLint() {
      LANG=C.UTF-8 @qmllint@ --bare "${qmlIncludeDirs[@]}" -I "${out}/${qtQmlPrefix}" "$@"
  }

  qmlLintCheck() {
      echo "Running qmlLintCheck"

      # intentionally scoped to the default QML prefix, as things in $out/share etc
      # can be used in random weird contexts and will cause spurious errors
      if [ -d "$out/$qtQmlPrefix" ]; then
        find "$out/$qtQmlPrefix" -name '*.qml' | while IFS= read -r i; do
            if [ -n "$(doQmlLint "$i" --json - | @jq@ '.files[] | .warnings[] | select(.id == "import") | select(.message | startswith("Failed to import"))')" ]; then
                echo "qmllint failed for file $i:"

                doQmlLint "$i"
                exit 1
            fi
        done
      fi
  }

  if [ -z "${dontQmlLint-}" ]; then
      postInstallCheckHooks+=('qmlLintCheck')
  fi
fi