From f5d412997ee8276afff2c4579b3094185e5f615b Mon Sep 17 00:00:00 2001 From: yukkop Date: Mon, 24 Nov 2025 14:20:03 +0000 Subject: [PATCH] feat(package): `hemar`: conected to nix config --- .gitignore | 2 +- legacy/helper/posix-shell/log.sh | 9 +- package/default.nix | 1 + package/hemar/default.nix | 36 +++++ package/hemar/hemar.sh | 10 +- package/hemar/log.sh | 0 package/hemar/plex/plex.sh | 18 --- .../hemar/{ => src}/plex/backend/env_var.sh | 0 package/hemar/{ => src}/plex/backend/file.sh | 21 +-- package/hemar/src/plex/backend/yq-go.sh | 40 ++++++ package/hemar/src/plex/plex.sh | 21 +++ package/hemar/test.sh | 8 +- .../plex/{time.sh => env_backend_time.sh} | 3 +- package/hemar/test/plex/jq_backend.sh | 7 + package/hemar/test/plex/jq_backend_time.sh | 129 ++++++++++++++++++ test/package/migrator/test/migrate-up/run.sh | 24 ++-- 16 files changed, 270 insertions(+), 59 deletions(-) create mode 100644 package/hemar/default.nix delete mode 100644 package/hemar/log.sh delete mode 100644 package/hemar/plex/plex.sh rename package/hemar/{ => src}/plex/backend/env_var.sh (100%) rename package/hemar/{ => src}/plex/backend/file.sh (64%) create mode 100644 package/hemar/src/plex/backend/yq-go.sh create mode 100644 package/hemar/src/plex/plex.sh rename package/hemar/test/plex/{time.sh => env_backend_time.sh} (98%) create mode 100644 package/hemar/test/plex/jq_backend.sh create mode 100644 package/hemar/test/plex/jq_backend_time.sh diff --git a/.gitignore b/.gitignore index 9fcfed7..80ee597 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ .env result -result-man +result-* rust-toolchain.toml target/ diff --git a/legacy/helper/posix-shell/log.sh b/legacy/helper/posix-shell/log.sh index ff1934a..07354dc 100644 --- a/legacy/helper/posix-shell/log.sh +++ b/legacy/helper/posix-shell/log.sh @@ -115,7 +115,7 @@ log_allowed() { # log(level, text...) log() { delimetr=${DELIMETR:-' '}; - level="${1:?}"; shift + level="${1:?}" log_allowed "$level" || return 0 case "$level" in @@ -125,9 +125,14 @@ log() { notice) color="$CYAN" ;; warn) color="$YELLOW" ;; error) color="$RED" ;; - *) color="$WHITE" ;; + *) + color="$WHITE" + NO_SHIFT=1 + ;; esac + [ ${NO_SHIFT+x} ] || shift + # shellcheck disable=SC1003 diff --git a/package/default.nix b/package/default.nix index de422ef..528d800 100644 --- a/package/default.nix +++ b/package/default.nix @@ -142,6 +142,7 @@ in { onlinepubs2man = pkgs.callPackage ./onlinepubs2man {}; migrator = pkgs.callPackage ./migrator {}; nbt2json = pkgs.callPackage ./nbt2json {}; + hemar = pkgs.callPackage ./hemar {}; pg-17-ext-http = buildHttpExt pkgs "17"; pg-17-ext-smtp-client = buildSmtpExt pkgs "17"; pg-17-ext-plhaskell = buildPlHaskellExt pkgs "17"; diff --git a/package/hemar/default.nix b/package/hemar/default.nix new file mode 100644 index 0000000..0696d25 --- /dev/null +++ b/package/hemar/default.nix @@ -0,0 +1,36 @@ +{ dash, hectic, symlinkJoin }: +let + shell = "${dash}/bin/dash"; + bashOptions = [ + "errexit" + "nounset" + ]; + + test = hectic.writeShellApplication { + inherit shell bashOptions; + name = "hemar-test"; + runtimeInputs = [ ]; + + text = '' + WORKSPACE=${./.} + ${builtins.readFile hectic.helpers.posix-shell.log} + ${builtins.readFile ./test.sh} + ''; + }; + + hemar = hectic.writeShellApplication { + inherit shell bashOptions; + name = "hemar"; + runtimeInputs = [ ]; + + text = '' + WORKSPACE=${./.} + ${builtins.readFile hectic.helpers.posix-shell.log} + ${builtins.readFile ./hemar.sh} + ''; + }; +in +symlinkJoin { + name = "hemar"; + paths = [ hemar test ]; +} diff --git a/package/hemar/hemar.sh b/package/hemar/hemar.sh index f391098..43c7566 100644 --- a/package/hemar/hemar.sh +++ b/package/hemar/hemar.sh @@ -1,5 +1,9 @@ #!/bin/dash +# shellcheck disable=SC1091 +. "${WORKSPACE:?}/src/plex/plex.sh" +init_plex jq + # Syntax scheme: # # hemar @@ -162,8 +166,8 @@ # # AbstarctSyntaxTree (ATS) = { # e = [Element] # elements array -#} -AST='' +# } +#AST='' # 0 - text # 1 - deside tag type @@ -218,6 +222,8 @@ find_open_pattern() { parse() { char="$1" + data_pointer= + case "$STAGE" in 0) if find_open_pattern "$char"; then diff --git a/package/hemar/log.sh b/package/hemar/log.sh deleted file mode 100644 index e69de29..0000000 diff --git a/package/hemar/plex/plex.sh b/package/hemar/plex/plex.sh deleted file mode 100644 index e0c05e1..0000000 --- a/package/hemar/plex/plex.sh +++ /dev/null @@ -1,18 +0,0 @@ -#!/bin/dash - -init_plex() { - local backend - backend=${1:?} - - case "$backend" in - env) - . ${WORKSPACE}/plex/backend/env_var.sh - ;; - file) - . ${WORKSPACE}/plex/backend/file.sh - ;; - *) - exit 1 - ;; - esac -} diff --git a/package/hemar/plex/backend/env_var.sh b/package/hemar/src/plex/backend/env_var.sh similarity index 100% rename from package/hemar/plex/backend/env_var.sh rename to package/hemar/src/plex/backend/env_var.sh diff --git a/package/hemar/plex/backend/file.sh b/package/hemar/src/plex/backend/file.sh similarity index 64% rename from package/hemar/plex/backend/file.sh rename to package/hemar/src/plex/backend/file.sh index 7912e15..1404dc6 100644 --- a/package/hemar/plex/backend/file.sh +++ b/package/hemar/src/plex/backend/file.sh @@ -2,29 +2,12 @@ PLEX_TEMP="$(mktemp -d)" +#plex_set(name, key, value) plex_set() { local plexfile key val regex base esc_key esc plexfile="${PLEX_TEMP:?}${1:?}" key="${2:?}" val="${3:?}" - # construct regex for ancestors - regex="^$key=" - - base=$key - while expr "$base" : '.*\.' >/dev/null; do - base=$(printf '%s\n' "$base" | sed 's/\.[^.]*$//') - esc=$(printf '%s\n' "$base" | sed 's/\./\\./g') - regex="$regex|^$esc=" - done - - # add descendants - esc_key="$(printf '%s\n' "$key" | sed 's/\./\\./g')" - regex="$regex|^${esc_key}\." - - # remove old - grep -v -E "$regex" "$plexfile" > "$plexfile.tmp" && mv "$plexfile.tmp" "$plexfile" - - # add new - printf '%s=%s\n' "$key" "$val" >> "$plexfile" + find PLEX_ } plex_child() { diff --git a/package/hemar/src/plex/backend/yq-go.sh b/package/hemar/src/plex/backend/yq-go.sh new file mode 100644 index 0000000..9386ba7 --- /dev/null +++ b/package/hemar/src/plex/backend/yq-go.sh @@ -0,0 +1,40 @@ +#!/bin/dash + +PLEX_TEMP="$(mktemp -d)" +trap 'rm -rf $PLEX_TEMP' EXIT + +#plex_set(name, key, value) +plex_set() { + local plexfile key val + plexfile="${PLEX_TEMP:?}/${1:?}" key="${2:?}" val="${3:?}" + + touch "$plexfile" + + yq -i ".$key += \"$val\"" "$plexfile" +} + +#plex_child(plexfile, key) +plex_child() { + plex_fetch "${1:?}" "${2:?}" +} + +#plex_val(plexfile, key) +plex_val() { + plex_fetch "${1:?}" "${2:?}" +} + +#plex_val(plexfile, key) +plex_fetch() { + local plexfile key + plexfile="${PLEX_TEMP:?}/${1:?}" key="${2:?}" + + yq -i ".$key" "$plexfile" +} + +#plex_push(name, prefix, val) +plex_push() { + local plexfile prefix val + plexfile="${PLEX_TEMP:?}/${1:?}" prefix="${2:?}" val="${3:?}" + + yq -i ".$prefix += [\"$val\"]" "$plexfile" +} diff --git a/package/hemar/src/plex/plex.sh b/package/hemar/src/plex/plex.sh new file mode 100644 index 0000000..01919a0 --- /dev/null +++ b/package/hemar/src/plex/plex.sh @@ -0,0 +1,21 @@ +#!/bin/dash + +init_plex() { + local backend + backend=${1:?} + + case "$backend" in + env) + . ${WORKSPACE}/src/plex/backend/env_var.sh + ;; + file) + . ${WORKSPACE}/src/plex/backend/file.sh + ;; + yq-go) + . ${WORKSPACE}/src/plex/backend/yq-go.sh + ;; + *) + exit 1 + ;; + esac +} diff --git a/package/hemar/test.sh b/package/hemar/test.sh index 1a69c20..317839f 100644 --- a/package/hemar/test.sh +++ b/package/hemar/test.sh @@ -1,6 +1,6 @@ #!/bin/dash -WORKSPACE="$(cd "$(dirname "$0")" && pwd)" - -#. "${WORKSPACE:?}/test/plex.sh" -. "${WORKSPACE:?}/test/plex/time.sh" +# shellcheck disable=SC1091 +#. "${WORKSPACE:?}/test/plex/jq_backend_time.sh" +#. "${WORKSPACE:?}/test/plex/env_backend_time.sh" +. "${WORKSPACE:?}/test/plex/jq_backend.sh" diff --git a/package/hemar/test/plex/time.sh b/package/hemar/test/plex/env_backend_time.sh similarity index 98% rename from package/hemar/test/plex/time.sh rename to package/hemar/test/plex/env_backend_time.sh index 0bf6858..1c2666f 100644 --- a/package/hemar/test/plex/time.sh +++ b/package/hemar/test/plex/env_backend_time.sh @@ -1,6 +1,7 @@ #!/bin/dash -. "${WORKSPACE:?}/plex.sh" +. "${WORKSPACE:?}/src/plex/plex.sh" +init_plex env MY_STRUCT='' diff --git a/package/hemar/test/plex/jq_backend.sh b/package/hemar/test/plex/jq_backend.sh new file mode 100644 index 0000000..4e76707 --- /dev/null +++ b/package/hemar/test/plex/jq_backend.sh @@ -0,0 +1,7 @@ +. "${WORKSPACE:?}/src/plex/plex.sh" +init_plex yq-go + +plex_set ZALUPA zalupa apulaz +log error "struct: $WHITE$(yq . "$PLEX_TEMP/ZALUPA")$NC" + +plex_set ZALUPA zalupa.zalupa apulaz diff --git a/package/hemar/test/plex/jq_backend_time.sh b/package/hemar/test/plex/jq_backend_time.sh new file mode 100644 index 0000000..0b7b0de --- /dev/null +++ b/package/hemar/test/plex/jq_backend_time.sh @@ -0,0 +1,129 @@ +#!/bin/dash + +# shellcheck disable=SC1091 +. "${WORKSPACE:?}/src/plex/plex.sh" +init_plex yq-go + +math() { + awk "BEGIN {print $1}" +} + +elapsed() { + local task time count decrease avg + task=$1 + time=$2 + count=$3 + decrease=${4:-0} + avg=$(math "$time/$count-$decrease") + + if [ "$time" -eq 0 ]; then + log info "\n[$WHITE${task}$NC]\ninstant\n" + else + log info "\n[$WHITE${task}$NC]\nelapsed $WHITE${avg}$NC seconds\n$WHITE$(math "1/$avg")$NC per second\n" + fi + printf '%s' "$avg" +} + +set_word_length() { + local length + length=${1:?} + + # shellcheck disable=SC2183 + __WORD_OFFSET_PATERN="$(printf '%*s' "$length" | tr ' ' '?')" +} + +UNIQ_8_WORDS_COUNT=1000 +DEFAULT_WORD_LENGTH=8 +set_word_length "$DEFAULT_WORD_LENGTH" + +randomword() { + local length + length=${1:-$DEFAULT_WORD_LENGTH} + LC_ALL=C tr -dc 'A-Za-z0-9' /dev/null +} + +DEFAULT_TRIES=1000 +ACCURATE_TRIES=10000 +SUPPER_ACCURATE_TRIES=100000 + +WORD_CREATE_ACCURACY="$SUPPER_ACCURATE_TRIES" +BENCH_ACCURACY="$DEFAULT_TRIES" + +count="$WORD_CREATE_ACCURACY" +set_word_length 8 +i=0 +start=$(date +%s) +while [ "$i" -lt "${count:?}" ]; do + new_word >/dev/null + i=$((i + 1)) +done +end=$(date +%s) +time=$((end - start)) +log debug "word creation time: $time" +wordtime=$(elapsed 'Word creation' "$time" "$count") + +bench_set 'Set element with depth 1 length 8' 1 "$BENCH_ACCURACY" "$wordtime" +bench_set 'Set element with depth 2 length 8' 2 "$BENCH_ACCURACY" "$wordtime" +bench_set 'Set element with depth 3 length 8' 3 "$BENCH_ACCURACY" "$wordtime" + +log notice - + +count="$WORD_CREATE_ACCURACY" +set_word_length 2 +i=0 +start=$(date +%s) +while [ "$i" -lt "${count:?}" ]; do + new_word >/dev/null + i=$((i + 1)) +done +end=$(date +%s) +wordtime=$(elapsed 'Word creation' "$((end - start))" "$count") + +bench_set 'Set element with depth 1 length 2' 1 "$BENCH_ACCURACY" "$wordtime" +bench_set 'Set element with depth 2 length 2' 2 "$BENCH_ACCURACY" "$wordtime" +bench_set 'Set element with depth 3 length 2' 3 "$BENCH_ACCURACY" "$wordtime" diff --git a/test/package/migrator/test/migrate-up/run.sh b/test/package/migrator/test/migrate-up/run.sh index 84c1d1b..8834d51 100644 --- a/test/package/migrator/test/migrate-up/run.sh +++ b/test/package/migrator/test/migrate-up/run.sh @@ -1,12 +1,12 @@ -#!/bin/dash - -HECTIC_NAMESPACE=test-migration-list - -psql "$DATABASE_URL" 'CREATE TABLE profile ( - id INTEGER, - username TEXT -)' - -migrator --db-url "$DATABASE_URL" migrate to 20251104192425-add-info-to-profile - -exit 1 +##!/bin/dash +# +#HECTIC_NAMESPACE=test-migration-list +# +#psql "$DATABASE_URL" 'CREATE TABLE profile ( +# id INTEGER, +# username TEXT +#)' +# +#migrator --db-url "$DATABASE_URL" migrate to 20251104192425-add-info-to-profile +# +#exit 1