diff --git a/CHANGELOG.md b/CHANGELOG.md index c3bce69c4..39acb69d6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,14 @@ Changelog **Note:** Version numbers for _stable_ (so marked) releases follow semantic versioning principles. Unstable releases do not. +### [Unreleased] + +Breaking changes: +* None. + +Other notable changes: +* Pulled in improved `bashy-lib`, and adjusted accordingly. + ### v0.5.18 -- 2023-11-02 Breaking changes: diff --git a/scripts/lib/bashy-basics/_setup.sh b/scripts/lib/bashy-basics/_setup.sh index ef6681670..4a8356b7e 100644 --- a/scripts/lib/bashy-basics/_setup.sh +++ b/scripts/lib/bashy-basics/_setup.sh @@ -61,6 +61,40 @@ function check-json-output-args { fi } +# Removes from the environment all the variables except for the ones listed. +# Note that it is probably best to only run this in a subshell, e.g. just before +# running code that can't be fully trusted with a fuller set of environment +# variables. +function env-clean { + local allowList=" $* " # Minor cleverness for easier regex matching below. + + local allNames + allNames=($(env-names)) || return "$?" + + local n + for n in "${allNames[@]}"; do + if [[ ! (${allowList} =~ " ${n} ") ]]; then + export -n "${n}" + fi + done +} + +# Runs `env-clean` passing it a reasonably "minimal" set of environment +# variables to keep. +function env-minimize { + env-clean HOME HOSTNAME LANG LOGNAME PATH PWD SHELL SHLVL TERM TMPDIR USER +} + +# Prints a list of the names of all defined environment variables. +function env-names { + # It turns out that the most straightforward way to get a list of + # currently-defined environment variables is arguably to use `awk`. Notably, + # the output of `declare -x` can't be trivially parsed in the case where an + # environment variable contains a newline (perhaps followed by a line that + # mimics the output of `declare -x`). + awk 'BEGIN { for (x in ENVIRON) print x; }' +} + # Interprets standardized (for this project) JSON "post-processing" arguments. # This processes `stdin`. The arguments must start with `::`. After that are # options and arguments just as with `jval`, except `--input` is not accepted. diff --git a/scripts/lib/bashy-node/node-project/build-main-module b/scripts/lib/bashy-node/node-project/build-main-module index fe0389772..52e988bc2 100755 --- a/scripts/lib/bashy-node/node-project/build-main-module +++ b/scripts/lib/bashy-node/node-project/build-main-module @@ -26,12 +26,11 @@ define-usage --with-help $' --allow-platform-specific-files If specified, the build does *not* check to see if any platform-specific files ended up in the built output. - --modules-dir= - Path to a directory containing all module sources to be used. - --modules-dirs= - 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." + --modules-dirs= + --modules-dirs[]= ... + Paths to one or more module source directories, collectively containing + all module sources to be used. If there are same-named modules under more + than one of the given paths, the first-listed path\'s one "wins." --out= Directory where built output goes. Defaults to `out` directly under the main product directory. @@ -46,11 +45,8 @@ define-usage --with-help $' # Allow the build to be platform-specific? opt-toggle --var=platformSpecific allow-platform-specific-files -# Directory or directories containing all the modules. -modulesOpt='' -opt-value --call='{ modulesOpt="--modules-dir=$1" }' --filter='/./' modules-dir -opt-value --call='{ modulesOpt="--modules-dirs=$1" }' --filter='/^\[.*\]$/' modules-dirs -require-exactly-one-arg-of modules-dir modules-dirs +# Paths to all module directories. +opt-multi --required --var=modulesDirs --filter='/./' modules-dirs # Built output directory. opt-value --var=outDir out @@ -71,8 +67,9 @@ process-args "$@" || exit "$?" # The main actions of this script. function build-project { local projectName="$1" - local modulesOpt="$2" - local destDir="$3" + local destDir="$2" + shift 2 + local modulesDirs=("$@") local destLibDir="${destDir}/lib" local mainModule="main-${projectName}" @@ -91,7 +88,9 @@ function build-project { local deps deps="$( - lib . find-module-dependencies "${modulesOpt}" "${mainModule}" + lib . find-module-dependencies \ + --modules-dirs[]="$(vals "${modulesDirs[@]}")" \ + "${mainModule}" )" \ || return "$?" @@ -372,7 +371,7 @@ if [[ ! -d ${outProjectDir} ]]; then || exit "$?" fi -build-project "${projectName}" "${modulesOpt}" "${outProjectDir}" \ +build-project "${projectName}" "${outProjectDir}" "${modulesDirs[@]}" \ || exit "$?" if (( !platformSpecific )); then diff --git a/scripts/lib/bashy-node/node-project/find-module-dependencies b/scripts/lib/bashy-node/node-project/find-module-dependencies index 57ea110ef..60c5c0234 100755 --- a/scripts/lib/bashy-node/node-project/find-module-dependencies +++ b/scripts/lib/bashy-node/node-project/find-module-dependencies @@ -13,9 +13,9 @@ define-usage --with-help $' ${name} [ ...] [--] - Transitively finds all module dependencies from the one given, which must - be the name of a module defined under the indicated modules directory. - Prints out a JSON object with bindings as follows: + Transitively finds all module dependencies from the given , + which must be the name of a module defined under one of the indicated + modules directories. Prints out a JSON object with bindings as follows: * `main: string` -- The originally requested module. * `localDeps: [name, ...]` -- Local module dependencies. @@ -30,20 +30,15 @@ define-usage --with-help $' 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= - Path to a directory containing all module sources to be used. - --modules-dirs= - 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." + --modules-dirs= + --modules-dirs[]= ... + Paths to one or more module source directories, collectively containing + all module sources to be used. If there are same-named modules under more + than one of the given paths, the first-listed path\'s one "wins." ' -# 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 +# Paths to all module directories. +opt-multi --required --var=modulesDirs --filter='/./' modules-dirs # The module to start at. positional-arg --required --var=moduleName module-name @@ -55,18 +50,6 @@ process-args "$@" || exit "$?" # Main script # -modulesDirs=() -if [[ ${modulesDir} != '' ]]; then - if [[ ! (-d ${modulesDir} && -r ${modulesDir}) ]]; then - error-msg "Not a readable directory: ${modulesDir}" - exit 1 - fi - modulesDirs=("${modulesDir}") -else - jset-array --raw modulesDirs "${modulesDirsJson}" \ - || exit "$?" -fi - # Collect all of the modules referenced by this package, transitively including # all referenced local modules. The result is two lists, one of local modules # and one of regular (published via npm) dependencies. This uses a work queue diff --git a/scripts/lib/lactoserv/build b/scripts/lib/lactoserv/build index 00d039a5e..9387bddad 100755 --- a/scripts/lib/lactoserv/build +++ b/scripts/lib/lactoserv/build @@ -60,9 +60,12 @@ srcDir="$(base-dir)/src" etcDir="$(base-dir)/etc" outDir="$(set-up-out-dir)" -lib node-project build-main-module \ - --out="${outDir}" --modules-dir="${srcDir}" lactoserv \ -|| exit "$?" +( + true \ + && env-minimize \ + && lib node-project build-main-module \ + --out="${outDir}" --modules-dirs="${srcDir}" lactoserv \ +) || exit "$?" if (( doDistro )); then progress-msg diff --git a/scripts/lib/lactoserv/lint b/scripts/lib/lactoserv/lint index 631b1edc7..eb114f6b6 100755 --- a/scripts/lib/lactoserv/lint +++ b/scripts/lib/lactoserv/lint @@ -70,7 +70,7 @@ function linter-path { # Build the linter, if necessary. if [[ ! -x ${toolPath} ]]; then lib 1>&2 node-project build-main-module \ - --out="${outDir}" --modules-dir="${srcDir}" "${toolModule}" \ + --out="${outDir}" --modules-dirs="${srcDir}" "${toolModule}" \ || return "$?" fi diff --git a/scripts/lib/lactoserv/run-tests b/scripts/lib/lactoserv/run-tests index c7cd3a21c..2f535d894 100755 --- a/scripts/lib/lactoserv/run-tests +++ b/scripts/lib/lactoserv/run-tests @@ -113,7 +113,7 @@ function tester-path { if [[ ! -x ${toolPath} ]]; then lib 1>&2 node-project build-main-module \ --allow-platform-specific-files \ - --out="${outDir}" --modules-dir="${srcDir}" "${toolModule}" \ + --out="${outDir}" --modules-dirs="${srcDir}" "${toolModule}" \ || return "$?" fi