at master 3.0 kB view raw
1#!/bin/bash 2 3declare -gA luaPathsSeen=() 4 5# shellcheck disable=SC2164,SC2041 6nix_print() { 7 if [ ${NIX_DEBUG:-0} -ge $1 ]; then 8 echo "$2" 9 fi 10} 11 12nix_debug() { 13 nix_print 3 "$1" 14} 15 16addToLuaSearchPathWithCustomDelimiter() { 17 local varName="$1" 18 local absPattern="$2" 19 20 # export only if we haven't already got this dir in the search path 21 if [[ ${!varName-} == *"$absPattern"* ]]; then return; fi 22 23 # if the path variable has not yet been set, initialize it to ";;" 24 # this is a magic value that will be replaced by the default, 25 # allowing relative modules to be used even when there are system modules. 26 if [[ ! -v "${varName}" ]]; then export "${varName}=;;"; fi 27 28 # export only if the folder contains lua files 29 shopt -s globstar 30 31 local adjustedPattern="${absPattern/\?/\*\*\/\*}" 32 for _file in $adjustedPattern; do 33 export "${varName}=${!varName:+${!varName};}${absPattern}" 34 shopt -u globstar 35 return; 36 done 37 shopt -u globstar 38} 39 40# used in setup Hooks to load LUA_PATH and LUA_CPATH 41# luaEnvHook 42luaEnvHook() { 43 _addToLuaPath "$1" 44} 45 46addToLuaPath() { 47 local dir="$1" 48 49 if [ ! -d "$dir" ]; then 50 nix_debug "$dir not a directory abort" 51 return 0 52 fi 53 cd "$dir" 54 for pattern in @luapathsearchpaths@; do 55 addToLuaSearchPathWithCustomDelimiter LUA_PATH "$PWD/$pattern" 56 done 57 58 # LUA_CPATH 59 for pattern in @luacpathsearchpaths@; do 60 addToLuaSearchPathWithCustomDelimiter LUA_CPATH "$PWD/$pattern" 61 done 62 cd - >/dev/null 63} 64 65 66_addToLuaPath() { 67 local dir="$1" 68 69 nix_debug "_addToLuaPath called for dir $dir" 70 71 if [[ ! -d "$dir" ]]; then 72 nix_debug "$dir not a directory abort" 73 return 0 74 fi 75 76# set -x 77 # if [ -n "${pythonPathsSeen[$dir]}" ]; then return; fi 78 if [[ -n "${luaPathsSeen[$dir]:-}" ]]; then 79 # if [ -n "${luaPathsSeen[$dir]}" ]; then 80 nix_debug "$dir already parsed" 81 return 82 fi 83 84 luaPathsSeen["$dir"]=true 85 86 # shellcheck disable=SC2164 87 cd "$dir" 88 for pattern in @luapathsearchpaths@; do 89 addToLuaSearchPathWithCustomDelimiter LUA_PATH "$PWD/$pattern" 90 done 91 92 # LUA_CPATH 93 for pattern in @luacpathsearchpaths@; do 94 addToLuaSearchPathWithCustomDelimiter LUA_CPATH "$PWD/$pattern" 95 done 96 97 cd - >/dev/null 98 99 addToSearchPath program_PATH "$dir"/bin 100 101 # Inspect the propagated inputs (if they exist) and recur on them. 102 local prop="$dir/nix-support/propagated-build-inputs" 103 if [ -e "$prop" ]; then 104 local new_path 105 for new_path in $(cat $prop); do 106 nix_debug "newpath: $new_path" 107 _addToLuaPath "$new_path" 108 done 109 fi 110 111} 112 113# Builds environment variables like LUA_PATH and PATH walking through closure 114# of dependencies. 115buildLuaPath() { 116 local luaPath="$1" 117 local path 118 119 nix_debug "BUILD_LUA_PATH" 120 121# # Create an empty table of paths (see doc on loadFromPropagatedInputs 122# # for how this is used). Build up the program_PATH variable. 123# # shellcheck disable=SC2034 124 program_PATH= 125 for path in $luaPath; do 126 _addToLuaPath "$path" 127 done 128} 129 130