Skip to content

Commit

Permalink
Add --runner-versions[].
Browse files Browse the repository at this point in the history
  • Loading branch information
danfuzz committed Feb 2, 2024
1 parent c819cce commit 744262c
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 15 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ Breaking changes:
* None.

Other notable changes:
* None.
* `bashy-node`:
* Added `--runner-versions[]` option to `node-project build-main-module`.

### v2.9 -- 2024-01-30

Expand Down
76 changes: 64 additions & 12 deletions scripts/lib/bashy-node/node-project/build-main-module
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ define-usage --with-help $'
--runner-script=<name>
Create a script to run the system in the `out` directory, with the given
name.
--runner-versions[]=<version> ...
What major versions of Node are allowed to run the project. Optional. If
specified, a check is inserted into the runner script to enforce the
restriction.
--unsafely-run-npm-install-scripts
If specified, runs any install-time scripts defined by modules. This is
reasonably considered an unsafe practice (it is a vector for
Expand All @@ -57,6 +61,9 @@ opt-value --var=outDir out
# Name of runner script to add to the output, if any.
opt-value --var=runnerScript runner-script

# Major versions of Node which are allowed to run the project.
opt-multi --var=runnerVersions --filter='/^[0-9]+$/' runner-versions

# Run npm install-time scripts?
opt-toggle --var=runNpmInstallScripts unsafely-run-npm-install-scripts

Expand Down Expand Up @@ -248,16 +255,66 @@ function copy-scripts {
done < <(jget --output=raw0 "${fileList}" '.[]')
}

# Creates the runner script.
function create-runner-script {
local outFile="$1"
shift
local versions=("$@")

local outDir="${outFile%/*}"

mkdir -p "${outDir}" \
|| return "$?"

(
# Available to the template code.
RUNNER_NODE_VERSIONS=("${versions[@]}")

# Because we `eval`, prefix these names to try to avoid conflict.
_bmm_collecting=0 # Are we collecting an embedded script?
_bmm_toEval=() # Lines to evaluate.
_bmm_line='' # Current line.
_bmm_error=0 # Got an error during eval?

while IFS='' read -r _bmm_line || [[ ${_bmm_line} != '' ]]; do
if (( _bmm_collecting )); then
if [[ ${_bmm_line} == '#}#' ]]; then
_bmm_collecting=0
eval "$(printf '%s\n' "${_bmm_toEval[@]}")" \
|| _bmm_error=1
else
_bmm_toEval+=("${_bmm_line}")
fi
elif [[ ${_bmm_line} == '#{#' ]]; then
_bmm_collecting=1
_bmm_toEval=()
else
printf '%s\n' "${_bmm_line}"
fi
done <"$(this-cmd-dir)/runner-script.txt"

if (( _bmm_error )); then
exit 1
fi
) >"${outFile}" 2>&1

if (( $? != 0 )); then
error-msg 'Error during runner script generation!'
return 1
fi

chmod 755 "${outFile}" \
|| return "$?"
}

# Writes a top-level `index.js` for a project.
function make-index-js {
local dir="$1"

progress-msg 'Making `index.js`...'

(
for (( n = 0; n < 10; n++ )); do
echo "// FILE GENERATED BY SCRIPT \`$(this-cmd-name)\`. DO NOT EDIT!"
done
echo "// THIS FILE WAS GENERATED BY SCRIPT \`$(this-cmd-name)\`."
echo ''
echo "import { default as main } from '@this/${mainModule}';"
echo ''
Expand All @@ -266,9 +323,7 @@ function make-index-js {
echo '// `main()` is not ever supposed to return.'
echo $'throw new Error(\'Shouldn\\\'t happen.\');'
echo ''
for (( n = 0; n < 10; n++ )); do
echo "// FILE GENERATED BY SCRIPT \`$(this-cmd-name)\`. DO NOT EDIT!"
done
echo "// THIS FILE WAS GENERATED BY SCRIPT \`$(this-cmd-name)\`."
) > "${dir}/index.js"
}

Expand Down Expand Up @@ -391,12 +446,9 @@ if (( !platformSpecific )); then
fi

if [[ ${runnerScript} != '' ]]; then
progress-msg 'Copying runner script...'
binDir="${outProjectDir}/bin"
true \
&& mkdir -p "${binDir}" \
&& cp "$(this-cmd-dir)/runner-script.txt" "${binDir}/${runnerScript}" \
&& chmod 755 "${binDir}/${runnerScript}" \
progress-msg 'Creating runner script...'
create-runner-script "${outProjectDir}/bin/${runnerScript}" \
"${runnerVersions[@]}" \
|| exit "$?"
fi

Expand Down
14 changes: 12 additions & 2 deletions scripts/lib/bashy-node/node-project/runner-script.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
# Copyright 2022-2024 the Bashy-lib Authors (Dan Bornstein et alia).
# SPDX-License-Identifier: Apache-2.0

# THIS FILE WAS GENERATED BY SCRIPT `build-main-module`.

# Figure out the symlink-resolved program name and directory.
cmdName="$(readlink -f "$0")" || exit "$?"
cmdDir="${cmdName%/*}"
Expand Down Expand Up @@ -122,8 +124,16 @@ function check-dependency {
check-dependency \
'Node' \
'node --version | sed -e "s/^v//"' \
'^(18|19|20|21)\.' \
'18..21' \
#{#
if (( ${#RUNNER_NODE_VERSIONS[@]} == 0 )); then
regex='^[1-9].\.'
desc='10 or later'
else
versionStr="$(tr <<<"${RUNNER_NODE_VERSIONS[*]}" ' ' '|')"
regex="^(${versionStr})\."
fi
printf $' %q \\\n' "${regex}" "${desc}"
#}#
|| exit "$?"

# Notes:
Expand Down

0 comments on commit 744262c

Please sign in to comment.