diff --git a/inc/dsl/nuxfs.apply.dsl b/inc/dsl/nuxfs.apply.dsl index 851ab8a..ec0c125 100644 --- a/inc/dsl/nuxfs.apply.dsl +++ b/inc/dsl/nuxfs.apply.dsl @@ -11,3 +11,8 @@ link.check.recover() { git.check.recover() { $GIT_BIN clone "$origin" "$abs_path" } + +cathegorize.process.dir() { + mkdir -p "$cat_dir" + nux.dsl.info "$cat_dir" Created directory +} diff --git a/inc/dsl/nuxfs.check.dsl b/inc/dsl/nuxfs.check.dsl index 5a1cda6..834c4c9 100644 --- a/inc/dsl/nuxfs.check.dsl +++ b/inc/dsl/nuxfs.check.dsl @@ -36,3 +36,25 @@ git.entered() { return; fi } + +cathegorize.cathegories() { + nux.dsl.error Proposed cathegories: $NC_White$cathegories$NC_No + for cat in $cathegories; do + cat_dir=$parent_dir/$cat; + if [ ! -d "$cat_dir" ]; then + nux.dsl.error $cat_dir Does not exits. Required by $NC_White$@$NC_No + fi + done +} + +cathegorize.files() { + (cd "$parent_dir"; find -maxdepth 1 -iname "$match") \ + | while read file + do + file=$(basename "$file") + cathegory=$(echo $file | cathegorize.cathegory) + if [[ $cathegories =~ "$cathegory" ]];then + nux.dsl.error "$parent_dir/$file" Should be in $cathegory + fi + done +} diff --git a/inc/dsl/nuxfs.dsl b/inc/dsl/nuxfs.dsl index 7e76ece..ad30280 100644 --- a/inc/dsl/nuxfs.dsl +++ b/inc/dsl/nuxfs.dsl @@ -66,6 +66,61 @@ ## .keyword should-not-exists +.keyword cathegorize match min delimiter +cathegorize.check() { + test $(find "$(dirname "$abs_path")" -maxdepth 1 -iname "$match" | wc -l) -gt 0 +} + +cathegorize.cathegory() { + sed -r -e "s/$delimiter/:/g" | cut -d: -f1 +} +cathegorize.entered() { + local parent_dir=$(dirname "$rel_path"); + local cathegories=$((cd "$parent_dir"; find -maxdepth 1 -iname "$match") \ + | cut -d"/" -f2 \ + | cathegorize.cathegory | sort | uniq -c \ + | while IFS=" " read count cathegory + do + if [ $count -ge $min ]; then + echo $theme $cathegory + fi + done + ) + + for cat in $cathegories; do + cat_dir=$parent_dir/$cat; + if [ ! -d "$cat_dir" ]; then + cathegorize.process.dir "$@" + fi + done + + (cd "$parent_dir"; find -maxdepth 1 -iname "$match") \ + | while read file + do + file=$(basename "$file") + cathegory=$(echo $file | cathegorize.cathegory) + + if [[ $cathegories =~ "$cathegory" ]];then + rel_file=$parent_dir/$file + cat_dir=$parent_dir/$cathegory + cathegorize.process.file "$@" + fi + done + +} + +cathegorize.process.dir() { + nux.dsl.error "$cat_dir" Does not exits. Required by cathegorize +} + +cathegorize.process.file() { + nux.dsl.error "$rel_file" Should be in $NC_White$cat_dir$NC_No +} + +cathegorize.check.failed() { + nux.log debug Noop, no files to cathegorize +} + directory() { dir } diff --git a/inc/dsl/nuxfs.fix.dsl b/inc/dsl/nuxfs.fix.dsl new file mode 100644 index 0000000..7078401 --- /dev/null +++ b/inc/dsl/nuxfs.fix.dsl @@ -0,0 +1,13 @@ +.use-dsl nuxfs.apply + +should-not-exists.check.failed() { +for f in "$rel_path"; do + rm -r "$f" + nux.dsl.info $f Deleted. +done +} + +cathegorize.process.file() { + mv "$rel_file" "$cat_dir" + nux.dsl.info "$rel_file" Moved to $NC_White$cat_dir$NC_No +} diff --git a/inc/nux-base.inc.sh b/inc/nux-base.inc.sh index fd436a1..a5e3951 100644 --- a/inc/nux-base.inc.sh +++ b/inc/nux-base.inc.sh @@ -47,6 +47,7 @@ readonly NC_LightPurple='\033[1;35m' readonly NC_LightCyan='\033[1;36m' readonly NC_White=$nc_white +readonly NC_error=$NC_Red ## #Public functions: ## ## ##Logging @@ -103,11 +104,11 @@ function nux.log.level { } function nux.echo.error { - echo -e "${NC_error}$* ${NC_No}"; + echo "${NC_error}$* ${NC_No}"; } function nux.echo.warning { - echo -e "${NC_warning}$* ${NC_No}"; + echo -e "${NC_warning}"$@" ${NC_No}"; } ## nux.use @@ -117,6 +118,23 @@ function nux.use { source "$NUX_INC_DIR/$incfile" } +function nux.fatal { + echo "$@"; + exit -1; +} + +## nux.require [] +function nux.require { + local binary=$1; + local package=${2:-$1} + if nux.check.exec "$binary" ; then + : + else + nux.fatal $1 is not present. Please check if $package is installed. + fi + +} + function nux.include { local incfile="$1.inc.sh" source "$NUX_INC_DIR/$incfile" @@ -129,6 +147,11 @@ function nux.check.function { return 1 } +function nux.check.exec { + local binary=$1; + test -n "$(which "$binary")" +} + ## nux.check.file.exists ## function nux.check.file.exists { @@ -172,3 +195,7 @@ function nux.help.shelldoc { -e "s/\*([^*]*)\*/${NC_White}\1${NC_No}/gI" \ } + +NUX_ENV_MACHINE=/usr/ +NUX_ENV_MACHINE_LOCAL=/usr/local/ +NUX_ENV_USER_LOCAL=$HOME/.local diff --git a/inc/nux.dsl.inc.sh b/inc/nux.dsl.inc.sh index a0849cd..16fa895 100644 --- a/inc/nux.dsl.inc.sh +++ b/inc/nux.dsl.inc.sh @@ -1,3 +1,5 @@ +## nux.dsl - NUX Script DSL Support Library + function nux.dsl.block.type { echo ${DSL_BLOCK_TYPE[${#DSL_BLOCK_TYPE[@]}-1]} }