diff --git a/inc/nudsl.inc.sh b/inc/nudsl.inc.sh deleted file mode 100644 index 8d2dec1..0000000 --- a/inc/nudsl.inc.sh +++ /dev/null @@ -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; -} diff --git a/inc/nux.cfg.inc.sh b/inc/nux.cfg.nuxsh.sh similarity index 51% rename from inc/nux.cfg.inc.sh rename to inc/nux.cfg.nuxsh.sh index 17beb54..2751744 100644 --- a/inc/nux.cfg.inc.sh +++ b/inc/nux.cfg.nuxsh.sh @@ -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:: [] +## :read:: [] ## 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,125 +62,125 @@ function nux.cfg.read { } -## nux.cfg.write:: +## :write:: ## 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"; - case $store in - global ) ;; - local ) ;; - dist ) - nux.fatal "Write to dist store is disabled." - ;; - * ) - nux.fatal "Unknown config store $1". - ;; - esac - shift; - nux.cfg.write.direct "$(nux.cfg.file.$store)" "$@" -} + function :write store { + nux.log trace "Store is: $store"; + case $store in + global ) ;; + local ) ;; + dist ) + nux.fatal "Write to dist store is disabled." + ;; + * ) + nux.fatal "Unknown config store $store". + ;; + esac + shift; + :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 { - echo "$HOME/.config/$NUX_APP_NAME" -} -## nux.cfg.dir.dist:: + function :dir.global { + echo "$HOME/.config/$NUX_APP_NAME" + } +## :dir.dist:: ## Returns path of *dist* config directory. SHOULD be overriden ## by library for customization of path format. -function nux.cfg.dir.dist { - echo "$NUX_APP_DIR/config/$NUX_APP_NAME" -} -## nux.cfg.dir.local:: + function :dir.dist { + echo "$NUX_APP_DIR/config/$NUX_APP_NAME" + } +## :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 { - echo $(nux.cfg.dir.global)/$nux_cfg_config_file -} + function :file.global { + echo $(nux.cfg.dir.global)/$nux_cfg_config_file + } -function nux.cfg.file.dist { - echo $(nux.cfg.dir.dist)/$nux_cfg_config_file -} + function :file.dist { + echo $(nux.cfg.dir.dist)/$nux_cfg_config_file + } -function nux.cfg.file.local { - echo $(nux.cfg.dir.local)/$nux_cfg_config_file -} + function :file.local { + echo $(nux.cfg.dir.local)/$nux_cfg_config_file + } -function nux.cfg.read.direct { - local file="$1";shift - nux.log trace "Direct read from $file"; - local path="$@"; - if nux.check.file.exists "$file"; then - if [ -n "$path" ]; then - value=$(yaml r "$file" "$path") - if [ "$value" != null ]; then - echo "$value" - fi - else - cat "$file"; - fi - fi -} - - - -function nux.cfg.write.direct { - file="$1"; - if ! nux.check.file.exists "$1" ; then - mkdir -p "$(dirname "$file")"; - touch "$file"; - fi - shift; - yaml w "$file" "$@" -i -} - - - -function nux.cfg.get.path { - nux.log trace "Reading configuration $@" - local only_first="" - if [ "$1" = "--first" ]; then - only_first="$1"; shift; - fi - - maybe_store="$1"; - local read_from="local global dist" - case "$maybe_store" in - global ) ;& - local ) ;& - dist ) - shift; - read_from=$maybe_store - ;; - * ) ;; - esac - nux.log trace "Reading from $read_from" - for store in $read_from ; do - path="$(nux.cfg.dir.$store)/$@" - nux.log trace "Testing $path" - if [ -e "$path" ] ; then - echo $path; - if [ -n "$only_first" ]; then - break; + function :read.direct { + local file="$1";shift + nux.log trace "Direct read from $file"; + local path="$@"; + if nux.check.file.exists "$file"; then + if [ -n "$path" ]; then + value=$(yaml r "$file" "$path") + if [ "$value" != null ]; then + echo "$value" + fi + else + cat "$file"; fi fi - done + } + + + + function :write.direct file { + if ! nux.check.file.exists "$file" ; then + mkdir -p "$(dirname "$file")"; + touch "$file"; + fi + shift; + yaml w "$file" "$@" -i + } + + + + function :get.path { + nux.log trace "Reading configuration $@" + local only_first="" + if [ "$1" = "--first" ]; then + only_first="$1"; shift; + fi + + maybe_store="$1"; + local read_from="local global dist" + case "$maybe_store" in + global ) ;& + local ) ;& + dist ) + shift; + read_from=$maybe_store + ;; + * ) ;; + esac + nux.log trace "Reading from $read_from" + for store in $read_from ; do + path="$(nux.cfg.dir.$store)/$@" + nux.log trace "Testing $path" + if [ -e "$path" ] ; then + echo $path; + if [ -n "$only_first" ]; then + break; + fi + fi + done + } + } diff --git a/inc/nux.json.inc.sh b/inc/nux.json.nuxsh.sh similarity index 85% rename from inc/nux.json.inc.sh rename to inc/nux.json.nuxsh.sh index d9c75a0..31a25ca 100644 --- a/inc/nux.json.inc.sh +++ b/inc/nux.json.nuxsh.sh @@ -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 @@ -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 [] @@ -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*; ## ## + +} diff --git a/inc/nux/color.nuxsh.sh b/inc/nux/color.nuxsh.sh index fa69967..85e2acc 100644 --- a/inc/nux/color.nuxsh.sh +++ b/inc/nux/color.nuxsh.sh @@ -42,6 +42,5 @@ if [ -t 1 ] { readonly NC_error=$NC_Red + nux.log trace Terminal colors enabled } - -nux.log trace Terminal colors enabled diff --git a/inc/nux/help.nuxsh.sh b/inc/nux/help.nuxsh.sh index 963df73..d33ecff 100644 --- a/inc/nux/help.nuxsh.sh +++ b/inc/nux/help.nuxsh.sh @@ -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" \ diff --git a/inc/nux/log.inc.sh b/inc/nux/log.inc.sh index 3e8532c..e462ca8 100644 --- a/inc/nux/log.inc.sh +++ b/inc/nux/log.inc.sh @@ -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