mirror of
https://github.com/tonydamage/nux-env.git
synced 2025-12-11 13:24:28 +01:00
297 lines
7.4 KiB
Text
Executable file
297 lines
7.4 KiB
Text
Executable file
#!/usr/bin/env nux-runner
|
||
|
||
## # nuxfs - Filesystem layout manager
|
||
##
|
||
## *nuxfs* command uses file structure definition present in *.nuxfs* file
|
||
## to understand intented state of directory / filesystem of user.
|
||
##
|
||
## This definition is not only used to create filesystem hierarchy, checkout
|
||
## git repositories but also to verify state of filesystem afterwards.
|
||
##
|
||
## ## Example of .nuxfs file in home directory
|
||
##
|
||
## *dir* github
|
||
## *git* nux-env https://github.com/tonydamage/nux-env.git
|
||
## *git* bats https://github.com/sstephenson/bats.git
|
||
## *enddir*
|
||
## *link* .bashrc github/nux-env/bashrc
|
||
##
|
||
## This *.nuxfs* file describes simple home structure. If we execute
|
||
## **nuxfs apply** command, it will performs filesystem changes in order to
|
||
## recreate structure described in *.nuxfs* file. In case of example it is
|
||
## equivalent of executing:
|
||
## mkdir -p github
|
||
## git clone https://github.com/tonydamage/nux-env.git github/nux-env
|
||
## git clone https://github.com/sstephenson/bats.git
|
||
## ln -s github/nux-env/bashrc .bashrc
|
||
##
|
||
|
||
nux.use nux.dsl
|
||
nux.use nuxfs
|
||
|
||
|
||
WORKDIR=$(pwd)
|
||
TEMPLATE_DIR=$NUX_ENV_DIR/templates
|
||
SUFFIX=".nuxfs"
|
||
TEMPLATE_FILTER="*$SUFFIX"
|
||
|
||
|
||
|
||
GIT_BIN=$(which git)
|
||
|
||
## Available commands:
|
||
|
||
## check:: [<subtree>]
|
||
## Verifies that directories and files matches the specification
|
||
## in *.nuxfs* definition
|
||
###
|
||
### Check is non-descructive operation, whose only output is printing
|
||
### files violating nuxfs definition.
|
||
task.check() {
|
||
nuxfs.dsl.process "$@";
|
||
}
|
||
|
||
task.describe() {
|
||
nuxfs.dsl.process "$@";
|
||
}
|
||
|
||
## apply:: [<subtree>]
|
||
## Creates missing files as specified in *.nuxfs* definition.
|
||
## **DOES NOT MODIFY** existing files breaking specification.
|
||
##
|
||
### Keywords have following resolution:
|
||
### dir::
|
||
### If directory does not exists, create it
|
||
### git::
|
||
### If git repository does not exists, clone it
|
||
### link::
|
||
### If symlink does not exists, create it
|
||
### cathegorize::
|
||
### If cathegory directories does not exists, create them
|
||
|
||
task.apply() {
|
||
nuxfs.dsl.process "$@";
|
||
}
|
||
|
||
## fix:: [<subtree>]
|
||
## Performs apply and tries to fix warnings and errors and files as
|
||
## specified in nuxfs definition. This operation **DOES MODIFY** and may **DELETE**
|
||
## existing files.
|
||
##
|
||
### Keywords have similar resolution as an *apply* with following exceptions:
|
||
### cathegorize::
|
||
### Matched files to cathegory directories
|
||
### should-not-exists::
|
||
### Matched files are deleted
|
||
###
|
||
task.fix() {
|
||
nuxfs.dsl.process "$@";
|
||
}
|
||
|
||
## help dsl::
|
||
## Displays help for **nuxfs DSL language**
|
||
##
|
||
task.help.dsl() {
|
||
nux.help.comment "$NUX_INC_DIR/dsl/nuxfs.dsl"
|
||
}
|
||
|
||
|
||
|
||
|
||
nuxfs.dsl.process() {
|
||
WORKDIR=$(pwd);
|
||
TARGET=$1;
|
||
if [ -n "$TARGET" ]; then
|
||
TARGET=$TARGET
|
||
else
|
||
TARGET=$(pwd);
|
||
fi;
|
||
|
||
TARGET=$(realpath -Lms "$TARGET")
|
||
nux.log debug Target: $TARGET
|
||
|
||
NUXFS_DEF=$(nuxfs.closest $SUFFIX "$TARGET");
|
||
#
|
||
if [ "$NUXFS_DEF" = "" ]; then
|
||
nuxfs.error $(pwd) "No nuxfs configuration found."
|
||
return 1;
|
||
fi
|
||
NUXFS_DEF_DIR=$(dirname $NUXFS_DEF);
|
||
TARGET_RELATIVE=$(realpath -ms "$TARGET" --relative-to "$NUXFS_DEF_DIR")
|
||
TARGET_ABSOLUTE=$(realpath -ms "$TARGET")
|
||
if [ "$TARGET_RELATIVE" != . ]; then
|
||
if [[ $$TARGET_RELATIVE != ./* ]]; then
|
||
TARGET_RELATIVE="./$TARGET_RELATIVE"
|
||
fi
|
||
nux.dsl.only.subtree "$TARGET_RELATIVE"
|
||
fi
|
||
|
||
nux.log debug nuxfs definition: $NC_White$NUXFS_DEF$NC_No
|
||
nux.log debug $NC_White"target directory:$Nc_No"
|
||
nux.log debug " relative to def:" $NC_White$TARGET_RELATIVE$NC_No
|
||
nux.log debug " absolute path: " $NC_White$TARGET_ABSOLUTE$NC_No
|
||
|
||
if test -e "$NUXFS_DEF"; then
|
||
# #NUXFS_TARGET_FOUND=0;
|
||
nux.dsl.execute "$NUX_INC_DIR/dsl/nuxfs.$TASK" "$NUXFS_DEF";
|
||
|
||
# #if [ $NUXFS_TARGET_FOUND = 0 ]; then
|
||
# # nuxfs.warning "$3" "Does not have definition in $DEF";
|
||
# fi
|
||
else
|
||
nux.dsl.error "$NUXFS_DEF" Definition file does not exists.
|
||
fi
|
||
}
|
||
|
||
## init Initializes a directory using template
|
||
##
|
||
function task.init {
|
||
if [ $# -eq 0 ]; then
|
||
nux.log debug "No template specified. Creating empty $SUFFIX definition."
|
||
touch .nuxfs
|
||
return;
|
||
fi
|
||
local TEMPLATE=$1
|
||
nux.log debug "User specified template is " $TEMPLATE
|
||
if [ $(echo $TEMPLATE | grep -c "$SUFFIX\$") -eq 0 ]; then
|
||
nux.log debug "Template is common template."
|
||
TEMPLATE=$(nux.cfg.get.path --first templates/${TEMPLATE}${SUFFIX})
|
||
if [ -z $TEMPLATE ]; then
|
||
echo "Template '$1' does not exists. "
|
||
nuxfs.template.list
|
||
return;
|
||
fi;
|
||
|
||
fi;
|
||
nux.log debug "Using '$TEMPLATE' to initialize."
|
||
if [ ! -e $TEMPLATE ]; then
|
||
echo "nuxfs template $1 does not exists."
|
||
return;
|
||
fi;
|
||
local template_relative=$(realpath -Lms $TEMPLATE --relative-to $(pwd))
|
||
nux.log debug "Creating link $SUFFIX: target is '$template_relative'";
|
||
ln -s "$template_relative" "$SUFFIX"
|
||
nux.log debug "Invoking apply task."
|
||
nuxr.run apply
|
||
}
|
||
|
||
## template - List available templates
|
||
##
|
||
function task.template {
|
||
nux.log debug $NUX_ENV_DIR
|
||
nuxfs.template.list
|
||
}
|
||
|
||
function task.info {
|
||
nuxfs.initcfg;
|
||
if [ -n "$NUXFS_DEF" ]; then
|
||
MANAGED_RELATIVE=$(realpath $NUXFS_DEF_DIR --relative-to $(pwd));
|
||
|
||
if [ "$MANAGED_RELATIVE" = . ]; then
|
||
echo $(pwd) is nuxfs managed directory.
|
||
else
|
||
echo $(pwd) is child of nuxfs managed directory.
|
||
echo Managed directory is $NUXFS_DEF_DIR.
|
||
fi;
|
||
else
|
||
echo "$(pwd) is not managed by nuxfs";
|
||
|
||
fi;
|
||
}
|
||
|
||
function nuxfs.dsl.capture.dir {
|
||
local CDIR=$1;
|
||
local LEVEL=$2;
|
||
nux.log debug "Inspecting directory $CDIR";
|
||
|
||
if [ -e "$RELATIVE/$SUFFIX" ]; then
|
||
return;
|
||
fi;
|
||
|
||
pushd "$CDIR" > /dev/null;
|
||
for file in ./* ; do
|
||
nuxfs.dsl.capture.file "$file" ".";
|
||
done;
|
||
popd > /dev/null;
|
||
}
|
||
|
||
function nuxfs.dsl.capture.file {
|
||
local file=$1;
|
||
local pdir=$2;
|
||
local RELATIVE=$(realpath -ms "$file" --relative-to "$pdir");
|
||
local ADDARGS="";
|
||
local TYPE="";
|
||
nux.log debug "Inspecting $file , Relative path is $RELATIVE";
|
||
if [ -h "$RELATIVE" ]; then
|
||
TYPE="link";
|
||
ADDARGS="\"$(readlink "$RELATIVE")\""
|
||
elif [ -d "$RELATIVE" -a -d "$RELATIVE/.git" ]; then
|
||
TYPE=git;
|
||
pushd "$RELATIVE" > /dev/null;
|
||
ADDARGS="\"$($GIT_BIN remote get-url origin)\""
|
||
popd > /dev/null;
|
||
elif [ -d "$RELATIVE" ]; then
|
||
TYPE="dir";
|
||
fi;
|
||
|
||
if [ "$TYPE" ]; then
|
||
echo -e "${LEVEL}$TYPE \"$RELATIVE\" $ADDARGS";
|
||
fi;
|
||
|
||
if [ "$TYPE" = "dir" -a "$LEVEL" != " " ]; then
|
||
nuxfs.dsl.capture.dir "$RELATIVE" "${LEVEL} "
|
||
echo "${LEVEL}enddir";
|
||
fi;
|
||
}
|
||
|
||
## capture - Captures current directory to nuxfs syntax
|
||
function task.capture {
|
||
if [ $# -eq 0 ]; then
|
||
nuxfs.dsl.capture.dir .
|
||
|
||
else
|
||
for file in "$@" ; do
|
||
nuxfs.dsl.capture.file "$file" .;
|
||
done;
|
||
fi
|
||
|
||
}
|
||
|
||
|
||
nuxr.repl.expose cd pwd ls find
|
||
|
||
nuxr.repl.prompt() {
|
||
echo "${nc_green}$NUX_SCRIPTNAME${nc_end}:${nc_blue}$(pwd)${nc_end}> "
|
||
}
|
||
|
||
function nuxfs.template.list {
|
||
nux.log trace "Templates folders are: $(nux.cfg.get.path templates)"
|
||
for _dir in $(nux.cfg.get.path templates) ; do
|
||
find $_dir -maxdepth 1 -iname "*.nuxfs"
|
||
done | xargs -n1 basename --suffix $SUFFIX | sort | uniq
|
||
|
||
}
|
||
|
||
|
||
function nuxfs.initcfg {
|
||
|
||
WORKDIR=$1;
|
||
if [ -n "$WORKDIR" ]; then
|
||
WORKDIR=$WORKDIR
|
||
else
|
||
WORKDIR=$(pwd);
|
||
fi;
|
||
|
||
nux.log debug Target: $WORKDIR
|
||
|
||
NUXFS_DEF=$(nuxfs.closest $SUFFIX "$WORKDIR");
|
||
|
||
if [ "$NUXFS_DEF" = "" ]; then
|
||
nuxfs.error $(pwd) "No nuxfs configuration found."
|
||
return;
|
||
fi;
|
||
NUXFS_DEF_DIR=$(dirname $NUXFS_DEF);
|
||
WORKDIR_RELATIVE=$(realpath -ms "$WORKDIR" --relative-to "$NUXFS_DEF_DIR")
|
||
WORKDIR_ABSOLUTE=$(realpath -ms "$WORKDIR")
|
||
|
||
}
|