From 4ee669ebba183b78b305a9f65d951aff1a654c21 Mon Sep 17 00:00:00 2001 From: Ulf Wiger Date: Mon, 12 May 2025 23:26:35 +0200 Subject: [PATCH] process plain args, update zompify.sh --- src/gmconfig.erl | 33 ++++++++++++++++++++++++++++++++- zomp.meta | 6 +++--- zompify.sh | 38 ++++++++++++++++++++++++++++++++++---- 3 files changed, 69 insertions(+), 8 deletions(-) diff --git a/src/gmconfig.erl b/src/gmconfig.erl index 1a76f4f..49082b5 100644 --- a/src/gmconfig.erl +++ b/src/gmconfig.erl @@ -31,6 +31,7 @@ -export([search_for_config_file/2]). -export([apply_os_env/0, apply_os_env/3]). +-export([process_plain_args/0]). -export([data_dir/1]). -export([check_config/1]). @@ -74,6 +75,7 @@ , config_file_basename => string() | 'undefined' , config_file_os_env => string() | 'undefined' , config_file_search_path => [string() | fun(() -> string())] + , config_plain_args => string() | 'undefined' , system_suffix => string() , config_formats => #{ extension() => decoder_fun() } , schema => string() | map() | fun(() -> map())}. @@ -159,6 +161,7 @@ default_gmconfig_env() -> , config_file_search_path => ["."] , config_formats => #{ "json" => fun json_decode/1 , "eterm" => fun eterm_consult/1 } + , config_plain_args => undefined , system_defaults_search_path => [fun setup:data_dir/0] , system_suffix => "" }. @@ -508,7 +511,6 @@ load_config_file(File, Mode) when Mode =:= check; do_load_user_config(File, store, Mode). apply_os_env() -> - ok = application:ensure_started(gproc), Pfx = os_env_prefix(), ConfigMap = pt_get_config(), case apply_os_env(Pfx, schema(), ConfigMap) of @@ -548,6 +550,35 @@ apply_os_env(Pfx, Schema, ConfigMap) -> error(E) end. +process_plain_args() -> + case gmconfig_env(config_plain_args, undefined) of + undefined -> + ok; + Str -> + PlainArgs = init:get_plain_arguments(), + Schema = schema(), + Map = process_plain_args_(PlainArgs, Str, schema(), #{}), + ?LOG_INFO("Map from plain args: ~p", [Map]), + if map_size(Map) > 0 -> + update_config_(Map, pt_get_config(), Schema, report); + true -> + no_change + end + end. + +process_plain_args_([Tag, K, V | Rest], Tag, Schema, Map) -> + Path = plain_path_arg(K), + Value = coerce_type(Path, V, Schema), + Map1 = update_map(to_map(Path, Value), Map), + process_plain_args_(Rest, Tag, Schema, Map1); +process_plain_args_([_ | T], Tag, Schema, Map) -> + process_plain_args_(T, Tag, Schema, Map); +process_plain_args_([], _, _, Map) -> + Map. + +plain_path_arg(Str) -> + re:split(Str, <<"__">>, [{return, binary}]). + to_map(K, V) -> to_map(K, V, #{}). diff --git a/zomp.meta b/zomp.meta index 8ca3783..3c0e814 100644 --- a/zomp.meta +++ b/zomp.meta @@ -2,10 +2,10 @@ {type,app}. {modules,[]}. {prefix,"gmconfig"}. -{desc,"Configuration management support"}. {author,"Ulf Wiger"}. -{package_id,{"uwiger","gmconfig",{0,1,0}}}. -{deps,[{"uwiger","setup",{2,2,3}}]}. +{desc,"Configuration management support"}. +{package_id,{"uwiger","gmconfig",{0,1,2}}}. +{deps,[{"uwiger","setup",{2,2,4}}]}. {key_name,none}. {a_email,"ulf@wiger.net"}. {c_email,"ulf@wiger.net"}. diff --git a/zompify.sh b/zompify.sh index 3c56b80..66aca1c 100755 --- a/zompify.sh +++ b/zompify.sh @@ -2,11 +2,41 @@ set -e APP=$(basename "$PWD") + SRC="_build/default/lib/$APP" DST="$PWD/_build/zomp/lib/$APP" +IGNORE_FILE="zomp.ignore" + mkdir -p "$DST" -find "$SRC" -type l ! -exec test -e {} \; -delete -cp -aR -L "$SRC/." "$DST/" + +# Remove broken symlinks +find "$SRC" -type l ! -exec test -e {} \; -delete || true + +# Build ignore matcher +IGNORE_TEMP=$(mktemp) +trap "rm -f $IGNORE_TEMP" EXIT + +# Expand globs in zomp.ignore to patterns suitable for grep +if [ -e "$IGNORE_FILE" ]; then + grep -v '^\s*#' "$IGNORE_FILE" | sed 's#/#\\/#g' | sed 's/\./\\./g' | sed 's/\*/.*/g' > "$IGNORE_TEMP" +fi + +# Copy Git-tracked and Zomp-allowed files +git ls-files -z | while IFS= read -r -d '' file; do + # Skip if ignored + echo "$file" | grep -Eq -f "$IGNORE_TEMP" && continue + # Only copy if file exists in the build dir + if [ -e "$SRC/$file" ]; then + mkdir -p "$DST/$(dirname "$file")" + cp -a "$SRC/$file" "$DST/$file" + fi +done + +rm "$IGNORE_TEMP" + +# Copy metadata cp "$PWD/zomp.meta" "$DST/" -cp "$PWD/Emakefile" "$DST" -rm "$DST"/ebin/*.beam +cp "$PWD/Emakefile" "$DST/" + +# Clean up beam files just in case +[ -d "$DST/ebin" ] && find "$DST/ebin" -name '*.beam' -exec rm -f {} + || true