Skip to content

Commit 04804db

Browse files
committed
core, path: Refactor plugin path handling
I realized the `while true` loop from `_@go.run_plugin_command_script` could live comfortably in `_@go.set_search_paths` and handle the top-level `_GO_PLUGINS_PATHS` case as well. Also, I realized that there was no need for `@go` to invoke `_@go.run_plugin_command_script` if the command script in question was from the `_GO_SCRIPTS_DIR` of the current plugin and not in `_GO_SCRIPTS_DIR/plugins`. Plus, there was a bug whereby it'd been presumed that the plugin command path would always reside in the top-level `_GO_SCRIPTS_DIR` of the plugin. Now the plugin's `_GO_SCRIPTS_DIR` is correctly set to the `/bin` parent directory of the command script. I'll add thorough tests for this in a future commit/PR.
1 parent 69312a7 commit 04804db

File tree

2 files changed

+19
-24
lines changed

2 files changed

+19
-24
lines changed

go-core.bash

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ declare _GO_INJECT_MODULE_PATH="$_GO_INJECT_MODULE_PATH"
259259
return 1
260260
fi
261261

262-
if [[ "$__go_cmd_path" =~ ^$_GO_PLUGINS_DIR/ ]]; then
262+
if [[ "${__go_cmd_path#$_GO_SCRIPTS_DIR}" =~ /plugins/ ]]; then
263263
_@go.run_plugin_command_script "$__go_cmd_path" "${__go_argv[@]}"
264264
else
265265
_@go.run_command_script "$__go_cmd_path" "${__go_argv[@]}"
@@ -273,26 +273,10 @@ _@go.source_builtin() {
273273
}
274274

275275
_@go.run_plugin_command_script() {
276-
local _GO_SCRIPTS_DIR="${__go_cmd_path%/*}"
276+
local _GO_SCRIPTS_DIR="${__go_cmd_path%/bin/*}/bin"
277277
local _GO_ROOTDIR="${_GO_SCRIPTS_DIR%/*}"
278-
local _GO_SEARCH_PATHS=()
279278
local _GO_PLUGINS_PATHS=()
280-
local plugins_dir="$_GO_SCRIPTS_DIR/plugins"
281-
local plugin_paths=()
282-
283-
# A plugin's own local plugin paths will appear before inherited ones. If
284-
# there is a version incompatibility issue with other installed plugins, this
285-
# allows a plugin's preferred version to take precedence.
286-
while true; do
287-
plugin_paths=("$plugins_dir"/*/bin)
288-
if [[ "${plugin_paths[0]}" != "$plugins_dir/*/bin" ]]; then
289-
_GO_PLUGINS_PATHS+=("${plugin_paths[@]}")
290-
fi
291-
if [[ "$plugins_dir" == "$_GO_PLUGINS_DIR" ]]; then
292-
break
293-
fi
294-
plugins_dir="${plugins_dir%/plugins/*}/plugins"
295-
done
279+
local _GO_SEARCH_PATHS=()
296280

297281
_@go.set_search_paths
298282
_@go.run_command_script "$__go_cmd_path" "${__go_argv[@]}"

lib/internal/path

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,29 @@
11
#! /bin/bash
22

33
_@go.set_search_paths() {
4+
local plugins_dir="$_GO_SCRIPTS_DIR/plugins"
5+
local plugins_paths=()
46
local plugin_path
57

68
if [[ -n "$_GO_INJECT_SEARCH_PATH" ]]; then
79
_GO_SEARCH_PATHS+=("$_GO_INJECT_SEARCH_PATH")
810
fi
911
_GO_SEARCH_PATHS+=("$_GO_CORE_DIR/libexec" "$_GO_SCRIPTS_DIR")
1012

13+
# A plugin's own local plugin paths will appear before inherited ones. If
14+
# there is a version incompatibility issue with other installed plugins, this
15+
# allows a plugin's preferred version to take precedence.
16+
while true; do
17+
plugin_paths=("$plugins_dir"/*/bin)
18+
if [[ "${plugin_paths[0]}" != "$plugins_dir/*/bin" ]]; then
19+
_GO_PLUGINS_PATHS+=("${plugin_paths[@]}")
20+
fi
21+
if [[ "$plugins_dir" == "$_GO_PLUGINS_DIR" ]]; then
22+
break
23+
fi
24+
plugins_dir="${plugins_dir%/plugins/*}/plugins"
25+
done
26+
1127
# A plugin's _GO_SCRIPTS_DIR may continue to appear in _GO_PLUGINS_PATHS so
1228
# that it's available to other plugins that depend upon it as a circular
1329
# dependency (though such dependencies are strongly discouraged). However, we
@@ -21,11 +37,6 @@ _@go.set_search_paths() {
2137

2238
if [[ "${#_GO_SEARCH_PATHS[@]}" -eq 0 ]]; then
2339
_GO_PLUGINS_DIR="$_GO_SCRIPTS_DIR/plugins"
24-
_GO_PLUGINS_PATHS=("$_GO_PLUGINS_DIR"/*/bin)
25-
26-
if [[ "${_GO_PLUGINS_PATHS[0]}" == "$_GO_PLUGINS_DIR/*/bin" ]]; then
27-
unset '_GO_PLUGINS_PATHS[0]'
28-
fi
2940
_@go.set_search_paths
3041
fi
3142

0 commit comments

Comments
 (0)