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:
parent
e148c76fbe
commit
cceebef784
6 changed files with 130 additions and 264 deletions
141
inc/nudsl.inc.sh
141
inc/nudsl.inc.sh
|
|
@ -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;
|
||||
}
|
||||
|
|
@ -17,8 +17,8 @@
|
|||
## #Usage
|
||||
##
|
||||
## Basic usage of nux.cfg is really simple:
|
||||
## - use *nux.cfg.read* function to read configuration value
|
||||
## - use *nux.cfg.write* to write configuration value.
|
||||
## - use *:read* function to read configuration value
|
||||
## - use *:write* to write configuration value.
|
||||
##
|
||||
## 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.
|
||||
|
|
@ -31,13 +31,14 @@ nux_cfg_config_file=config.yaml
|
|||
## #Public functions:
|
||||
##
|
||||
|
||||
@namespace nux.cfg. {
|
||||
|
||||
## nux.cfg.read:: [<store>] <path>
|
||||
## :read:: [<store>] <path>
|
||||
## Reads configuration value stored at *path* from specified *store*.
|
||||
## If no store is specified returns first found value in all stores
|
||||
## in following order *local, global, dist*.
|
||||
##
|
||||
function nux.cfg.read {
|
||||
function :read {
|
||||
nux.log trace "Reading configuration $@"
|
||||
maybe_store="$1";
|
||||
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.
|
||||
## If configuration file does not exists, it creates it.
|
||||
##
|
||||
function nux.cfg.write {
|
||||
store="$1";
|
||||
nux.log trace "Store is: $1";
|
||||
function :write store {
|
||||
nux.log trace "Store is: $store";
|
||||
case $store in
|
||||
global ) ;;
|
||||
local ) ;;
|
||||
|
|
@ -75,55 +75,55 @@ function nux.cfg.write {
|
|||
nux.fatal "Write to dist store is disabled."
|
||||
;;
|
||||
* )
|
||||
nux.fatal "Unknown config store $1".
|
||||
nux.fatal "Unknown config store $store".
|
||||
;;
|
||||
esac
|
||||
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
|
||||
## by library for customization of path format.
|
||||
##
|
||||
function nux.cfg.dir.global {
|
||||
function :dir.global {
|
||||
echo "$HOME/.config/$NUX_APP_NAME"
|
||||
}
|
||||
## nux.cfg.dir.dist::
|
||||
## :dir.dist::
|
||||
## Returns path of *dist* config directory. SHOULD be overriden
|
||||
## by library for customization of path format.
|
||||
|
||||
function nux.cfg.dir.dist {
|
||||
function :dir.dist {
|
||||
echo "$NUX_APP_DIR/config/$NUX_APP_NAME"
|
||||
}
|
||||
## nux.cfg.dir.local::
|
||||
## :dir.local::
|
||||
## Returns path of *local* config directory. SHOULD be overriden
|
||||
## by library for customization of path format.
|
||||
##
|
||||
function nux.cfg.dir.local {
|
||||
nux.cfg.dir.global
|
||||
function :dir.local {
|
||||
:dir.global
|
||||
}
|
||||
|
||||
## nux.cfg.file.global::
|
||||
## nux.cfg.file.local::
|
||||
## nux.cfg.file.dist::
|
||||
## :file.global::
|
||||
## :file.local::
|
||||
## :file.dist::
|
||||
## Returns path of *global* config file. Default implementation appends
|
||||
## *config.yaml* to the respective path. May be overriden if main config
|
||||
## file is determined in different way.
|
||||
##
|
||||
function nux.cfg.file.global {
|
||||
function :file.global {
|
||||
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
|
||||
}
|
||||
|
||||
function nux.cfg.file.local {
|
||||
function :file.local {
|
||||
echo $(nux.cfg.dir.local)/$nux_cfg_config_file
|
||||
}
|
||||
|
||||
function nux.cfg.read.direct {
|
||||
function :read.direct {
|
||||
local file="$1";shift
|
||||
nux.log trace "Direct read from $file";
|
||||
local path="$@";
|
||||
|
|
@ -141,9 +141,8 @@ function nux.cfg.read.direct {
|
|||
|
||||
|
||||
|
||||
function nux.cfg.write.direct {
|
||||
file="$1";
|
||||
if ! nux.check.file.exists "$1" ; then
|
||||
function :write.direct file {
|
||||
if ! nux.check.file.exists "$file" ; then
|
||||
mkdir -p "$(dirname "$file")";
|
||||
touch "$file";
|
||||
fi
|
||||
|
|
@ -153,7 +152,7 @@ function nux.cfg.write.direct {
|
|||
|
||||
|
||||
|
||||
function nux.cfg.get.path {
|
||||
function :get.path {
|
||||
nux.log trace "Reading configuration $@"
|
||||
local only_first=""
|
||||
if [ "$1" = "--first" ]; then
|
||||
|
|
@ -183,3 +182,5 @@ function nux.cfg.get.path {
|
|||
fi
|
||||
done
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,11 +1,12 @@
|
|||
## #nux.json NUX Script JSON Helper library
|
||||
##
|
||||
## #Public functions
|
||||
|
||||
@namespace nux.json. {
|
||||
## nux.json.start
|
||||
## Starts operation on new JSON. Resets write operations stack and JSON
|
||||
## source file to empty (useful for creating JSON from scratch.)
|
||||
function nux.json.start {
|
||||
|
||||
function :start {
|
||||
nux_json_opstack=".";
|
||||
nux_json_source="-n";
|
||||
}
|
||||
|
|
@ -19,8 +20,7 @@ function nux.json.start {
|
|||
##
|
||||
## NOTE: Open does not reset operation stack. To reset operation stack
|
||||
## and opened file use *nux.json.start*
|
||||
function nux.json.open {
|
||||
local file="$1"
|
||||
function :open file {
|
||||
if [ -f "$file" ]; then
|
||||
nux_json_source="$file"
|
||||
fi
|
||||
|
|
@ -30,9 +30,8 @@ function nux.json.open {
|
|||
## Reads *path* from currently opened JSON file.
|
||||
## NOTE: Read does not see changes performed by *nux.json.write* unless
|
||||
## these changes were flushed using *nux.json.flush*.
|
||||
function nux.json.read {
|
||||
local path=".$1";
|
||||
jq -r "$path" "$nux_json_source";
|
||||
function :read path {
|
||||
jq -r ".$path" "$nux_json_source";
|
||||
}
|
||||
|
||||
## nux.json.write <path> <value>
|
||||
|
|
@ -40,16 +39,12 @@ function nux.json.read {
|
|||
## immediately, but rather when *nux.json.flush* is invoked.
|
||||
## This allows for batching of operations or opting out of actually
|
||||
## modifying file.
|
||||
function nux.json.write {
|
||||
local path=".$1";
|
||||
local value="$2";
|
||||
nux_json_opstack="${nux_json_opstack} | $path |= \"$value\""
|
||||
function :write path value {
|
||||
nux_json_opstack="${nux_json_opstack} | .$path |= \"$value\""
|
||||
}
|
||||
|
||||
function nux.json.write.raw {
|
||||
local path=".$1";
|
||||
local value="$2";
|
||||
nux_json_opstack="${nux_json_opstack} | $path |= $value"
|
||||
function :write.raw path value {
|
||||
nux_json_opstack="${nux_json_opstack} | .$path |= $value"
|
||||
}
|
||||
|
||||
## nux.json.flush [<target>]
|
||||
|
|
@ -59,8 +54,7 @@ function nux.json.write.raw {
|
|||
## NOTE: Flush does not reset operation stack. To reset
|
||||
## operation stack and opened file use *nux.json.start*
|
||||
##
|
||||
function nux.json.flush {
|
||||
local target="$1"
|
||||
function :flush target {
|
||||
if [ -n "$target" ]; then
|
||||
local write_target="$target";
|
||||
if [ "$nux_json_source" == "$target" ]; then
|
||||
|
|
@ -80,7 +74,7 @@ function nux.json.flush {
|
|||
## *njw* - nux.json.write
|
||||
## *njr* - nux.json.read
|
||||
##
|
||||
function nux.json.shorthands {
|
||||
function :shorthands {
|
||||
function njw {
|
||||
nux.json.write "$@"
|
||||
}
|
||||
|
|
@ -111,3 +105,5 @@ function nux.json.shorthands {
|
|||
## *done*;
|
||||
##
|
||||
##
|
||||
|
||||
}
|
||||
|
|
@ -42,6 +42,5 @@ if [ -t 1 ] {
|
|||
|
||||
readonly NC_error=$NC_Red
|
||||
|
||||
}
|
||||
|
||||
nux.log trace Terminal colors enabled
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,9 @@
|
|||
#!/usr/bin/env nuxsh
|
||||
@namespace nux.help. {
|
||||
|
||||
## nux.help.shelldoc::
|
||||
## Reads input from std-in and perform terminal-based colorization based on
|
||||
## shelldoc format.
|
||||
function :shelldoc {
|
||||
sed -r \
|
||||
-e "s/^## ?(.*)/${NC_White}\1${NC_No}/gI" \
|
||||
|
|
|
|||
|
|
@ -9,7 +9,6 @@ if [ -t 1 ]; then
|
|||
NC_LOG_No=`tput sgr0`
|
||||
fi
|
||||
|
||||
NC_LOG_current=${NC_LOG_current:=3}
|
||||
|
||||
NC_LOG_id_none=0
|
||||
NC_LOG_id_error=1
|
||||
|
|
@ -19,6 +18,10 @@ NC_LOG_id_info=3
|
|||
NC_LOG_id_debug=4
|
||||
NC_LOG_id_trace=5
|
||||
|
||||
NC_LOG_current=${NC_LOG_current:=3}
|
||||
|
||||
|
||||
|
||||
##
|
||||
## NUX Script environment provides basic logging capabilities.
|
||||
##
|
||||
|
|
@ -55,4 +58,8 @@ function nux.log.level {
|
|||
NC_LOG_current=${!level_id}
|
||||
}
|
||||
|
||||
if [ -n "$NUX_LOG_LEVEL" ]; then
|
||||
nux.log.level "$NUX_LOG_LEVEL";
|
||||
fi
|
||||
|
||||
nux.log trace Nux Logger included
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue