#!/usr/bin/env nux-runner ### Filesystem Layout manager ### local WORKDIR=$(pwd) local TEMPLATE_DIR=$NUX_ENV_DIR/templates local SUFFIX=".nuxfs" local TEMPLATE_FILTER="*$SUFFIX" nux.use nux.dsl nux.use nuxfs GIT_BIN=$(which git) nux.fs.dsl() { .block dir .keyword link .keyword git .keyword origin .keyword name .keyword template sdir() { dir "$@" enddir } .preprocess() { abs_path=$NUXFS_DEF_DIR/$path; rel_path=$(realpath -m -s "$abs_path" --relative-base="$WORKDIR"); } .check() { nux.check.file.exists "$abs_path" } .check.failed() { nux.dsl.error "$rel_path" does not exists. } } task.check() { link.entered() { nux.log debug "Testing '$rel_path' as link" if test ! -h "$abs_path"; then nuxfs.error "$rel_path" "is not symlink." return fi local REAL_LINK=$(readlink "$abs_path") local TARGET="$3"; if test ! "$REAL_LINK" = "$TARGET"; then MAT_REAL=$(realpath "$REAL_LINK") local cdir=$(dirname "$abs_path") MAT_TARGET=$(realpath "$cdir/$TARGET") if test "$MAT_REAL" = "$MAT_TARGET"; then nuxfs.warning "$rel_path" "is using different definition for target '$3'" else nuxfs.error "$rel_path" "links to $REAL_LINK instead of $TARGET" return fi fi if test ! -e "$1"; then nuxfs.warning "$rel_path" "target '$NC_White$2$NC_No' does not exists." fi } git.entered() { nux.log debug "Testing '$rel_path' as git repository" if test ! -e "$1/.git"; then nuxfs.error "$rel_path" "is not git repository" return fi local remotes=$(grep "$3" "$rel_path/.git/config" | wc -l) if [ $remotes -eq 0 ]; then nuxfs.error "$rel_path" "Does not refer git remote '$3'" return; fi } process "$@"; } ## apply - Creates missing files as described in nuxfs definition. task.apply() { dir.check.recover() { nuxfs.info "$rel_path" "Created directory"; mkdir -p "$abs_path" } link.check.recover() { nuxfs.info "$rel_path" "Creating link to '$3'"; ln -s "$3" "$abs_path" } git.check.recover() { $GIT_BIN clone "$3" "$abs_path" } process "$@"; } nux.dsl.load nux.fs.dsl process() { WORKDIR=$(pwd); TARGET=$1; if [ -n "$TARGET" ]; then TARGET=$TARGET else TARGET=$(pwd); fi; nux.log debug Target: $TARGET NUXFS_DEF=$(nuxfs.closest $SUFFIX "$TARGET"); # if [ "$NUXFS_DEF" = "" ]; then nuxfs.error $(pwd) "No nuxfs configuration found." exit 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 " absoulte path: " $NC_White$TARGET_ABSOLUTE$NC_No if test -e "$NUXFS_DEF"; then # #NUXFS_TARGET_FOUND=0; source "$NUXFS_DEF"; # #if [ $NUXFS_TARGET_FOUND = 0 ]; then # # nuxfs.warning "$3" "Does not have definition in $DEF"; # fi else nuxfs.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=$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 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 } 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=$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") }