From b924ba5c293de1e94d1cb55abbb53f2a642d92d5 Mon Sep 17 00:00:00 2001 From: Tony Tkacik Date: Thu, 28 Nov 2019 11:20:24 +0100 Subject: [PATCH] Added mark tool for managing .by directory Signed-off-by: Tony Tkacik --- bin/mark | 109 ++++++++++++++++++++++++++++++++++++++++++++++ inc/nuxfs.inc.sh | 2 +- inc/nuxr.nuxsh.sh | 1 + 3 files changed, 111 insertions(+), 1 deletion(-) create mode 100755 bin/mark diff --git a/bin/mark b/bin/mark new file mode 100755 index 0000000..23c65d8 --- /dev/null +++ b/bin/mark @@ -0,0 +1,109 @@ +#!/usr/bin/env nuxr-nuxsh + +nux.use nuxfs + +MARK_DIR_NAME=.by +MARK_PREFIX="" + +@namespace nuxfs. { + function :path.relative base target { + realpath -Lms --relative-to="$base" "$target" + } + + function :path.display target { + echo $NC_LightPurple$(nuxfs.relative-to-pwd "$target")$NC_No + } + + function :symlink target dir name { + relative=$(nuxfs.path.relative "$dir" "$target") + nux.log debug "Relative path is: $relative" + :stage mkdir -p "$dir" + if [ -n "$name" ]; then + :stage ln -sf "$relative" "$dir/$name" + else + :stage ln -sf "$relative" "$dir" + fi + } + + function :stage { + if [ -n "$NUXFS_STAGE" ]; then + echo "[stage]" "$@" + else + "$@" + fi + } +} + +## Manages symlinks in closest mark (**.by**) directory, provides functionality to batch create +## them with relative paths. +## +## #Available tasks: +@namespace mark. { + function :dir item { + if [ -n "$MARK_DIR" ]; then + echo $MARK_DIR; + else + nuxfs.closest "$MARK_DIR_NAME" "$item" + fi + } + + function :mark root item mark { + nuxfs.info "$item" Creating symlink in $(nuxfs.path.display "$root/$mark") + nuxfs.symlink "$item" "$root/$mark" + } + + +} + +@namespace task. { + +## tag:: +## Performs specified task in tag namespace (marks prefixed with **tag/**) + function :tag task { + MARK_PREFIX="tag/" + nuxr.run "$task" "$@" + } + +## this:: [marks...] +## Marks **current folder** with specific markers. +## This creates symlinks in **mark** folder pointing to **current folder**. + function :this mark { + nux.log debug "Args $#" + item=$(pwd) + mark_root=$(mark.dir "$item") + mark.mark "$mark_root" "$item" "$MARK_PREFIX$mark" + while [ "$#" -gt 0 ]; do + mark="$1"; shift; + mark.mark "$mark_root" "$item" "$MARK_PREFIX$mark" + done + } + +## multiple:: +## Marks **files** with specific **mark**. +## This creates symlinks for files in **mark** folder. + function :multiple mark { + mark_root=$(mark.dir "$pwd") + while [ "$#" -gt 0 ]; do + item="$1"; shift; + mark.mark "$mark_root" "$item" "$MARK_PREFIX$mark" + done + } + +## display:: [mark] +## Displays path to current mark folder and displays available marks. +## If **mark** is provided list nested marks. + function :display mark { + mark_root=$(mark.dir $pwd) + prefix="$MARK_PREFIX" + if [ -n "$mark" ]; then + prefix="$MARK_PREFIX$mark/"; + fi + echo $mark_root: + ( + cd $(mark_root); + for mark in "$prefix"* ; do + echo ${mark#$MARK_PREFIX}; + done; + ) + } +} diff --git a/inc/nuxfs.inc.sh b/inc/nuxfs.inc.sh index 72b7394..cf3886a 100644 --- a/inc/nuxfs.inc.sh +++ b/inc/nuxfs.inc.sh @@ -3,7 +3,7 @@ function nuxfs.relative { } function nuxfs.relative-to-pwd { - realpath -m -s "$1" --relative-to "$(realpath "$(pwd)")" + realpath -Lms "$1" --relative-to "$(pwd)" } function nuxfs.error { diff --git a/inc/nuxr.nuxsh.sh b/inc/nuxr.nuxsh.sh index fb14f71..f200a6c 100644 --- a/inc/nuxr.nuxsh.sh +++ b/inc/nuxr.nuxsh.sh @@ -7,6 +7,7 @@ nux.use nuxr/repl @namespace nuxr. { function :run TASK { + # FIXME: Add default task if check:function task.$TASK { nux.log debug "Running task: $TASK"; nux.log debug "Working dir: $(pwd)"