1
1
Fork 0
mirror of https://github.com/tonydamage/nux-env.git synced 2025-12-11 13:24:28 +01:00
nux-env/bin/mark
Tony Tkacik e148c76fbe nuxsh: Added support for @command keyword and use it
Signed-off-by: Tony Tkacik <tonydamage@gmail.com>
2020-01-01 13:14:40 +01:00

115 lines
2.8 KiB
Text
Executable file

#!/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"
}
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
}
}
@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 {
name=""
if [ -e "$root/.path-names" ]; then
rel_path=$(nuxfs.path.relative "$root/.." "$item");
name=${rel_path//\//-}
nuxfs.info "$item" Creating symlink: $(nuxfs.path.display "$root/$mark/$name")
else
nuxfs.info "$item" Creating symlink in $(nuxfs.path.display "$root/$mark")
fi
nuxfs.symlink "$item" "$root/$mark" "$name"
}
}
## Manages symlinks in closest mark (**.by**) directory, provides functionality to batch create
## them with relative paths.
##
## #Available tasks:
@namespace task. {
## tag:: <task> <task arguments...>
## Performs specified task in tag namespace (marks prefixed with **tag/**)
@command tag task {
MARK_PREFIX="tag/"
nuxr.run "$task" "$@"
}
## this:: <mark> [marks...]
## Marks **current folder** with specific markers.
## This creates symlinks in **mark** folder pointing to **current folder**.
@command 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:: <mark> <files...>
## Marks **files** with specific **mark**.
## This creates symlinks for files in **mark** folder.
@command multiple mark {
pwd=$(pwd)
mark_root=$(mark.dir "$pwd")
while [ "$#" -gt 0 ]; do
item="$1"; shift;
mark.mark "$mark_root" "$pwd/$item" "$MARK_PREFIX$mark"
done
}
## display:: [mark]
## Displays path to current mark folder and displays available marks.
## If **mark** is provided list nested marks.
@command 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;
)
}