From 87d6284d67fbd882e3cac4f047a3008c1d319a08 Mon Sep 17 00:00:00 2001 From: Jason Mehring Date: Tue, 7 Apr 2015 19:38:43 -0400 Subject: [PATCH] Reworked the way template flavor files are found to allow flavors - This better allows flavors to be within their own packages such as Whonix The previous method of discovering flavor configurations was kind of broke when using a flavor within a different package and including additional options such as +standard. --- functions.sh | 113 ++++++++++++++++++--------------- tests/template-flavors/test.sh | 5 +- 2 files changed, 65 insertions(+), 53 deletions(-) diff --git a/functions.sh b/functions.sh index 23ae921..2e75ef5 100755 --- a/functions.sh +++ b/functions.sh @@ -118,7 +118,7 @@ setXtrace() { # ------------------------------------------------------------------------------ # Only output text under certain conditions output() { - if [ "${VERBOSE}" -ge 1 ]; then + if [ "${VERBOSE}" -ge 1 ] && [[ -z ${TEST} ]]; then # Don't echo if -x is set since it will already be displayed via true [[ ${-/x} != $- ]] || echo -e ""$@"" fi @@ -246,8 +246,10 @@ templateDir() { exists() { filename="${1}" - if [ -e "${filename}" ]; then - echo "${filename}" + + if [ -e "${filename}" ] && ! elementIn "${filename}" "${GLOBAL_CACHE[@]}"; then + # Cache $script + GLOBAL_CACHE["${#GLOBAL_CACHE[@]}"]="${filename}" return 0 fi return 1 @@ -262,61 +264,52 @@ templateFile() { splitPath "${file}" path_parts # No template flavor - if [ "X{template_flavor}" == "X" ]; then - if [ -e "${file}" ]; then - echo "${file}" - fi + if [ -z "${template_flavor}" ]; then + if [ "${suffix}" ]; then + exists "${SCRIPTSDIR}/${path_parts[base]}_${suffix}${path_parts[dotext]}" || true + else + exists "${SCRIPTSDIR}/${path_parts[base]}${path_parts[dotext]}" || true + fi return fi # Locate file in directory named after flavor if [ "${suffix}" ]; then # Append suffix to filename (before extension) + # `minimal` is the template_flavor being used in comment example + + # (TEMPLATE_FLAVOR_DIR/minimal/packages_qubes_suffix.list) + exists "${template_dir}/${template_flavor}/${path_parts[base]}_${suffix}${path_parts[dotext]}" || true - # (../SCRIPTSDIR/minimal/packages_qubes_suffix.list) + # (TEMPLATE_FLAVOR_DIR/minimal/packages_qubes_suffix.list) + exists "${template_dir}/${template_flavor}/${path_parts[base]}_${suffix}${path_parts[dotext]}" || true + + # (TEMPLATE_FLAVOR_DIR/packages_qubes_suffix.list) exists "${template_dir}/${path_parts[base]}_${suffix}${path_parts[dotext]}" || true - # (../SCRIPTSDIR/minimal/packages_qubes_minimal_suffix.list) + # (TEMPLATE_FLAVOR_DIR/packages_qubes_minimal_suffix.list) exists "${template_dir}/${path_parts[base]}_${suffix}_${template_flavor}${path_parts[dotext]}" || true - # (../SCRIPTSDIR/packages_qubes_minimal_suffix.list) + # (SCRIPTSDIR/packages_qubes_minimal_suffix.list) exists "${SCRIPTSDIR}/${path_parts[base]}_${suffix}_${template_flavor}${path_parts[dotext]}" || true else - # (../SCRIPTSDIR/minimal/packages_qubes.list) + # (TEMPLATE_FLAVOR_DIR/minimal/packages_qubes.list) + exists "${template_dir}/${template_flavor}/${path_parts[base]}${path_parts[dotext]}" || true + + # (TEMPLATE_FLAVOR_DIR/minimal/packages_qubes_minimal.list) + exists "${template_dir}/${template_flavor}/${path_parts[base]}_${template_flavor}${path_parts[dotext]}" || true + + # (TEMPLATE_FLAVOR_DIR/packages_qubes.list) exists "${template_dir}/${path_parts[base]}${path_parts[dotext]}" || true - # (../SCRIPTSDIR/minimal/packages_qubes_minimal.list) + # (TEMPLATE_FLAVOR_DIR/packages_qubes_minimal.list) exists "${template_dir}/${path_parts[base]}_${template_flavor}${path_parts[dotext]}" || true - # (../SCRIPTSDIR/packages_qubes_minimal.list) + # (SCRIPTSDIR/packages_qubes_minimal.list) exists "${SCRIPTSDIR}/${path_parts[base]}_${template_flavor}${path_parts[dotext]}" || true fi } -buildStepExec() { - local filename="$1" - local suffix="$2" - local template_flavor="$3" - - script="$(templateFile "${filename}" "${suffix}" "${template_flavor}")" - - if [ -f "${script}" ] && [ ! ${GLOBAL_CACHE[$script]+_} ]; then - - # Test module expects raw output back only used to asser test results - if [[ -n ${TEST} ]]; then - echo "${script}" - else - output "${bold}${under}INFO: Currently running script: ${script}${reset}" - fi - - # Cache $script - GLOBAL_CACHE[$script]=1 - - # Execute $script - "${script}" - fi -} - copyTreeExec() { local source_dir="$1" local dir="$2" @@ -347,10 +340,6 @@ copyTreeExec() { } callTemplateFunction() { - # Reset Cache - unset GLOBAL_CACHE - declare -A -g GLOBAL_CACHE - local calling_script="$1" local calling_arg="$2" local functionExec="$3" @@ -404,13 +393,18 @@ getFileLocations() { local suffix="$3" local function="templateFile" - files="$(callTemplateFunction "${filename}" "${suffix}" "${function}")" + unset GLOBAL_CACHE + declare -A -g GLOBAL_CACHE - IFS_orig="${IFS}}"; IFS=$'\n' - files=( "${files}" ) - setArrayAsGlobal files $return_global_var + callTemplateFunction "${filename}" "${suffix}" "${function}" + setArrayAsGlobal GLOBAL_CACHE $return_global_var - IFS="${IFS_orig}" + if [ ! ${#GLOBAL_CACHE[@]} -eq 0 ]; then + debug "Smart files located for: '${filename##*/}' (suffix: ${suffix}):" + for filename in "${GLOBAL_CACHE[@]}"; do + debug "${filename}" + done + fi } # ------------------------------------------------------------------------------ @@ -432,9 +426,23 @@ getFileLocations() { buildStep() { local filename="$1" local suffix="$2" - local function="buildStepExec" - callTemplateFunction "${filename}" "${suffix}" "${function}" + info "Locating buildStep files: ${filename##*/} suffix: ${suffix}" + getFileLocations "build_step_files" "${filename}" "${suffix}" + + for script in "${build_step_files[@]}"; do + if [ -e "${script}" ]; then + # Test module expects raw output back only used to asser test results + if [[ -n ${TEST} ]]; then + echo "${script}" + else + output "${bold}${under}INFO: Currently running script: ${script}${reset}" + fi + + # Execute $script + "${script}" + fi + done } # ------------------------------------------------------------------------------ @@ -460,8 +468,13 @@ copyTree() { local target_dir="$3" local function="copyTreeExec" - if [ "x${source_dir}" == "x" ]; then - callTemplateFunction "" "${dir}" "${function}" + if [ -z "${source_dir}" ]; then + splitPath "${0}" path_parts + if [ -d "${path_parts[dir]}/${dir}" ]; then + copyTreeExec "${path_parts[dir]}" "${dir}" "" "" + else + callTemplateFunction "" "${dir}" "${function}" + fi else copyTreeExec "${source_dir}" "${dir}" "" "${target_dir}" fi diff --git a/tests/template-flavors/test.sh b/tests/template-flavors/test.sh index c268af5..4c2e23c 100755 --- a/tests/template-flavors/test.sh +++ b/tests/template-flavors/test.sh @@ -200,7 +200,7 @@ header <