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/nuxfs
Tony Tkacik d40b82bfc8 nuxfs: Added support for invoking in nested dir.
Signed-off-by: Tony Tkacik <tonydamage@gmail.com>
2016-12-01 18:22:46 +01:00

148 lines
3.6 KiB
Text
Executable file

#!/usr/bin/env nux-runner
### Filesystem Layout manager
###
local STARTDIR=$(pwd)
local TEMPLATE_DIR=$NUX_ENV_DIR/templates
local SUFFIX=".nuxfs"
local TEMPLATE_FILTER="*$SUFFIX"
nux.include nuxfs
nux.include nuxfs.dsl
GIT_BIN=$(which git)
## verify - Verifies that directory matches nuxfs specification
function task.verify {
link.exists() {
nux.log debug "Testing '$1' as link"
if test ! -h "$1"; then
nuxfs.error "$1" "is not symlink."
return
fi
local REAL_LINK=$(readlink "$1")
local TARGET="$2";
if test ! "$REAL_LINK" = "$TARGET"; then
MAT_REAL=$(realpath "$REAL_LINK")
local cdir=$(dirname "$1")
MAT_TARGET=$(realpath "$cdir/$TARGET")
if test "$MAT_REAL" = "$MAT_TARGET"; then
nuxfs.warning "$1" "is using different definition for target '$2'"
else
nuxfs.error "$1" "links to $REAL_LINK instead of $TARGET"
return
fi
fi
if test ! -e "$1"; then
nuxfs.warning "$1" "target '$NC_White$2$NC_No' does not exists."
fi
}
git.exists() {
nux.log debug "Testing '$1' as git repository"
if test ! -e "$1/.git"; then
nuxfs.error "$1" "is not git repository"
return
fi
local remotes=$(grep "$2" "$1/.git/config" | wc -l)
if [ $remotes -eq 0 ]; then
nuxfs.error "$1" "Does not refer git remote '$2'"
return;
fi
}
def.notexists() {
nuxfs.error "$1" "does not exists".
}
nuxfs.initcfg
nuxfs.dsl.execute "$NUXFS_DEF" "$NUXFS_DEF_DIR" "$WORKDIR_ABSOLUTE"
}
## create - Creates missing files as described in nuxfs definition.
function task.create {
directory.notexists() {
nuxfs.info "$1" "Created directory";
mkdir -p "$1"
}
link.notexists() {
nuxfs.info "$1" "Creating link to '$2'";
ln -s "$2" "$1"
}
git.notexists() {
local cur_dir=$(nuxfs.dir.get);
local rel_path=$(realpath -Lms $1 --relative-to $cur_dir)
pushd $(nuxfs.dir.get) > /dev/null;
$GIT_BIN clone "$2" $rel_path
popd > /dev/null;
}
nuxfs.initcfg
nuxfs.dsl.execute "$NUXFS_DEF" "$NUXFS_DEF_DIR" "$WORKDIR_ABSOLUTE"
}
## 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=$TEMPLATE_DIR/$TEMPLATE$SUFFIX
if [ ! -e $TEMPLATE ]; then
echo -n "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 create task."
task.create
}
##
## template - List available templates
#
function task.template {
nux.log debug $NUX_ENV_DIR
nuxfs.template.list
}
function nuxfs.template.list {
echo "Available templates:";
pushd $TEMPLATE_DIR > /dev/null;
for i in $TEMPLATE_FILTER; do
local TEMPLATE_ID=$(basename $i $SUFFIX)
echo -n " $TEMPLATE_ID - " ;
grep -m1 "^name " $i | sed -e "s/name *//"
echo
done;
popd > /dev/null;
}
function nuxfs.initcfg {
WORKDIR=$(pwd);
NUXFS_DEF=$(nuxfs.closest $SUFFIX);
if [ "$NUXFS_DEF" = "" ]; then
nuxfs.error $(pwd) "No nuxfs configuration found."
exit 1;
fi;
NUXFS_DEF_DIR=$(dirname $NUXFS_DEF);
WORKDIR_RELATIVE=$(realpath "$WORKDIR" --relative-to "$NUXFS_DEF_DIR")
WORKDIR_ABSOLUTE=$(realpath "$WORKDIR")
}