Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
2a9ecc8
first commit
rcannood Aug 12, 2024
77cd691
fix usage of wrong varible
rcannood Aug 12, 2024
8190a76
fix missing $
rcannood Aug 12, 2024
a0b8c62
fix variables again
rcannood Aug 12, 2024
8d96497
unset first, just in case
rcannood Aug 12, 2024
3b96160
replace `IFS=... for var in $myarg` with `for i in ${!myarg[@]}`
rcannood Aug 12, 2024
5aaa7df
fix type mismatch error
rcannood Aug 12, 2024
df650b1
move dependencies to mods
rcannood Aug 12, 2024
2b81cc9
wip helper functions
rcannood Aug 12, 2024
af2bf7f
rename helper functions
rcannood Aug 13, 2024
140d2f9
fixes to generated code
rcannood Aug 13, 2024
b47957a
add more comments
rcannood Aug 13, 2024
4e016be
remove unused code
rcannood Aug 13, 2024
6ffee93
make certain BashWrapper functions private
rcannood Aug 13, 2024
aaea0ef
Merge remote-tracking branch 'origin/develop' into switch_to_arrays
rcannood Aug 14, 2024
147290d
improve yaml rendering
rcannood Aug 14, 2024
921fa8b
fixes
rcannood Aug 14, 2024
653b328
more fixes
rcannood Aug 14, 2024
18f594b
only input multiples are arrays
rcannood Aug 14, 2024
9b0d19c
Merge remote-tracking branch 'origin/develop' into switch_to_arrays
rcannood Dec 16, 2024
488aab5
wip add tests
rcannood Dec 16, 2024
c6086b3
implement escaping en quoting during bashwrapper parsing
rcannood Mar 4, 2025
d633599
Merge remote-tracking branch 'origin/develop_0_9' into switch_to_arrays
rcannood Mar 4, 2025
feda25b
fix renderyaml function
rcannood Mar 6, 2025
c8181c9
add docs
rcannood Mar 6, 2025
5ba4936
unset variables if they contain @@VIASH_UNDEFINED@@
rcannood Mar 6, 2025
a1b2db8
Merge branch 'develop' into switch_to_arrays
rcannood Mar 6, 2025
f9a149a
wip python render attempt
rcannood Mar 24, 2025
16189fd
create languages with viash yaml parsers
rcannood Aug 28, 2025
09a415a
add a unit test
rcannood Aug 28, 2025
c7df019
restructure code
rcannood Aug 28, 2025
a996fa2
fix parsers, add tests
rcannood Aug 28, 2025
eec566e
fix tests and parsers
rcannood Aug 28, 2025
0e7c6b9
try to create script in local tempdir
rcannood Aug 30, 2025
23fbde8
switch to json parsers
rcannood Oct 5, 2025
0c93222
add tests
rcannood Oct 5, 2025
31706ec
refactor languages <> scripts
rcannood Oct 6, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
# ViashParseArgumentValue: Parse the value of an argument
#
# This script is used by to parse the value of an argument and set
# the corresponding environment variable.
#
# If the argument is multiple: false:
#
# * If the variable is already set, an error is thrown.
# * If the value is equal to UNDEFINED, it is replaced with
# @@VIASH_UNDEFINED@@.
# * If the value is quoted, the quotes are removed.
#
# If the argument is multiple: true:
#
# * If the value is equal to UNDEFINED, it is replaced with
# @@VIASH_UNDEFINED@@.
# * If the value is quoted, the quotes are removed.
# * If the value is a list of values, the values are split by semicolons.
# * If the value contains escaped characters '"\;, they are unescaped.
#
# Arguments:
# $1: The name of an argument (used for error messages)
# $2: The name of the environment variable to set
# $3: Whether the argument can be passed multiple times (true/false)
# $4: The value of the argument
# return: None, but sets the environment variable
#
# Examples:
#
# ViashParseArgumentValue "--input" "par_input" "true" "'UNDEFINED'" "false"
#
# See ViashParseArgumentValue.test.sh for additional examples.
function ViashParseArgumentValue {
local flag="$1"
local env_name="$2"
local multiple="$3"
local value="$4"

if [ $# -lt 4 ]; then
ViashError "Not enough arguments passed to ${flag}. Use '--help' to get more information on the parameters."
exit 1
fi

if [ "$multiple" == "false" ]; then
# check whether the variable is already set
if [ ! -z ${!env_name+x} ]; then
local -n prev_value="$env_name"
ViashError "Pass only one argument to argument '${flag}'. Found: ${prev_value@Q} & ${value@Q}"
exit 1
fi

value=$(ViashParseSingleString "$value")

# set the variable
declare -g "$env_name=${value}"
else
local -n prev_values="$env_name"

local new_values=()
readarray -t "new_values" < <(ViashParseMultipleStringAsArray "$value")

local combined_values=( "${prev_values[@]}" "${new_values[@]}" )

# if length is larger than 1 and some element is @@VIASH_UNDEFINED@@, throw error
if [ ${#combined_values[@]} -gt 1 ]; then
for element in "${combined_values[@]}"; do
if [ "$element" == "@@VIASH_UNDEFINED@@" ]; then
ViashError "Argument '${flag}': If argument value 'UNDEFINED' is passed, no other values should be provided.\nFound: ${combined_values@Q}"
exit 1
fi
done
fi

declare -g -a "$env_name=(\"\${combined_values[@]}\")"
fi
}

function ViashParseSingleString() {
local value="$1"

# if value is equal to UNDEFINED, replace with @@VIASH_UNDEFINED@@
if [ "$value" == "UNDEFINED" ]; then
value="@@VIASH_UNDEFINED@@"
fi

# if value is quoted, remove the quotes
if [[ "$value" =~ ^\".*\"$ ]]; then
value="${value:1:${#value}-2}"
elif [[ "$value" =~ ^\'.*\'$ ]]; then
value="${value:1:${#value}-2}"
fi

echo "$value"
}

function ViashParseMultipleStringAsArray() {
local value="$1"

# if value is equal to UNDEFINED, replace with @@VIASH_UNDEFINED@@
if [ "$value" == "UNDEFINED" ]; then
value="@@VIASH_UNDEFINED@@"
fi

# escape slashes
value="${value//\\\\/@@VIASH_ESCAPE_SLASH@@}"
# escape semicolons
value="${value//\\;/@@VIASH_ESCAPE_SEMICOLON@@}"
# escape quotes
value="${value//\\\"/@@VIASH_ESCAPE_QUOTE@@}"
# escape single quotes
value="${value//\\\'/@@VIASH_ESCAPE_SINGLE_QUOTE@@}"

local gawk_script="
BEGIN {
FPAT = \"([^;]+)|(\\\"[^\\\"]+\\\")|('[^']+')\";
}

{
for (i = 1; i <= NF; i++) {
field = \$i;

# if field is equal to 'UNDEFINED_ITEM', replace it with @@VIASH_UNDEFINED_ITEM@@
if (field == \"UNDEFINED_ITEM\") {
field = \"@@VIASH_UNDEFINED_ITEM@@\";
}

# Remove leading and trailing double quotes.
if (field ~ /^\".*\"$/) {
gsub(/^\"|\"$/, \"\", field);
} else if (field ~ /^'.*'$/) {
gsub(/^'|'$/, \"\", field);
}

# Unescape escaped characters
gsub(/@@VIASH_ESCAPE_SINGLE_QUOTE@@/, \"'\", field);
gsub(/@@VIASH_ESCAPE_QUOTE@@/, \"\\\"\", field);
gsub(/@@VIASH_ESCAPE_SEMICOLON@@/, \";\", field);
gsub(/@@VIASH_ESCAPE_SLASH@@/, \"\\\\\", field);

# Output each field on a separate line.
print field;
}
}
"

echo "${value}" | awk "$gawk_script"
}
97 changes: 97 additions & 0 deletions src/main/resources/io/viash/helpers/bashutils/ViashRenderJson.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# ViashRenderJsonKeyValue: renders a key-value pair in JSON format
#
# Arguments:
# $1: The key
# $2: The type of the value (string, boolean, boolean_true, boolean_false, file, double, integer)
# $3: Whether the value can be passed multiple times (true/false)
# $4+: The value(s) of the key
# return: prints the key-value pair in JSON format
#
# Examples:
#
# ViashRenderJsonKeyValue "input" "string" "false" "file.txt"
# ViashRenderJsonKeyValue "input" "string" "true" "file1.txt" "file2.txt"
function ViashRenderJsonKeyValue {
local key="$1"
local type="$2"
local multiple="$3"
shift 3

local out=" \"${key}\":"

# Handle null case
if [ $# -eq 1 ] && [ "$1" == "@@VIASH_UNDEFINED@@" ]; then
out+=" null"
echo "$out"
return
fi

# Handle multiple values (array)
if [ "$multiple" == "true" ]; then
out+=" ["
local first=true
while [ $# -gt 0 ]; do
if [ "$first" == "false" ]; then
out+=","
fi
first=false

if [ "$1" == "@@VIASH_UNDEFINED_ITEM@@" ]; then
out+=" null"
elif [ "$type" == "string" ] || [ "$type" == "file" ]; then
out+=" $(ViashRenderJsonQuotedValue "$key" "$1")"
elif [ "$type" == "boolean" ] || [ "$type" == "boolean_true" ] || [ "$type" == "boolean_false" ]; then
out+=" $(ViashRenderJsonBooleanValue "$key" "$1")"
else
out+=" $(ViashRenderJsonUnquotedValue "$key" "$1")"
fi
shift
done
out+=" ]"
else
# Handle single value
out+=" "
if [ "$1" == "@@VIASH_UNDEFINED_ITEM@@" ]; then
out+="null"
elif [ "$type" == "string" ] || [ "$type" == "file" ]; then
out+="$(ViashRenderJsonQuotedValue "$key" "$1")"
elif [ "$type" == "boolean" ] || [ "$type" == "boolean_true" ] || [ "$type" == "boolean_false" ]; then
out+="$(ViashRenderJsonBooleanValue "$key" "$1")"
else
out+="$(ViashRenderJsonUnquotedValue "$key" "$1")"
fi
fi

echo "$out"
}

function ViashRenderJsonQuotedValue {
local key="$1"
local value="$2"
# escape backslashes, quotes, and newlines for JSON
echo "$value" | \
sed 's#\\#\\\\#g' | \
sed 's#"#\\"#g' | \
sed ':a;N;$!ba;s/\n/\\n/g' | \
sed 's#^#"#g;s#$#"#g'
}

function ViashRenderJsonBooleanValue {
local key="$1"
local value="$2"
# convert to lowercase
value=$(echo "$value" | tr '[:upper:]' '[:lower:]')
if [[ "$value" == "true" || "$value" == "yes" ]]; then
echo "true"
elif [[ "$value" == "false" || "$value" == "no" ]]; then
echo "false"
else
echo "Argument '$key' has to be a boolean, but got '$value'. Use '--help' to get more information on the parameters."
fi
}

function ViashRenderJsonUnquotedValue {
local key="$1"
local value="$2"
echo "$value"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@

# ViashRenderPythonValue 'string' "false" "${VIASH_META_NAME[@]:-@@VIASH_UNDEFINED@@}"
function ViashRenderPythonValue {
local type="$1"
local multiple="$2"
shift 2

if [ $# -eq 1 ] && [ "$1" == "@@VIASH_UNDEFINED@@" ]; then
echo "None"
return
fi

local out=""

if [ "$multiple" == "true" ]; then
out+="["
fi
while [ $# -gt 0 ]; do
if [ "$1" == "@@VIASH_UNDEFINED_ITEM@@" ]; then
out+="None"
elif [ "$type" == "string" ] || [ "$type" == "file" ]; then
# render it as r'...' but escape single quotes
out+=$(echo "$1" | sed "s/'/\\\'/g" | sed "s/^/r\'/;s/$/\'/")
elif [ "$type" == "boolean" ] || [ "$type" == "boolean_true" ] || [ "$type" == "boolean_false" ]; then
out+="$1"
else
out+="$1"
fi
if [ "$multiple" == "true" ] && [ $# -gt 1 ]; then
out+=", "
fi
shift
done
if [ "$multiple" == "true" ]; then
out+="]"
fi
echo "$out"
}
79 changes: 79 additions & 0 deletions src/main/resources/io/viash/helpers/bashutils/ViashRenderYaml.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# ViashRenderYamlKeyValue: renders a key-value pair in YAML format
#
# Arguments:
# $1: The key
# $2: The type of the value (string, boolean, boolean_true, boolean_false, file, double, integer)
# $3: Whether the value can be passed multiple times (true/false)
# $4+: The value(s) of the key
# return: prints the key-value pair in YAML format
#
# Examples:
#
# ViashRenderYamlKeyValue "input" "string" "false" "file.txt"
# ViashRenderYamlKeyValue "input" "string" "true" "file1.txt" "file2.txt"
function ViashRenderYamlKeyValue {
local key="$1"
local type="$2"
local multiple="$3"
shift 3

local out=" ${key}:"

if [ $# -eq 1 ] && [ "$1" == "@@VIASH_UNDEFINED@@" ]; then
out+=" null"
echo "$out"
return
fi

while [ $# -gt 0 ]; do
if [ "$multiple" == "true" ]; then
out+=$'\n - '
else
out+=" "
fi
if [ "$1" == "@@VIASH_UNDEFINED_ITEM@@" ]; then
out+="null"
elif [ "$type" == "string" ] || [ "$type" == "file" ]; then
out+="$(ViashRenderYamlQuotedValue "$key" "$1")"
elif [ "$type" == "boolean" ] || [ "$type" == "boolean_true" ] || [ "$type" == "boolean_false" ]; then
out+="$(ViashRenderYamlBooleanValue "$key" "$1")"
else
out+="$(ViashRenderYamlUnquotedValue "$key" "$1")"
fi
shift
done

echo "$out"
}

function ViashRenderYamlQuotedValue {
local key="$1"
local value="$2"
# escape quotes, backslashes and newlines, and then surround by quotes
echo "$value" | \
sed 's#\\#\\\\#g' | \
sed 's#"#\\"#g' | \
sed ':a;N;$!ba;s/\n/\\n/g' | \
sed 's#^#"#g;s#$#"#g'
}

function ViashRenderYamlBooleanValue {
local key="$1"
local value="$2"
# convert to lowercase
value=$(echo "$value" | tr '[:upper:]' '[:lower:]')
if [[ "$value" == "true" || "$value" == "yes" ]]; then
echo "true"
elif [[ "$value" == "false" || "$value" == "no" ]]; then
echo "false"
else
echo "Argument '$key' has to be a boolean, but got '$value'. Use '--help' to get more information on the parameters."
fi
}

# can be infinite too
function ViashRenderYamlUnquotedValue {
local key="$1"
local value="$2"
echo "$value"
}
Loading
Loading