scwrypts/zsh/lib/import.driver.zsh
yage 76a746a53e v3.0.0 "The Great Overhaul"
=====================================================================

Notice the major version change which comes with breaking changes to
2.x! Reconstructs "library" functions for both python and zsh scwrypts,
with changes to virtualenv naming conventions (you'll need to refresh
all virtualenv with the appropriate scwrypt).

--- Changes ------------------------------

- changed a naming convention across zsh scripts, particularly
  removing underscores where there is no need to avoid naming clash
  (e.g. 'zsh/lib/utils/io.zsh' renames '__STATUS' to 'STATUS')

- moved clients reliant on py.lib.http to the py.lib.http module

- python scripts now rely on py.lib.scwrypts.execute

- updated package.json in zx scripts to include `type = module`

- 'scwrypts --list' commandline argument now includes additional
  relevant data for each scwrypt

- environment variables no longer add themselves to be staged in the
  '.env.template'

--- New Features -------------------------

- new 'use' syntax for disjoint import within zsh scripts; took me
  a very long time to convince myself this would be necessary

- introduced scwrypt "groups" to allow portable module creation;
  (i.e. ability add your own scripts from another repo!)

- py.lib.scwrypts.io provides a combined IO stream for quick, hybrid
  use of input/output files and stdin/stdout

- py.lib.fzf provides a wrapper to provide similar functionality to
  zsh/utils/io.zsh including fzf_(head|tail)

- improved efficiency of various scwrypts; notably reducing runtime
  of scwrypts/environment sync

- improved scwrypts CLI by adding new options for exact scwrypt
  matching, better filtering, and prettier/more-detailed interfaces

--- New Scripts --------------------------

- py/twilio )
    basic SMS integration with twilio
     - send-sms

- py/directus )
    interactive directus GET query
     - get-items

- py/discord )
    post message to discord channel or webhook
     - post-message
2023-06-21 20:04:30 -06:00

148 lines
5.1 KiB
Bash

[[ $SCWRYPTS_IMPORT_DRIVER_READY -eq 1 ]] && return 0
###################################################################
# #
# usage: use [OPTIONS ...] zsh/module/path #
# #
###################################################################
# #
# OPTIONS: #
# #
# -g, --group lookup library root from friendly group #
# name (requires configuration) #
# (default: scwrypts) #
# #
# -r, --library-root fully qualified path to a library root #
# #
# --check-environment check environment immediately rather than #
# wait for downstream CHECK_ENVIRONMENT call #
# #
# #
# Allows for import-style library loading in zsh. No matter what #
# scwrypt is run, this function (and required helpers) are *also* #
# loaded, ensuring that 'use' is always available in scwrypts #
# context. #
# #
# #
# Friendly group-names can be configured by setting the variable #
# 'SCWRYPTS_LIBRARY_ROOT__<group-name>' to the fully qualified path #
# to the root directory of the modules library. #
# #
# #
###################################################################
source "${0:a:h}/config.zsh"
use() {
local SCWRYPTS_LIBRARY SCWRYPTS_LIBRARY_ROOT SCWRYPTS_LIBRARY_GROUP
local DEFER_ENVIRONMENT_CHECK=1
while [[ $# -gt 0 ]]
do
case $1 in
-g | --group )
[ $SCWRYPTS_LIBRARY_ROOT ] && ERROR 'specify only one of {(-g), (-r)}'
SCWRYPTS_LIBRARY_GROUP=$2
shift 1
;;
-r | --library-root )
[ $SCWRYPTS_LIBRARY_GROUP ] && ERROR 'specify only one of {(-g), (-r)}'
SCWRYPTS_LIBRARY_ROOT=$2
shift 1
;;
--check-environment )
DEFER_ENVIRONMENT_CHECK=0
;;
* )
[ ! $SCWRYPTS_LIBRARY ] \
&& SCWRYPTS_LIBRARY=$1 \
|| ERROR 'too many arguments; expected exactly 1 argument' \
;;
esac
shift 1
done
[ ! $SCWRYPTS_LIBRARY ] && ERROR 'no library specified for import'
: \
&& [ ! $SCWRYPTS_LIBRARY_GROUP ] \
&& [ ! $SCWRYPTS_LIBRARY_ROOT ] \
&& SCWRYPTS_LIBRARY_GROUP=scwrypts
[ ! $SCWRYPTS_LIBRARY_ROOT ] && SCWRYPTS_LIBRARY_ROOT=$(GET_SCWRYPTS_LIBRARY_ROOT)
[ ! $SCWRYPTS_LIBRARY_ROOT ] && ERROR "unable to determine library root from group name '$SCWRYPTS_LIBRARY_GROUP'"
#####################################################################
local LIBRARY_FILE LIBRARY_FILE_TEMP
[ ! $LIBRARY_FILE ] \
&& LIBRARY_FILE_TEMP="$SCWRYPTS_LIBRARY_ROOT/$SCWRYPTS_LIBRARY.module.zsh" \
&& [ -f "$LIBRARY_FILE_TEMP" ] \
&& LIBRARY_FILE="$LIBRARY_FILE_TEMP"
[ ! $LIBRARY_FILE ] \
&& LIBRARY_FILE_TEMP="$SCWRYPTS_LIBRARY_ROOT/$SCWRYPTS_LIBRARY/$(echo $SCWRYPTS_LIBRARY | sed 's/.*\///').module.zsh" \
&& [ -f "$LIBRARY_FILE_TEMP" ] \
&& LIBRARY_FILE="$LIBRARY_FILE_TEMP" \
[ ! $LIBRARY_FILE ] \
&& ERROR "no such library '$SCWRYPTS_LIBRARY_GROUP/$SCWRYPTS_LIBRARY'"
#####################################################################
CHECK_ERRORS --no-fail || {
((IMPORT_ERRORS+=1))
return 1
}
#####################################################################
IS_LOADED && return 0
source "$LIBRARY_FILE" || {
((IMPORT_ERRORS+=1))
ERROR "import error for '$SCWRYPTS_LIBRARY_GROUP/$SCWRYPTS_LIBRARY'"
return 1
}
[[ $DEFER_ENVIRONMENT_CHECK -eq 0 ]] && {
CHECK_ENVIRONMENT || {
((IMPORT_ERRORS+=1))
ERROR "import error for '$SCWRYPTS_LIBRARY_GROUP/$SCWRYPTS_LIBRARY'"
return 1
}
}
IS_LOADED --set
}
GET_SCWRYPTS_LIBRARY_ROOT() {
eval echo '$SCWRYPTS_LIBRARY_ROOT__'$SCWRYPTS_LIBRARY_GROUP
}
IS_LOADED() {
local VARIABLE_NAME="SCWRYPTS_LIBRARY_LOADED__${SCWRYPTS_LIBRARY_GROUP}__$(echo $SCWRYPTS_LIBRARY | sed 's|[/-]|_|g')"
[[ $1 =~ ^--set$ ]] \
&& eval $VARIABLE_NAME=1 \
[[ $(eval echo '$'$VARIABLE_NAME || echo 0) -eq 1 ]]
}
# temporary definitions for first load
CHECK_ERRORS() { return 0; unset -f CHECK_ERRORS; }
CHECK_ENVIRONMENT() { return 0; unset -f CHECK_ENVIRONMENT; }
ERROR() { echo $@ >&2; exit 1; }
#####################################################################
# ensures that zsh/utils and zsh/scwrypts/meta are always present!
use utils
use scwrypts/meta
#####################################################################
SCWRYPTS_IMPORT_DRIVER_READY=1