about summary refs log tree commit diff
path: root/pkgs/development/interpreters/lua-5/utils.sh
blob: 2365af08dc9cbe17cae8ed79325d8d25235973dc (plain) (blame)
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/bin/bash

declare -gA luaPathsSeen=()

# shellcheck disable=SC2164,SC2041
nix_print() {
  if [ ${NIX_DEBUG:-0} -ge $1 ]; then
    echo "$2"
  fi
}

nix_debug() {
  nix_print 3 "$1"
}

addToLuaSearchPathWithCustomDelimiter() {
  local varName="$1"
  local absPattern="$2"

  # export only if we haven't already got this dir in the search path
  if [[ ${!varName-} == *"$absPattern"* ]]; then return; fi

  # if the path variable has not yet been set, initialize it to ";;"
  # this is a magic value that will be replaced by the default,
  # allowing relative modules to be used even when there are system modules.
  if [[ ! -v "${varName}" ]]; then export "${varName}=;;"; fi

  # export only if the folder contains lua files
  shopt -s globstar

  local adjustedPattern="${absPattern/\?/\*\*\/\*}"
  for _file in $adjustedPattern; do
    export "${varName}=${!varName:+${!varName};}${absPattern}"
    shopt -u globstar
    return;
  done
  shopt -u globstar
}

# used in setup Hooks to load LUA_PATH and LUA_CPATH
# luaEnvHook
luaEnvHook() {
    _addToLuaPath "$1"
}

addToLuaPath() {
  local dir="$1"

  if [ ! -d "$dir" ]; then
    nix_debug "$dir not a directory abort"
    return 0
  fi
  cd "$dir"
  for pattern in @luapathsearchpaths@; do
    addToLuaSearchPathWithCustomDelimiter LUA_PATH "$PWD/$pattern"
  done

  # LUA_CPATH
  for pattern in @luacpathsearchpaths@; do
    addToLuaSearchPathWithCustomDelimiter LUA_CPATH "$PWD/$pattern"
  done
  cd - >/dev/null
}


_addToLuaPath() {
  local dir="$1"

  echo "_addToLuaPath called for dir $dir"

  if [[ ! -d "$dir" ]]; then
    nix_debug "$dir not a directory abort"
    return 0
  fi

# set -x
  # if [ -n "${pythonPathsSeen[$dir]}" ]; then return; fi
  if [[ -n "${luaPathsSeen[$dir]:-}" ]]; then
  # if [ -n "${luaPathsSeen[$dir]}" ]; then
    echo "$dir already parsed"
    return
  fi

  luaPathsSeen["$dir"]=true

  # shellcheck disable=SC2164
  cd "$dir"
  for pattern in @luapathsearchpaths@; do
    addToLuaSearchPathWithCustomDelimiter LUA_PATH "$PWD/$pattern"
  done

  # LUA_CPATH
  for pattern in @luacpathsearchpaths@; do
    addToLuaSearchPathWithCustomDelimiter LUA_CPATH "$PWD/$pattern"
  done

  cd - >/dev/null

  addToSearchPath program_PATH "$dir"/bin

  # Inspect the propagated inputs (if they exist) and recur on them.
  local prop="$dir/nix-support/propagated-build-inputs"
  if [ -e "$prop" ]; then
    local new_path
    for new_path in $(cat $prop); do
        echo "newpath: $new_path"
        _addToLuaPath "$new_path"
    done
  fi

}

# Builds environment variables like LUA_PATH and PATH walking through closure
# of dependencies.
buildLuaPath() {
  local luaPath="$1"
  local path

  echo "BUILD_LUA_PATH"

#   # set -x
#   # Create an empty table of paths (see doc on loadFromPropagatedInputs
#   # for how this is used). Build up the program_PATH and program_LUA_PATH
#   # variables.
  # declare -gA luaPathsSeen=()
#   # shellcheck disable=SC2034
  program_PATH=
  luaPathsSeen["@lua@"]=1
#   addToSearchPath program_PATH @lua@/bin
  for path in $luaPath; do
    _addToLuaPath "$path"
  done
}