mirror of
https://github.com/tonydamage/nux-env.git
synced 2025-12-11 13:24:28 +01:00
Added taskie configuration & other stuff.
Signed-off-by: Tony Tkacik <tonydamage@gmail.com>
This commit is contained in:
parent
6dafaf5b9d
commit
fc1b5bbc87
19 changed files with 744 additions and 7 deletions
141
inc/nudsl.inc.sh
Normal file
141
inc/nudsl.inc.sh
Normal file
|
|
@ -0,0 +1,141 @@
|
|||
#!/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;
|
||||
}
|
||||
68
inc/nudsl/nuxdsl.inc.sh
Normal file
68
inc/nudsl/nuxdsl.inc.sh
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
nux.use nudsl/yanglike
|
||||
|
||||
|
||||
lang.nuxdsl.strict.def() {
|
||||
lang.yanglike.def
|
||||
|
||||
.keyword.plan() {
|
||||
nudsl.process.fail "undefined keyword: $keyword";
|
||||
}
|
||||
.block.start.plan() {
|
||||
nudsl.process.fail "undefined block: $keyword";
|
||||
}
|
||||
.block.end.plan() {
|
||||
echo "${indent}end${keyword}";
|
||||
}
|
||||
|
||||
.match._unmatched.plan() {
|
||||
nudsl.process.fail "invalid syntax: $line";
|
||||
}
|
||||
}
|
||||
|
||||
lang.nuxdsl.def() {
|
||||
lang.nuxdsl.strict.def
|
||||
|
||||
.keyword.plan() {
|
||||
echo "$indent$keyword $args";
|
||||
}
|
||||
.block.start.plan() {
|
||||
.keyword.plan
|
||||
}
|
||||
.block.end.plan() {
|
||||
echo "${indent}end${keyword}";
|
||||
}
|
||||
|
||||
.match._unmatched.plan() {
|
||||
echo "$line";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
.default.plan() {
|
||||
echo "$indent$keyword $args";
|
||||
}
|
||||
.blockend.plan() {
|
||||
echo "${indent}end${keyword}";
|
||||
}
|
||||
|
||||
.block() {
|
||||
eval """
|
||||
.block.$1.start.plan() {
|
||||
.default.plan
|
||||
}
|
||||
.block.$1.end.plan() {
|
||||
.blockend.plan
|
||||
}
|
||||
.keyword.$1.plan() {
|
||||
.default.plan
|
||||
}
|
||||
"""
|
||||
}
|
||||
|
||||
.keyword() {
|
||||
eval """
|
||||
.keyword.$1.plan() {
|
||||
.default.plan
|
||||
}
|
||||
"""
|
||||
}
|
||||
10
inc/nudsl/nuxfs.inc.sh
Normal file
10
inc/nudsl/nuxfs.inc.sh
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
nux.use nudsl/nuxdsl
|
||||
|
||||
lang.nuxfs.def() {
|
||||
lang.nuxdsl.strict.def
|
||||
}
|
||||
|
||||
.block dir
|
||||
.keyword link
|
||||
.keyword git
|
||||
.keyword should-not-exists
|
||||
69
inc/nudsl/yanglike.inc.sh
Normal file
69
inc/nudsl/yanglike.inc.sh
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
#!/usr/bin/env bash
|
||||
|
||||
nux.use nudsl/blocktrac
|
||||
|
||||
## # nudsl/yanglike - Basic skeleton for YANG-like languages
|
||||
|
||||
##
|
||||
## yanglike is barebones support for development of YANG-like languages
|
||||
## with two main concepts: blocks and simple statements.
|
||||
##
|
||||
## The syntax of language is following:
|
||||
##
|
||||
## *statement* = <*block*> | <*simple*>
|
||||
## *block* = <*keyword*> [*arg*]* **{**
|
||||
## [*statement*]*
|
||||
## *}*
|
||||
## *simple* = <*keyword*> [*arg*]*;
|
||||
## *arg* = <*uarg*> | <*darg*> | <*sarg*>
|
||||
## *uarg* - unquoted argument
|
||||
## *sarg* - single quoted argument
|
||||
## *darg* - double quoted argument
|
||||
##
|
||||
## Language support is basic with following restrictions:
|
||||
## - one statement per line
|
||||
|
||||
|
||||
##
|
||||
## lang.yanglike.def ::
|
||||
## Definition of *yanglike* language
|
||||
##
|
||||
lang.yanglike.def() {
|
||||
local comment='(( *)(#.*))?'
|
||||
local whitespace="([ ]*)"
|
||||
local uarg="([^ #\{\}\"'\"'\"';]+)";
|
||||
local sarg="('\"'\"'[^'\"'\"']+'\"'\"')";
|
||||
local darg='("[^"]*")';
|
||||
local args="((($uarg|$darg|$sarg) *)*)";
|
||||
.match.line() {
|
||||
local type="$1";
|
||||
local match="^( *)$2$comment$";
|
||||
shift;shift;
|
||||
.match "$type" "$match" indent "$@" - indent_comment comment;
|
||||
}
|
||||
|
||||
.match.line comment ''
|
||||
.match.line block_end '(\})' \
|
||||
syntax
|
||||
.match.line block_start "([^ ]+)( +)$args( *)(\{)" \
|
||||
keyword indent2 args - - - - - indent3 syntax
|
||||
.match.line keyword "([^ ]+)( +)$args?( *)(;)"\
|
||||
keyword indent2 args - - - - - indent3 syntax
|
||||
|
||||
|
||||
.highlight keyword green
|
||||
.highlight syntax blue
|
||||
.highlight args yellow
|
||||
.highlight unmatched bg_red
|
||||
}
|
||||
|
||||
|
||||
.match.comment.plan() {
|
||||
nux.exec.optional .comment.plan
|
||||
}
|
||||
|
||||
|
||||
.match.keyword.plan() {
|
||||
#nux.log trace "Executing plan for ''$keyword'"
|
||||
nux.exec.or .keyword.$keyword.plan .keyword.plan
|
||||
}
|
||||
124
inc/nux.xml.inc.sh
Normal file
124
inc/nux.xml.inc.sh
Normal file
|
|
@ -0,0 +1,124 @@
|
|||
## #nux.json NUX Script JSON Helper library
|
||||
##
|
||||
## #Public functions
|
||||
|
||||
## nux.xml.start
|
||||
## Starts operation on new JSON. Resets write operations stack and JSON
|
||||
## source file to empty (useful for creating JSON from scratch.)
|
||||
function nux.xml.start {
|
||||
nux_xml_opstack="";
|
||||
nux_xml_source="";
|
||||
nux_xml_root="";
|
||||
}
|
||||
|
||||
## nux.xml.open <file>
|
||||
## Opens JSON File for reading and writing using *nux.xml.open* and
|
||||
## *nux.xml.write* commands.
|
||||
##
|
||||
## In order to work with empty JSON use *-n* as *file*. This allows
|
||||
## for creating new JSON from scratch.
|
||||
##
|
||||
## NOTE: Open does not reset operation stack. To reset operation stack
|
||||
## and opened file use *nux.xml.start*
|
||||
function nux.xml.open {
|
||||
local file="$1"
|
||||
if [ -f "$file" ]; then
|
||||
nux_xml_source="$file"
|
||||
fi
|
||||
}
|
||||
|
||||
## nux.xml.read <path>
|
||||
## Reads *path* from currently opened JSON file.
|
||||
## NOTE: Read does not see changes performed by *nux.xml.write* unless
|
||||
## these changes were flushed using *nux.xml.flush*.
|
||||
function nux.xml.read {
|
||||
local path=".$1";
|
||||
xmlstarlet -r "$path" "$nux_xml_source";
|
||||
}
|
||||
|
||||
## nux.xml.write <path> <value>
|
||||
## Adds write operation to action stack. Writes are not performed
|
||||
## immediately, but rather when *nux.xml.flush* is invoked.
|
||||
## This allows for batching of operations or opting out of actually
|
||||
## modifying file.
|
||||
function nux.xml.write {
|
||||
local path="/${1//\./\/}";
|
||||
if [ -z "$nux_xml_root" ] ; then
|
||||
nux_xml_root=$(cut -d "/" -f2 <<< "$path")
|
||||
nux.log debug "Root is $nux_xml_root";
|
||||
fi
|
||||
nux.log debug "Path to write is: $path";
|
||||
local value="$2";
|
||||
nux_xml_opstack="${nux_xml_opstack} -a $path -t elem -v \"$value\""
|
||||
}
|
||||
|
||||
function nux.xml.write.raw {
|
||||
local path=".$1";
|
||||
local value="$2";
|
||||
nux_xml_opstack="${nux_xml_opstack} | $path |= $value"
|
||||
}
|
||||
|
||||
## nux.xml.flush [<target>]
|
||||
## Flushes any write operations to specified *target* file.
|
||||
## If *target* is not specified JSON is outputted to *STDIN*.
|
||||
##
|
||||
## NOTE: Flush does not reset operation stack. To reset
|
||||
## operation stack and opened file use *nux.xml.start*
|
||||
##
|
||||
function nux.xml.flush {
|
||||
local target="$1"
|
||||
if [ -z "$nux_xml_source" ] ; then
|
||||
nux_xml_source=$(mktemp);
|
||||
echo "<$nux_xml_root></$nux_xml_root>";
|
||||
fi
|
||||
nux.log debug Opstack is: $nux_xml_opstack
|
||||
if [ -n "$target" ]; then
|
||||
local write_target="$target";
|
||||
if [ "$nux_xml_source" == "$target" ]; then
|
||||
write_target=$(mktemp "$(dirname "$target")/tempXXXXXX.json")
|
||||
fi
|
||||
xmlstarlet ed $nux_xml_opstack "$nux_xml_source" > "$write_target"
|
||||
if [ "$nux_xml_source" == "$target" ]; then
|
||||
mv -f "$write_target" "$target"
|
||||
fi
|
||||
else
|
||||
xmlstarlet ed $nux_xml_opstack "$nux_xml_source"
|
||||
fi
|
||||
}
|
||||
|
||||
## nux.xml.shorthands
|
||||
## Exposes shorthands for writing and reading from JSON file.
|
||||
## *njw* - nux.xml.write
|
||||
## *njr* - nux.xml.read
|
||||
##
|
||||
function nux.xml.shorthands {
|
||||
function nxw {
|
||||
nux.xml.write "$@"
|
||||
}
|
||||
function nxr {
|
||||
nux.xml.read "$@"
|
||||
}
|
||||
}
|
||||
|
||||
## # Usage Notes
|
||||
##
|
||||
## ## Operation stack and flush
|
||||
##
|
||||
## As mentioned in documentation for *nux.xml.flush* write stack
|
||||
## is not removed, but rather kept alone, separatly from reference
|
||||
## to open file. This allows for having modification template
|
||||
## which could be executed on multiple files.
|
||||
##
|
||||
## The following example adds meta.author and meta.email
|
||||
## to every JSON in directory.
|
||||
##
|
||||
## *nux.xml.start*
|
||||
## *nux.xml.write* meta.author "Tony Tkacik"
|
||||
## *nux.xml.write* meta.email "example@example.com"
|
||||
## *for* f *in* "*.xml";
|
||||
## *do*
|
||||
## *nux.xml.open* "$f"
|
||||
## *nux.xml.flush* "$f"
|
||||
## *done*;
|
||||
##
|
||||
##
|
||||
|
|
@ -2,8 +2,9 @@
|
|||
##
|
||||
function nux.check.function {
|
||||
nux.log trace "Checking if $1 is function."
|
||||
declare -f "$1" &>/dev/null && return 0
|
||||
test "$(type -t "$1")" = "function" && return 0
|
||||
return 1
|
||||
|
||||
}
|
||||
|
||||
function nux.check.nuxenv.file {
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@ nux.nuxsh.language.def() {
|
|||
|
||||
local args="((($uarg|$darg|$sarg) *)*)";
|
||||
|
||||
local prefixed_id="([^ :]*:)?($identifier)"
|
||||
local prefixed_id="([^= :]*:)?($identifier)"
|
||||
|
||||
.match.line() {
|
||||
local type="$1";
|
||||
|
|
@ -107,7 +107,12 @@ nux.nuxsh.language.def() {
|
|||
.identifier() {
|
||||
if [ -n "$prefix" ]; then
|
||||
local var=_import_prefix_${prefix%:}
|
||||
local prepend=${!var};
|
||||
local prepend;
|
||||
if [ -n "$var" ]; then
|
||||
prepend=${!var};
|
||||
else
|
||||
prepend=$_namespace;
|
||||
fi
|
||||
if [ -z "$prepend" ] ; then
|
||||
nudsl.process.fail "undefined prefix: $prefix";
|
||||
fi
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ nux.use nuxr/repl
|
|||
function :run TASK {
|
||||
if check:function task.$TASK {
|
||||
nux.log debug "Running task: $TASK";
|
||||
nux.log debug "Working dir: $(pwd)"
|
||||
task.$TASK "$@" # Runs task
|
||||
else
|
||||
echo "$NUX_SCRIPTNAME: Unrecognized task '$TASK' not available."
|
||||
|
|
@ -67,7 +68,6 @@ nux.use nuxr/repl
|
|||
topic="$@"
|
||||
topic_dot=$(tr " " "." <<< $topic)
|
||||
nux.log trace "Displaying topic for: '$topic' '$topic_dot'"
|
||||
|
||||
if check:function "task.help.$topic_dot" {
|
||||
shift;
|
||||
task.help.$topic "$@";
|
||||
|
|
|
|||
0
inc/nweb.inc.sh
Normal file
0
inc/nweb.inc.sh
Normal file
Loading…
Add table
Add a link
Reference in a new issue