|
73 | 73 | # for modules imported within a Bash command script or function: |
74 | 74 | # @go modules --imported |
75 | 75 |
|
76 | | -declare __go_module_name |
77 | | -declare __go_module_file |
78 | | -declare __go_loaded_module |
79 | | -declare __go_loaded_file |
80 | | -declare __go_module_index |
81 | | -declare __go_use_caller |
82 | | -declare __go_use_prev_caller |
83 | | - |
84 | | -# Wrapper function necessary due to BASH_SOURCE and FUNCNAME bug in Bash <4.3. |
85 | | -_@go.set_use_caller() { |
86 | | - __go_use_caller="${BASH_SOURCE[2]}:${BASH_LINENO[1]} ${FUNCNAME[2]}" |
87 | | -} |
88 | | - |
89 | 76 | _@go.find_plugin_module() { |
90 | 77 | [[ -f "$1/$__go_module_file" ]] && __go_module_file="$1/$__go_module_file" |
91 | 78 | } |
92 | 79 |
|
93 | 80 | _@go.find_module() { |
94 | | - # If a script imports a plugin module, and that module (`__go_use_caller`) |
95 | | - # tries to import another module from the same plugin, this block will adjust |
96 | | - # the search parameters accordingly. |
97 | | - if [[ -n "$_GO_PLUGINS_DIR" && |
98 | | - "$__go_use_caller" =~ ^$_GO_PLUGINS_DIR/.*/lib/ ]]; then |
99 | | - local _GO_SCRIPTS_DIR="${__go_use_caller%/lib/*}/bin" |
100 | | - local _GO_ROOTDIR="${_GO_SCRIPTS_DIR%/bin}" |
| 81 | + if [[ -n "$_GO_INJECT_MODULE_PATH" ]]; then |
| 82 | + __go_module_file="$_GO_INJECT_MODULE_PATH/$__go_module_name" |
| 83 | + if [[ -f "$__go_module_file" ]]; then |
| 84 | + return |
| 85 | + fi |
101 | 86 | fi |
102 | | - __go_module_file="$_GO_SCRIPTS_DIR/lib/$__go_module_name" |
| 87 | + __go_module_file="$_GO_CORE_DIR/lib/$__go_module_name" |
103 | 88 |
|
104 | 89 | if [[ ! -f "$__go_module_file" ]]; then |
105 | | - __go_module_file="$_GO_ROOTDIR/lib/$__go_module_name" |
| 90 | + __go_module_file="$_GO_SCRIPTS_DIR/lib/$__go_module_name" |
106 | 91 |
|
107 | 92 | if [[ ! -f "$__go_module_file" ]]; then |
108 | | - # Convert <plugin>/<module> to plugins/<plugin>/lib/<module> |
109 | | - __go_module_file="${__go_module_name/\///lib/}" |
110 | | - @go.search_plugins '_@go.find_plugin_module' |
| 93 | + __go_module_file="$_GO_ROOTDIR/lib/$__go_module_name" |
| 94 | + |
| 95 | + if [[ ! -f "$__go_module_file" ]]; then |
| 96 | + # Convert <plugin>/<module> to plugins/<plugin>/lib/<module> |
| 97 | + __go_module_file="${__go_module_name/\///lib/}" |
| 98 | + @go.search_plugins '_@go.find_plugin_module' |
| 99 | + fi |
111 | 100 | fi |
112 | 101 | fi |
113 | 102 | } |
114 | 103 |
|
115 | | -for __go_module_name in "$@"; do |
116 | | - # Since every import is in the same scope, setting the caller each time is |
117 | | - # necessary in case any imported modules import other modules. |
118 | | - _@go.set_use_caller |
119 | | - __go_module_file="$_GO_CORE_DIR/lib/$__go_module_name" |
120 | | - |
121 | | - if [[ -n "$_GO_INJECT_MODULE_PATH" ]]; then |
122 | | - __go_module_file="$_GO_INJECT_MODULE_PATH/$__go_module_name" |
123 | | - if [[ ! -f "$__go_module_file" ]]; then |
124 | | - __go_module_file="$_GO_CORE_DIR/lib/$__go_module_name" |
125 | | - fi |
| 104 | +_@go.import_module() { |
| 105 | + if [[ "${__go_module_file#$_GO_SCRIPTS_DIR}" =~ /plugins/[^/]+/lib/ ]]; then |
| 106 | + local _GO_SCRIPTS_DIR="${__go_module_file%/lib/*}/bin" |
| 107 | + local _GO_ROOTDIR="${_GO_SCRIPTS_DIR%/bin}" |
| 108 | + local _GO_PLUGINS_PATHS=() |
| 109 | + local _GO_SEARCH_PATHS=() |
126 | 110 | fi |
127 | 111 |
|
128 | | - if [[ ! -f "$__go_module_file" ]] && ! _@go.find_module; then |
129 | | - @go.printf 'ERROR: Module %s not found at:\n' "$__go_module_name" >&2 |
130 | | - @go.print_stack_trace 1 >&2 |
131 | | - exit 1 |
132 | | - fi |
| 112 | + # Prevent self- and circular importing by registering info before sourcing. |
| 113 | + _GO_IMPORTED_MODULES+=("$__go_module_name") |
| 114 | + _GO_IMPORTED_MODULE_FILES+=("$__go_module_file") |
| 115 | + _GO_IMPORTED_MODULE_CALLERS+=("$current_caller") |
| 116 | + . "$__go_module_file" |
| 117 | +} |
133 | 118 |
|
134 | | - # If we found the module in our project, but we're installed as a plugin, |
135 | | - # change the loaded module name to reflect that. |
136 | | - # |
137 | | - # Using `##` to trim the string instead of `#` flattens the module name |
138 | | - # namespace. We could use `#` to keep it hierarchical, but since the Bash |
139 | | - # namespace itself is flat, this might lead to hard-to-debug collisions if |
140 | | - # functions and variables get redefined. Keeping a flat module name namespace |
141 | | - # allows us to detect such potential collisions and issue a warning below. |
142 | | - if [[ -n "$_GO_PLUGINS_DIR" && |
143 | | - "$__go_module_file" =~ ^$_GO_PLUGINS_DIR ]]; then |
144 | | - __go_module_name="${__go_module_file##*/plugins/}" |
145 | | - __go_module_name="${__go_module_name/\/bin\///}" |
146 | | - __go_module_name="${__go_module_name/\/lib\///}" |
147 | | - fi |
| 119 | +_@go.use_modules() { |
| 120 | + local __go_module_name |
| 121 | + local __go_module_file |
| 122 | + local loaded_module |
| 123 | + local loaded_file |
| 124 | + local module_index |
| 125 | + local current_caller="${BASH_SOURCE[2]}:${BASH_LINENO[1]} ${FUNCNAME[2]}" |
| 126 | + local prev_caller |
148 | 127 |
|
149 | | - __go_module_index=0 |
150 | | - for __go_loaded_module in "${_GO_IMPORTED_MODULES[@]}"; do |
151 | | - if [[ "$__go_module_name" == "$__go_loaded_module" ]]; then |
152 | | - __go_loaded_file="${_GO_IMPORTED_MODULE_FILES[$__go_module_index]}" |
153 | | - __go_use_prev_caller="${_GO_IMPORTED_MODULE_CALLERS[$__go_module_index]}" |
| 128 | + for __go_module_name in "$@"; do |
| 129 | + if ! _@go.find_module; then |
| 130 | + @go.printf 'ERROR: Module %s not found at:\n' "$__go_module_name" >&2 |
| 131 | + @go.print_stack_trace 1 >&2 |
| 132 | + exit 1 |
| 133 | + fi |
154 | 134 |
|
155 | | - # This may happen if a plugin appears more than once in a project tree. |
156 | | - if [[ "$__go_module_file" != "$__go_loaded_file" ]]; then |
157 | | - @go.printf '%s\n' "WARNING: Module: $__go_module_name" \ |
158 | | - "imported at: $__go_use_caller" \ |
159 | | - "from file: $__go_module_file" \ |
160 | | - "previously imported at: $__go_use_prev_caller" \ |
161 | | - "from file: $__go_loaded_file" >&2 |
162 | | - fi |
163 | | - continue 2 |
| 135 | + # If we found the module in our project, but we're installed as a plugin, |
| 136 | + # change the loaded module name to reflect that. |
| 137 | + # |
| 138 | + # Using `##` to trim the string instead of `#` flattens the module name |
| 139 | + # namespace. We could use `#` to keep it hierarchical, but since the Bash |
| 140 | + # namespace itself is flat, this might lead to hard-to-debug collisions if |
| 141 | + # functions and variables get redefined. Keeping a flat module name |
| 142 | + # namespace allows us to detect such potential collisions and issue a |
| 143 | + # warning below. |
| 144 | + if [[ "$__go_module_file" =~ ^$_GO_PLUGINS_DIR/ ]]; then |
| 145 | + __go_module_name="${__go_module_file##*/plugins/}" |
| 146 | + __go_module_name="${__go_module_name/\/bin\///}" |
| 147 | + __go_module_name="${__go_module_name/\/lib\///}" |
164 | 148 | fi |
165 | | - ((++__go_module_index)) |
166 | | - done |
167 | 149 |
|
168 | | - # Prevent self- and circular importing by registering info before sourcing. |
169 | | - _GO_IMPORTED_MODULES+=("$__go_module_name") |
170 | | - _GO_IMPORTED_MODULE_FILES+=("$__go_module_file") |
171 | | - _GO_IMPORTED_MODULE_CALLERS+=("$__go_use_caller") |
| 150 | + module_index=0 |
| 151 | + for loaded_module in "${_GO_IMPORTED_MODULES[@]}"; do |
| 152 | + if [[ "$__go_module_name" == "$loaded_module" ]]; then |
| 153 | + loaded_file="${_GO_IMPORTED_MODULE_FILES[$module_index]}" |
| 154 | + prev_caller="${_GO_IMPORTED_MODULE_CALLERS[$module_index]}" |
172 | 155 |
|
173 | | - if ! . "$__go_module_file"; then |
174 | | - @go.printf 'ERROR: Failed to import %s module from %s at:\n' \ |
175 | | - "$__go_module_name" "$__go_module_file" >&2 |
176 | | - @go.print_stack_trace 1 >&2 |
177 | | - exit 1 |
178 | | - fi |
179 | | -done |
| 156 | + # This may happen if a plugin appears more than once in a project tree. |
| 157 | + if [[ "$__go_module_file" != "$loaded_file" ]]; then |
| 158 | + @go.printf '%s\n' "WARNING: Module: $__go_module_name" \ |
| 159 | + "imported at: $current_caller" \ |
| 160 | + "from file: $__go_module_file" \ |
| 161 | + "previously imported at: $prev_caller" \ |
| 162 | + "from file: $loaded_file" >&2 |
| 163 | + fi |
| 164 | + continue 2 |
| 165 | + fi |
| 166 | + ((++module_index)) |
| 167 | + done |
| 168 | + |
| 169 | + if ! _@go.import_module; then |
| 170 | + @go.printf 'ERROR: Failed to import %s module from %s at:\n' \ |
| 171 | + "$__go_module_name" "$__go_module_file" >&2 |
| 172 | + @go.print_stack_trace 1 >&2 |
| 173 | + exit 1 |
| 174 | + fi |
| 175 | + done |
| 176 | +} |
180 | 177 |
|
181 | | -unset __go_use_prev_caller |
182 | | -unset __go_use_caller |
183 | | -unset __go_module_index |
184 | | -unset __go_loaded_file |
185 | | -unset __go_loaded_module |
186 | | -unset __go_module_file |
187 | | -unset __go_module_name |
| 178 | +_@go.use_modules "$@" |
0 commit comments