Skip to content

Commit

Permalink
Updates from upstream.
Browse files Browse the repository at this point in the history
  • Loading branch information
danfuzz committed Sep 29, 2023
1 parent cbeba9c commit 7c1a3ec
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 41 deletions.
67 changes: 40 additions & 27 deletions scripts/lib/bashy-basics/_setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -62,42 +62,55 @@ function check-json-output-args {

# Converts a JSON array to a Bash array of elements. Stores into the named
# variable. With option `--lenient`, treats a non-array as a single-element
# array.
# array. By default, the set elements are JSON values; with option `--raw`,
# produces raw (unquoted strings, etc.) elements.
function jbash-array {
# Note: Because we use `eval`, local variables are given name prefixes to
# avoid conflicts with the caller.

local _bashy_lenient=0
if [[ $1 == --lenient ]]; then
_bashy_lenient=1
local _bashy_lenient=false
local _bashy_raw=false
while true; do
case "$1" in
--lenient)
_bashy_lenient=true
;;
--raw)
_bashy_raw=true
;;
*)
break
;;
esac
shift
fi
done

local _bashy_name="$1"
local _bashy_value="$2"

# `--output=compact` guarantees an element per line.
if (( _bashy_lenient )); then
_bashy_value="$(
jget --output=compact "${_bashy_value}" \
'if type == "array" then .[] else . end'
)" \
|| return "$?"
else
_bashy_value="$(
jget --output=compact "${_bashy_value}" '
if type == "array" then
.[]
else
"Not an array: \(.)" | halt_error(1)
end
'
)" \
|| return "$?"
fi

# This uses `eval`.
set-array-from-lines "${_bashy_name}" "${_bashy_value}"
eval "$(
jget --output=raw "${_bashy_value}" \
lenient:json="${_bashy_lenient}" \
name="${_bashy_name}" \
raw:json="${_bashy_raw}" \
'
def processOne:
if $raw and (type == "string") then . else tojson end
| " \(@sh)"
;
if $lenient then
if type == "array" then . else [.] end
elif type == "array" then
.
else
"Not an array: \(.)" | halt_error(1)
end
| map(processOne)
| ["\($name)=(", .[], ")"]
| join("\n")
'
)"
}

# Interprets standardized (for this project) JSON "post-processing" arguments.
Expand Down
54 changes: 40 additions & 14 deletions scripts/lib/bashy-node/node-project/find-module-dependencies
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ define-usage $'
${name} [<opt> ...] [--] <module-name>
Transitively finds all module dependencies from the one given, which must
be the name of a module defined in this codebase. Prints out a JSON object
with bindings as follows:
be the name of a module defined under the indicated modules directory.
Prints out a JSON object with bindings as follows:
* `main: string` -- The originally requested module.
* `localDeps: [name, ...]` -- Local module dependencies.
Expand All @@ -27,19 +27,28 @@ define-usage $'
If a dependency cycle is detected, this prints a diagnostic message and
exits with an error.
This tool is opinionated: The modules directories are taken to define
submodules (in the Node sense) under the top-level name `@this`.
**Note:** Exactly one of the two `--modules-*` options must be specified.
--modules-dir=<dir>
Directory containing all source modules. Required. **Note:** Each of the
modules is expected to be defined to have the name `@this/<name>` where
<name> names the directory the module is in.
Path to a directory containing all module sources to be used.
--modules-dirs=<json>
JSON array of one or more paths to directories containing module sources.
If the same-named module exists in more than one modules directory, the
first one listed "wins."
${help}
'

# Want help?
opt-action --call='{ usage; exit }' help/h

# Directory containing all the modules
opt-value --required --var=modulesDir --filter='/./' modules-dir
# Directory or directories containing all the modules.
opt-value --var=modulesDir --filter='/./' modules-dir
opt-value --var=modulesDirsJson --filter='/^\[.*\]$/' modules-dirs
require-exactly-one-arg-of modules-dir modules-dirs

# The module to start at.
positional-arg --required --var=moduleName module-name
Expand All @@ -51,9 +60,16 @@ process-args "$@" || usage --short
# Main script
#

if [[ ! (-d ${modulesDir} && -r ${modulesDir}) ]]; then
error-msg "Not a readable directory: ${modulesDir}"
exit 1
modulesDirs=()
if [[ ${modulesDir} != '' ]]; then
if [[ ! (-d ${modulesDir} && -r ${modulesDir}) ]]; then
error-msg "Not a readable directory: ${modulesDir}"
exit 1
fi
modulesDirs=("${modulesDir}")
else
jbash-array --raw modulesDirs "${modulesDirsJson}" \
|| exit "$?"
fi

# Collect all of the modules referenced by this package, transitively including
Expand All @@ -79,11 +95,21 @@ while true; do

# Reminder: `${var##*/}` removes everything up to the last slash. In this
# case, it's trimming `@this/` off of `oneDep`.
moduleDir="${modulesDir}/${oneDep##*/}"
pkgFile="${moduleDir}/package.json"
oneDepName="${oneDep##*/}"

for moduleDir in "${modulesDirs[@]}"; do
moduleDir="${moduleDir}/${oneDepName}"
pkgFile="${moduleDir}/package.json"

if [[ -r ${pkgFile} ]]; then
break
fi

moduleDir=''
done

if [[ ! -r "${pkgFile}" ]]; then
echo "Not readable: ${pkgFile}" 1>&2
if [[ ${moduleDir} == '' ]]; then
error-msg "Could not find module: ${oneDep}"
exit 1
fi

Expand Down

0 comments on commit 7c1a3ec

Please sign in to comment.