1
1
Fork 0
mirror of https://github.com/tonydamage/nux-env.git synced 2025-12-11 13:24:28 +01:00

Converted nux.cfg nux.json to nuxsh

Signed-off-by: Tony Tkacik <tonydamage@gmail.com>
This commit is contained in:
Tony Tkáčik 2020-01-01 13:16:18 +01:00
parent e148c76fbe
commit cceebef784
6 changed files with 130 additions and 264 deletions

View file

@ -1,141 +0,0 @@
#!/usr/bin/env bash
## nulang - NUX Custom DSL Support Library
##
## # Language Definition
##
## Language is defined in terms of BASH REGEX matches and functions that process
## or execute particular match.
NUDSL_CACHE_SUFFIX=".nudsl.sh"
nudsl.eval() {
$nudsl_eval "$@"
}
nudsl.dsl.func() {
.process.highlight() {
echo "$line";
}
.match._unmatched.highlight() {
echo "${_gen_highlight_unmatched}$line${nc_end}"
}
.gen.parser._unmatched.process() {
nux.exec.or .match._unmatched.$action .process.$action
}
.highlight() {
nudsl.eval _gen_highlight_$1='$nc_'$2
}
.match() {
local type=$1;
local pattern=$2;
shift; shift;
i=0;
local parse_body="";
nudsl.eval _gen_parser_types='"$_gen_parser_types '$type'"'
nudsl.eval _gen_parser_pattern_$type="'"$pattern"'"
nudsl.eval """.gen.parser.$type.process() {
$(
for group in "$@"; do
let i=$i+1;
if [ "$group" != "-" ]; then
echo local ${group}='${BASH_REMATCH['$i']}'
fi
done
)
nux.exec.or .match.$type.\$action .process.\$action
}
"""
nudsl.eval """.match.$type.highlight() {
$(
for group in "$@"; do
let i=$i+1;
if [ "$group" != "-" ]; then
echo ' echo -n "${_gen_highlight_'$group'}$'$group'${nc_end}"'
fi
done
)
echo;
}
"""
}
}
nudsl_eval=eval
nudsl.process() {
local action=$1;
local language=$2;
local file=$3;
(
nudsl.dsl.func
$language
cat "$file" | nudsl.process0 $action)
}
nudsl.exec() {
language="$1";
file="$2";
cached="$file${NUDSL_CACHE_SUFFIX}";
if nudsl.plan "$language" "$file"; then
source "$cached";
fi
}
nudsl.plan.file() {
local language="$1"
local file="$2";
echo "$file${NUDSL_CACHE_SUFFIX}";
}
nudsl.plan() {
local language="$1";
local file="$2";
cached="$(nudsl.plan.file $language $file)";
if [ "$file" -ot "$cached" -a -e "$nudsl_refresh" ]; then
nux.log debug nudsl: $file: No need to recompile.
return;
fi
nux.log debug Needs regeneration, creating new version.
local dirname=$(dirname "$file")
local execution_plan=$(mktemp "$dirname/.nudsl.XXXX")
if (nudsl.process plan "$language" "$file" > "$execution_plan") ; then
mv -f "$execution_plan" "$cached";
else
echo "Plan could not be generated. See errors."
rm "$execution_plan"
return -1;
fi
}
nudsl.process.fail() {
process_failed=true
echo "$linenum:$@" >&2
}
nudsl.process0() {
local _gen_parser_pattern__unmatched='(.*)';
local patterns="$_gen_parser_types _unmatched";
local linenum=0;
while IFS= read -r line ;
do
let linenum=$linenum+1
for t in $patterns; do
local pattern=_gen_parser_pattern_$t
if [[ "$line" =~ ${!pattern} ]]; then
.gen.parser.$t.process
break;
fi
done
if [ -n "$process_failed" ]; then
return -1;
fi
done;
}

View file

