From 87aad362627137c2cc1a2cd330df532a8948b748 Mon Sep 17 00:00:00 2001 From: Jason Mehring Date: Sun, 15 Mar 2015 14:56:08 -0400 Subject: [PATCH 1/3] Added the ability for template files to be found side by side the original file which allows template flavor suffix to be added to package lists as well as most any other file type now. - created a new function to check if a file exists that was refactored out od the templateFile function - Added elementIn function which checks if an element exists within an array --- functions.sh | 56 +++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 49 insertions(+), 7 deletions(-) diff --git a/functions.sh b/functions.sh index 548a531..23ae921 100755 --- a/functions.sh +++ b/functions.sh @@ -164,6 +164,19 @@ setArrayAsGlobal() { } +# ------------------------------------------------------------------------------ +# Checks if the passed element exists in passed array +# $1: Element to check for +# $2: Array to check for element in +# +# Returns 0 if True, or 1 if False +# ------------------------------------------------------------------------------ +elementIn () { + local element + for element in "${@:2}"; do [[ "$element" == "$1" ]] && return 0; done + return 1 +} + # ------------------------------------------------------------------------------ # Spilts the path and returns an array of parts # @@ -231,6 +244,15 @@ templateDir() { echo "${dir}" } +exists() { + filename="${1}" + if [ -e "${filename}" ]; then + echo "${filename}" + return 0 + fi + return 1 +} + templateFile() { local file="$1" local suffix="$2" @@ -239,15 +261,35 @@ templateFile() { splitPath "${file}" path_parts - # Append suffix to filename (before extension) - if [ "${suffix}" ]; then - file="${template_dir}/${path_parts[base]}_${suffix}${path_parts[dotext]}" - else - file="${template_dir}/${path_parts[base]}${path_parts[dotext]}" + # No template flavor + if [ "X{template_flavor}" == "X" ]; then + if [ -e "${file}" ]; then + echo "${file}" + fi + return fi - if [ -f "${file}" ]; then - echo "${file}" + # Locate file in directory named after flavor + if [ "${suffix}" ]; then + # Append suffix to filename (before extension) + + # (../SCRIPTSDIR/minimal/packages_qubes_suffix.list) + exists "${template_dir}/${path_parts[base]}_${suffix}${path_parts[dotext]}" || true + + # (../SCRIPTSDIR/minimal/packages_qubes_minimal_suffix.list) + exists "${template_dir}/${path_parts[base]}_${suffix}_${template_flavor}${path_parts[dotext]}" || true + + # (../SCRIPTSDIR/packages_qubes_minimal_suffix.list) + exists "${SCRIPTSDIR}/${path_parts[base]}_${suffix}_${template_flavor}${path_parts[dotext]}" || true + else + # (../SCRIPTSDIR/minimal/packages_qubes.list) + exists "${template_dir}/${path_parts[base]}${path_parts[dotext]}" || true + + # (../SCRIPTSDIR/minimal/packages_qubes_minimal.list) + exists "${template_dir}/${path_parts[base]}_${template_flavor}${path_parts[dotext]}" || true + + # (../SCRIPTSDIR/packages_qubes_minimal.list) + exists "${SCRIPTSDIR}/${path_parts[base]}_${template_flavor}${path_parts[dotext]}" || true fi } From 7dc2bcaa5626a5483c52611677feda8c8fc956a2 Mon Sep 17 00:00:00 2001 From: Jason Mehring Date: Sun, 15 Mar 2015 15:00:50 -0400 Subject: [PATCH 2/3] Fixed templateName function where it was throwing an error that template name was too long, even though it should have been reported as okay - Added better code to shorten long names automatically - remove reference to 'infi' function since this library does not import functions.sh --- functions-name.sh | 61 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 42 insertions(+), 19 deletions(-) diff --git a/functions-name.sh b/functions-name.sh index 229a1c7..cb6fa99 100644 --- a/functions-name.sh +++ b/functions-name.sh @@ -6,6 +6,23 @@ set -e VERBOSE=${VERBOSE:-1} DEBUG=${DEBUG:-0} +containsFlavor() { + flavor="${1}" + retval=1 + + # Check the template flavor first + if [ "${flavor}" == "${TEMPLATE_FLAVOR}" ]; then + retval=0 + fi + + # Check the template flavors next + elementIn "${flavor}" ${TEMPLATE_OPTIONS[@]} && { + retval=0 + } + + return ${retval} +} + templateFlavorPrefix() { local template_flavor=${1-${TEMPLATE_FLAVOR}} @@ -30,18 +47,31 @@ templateFlavorPrefix() { fi } +templateNameFixLength() { + local template_name="${1}" + local temp_name=(${template_name//+/ }) + local index=$(( ${#temp_name[@]}-1 )) + + while [ ${#template_name} -ge 32 ]; do + template_name=$(printf '%s' ${temp_name[0]}) + if [ $index -gt 0 ]; then + template_name+=$(printf '+%s' ${temp_name[@]:1:index}) + fi + (( index-- )) + if [ $index -lt 1 ]; then + template_name="${template_name:0:31}" + fi + done + + echo "${template_name}" +} + templateNameDist() { local dist_name="${1}" template_name="$(templateName)" && dist_name="${template_name}" - # XXX: Temp hack to shorten name - if [ ${#dist_name} -ge 32 ]; then - if [ ${#template_name} -lt 32 ]; then - dist_name="${template_name}" - else - dist_name="${dist_name:0:31}" - fi - fi + # Automaticly correct name length if it's greater than 32 chars + dist_name="$(templateNameFixLength ${template_name})" # Remove and '+' characters from name since they are invalid for name dist_name="${dist_name//+/-}" @@ -53,13 +83,13 @@ templateName() { retval=1 # Default is 1; mean no replace happened # Only apply options if $1 was not passed - if [ -n "${1}" ]; then + if [ -n "${1}" ] || [ "X${TEMPLATE_OPTIONS}" == "X" ]; then local template_options= else - local template_options="${TEMPLATE_OPTIONS// /+}" + local template_options=$(printf '%s' ${TEMPLATE_OPTIONS[@]/#/+}) fi - local template_name="$(templateFlavorPrefix ${template_flavor})${template_flavor}${template_options:++}${template_options}" + local template_name="$(templateFlavorPrefix ${template_flavor})${template_flavor}${template_options}" # If TEMPLATE_LABEL is not already an array, make it one if ! [[ "$(declare -p TEMPLATE_LABEL 2>/dev/null)" =~ ^declare\ -a.* ]] ; then @@ -74,13 +104,6 @@ templateName() { fi done - if [ ${#template_name} -ge 32 ]; then - error "Template name is greater than 31 characters: ${template_name}" - error "Please set an alias" - error "Exiting!!!" - exit 1 - fi - - echo ${template_name} + echo "$(templateNameFixLength ${template_name})" return $retval } From 79de4f199796ca671038488ece3878f32550b440 Mon Sep 17 00:00:00 2001 From: Jason Mehring Date: Sun, 15 Mar 2015 15:04:23 -0400 Subject: [PATCH 3/3] Updated tests to include tests for new features added to functions*.sh --- tests/template-flavors/test.sh | 62 ++++++++++++++++++++++++++++------ 1 file changed, 51 insertions(+), 11 deletions(-) diff --git a/tests/template-flavors/test.sh b/tests/template-flavors/test.sh index 60fad4b..c268af5 100755 --- a/tests/template-flavors/test.sh +++ b/tests/template-flavors/test.sh @@ -12,6 +12,8 @@ ROOT_DIR=$(readlink -m .) . ./functions.sh . ./tests/assert/assert.sh +set +e + header() { echo echo @@ -217,7 +219,7 @@ header <