From e148c76fbe850b05989067d72a94a81ddb11f773 Mon Sep 17 00:00:00 2001 From: Tony Tkacik Date: Wed, 1 Jan 2020 13:12:55 +0100 Subject: [PATCH] nuxsh: Added support for @command keyword and use it Signed-off-by: Tony Tkacik --- bin/mark | 14 +++---- bin/taskie | 33 +++++++---------- bin/tdm-media | 20 +++++----- bin/vfs | 87 ++++++++++++++++++++++++-------------------- inc/nux/nuxsh.inc.sh | 26 +++++++++++-- inc/nuxr.nuxsh.sh | 5 +-- 6 files changed, 103 insertions(+), 82 deletions(-) diff --git a/bin/mark b/bin/mark index a21eeda..33e1cfa 100755 --- a/bin/mark +++ b/bin/mark @@ -1,9 +1,11 @@ #!/usr/bin/env nuxr-nuxsh + nux.use nuxfs MARK_DIR_NAME=.by MARK_PREFIX="" +# FIXME: This should be probably in nux/fs @namespace nuxfs. { function :path.relative base target { realpath -Lms --relative-to="$base" "$target" @@ -33,7 +35,6 @@ MARK_PREFIX="" } } - @namespace mark. { function :dir item { if [ -n "$MARK_DIR" ]; then @@ -54,6 +55,7 @@ MARK_PREFIX="" fi nuxfs.symlink "$item" "$root/$mark" "$name" } + } ## Manages symlinks in closest mark (**.by**) directory, provides functionality to batch create @@ -61,10 +63,9 @@ MARK_PREFIX="" ## ## #Available tasks: @namespace task. { - ## tag:: ## Performs specified task in tag namespace (marks prefixed with **tag/**) - function :tag task { +@command tag task { MARK_PREFIX="tag/" nuxr.run "$task" "$@" } @@ -72,7 +73,7 @@ MARK_PREFIX="" ## this:: [marks...] ## Marks **current folder** with specific markers. ## This creates symlinks in **mark** folder pointing to **current folder**. - function :this mark { +@command this mark { nux.log debug "Args $#" item=$(pwd) mark_root=$(mark.dir "$item") @@ -86,7 +87,7 @@ MARK_PREFIX="" ## multiple:: ## Marks **files** with specific **mark**. ## This creates symlinks for files in **mark** folder. - function :multiple mark { +@command multiple mark { pwd=$(pwd) mark_root=$(mark.dir "$pwd") while [ "$#" -gt 0 ]; do @@ -98,7 +99,7 @@ MARK_PREFIX="" ## display:: [mark] ## Displays path to current mark folder and displays available marks. ## If **mark** is provided list nested marks. - function :display mark { +@command display mark { mark_root=$(mark.dir $pwd) prefix="$MARK_PREFIX" if [ -n "$mark" ]; then @@ -112,4 +113,3 @@ MARK_PREFIX="" done; ) } -} diff --git a/bin/taskie b/bin/taskie index bf8f41d..1dfdadd 100755 --- a/bin/taskie +++ b/bin/taskie @@ -1,4 +1,4 @@ -#!/usr/bin/env nux-runner +#!/usr/bin/env nuxr-nuxsh # FIXME: Check for configuration @@ -12,19 +12,19 @@ nux.use taskie/backend.github nux.use taskie/backend.gogs nux.use taskie/backend.dir -with.backend() { - backendId="$1"; +function with.backend backendId { backend=$(echo $backendId | cut -d: -f1); nux.exec.optional backend.$backend.with; } -endwith.backend() { +function endwith.backend { unset backendId; unset backendEngine; } -task.labels() { + +@command labels { with.backend $(backend.detect); backend.$backend.labels "$@"; endwith.backend; @@ -35,7 +35,7 @@ task.labels() { ## Adds issue to the task repository. If issue of same title is ## already known locally this fails. ## -task.add() { +@command add { with.backend $(backend.detect); if ! backend.$backend.issue.exists "$@" ; then local labels=$(backend.$backend.labels.id) @@ -58,14 +58,15 @@ task.add() { } nuxr.repl.expose cd pwd ls clear -nuxr.repl.prompt() { + +function nuxr.repl.prompt { echo "${nc_green}$NUX_SCRIPTNAME${nc_end}:${nc_blue}$(pwd)${nc_end}> " } ## ## ## -task.list() { +@command list { with.backend $(backend.detect); backend.$backend.issue.list "$@"; endwith.backend; @@ -73,18 +74,18 @@ task.list() { taskie_backends=$(nux.cfg.read backends.preference); -git.origins() { +function git.origins { nux.log debug Closest git parent is $closest_git; if [ -n "$closest_git" ]; then grep -E "url *=" "$closest_git/config" | cut -d= -f2 fi } -task.detect() { +@command detect { backend.detect --all "$@"; } -backend.detect() { +function backend.detect { all=$1; if [ "$all" = "--all" ]; then shift; @@ -101,16 +102,10 @@ backend.detect() { done; } - - - - - - -gogs.config.site() { +function gogs.config.site { nux.cfg.read "gogs.\"$1\"$2"; } -backend.file.detect() { +function backend.file.detect { : } diff --git a/bin/tdm-media b/bin/tdm-media index de51f8d..745f430 100755 --- a/bin/tdm-media +++ b/bin/tdm-media @@ -3,13 +3,12 @@ type ffmpeg > /dev/null 2>&1 && FFMPEG_OR_LIBAV=ffmpeg type avconv > /dev/null 2>&1 && FFMPEG_OR_LIBAV=avconv -type gm /dev/null 2>&1 && NUX_MAGICK=gm +type gm > /dev/null 2>&1 && NUX_MAGICK=gm QUALITY=${QUALITY:=90} -@namespace task. { - function :video.change.container { +@command video.change.container { CONTAINER=$1; shift; echo "Using $FFMPEG_OR_LIBAV for conversion." @@ -20,11 +19,11 @@ QUALITY=${QUALITY:=90} done } - function :nikon-mp4 { +@command function :nikon-mp4 { task.video.change.container mp4 "$@" } - function :downscale TARGET SIZE { +@command downscale TARGET SIZE { local i=0; local count="$#"; mkdir -p $TARGET; @@ -47,11 +46,10 @@ QUALITY=${QUALITY:=90} } - ## to:: - ## - ## Convert image to - ## - function :to target { +## to:: +## Convert image to specified format +## +@command to target { if ! nux.check.function "media.to.$target" ; then echo Target type "$target" is not supported. return -1 @@ -65,7 +63,7 @@ QUALITY=${QUALITY:=90} done } -} + function media.to.jpg { $NUX_MAGICK convert "$1" -quality $QUALITY -auto-orient "$2" diff --git a/bin/vfs b/bin/vfs index 873298f..77ffc2a 100755 --- a/bin/vfs +++ b/bin/vfs @@ -30,35 +30,42 @@ function vfs.path.real file { } -@namespace task. { - - ## list:: ## Lists all **mergerfs** based virtual filesystems managed by **vfs** tool - function :list { - echo "1" - } +@command list { + echo "1" +} ## info:: ## Displays info about current path - function :info { - nux.log debug "VFS mount: ${CURRENT_MOUNT}"; - echo "path:" $CURRENT_MOUNT; - echo "sources:" - for key in "${!CURRENT_SOURCES[@]}"; do - echo " $key: ${CURRENT_SOURCES[$key]}"; - done +@command info { + nux.log debug "VFS mount: ${CURRENT_MOUNT}"; + echo "path:" $CURRENT_MOUNT; + echo "sources:" + for key in "${!CURRENT_SOURCES[@]}"; do + echo " $key: ${CURRENT_SOURCES[$key]}"; + done +} } ## switch:: ## Moves specified **paths** to named **storage** for particular **vfs**. ## This is ideal for marking files as keep or migrating them to remote, ## rather then local. - function :switch storage { + +### +### The switch uses **.vfs.sources** file to determine location of target +### directory and creates necessary directory structures in target directory +### to preserve local rooted path. +### +### FIXME: Switch does not support merging of directories + + + @command switch storage { target="$(vfs.path "$storage")" nux.log debug "Target path $target" for arg_path in "$@" ; do - rooted_path="/$(realpath -Lms --relative-to="$CURRENT_MOUNT" ${arg_path%/})"; + rooted_path="/$(realpath -m --relative-to="$CURRENT_MOUNT" ${arg_path%/})"; target_dir="${target}${rooted_path%/*}" real_file="$(vfs.path.real "$rooted_path")" nux.log trace "Rooted path: $rooted_path"; @@ -73,35 +80,37 @@ function vfs.path.real file { fi done } + ## mount:: [+] -## Creates **mergerfs** mount for specified pairs of storage **name** and -## **path** - function :mount target { - local mount_paths=""; - nux.log debug "MergerFS mount: $target" +## Mounts **mergerfs** on **target**. The mount is merge of specified pairs +## of storage **name** and **path**. +## - source_tempfs="$(mktemp -d)" - for source in "$@" ; do - source_name="${source%%:*}" - source_path="$(realpath "${source#*:}")" - source_path="${source_path%/}" - nux.log debug " Source: $source_name Path: $source_path"; - mount_paths="$mount_paths:${source_path}" - echo "$source_name $source_path" >> "$source_tempfs/.vfs.sources" - done - echo "temp $source_tempfs" >> "$source_tempfs/$VFS_SOURCES_FILE=" +@command mount target { + local mount_paths=""; + nux.log debug "MergerFS mount: $target" - mergerfs_options="$source_tempfs:${mount_paths}" - nux.log debug "MergerFS command:" $mergerfs_options; - mergerfs "$mergerfs_options" "$target" - (cd $target; vfs info ) + source_tempfs="$(mktemp -d)" + for source in "$@" ; do + source_name="${source%%:*}" + source_path="$(realpath "${source#*:}")" + source_path="${source_path%/}" + nux.log debug " Source: $source_name Path: $source_path"; + mount_paths="$mount_paths:${source_path}" + echo "$source_name $source_path" >> "$source_tempfs/.vfs.sources" + done + echo "temp $source_tempfs" >> "$source_tempfs/$VFS_SOURCES_FILE=" + + mergerfs_options="$source_tempfs:${mount_paths}" + nux.log debug "MergerFS command:" $mergerfs_options; + mergerfs "$mergerfs_options" "$target" + (cd $target; vfs info ) } ## unmount:: ## Unmounts target VFS filesystem. - function :unmount target { - fusermount -u "$target" - nuxfs.info "${CURRENT_SOURCES[temp]}" removing temporary metadata. - rm -rf "${CURRENT_SOURCES[temp]}" +@command :unmount target { + fusermount -u "$target" + nuxfs.info "${CURRENT_SOURCES[temp]}" removing temporary metadata. + rm -rf "${CURRENT_SOURCES[temp]}" - } } diff --git a/inc/nux/nuxsh.inc.sh b/inc/nux/nuxsh.inc.sh index 501ffd9..294f893 100644 --- a/inc/nux/nuxsh.inc.sh +++ b/inc/nux/nuxsh.inc.sh @@ -30,13 +30,19 @@ nux.nuxsh.language.def() { .match.line block_end '(\})' \ syntax - .match.line if_start "(if)( +)$prefixed_id( +)$args?( *)(\{)" \ keyword indent2 prefix identifier indent3 args - - - - - indent4 syntax3 + + .match.line task_start "((@command)( +))($identifier)((\()|( *))(($identifier,? *)*)(\))?( *)(\{)" \ + - keyword indent2 identifier - syntax indent3 args - syntax2 indent4 syntax3 + + .match.line function_start "((function)( +))($identifier)((\()|( *))(($identifier,? *)*)(\))?( *)(\{)" \ - keyword indent2 identifier - syntax indent3 args - syntax2 indent4 syntax3 + + .match.line block_start "($identifier)(( +)$args)?( *)(\{)" \ identifier - indent2 args - - - - - indent3 syntax3 @@ -51,12 +57,12 @@ nux.nuxsh.language.def() { .highlight prefix cyan .highlight identifier green - .highlight keyword blue + .highlight keyword cyan .highlight args yellow .highlight comment magenta - .highlight unmatched red + .highlight unmatched white .highlight syntax white .highlight syntax2 white @@ -172,6 +178,20 @@ nux.nuxsh.language.def() { done } + .match.task_start.plan() { + .block.push task + case $identifier in + :*) identifier="task.${identifier#:}";; + *) identifier="task.$identifier" + esac; + echo "${indent}$identifier() {"; + echo "${indent} nux.log trace $identifier: invoked"; + for arg in ${args//,/ }; do + echo "${indent} local $arg="'"$1"'";shift;" + echo "${indent} nux.log trace ' ' arg $arg: "'$'$arg";" + done + } + .block.start.plan() { case $identifier in function) echo "$line";; diff --git a/inc/nuxr.nuxsh.sh b/inc/nuxr.nuxsh.sh index f200a6c..34cbd2c 100644 --- a/inc/nuxr.nuxsh.sh +++ b/inc/nuxr.nuxsh.sh @@ -1,4 +1,3 @@ - nux.use nux/color nux.use nuxr/repl @@ -86,7 +85,7 @@ nux.use nuxr/repl local task_dot=$(tr " " "." <<< "$task") nux.log trace "Trying to figure task documentation location for $task $task_dot" doc_start=$(grep -hn -E "## +($task)::" "$script" | cut -d: -f1) - code_start=$(grep -hn -E "((function +task.$task_dot)|(task.$task_dot *\(\))) +{" "$script" | cut -d: -f1) + code_start=$(grep -hn -E "((@command +:?$task_dot .*)|(function +task.$task_dot)|(task.$task_dot *\(\))) +{" "$script" | cut -d: -f1) nux.log trace "doc_start" $doc_start $code_start if [ -n "$doc_start" -a -n "$code_start" ] { sed -n "$doc_start,$code_start"p "$script" \ @@ -96,6 +95,6 @@ nux.use nuxr/repl return 0 else return -1 - } } + } }