@ -17,8 +17,8 @@
## #Usage ## #Usage
## ##
## Basic usage of nux.cfg is really simple: ## Basic usage of nux.cfg is really simple:
## - use *nux.cfg.read* function to read configuration value ## - use *:read* function to read configuration value
## - use *nux.cfg.write* to write configuration value. ## - use *:write* to write configuration value.
## ##
## If you are using *nux-runner* for your scripts, it automaticly add *config* ## If you are using *nux-runner* for your scripts, it automaticly add *config*
## task which can be used by user to read / modify configuration values. ## task which can be used by user to read / modify configuration values.
@ -31,13 +31,14 @@ nux_cfg_config_file=config.yaml
## #Public functions: ## #Public functions:
## ##
@namespace nux.cfg. {
## nux.cfg.read:: [<store>] <path> ## :read:: [<store>] <path>
## Reads configuration value stored at *path* from specified *store*. ## Reads configuration value stored at *path* from specified *store*.
## If no store is specified returns first found value in all stores ## If no store is specified returns first found value in all stores
## in following order *local, global, dist*. ## in following order *local, global, dist*.
## ##
function nux.cfg.read { function :read {
nux.log trace "Reading configuration $@" nux.log trace "Reading configuration $@"
maybe_store="$1"; maybe_store="$1";
local read_from="local global dist" local read_from="local global dist"
@ -61,13 +62,12 @@ function nux.cfg.read {
} }
## nux.cfg.write:: <global | local> <path> <value> ## :write:: <global | local> <path> <value>
## Writes specified value to *global* or *local* store. ## Writes specified value to *global* or *local* store.
## If configuration file does not exists, it creates it. ## If configuration file does not exists, it creates it.
## ##
function nux.cfg.write { function :write store {
store="$1"; nux.log trace "Store is: $store";
nux.log trace "Store is: $1";
case $store in case $store in
global ) ;; global ) ;;
local ) ;; local ) ;;
@ -75,55 +75,55 @@ function nux.cfg.write {
nux.fatal "Write to dist store is disabled." nux.fatal "Write to dist store is disabled."
;; ;;
* ) * )
nux.fatal "Unknown config store $1". nux.fatal "Unknown config store $store".
;; ;;
esac esac
shift; shift;
nux.cfg.write.direct "$(nux.cfg.file.$store)" "$@" :write.direct "$(nux.cfg.file.$store)" "$@"
} }
## nux.cfg.dir.global:: ## :dir.global::
## Returns path of *global* config directory. May be overriden ## Returns path of *global* config directory. May be overriden
## by library for customization of path format. ## by library for customization of path format.
## ##
function nux.cfg.dir.global { function :dir.global {
echo "$HOME/.config/$NUX_APP_NAME" echo "$HOME/.config/$NUX_APP_NAME"
} }
## nux.cfg.dir.dist:: ## :dir.dist::
## Returns path of *dist* config directory. SHOULD be overriden ## Returns path of *dist* config directory. SHOULD be overriden
## by library for customization of path format. ## by library for customization of path format.
function nux.cfg.dir.dist { function :dir.dist {
echo "$NUX_APP_DIR/config/$NUX_APP_NAME" echo "$NUX_APP_DIR/config/$NUX_APP_NAME"
} }
## nux.cfg.dir.local:: ## :dir.local::
## Returns path of *local* config directory. SHOULD be overriden ## Returns path of *local* config directory. SHOULD be overriden
## by library for customization of path format. ## by library for customization of path format.
## ##
function nux.cfg.dir.local { function :dir.local {
nux.cfg.dir.global :dir.global
} }
## nux.cfg.file.global:: ## :file.global::
## nux.cfg.file.local:: ## :file.local::
## nux.cfg.file.dist:: ## :file.dist::
## Returns path of *global* config file. Default implementation appends ## Returns path of *global* config file. Default implementation appends
## *config.yaml* to the respective path. May be overriden if main config ## *config.yaml* to the respective path. May be overriden if main config
## file is determined in different way. ## file is determined in different way.
## ##
function nux.cfg.file.global { function :file.global {
echo $(nux.cfg.dir.global)/$nux_cfg_config_file echo $(nux.cfg.dir.global)/$nux_cfg_config_file
} }
function nux.cfg.file.dist { function :file.dist {
echo $(nux.cfg.dir.dist)/$nux_cfg_config_file echo $(nux.cfg.dir.dist)/$nux_cfg_config_file
} }
function nux.cfg.file.local { function :file.local {
echo $(nux.cfg.dir.local)/$nux_cfg_config_file echo $(nux.cfg.dir.local)/$nux_cfg_config_file
} }
function nux.cfg.read.direct { function :read.direct {
local file="$1";shift local file="$1";shift
nux.log trace "Direct read from $file"; nux.log trace "Direct read from $file";
local path="$@"; local path="$@";
@ -141,9 +141,8 @@ function nux.cfg.read.direct {
function nux.cfg.write.direct { function :write.direct file {
file="$1"; if ! nux.check.file.exists "$file" ; then
if ! nux.check.file.exists "$1" ; then
mkdir -p "$(dirname "$file")"; mkdir -p "$(dirname "$file")";
touch "$file"; touch "$file";
fi fi
@ -153,7 +152,7 @@ function nux.cfg.write.direct {
function nux.cfg.get.path { function :get.path {
nux.log trace "Reading configuration $@" nux.log trace "Reading configuration $@"
local only_first="" local only_first=""
if [ "$1" = "--first" ]; then if [ "$1" = "--first" ]; then
@ -183,3 +182,5 @@ function nux.cfg.get.path {
fi fi
done done
} }
}

View file

@ -1,11 +1,12 @@
## #nux.json NUX Script JSON Helper library ## #nux.json NUX Script JSON Helper library
## ##
## #Public functions ## #Public functions
@namespace nux.json. {
## nux.json.start ## nux.json.start
## Starts operation on new JSON. Resets write operations stack and JSON ## Starts operation on new JSON. Resets write operations stack and JSON
## source file to empty (useful for creating JSON from scratch.) ## source file to empty (useful for creating JSON from scratch.)
function nux.json.start {
function :start {
nux_json_opstack="."; nux_json_opstack=".";
nux_json_source="-n"; nux_json_source="-n";
} }
@ -19,8 +20,7 @@ function nux.json.start {
## ##
## NOTE: Open does not reset operation stack. To reset operation stack ## NOTE: Open does not reset operation stack. To reset operation stack
## and opened file use *nux.json.start* ## and opened file use *nux.json.start*
function nux.json.open { function :open file {
local file="$1"
if [ -f "$file" ]; then if [ -f "$file" ]; then
nux_json_source="$file" nux_json_source="$file"
fi fi
@ -30,9 +30,8 @@ function nux.json.open {
## Reads *path* from currently opened JSON file. ## Reads *path* from currently opened JSON file.
## NOTE: Read does not see changes performed by *nux.json.write* unless ## NOTE: Read does not see changes performed by *nux.json.write* unless
## these changes were flushed using *nux.json.flush*. ## these changes were flushed using *nux.json.flush*.
function nux.json.read { function :read path {
local path=".$1"; jq -r ".$path" "$nux_json_source";
jq -r "$path" "$nux_json_source";
} }
## nux.json.write <path> <value> ## nux.json.write <path> <value>
@ -40,16 +39,12 @@ function nux.json.read {
## immediately, but rather when *nux.json.flush* is invoked. ## immediately, but rather when *nux.json.flush* is invoked.
## This allows for batching of operations or opting out of actually ## This allows for batching of operations or opting out of actually
## modifying file. ## modifying file.
function nux.json.write { function :write path value {
local path=".$1"; nux_json_opstack="${nux_json_opstack} | .$path |= \"$value\""
local value="$2";
nux_json_opstack="${nux_json_opstack} | $path |= \"$value\""
} }
function nux.json.write.raw { function :write.raw path value {
local path=".$1"; nux_json_opstack="${nux_json_opstack} | .$path |= $value"
local value="$2";
nux_json_opstack="${nux_json_opstack} | $path |= $value"
} }
## nux.json.flush [<target>] ## nux.json.flush [<target>]
@ -59,8 +54,7 @@ function nux.json.write.raw {
## NOTE: Flush does not reset operation stack. To reset ## NOTE: Flush does not reset operation stack. To reset
## operation stack and opened file use *nux.json.start* ## operation stack and opened file use *nux.json.start*
## ##
function nux.json.flush { function :flush target {
local target="$1"
if [ -n "$target" ]; then if [ -n "$target" ]; then
local write_target="$target"; local write_target="$target";
if [ "$nux_json_source" == "$target" ]; then if [ "$nux_json_source" == "$target" ]; then
@ -80,7 +74,7 @@ function nux.json.flush {
## *njw* - nux.json.write ## *njw* - nux.json.write
## *njr* - nux.json.read ## *njr* - nux.json.read
## ##
function nux.json.shorthands { function :shorthands {
function njw { function njw {
nux.json.write "$@" nux.json.write "$@"
} }
@ -111,3 +105,5 @@ function nux.json.shorthands {
## *done*; ## *done*;
## ##
## ##
}

View file

@ -42,6 +42,5 @@ if [ -t 1 ] {
readonly NC_error=$NC_Red readonly NC_error=$NC_Red
}
nux.log trace Terminal colors enabled nux.log trace Terminal colors enabled
}

View file

@ -1,5 +1,9 @@
#!/usr/bin/env nuxsh #!/usr/bin/env nuxsh
@namespace nux.help. { @namespace nux.help. {
## nux.help.shelldoc::
## Reads input from std-in and perform terminal-based colorization based on
## shelldoc format.
function :shelldoc { function :shelldoc {
sed -r \ sed -r \
-e "s/^## ?(.*)/${NC_White}\1${NC_No}/gI" \ -e "s/^## ?(.*)/${NC_White}\1${NC_No}/gI" \

View file

@ -9,7 +9,6 @@ if [ -t 1 ]; then
NC_LOG_No=`tput sgr0` NC_LOG_No=`tput sgr0`
fi fi
NC_LOG_current=${NC_LOG_current:=3}
NC_LOG_id_none=0 NC_LOG_id_none=0
NC_LOG_id_error=1 NC_LOG_id_error=1
@ -19,6 +18,10 @@ NC_LOG_id_info=3
NC_LOG_id_debug=4 NC_LOG_id_debug=4
NC_LOG_id_trace=5 NC_LOG_id_trace=5
NC_LOG_current=${NC_LOG_current:=3}
## ##
## NUX Script environment provides basic logging capabilities. ## NUX Script environment provides basic logging capabilities.
## ##
@ -55,4 +58,8 @@ function nux.log.level {
NC_LOG_current=${!level_id} NC_LOG_current=${!level_id}
} }
if [ -n "$NUX_LOG_LEVEL" ]; then
nux.log.level "$NUX_LOG_LEVEL";
fi
nux.log trace Nux Logger included nux.log trace Nux Logger included