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

nuxsh: Added support for @command keyword and use it

Signed-off-by: Tony Tkacik <tonydamage@gmail.com>
This commit is contained in:
Tony Tkáčik 2020-01-01 13:12:55 +01:00
parent 27f3986528
commit e148c76fbe
6 changed files with 103 additions and 82 deletions

View file

@ -1,9 +1,11 @@
#!/usr/bin/env nuxr-nuxsh #!/usr/bin/env nuxr-nuxsh
nux.use nuxfs nux.use nuxfs
MARK_DIR_NAME=.by MARK_DIR_NAME=.by
MARK_PREFIX="" MARK_PREFIX=""
# FIXME: This should be probably in nux/fs
@namespace nuxfs. { @namespace nuxfs. {
function :path.relative base target { function :path.relative base target {
realpath -Lms --relative-to="$base" "$target" realpath -Lms --relative-to="$base" "$target"
@ -33,7 +35,6 @@ MARK_PREFIX=""
} }
} }
@namespace mark. { @namespace mark. {
function :dir item { function :dir item {
if [ -n "$MARK_DIR" ]; then if [ -n "$MARK_DIR" ]; then
@ -54,6 +55,7 @@ MARK_PREFIX=""
fi fi
nuxfs.symlink "$item" "$root/$mark" "$name" nuxfs.symlink "$item" "$root/$mark" "$name"
} }
} }
## Manages symlinks in closest mark (**.by**) directory, provides functionality to batch create ## Manages symlinks in closest mark (**.by**) directory, provides functionality to batch create
@ -61,10 +63,9 @@ MARK_PREFIX=""
## ##
## #Available tasks: ## #Available tasks:
@namespace task. { @namespace task. {
## tag:: <task> <task arguments...> ## tag:: <task> <task arguments...>
## Performs specified task in tag namespace (marks prefixed with **tag/**) ## Performs specified task in tag namespace (marks prefixed with **tag/**)
function :tag task { @command tag task {
MARK_PREFIX="tag/" MARK_PREFIX="tag/"
nuxr.run "$task" "$@" nuxr.run "$task" "$@"
} }
@ -72,7 +73,7 @@ MARK_PREFIX=""
## this:: <mark> [marks...] ## this:: <mark> [marks...]
## Marks **current folder** with specific markers. ## Marks **current folder** with specific markers.
## This creates symlinks in **mark** folder pointing to **current folder**. ## This creates symlinks in **mark** folder pointing to **current folder**.
function :this mark { @command this mark {
nux.log debug "Args $#" nux.log debug "Args $#"
item=$(pwd) item=$(pwd)
mark_root=$(mark.dir "$item") mark_root=$(mark.dir "$item")
@ -86,7 +87,7 @@ MARK_PREFIX=""
## multiple:: <mark> <files...> ## multiple:: <mark> <files...>
## Marks **files** with specific **mark**. ## Marks **files** with specific **mark**.
## This creates symlinks for files in **mark** folder. ## This creates symlinks for files in **mark** folder.
function :multiple mark { @command multiple mark {
pwd=$(pwd) pwd=$(pwd)
mark_root=$(mark.dir "$pwd") mark_root=$(mark.dir "$pwd")
while [ "$#" -gt 0 ]; do while [ "$#" -gt 0 ]; do
@ -98,7 +99,7 @@ MARK_PREFIX=""
## display:: [mark] ## display:: [mark]
## Displays path to current mark folder and displays available marks. ## Displays path to current mark folder and displays available marks.
## If **mark** is provided list nested marks. ## If **mark** is provided list nested marks.
function :display mark { @command display mark {
mark_root=$(mark.dir $pwd) mark_root=$(mark.dir $pwd)
prefix="$MARK_PREFIX" prefix="$MARK_PREFIX"
if [ -n "$mark" ]; then if [ -n "$mark" ]; then
@ -112,4 +113,3 @@ MARK_PREFIX=""
done; done;
) )
} }
}

View file

@ -1,4 +1,4 @@
#!/usr/bin/env nux-runner #!/usr/bin/env nuxr-nuxsh
# FIXME: Check for configuration # FIXME: Check for configuration
@ -12,19 +12,19 @@ nux.use taskie/backend.github
nux.use taskie/backend.gogs nux.use taskie/backend.gogs
nux.use taskie/backend.dir nux.use taskie/backend.dir
with.backend() { function with.backend backendId {
backendId="$1";
backend=$(echo $backendId | cut -d: -f1); backend=$(echo $backendId | cut -d: -f1);
nux.exec.optional backend.$backend.with; nux.exec.optional backend.$backend.with;
} }
endwith.backend() { function endwith.backend {
unset backendId; unset backendId;
unset backendEngine; unset backendEngine;
} }
task.labels() {
@command labels {
with.backend $(backend.detect); with.backend $(backend.detect);
backend.$backend.labels "$@"; backend.$backend.labels "$@";
endwith.backend; endwith.backend;
@ -35,7 +35,7 @@ task.labels() {
## Adds issue to the task repository. If issue of same title is ## Adds issue to the task repository. If issue of same title is
## already known locally this fails. ## already known locally this fails.
## ##
task.add() { @command add {
with.backend $(backend.detect); with.backend $(backend.detect);
if ! backend.$backend.issue.exists "$@" ; then if ! backend.$backend.issue.exists "$@" ; then
local labels=$(backend.$backend.labels.id) local labels=$(backend.$backend.labels.id)
@ -58,14 +58,15 @@ task.add() {
} }
nuxr.repl.expose cd pwd ls clear 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}> " echo "${nc_green}$NUX_SCRIPTNAME${nc_end}:${nc_blue}$(pwd)${nc_end}> "
} }
## ##
## ##
## ##
task.list() { @command list {
with.backend $(backend.detect); with.backend $(backend.detect);
backend.$backend.issue.list "$@"; backend.$backend.issue.list "$@";
endwith.backend; endwith.backend;
@ -73,18 +74,18 @@ task.list() {
taskie_backends=$(nux.cfg.read backends.preference); taskie_backends=$(nux.cfg.read backends.preference);
git.origins() { function git.origins {
nux.log debug Closest git parent is $closest_git; nux.log debug Closest git parent is $closest_git;
if [ -n "$closest_git" ]; then if [ -n "$closest_git" ]; then
grep -E "url *=" "$closest_git/config" | cut -d= -f2 grep -E "url *=" "$closest_git/config" | cut -d= -f2
fi fi
} }
task.detect() { @command detect {
backend.detect --all "$@"; backend.detect --all "$@";
} }
backend.detect() { function backend.detect {
all=$1; all=$1;
if [ "$all" = "--all" ]; then if [ "$all" = "--all" ]; then
shift; shift;
@ -101,16 +102,10 @@ backend.detect() {
done; done;
} }
function gogs.config.site {
gogs.config.site() {
nux.cfg.read "gogs.\"$1\"$2"; nux.cfg.read "gogs.\"$1\"$2";
} }
backend.file.detect() { function backend.file.detect {
: :
} }

View file

@ -3,13 +3,12 @@
type ffmpeg > /dev/null 2>&1 && FFMPEG_OR_LIBAV=ffmpeg type ffmpeg > /dev/null 2>&1 && FFMPEG_OR_LIBAV=ffmpeg
type avconv > /dev/null 2>&1 && FFMPEG_OR_LIBAV=avconv 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} QUALITY=${QUALITY:=90}
@namespace task. {
function :video.change.container { @command video.change.container {
CONTAINER=$1; CONTAINER=$1;
shift; shift;
echo "Using $FFMPEG_OR_LIBAV for conversion." echo "Using $FFMPEG_OR_LIBAV for conversion."
@ -20,11 +19,11 @@ QUALITY=${QUALITY:=90}
done done
} }
function :nikon-mp4 { @command function :nikon-mp4 {
task.video.change.container mp4 "$@" task.video.change.container mp4 "$@"
} }
function :downscale TARGET SIZE { @command downscale TARGET SIZE {
local i=0; local i=0;
local count="$#"; local count="$#";
mkdir -p $TARGET; mkdir -p $TARGET;
@ -47,11 +46,10 @@ QUALITY=${QUALITY:=90}
} }
## to:: <jpg|png> <image...> ## to:: <jpg|png> <image...>
## ## Convert image to specified format
## Convert image to ##
## @command to target {
function :to target {
if ! nux.check.function "media.to.$target" ; then if ! nux.check.function "media.to.$target" ; then
echo Target type "$target" is not supported. echo Target type "$target" is not supported.
return -1 return -1
@ -65,7 +63,7 @@ QUALITY=${QUALITY:=90}
done done
} }
}
function media.to.jpg { function media.to.jpg {
$NUX_MAGICK convert "$1" -quality $QUALITY -auto-orient "$2" $NUX_MAGICK convert "$1" -quality $QUALITY -auto-orient "$2"

87
bin/vfs
View file

@ -30,35 +30,42 @@ function vfs.path.real file {
} }
@namespace task. {
## list:: ## list::
## Lists all **mergerfs** based virtual filesystems managed by **vfs** tool ## Lists all **mergerfs** based virtual filesystems managed by **vfs** tool
function :list { @command list {
echo "1" echo "1"
} }
## info:: ## info::
## Displays info about current path ## Displays info about current path
function :info { @command info {
nux.log debug "VFS mount: ${CURRENT_MOUNT}"; nux.log debug "VFS mount: ${CURRENT_MOUNT}";
echo "path:" $CURRENT_MOUNT; echo "path:" $CURRENT_MOUNT;
echo "sources:" echo "sources:"
for key in "${!CURRENT_SOURCES[@]}"; do for key in "${!CURRENT_SOURCES[@]}"; do
echo " $key: ${CURRENT_SOURCES[$key]}"; echo " $key: ${CURRENT_SOURCES[$key]}";
done done
}
} }
## switch:: <storage> <path+> ## switch:: <storage> <path+>
## Moves specified **paths** to named **storage** for particular **vfs**. ## Moves specified **paths** to named **storage** for particular **vfs**.
## This is ideal for marking files as keep or migrating them to remote, ## This is ideal for marking files as keep or migrating them to remote,
## rather then local. ## 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")" target="$(vfs.path "$storage")"
nux.log debug "Target path $target" nux.log debug "Target path $target"
for arg_path in "$@" ; do 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%/*}" target_dir="${target}${rooted_path%/*}"
real_file="$(vfs.path.real "$rooted_path")" real_file="$(vfs.path.real "$rooted_path")"
nux.log trace "Rooted path: $rooted_path"; nux.log trace "Rooted path: $rooted_path";
@ -73,35 +80,37 @@ function vfs.path.real file {
fi fi
done done
} }
## mount:: <target> <name:path> [<name:path>+] ## mount:: <target> <name:path> [<name:path>+]
## Creates **mergerfs** mount for specified pairs of storage **name** and ## Mounts **mergerfs** on **target**. The mount is merge of specified pairs
## **path** ## of storage **name** and **path**.
function :mount target { ##
local mount_paths="";
nux.log debug "MergerFS mount: $target"
source_tempfs="$(mktemp -d)" @command mount target {
for source in "$@" ; do local mount_paths="";
source_name="${source%%:*}" nux.log debug "MergerFS mount: $target"
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}" source_tempfs="$(mktemp -d)"
nux.log debug "MergerFS command:" $mergerfs_options; for source in "$@" ; do
mergerfs "$mergerfs_options" "$target" source_name="${source%%:*}"
(cd $target; vfs info ) 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:: <target> ## unmount:: <target>
## Unmounts target VFS filesystem. ## Unmounts target VFS filesystem.
function :unmount target { @command :unmount target {
fusermount -u "$target" fusermount -u "$target"
nuxfs.info "${CURRENT_SOURCES[temp]}" removing temporary metadata. nuxfs.info "${CURRENT_SOURCES[temp]}" removing temporary metadata.
rm -rf "${CURRENT_SOURCES[temp]}" rm -rf "${CURRENT_SOURCES[temp]}"
}
} }

View file

@ -30,13 +30,19 @@ nux.nuxsh.language.def() {
.match.line block_end '(\})' \ .match.line block_end '(\})' \
syntax syntax
.match.line if_start "(if)( +)$prefixed_id( +)$args?( *)(\{)" \ .match.line if_start "(if)( +)$prefixed_id( +)$args?( *)(\{)" \
keyword indent2 prefix identifier indent3 args - - - - - indent4 syntax3 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,? *)*)(\))?( *)(\{)" \ .match.line function_start "((function)( +))($identifier)((\()|( *))(($identifier,? *)*)(\))?( *)(\{)" \
- keyword indent2 identifier - syntax indent3 args - syntax2 indent4 syntax3 - keyword indent2 identifier - syntax indent3 args - syntax2 indent4 syntax3
.match.line block_start "($identifier)(( +)$args)?( *)(\{)" \ .match.line block_start "($identifier)(( +)$args)?( *)(\{)" \
identifier - indent2 args - - - - - indent3 syntax3 identifier - indent2 args - - - - - indent3 syntax3
@ -51,12 +57,12 @@ nux.nuxsh.language.def() {
.highlight prefix cyan .highlight prefix cyan
.highlight identifier green .highlight identifier green
.highlight keyword blue .highlight keyword cyan
.highlight args yellow .highlight args yellow
.highlight comment magenta .highlight comment magenta
.highlight unmatched red .highlight unmatched white
.highlight syntax white .highlight syntax white
.highlight syntax2 white .highlight syntax2 white
@ -172,6 +178,20 @@ nux.nuxsh.language.def() {
done 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() { .block.start.plan() {
case $identifier in case $identifier in
function) echo "$line";; function) echo "$line";;

View file

@ -1,4 +1,3 @@
nux.use nux/color nux.use nux/color
nux.use nuxr/repl nux.use nuxr/repl
@ -86,7 +85,7 @@ nux.use nuxr/repl
local task_dot=$(tr " " "." <<< "$task") local task_dot=$(tr " " "." <<< "$task")
nux.log trace "Trying to figure task documentation location for $task $task_dot" nux.log trace "Trying to figure task documentation location for $task $task_dot"
doc_start=$(grep -hn -E "## +($task)::" "$script" | cut -d: -f1) 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 nux.log trace "doc_start" $doc_start $code_start
if [ -n "$doc_start" -a -n "$code_start" ] { if [ -n "$doc_start" -a -n "$code_start" ] {
sed -n "$doc_start,$code_start"p "$script" \ sed -n "$doc_start,$code_start"p "$script" \
@ -96,6 +95,6 @@ nux.use nuxr/repl
return 0 return 0
else else
return -1 return -1
}
} }
}
} }