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
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:: <task> <task arguments...>
## 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:: <mark> [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:: <mark> <files...>
## 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;
)
}
}

View file

@ -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 {
:
}

View file

@ -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;
@ -48,10 +47,9 @@ QUALITY=${QUALITY:=90}
## to:: <jpg|png> <image...>
## Convert image to specified format
##
## Convert image to
##
function :to target {
@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"

33
bin/vfs
View file

@ -30,35 +30,42 @@ function vfs.path.real file {
}
@namespace task. {
## list::
## Lists all **mergerfs** based virtual filesystems managed by **vfs** tool
function :list {
@command list {
echo "1"
}
## info::
## Displays info about current path
function :info {
@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:: <storage> <path+>
## 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,10 +80,13 @@ function vfs.path.real file {
fi
done
}
## mount:: <target> <name:path> [<name:path>+]
## Creates **mergerfs** mount for specified pairs of storage **name** and
## **path**
function :mount target {
## Mounts **mergerfs** on **target**. The mount is merge of specified pairs
## of storage **name** and **path**.
##
@command mount target {
local mount_paths="";
nux.log debug "MergerFS mount: $target"
@ -98,10 +108,9 @@ function vfs.path.real file {
}
## unmount:: <target>
## Unmounts target VFS filesystem.
function :unmount target {
@command :unmount target {
fusermount -u "$target"
nuxfs.info "${CURRENT_SOURCES[temp]}" removing temporary metadata.
rm -rf "${CURRENT_SOURCES[temp]}"
}
}

View file

@ -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";;

View file

@ -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" \