From c6ae51b95c7fcbeafd2af329277a6e301b0e9997 Mon Sep 17 00:00:00 2001 From: Kevin Day Date: Thu, 30 Apr 2020 23:56:26 -0500 Subject: [PATCH] Cleanup: restructure bash script cleanup process Explicitly return 0 in main. The cleanup function can be called within the functions being removed. This is handy in that the cleanup no longer has to be called after the main function. The error code can then be properly returned without being lost due to the cleanup function. Once this is done, return can be used instead of exit (which is generally safer). --- build/scripts/generate.sh | 25 +++++++++++++++--------- build/scripts/install.sh | 36 ++++++++++++++++++++++------------ build/scripts/package.sh | 50 ++++++++++++++++++++++++++++++----------------- 3 files changed, 72 insertions(+), 39 deletions(-) diff --git a/build/scripts/generate.sh b/build/scripts/generate.sh index 78058fd..04630ab 100644 --- a/build/scripts/generate.sh +++ b/build/scripts/generate.sh @@ -134,7 +134,7 @@ generate_main(){ if [[ $do_help == "yes" ]] ; then generate_help generate_cleanup - exit 0 + return 0 fi generate_load_settings @@ -144,28 +144,31 @@ generate_main(){ elif [[ ! -d $path_bash ]] ; then echo -e "${c_error}ERROR: the bash path of $c_notice$path_bash$c_error is not a valid directory.$c_reset" generate_cleanup - exit 0 + return 0 fi if [[ $work_directory != "" && ! -d $work_directory ]] ; then echo -e "${c_error}ERROR: the work directory $c_notice$work_directory$c_error is not a valid directory.$c_reset" - exit 1 + generate_cleanup + return 1 fi if [[ $defines_override != "" && $(echo "$defines_override" | grep -s -o "[^_[:alnum:][:space:]]") != "" ]] ; then echo -e "${c_error}ERROR: the defines override $c_notice$defines_override$c_error includes invalid characters, only alphanumeric, whitespace, and underscore are allowed.$c_reset" - exit 1 + generate_cleanup + return 1 fi if [[ ! -d $path_c && ( ${variables[$(generate_id build_sources_library)]} != "" || ${variables[$(generate_id build_sources_program)]} != "" || ${variables[$(generate_id build_sources_headers)]} != "" ) ]] ; then echo -e "${c_error}ERROR: the c path of '$c_notice$path_c$c_error' is invalid.$c_reset" generate_cleanup - exit 1 + return 1 fi if [[ $operation_failure == "fail-multiple" ]] ; then echo -e "${c_error}ERROR: only one operation may be specified at a time.$c_reset" - exit 1 + generate_cleanup + return 1 elif [[ $operation == "build" ]] ; then if [[ -f ${path_build}.built$project_built ]] ; then echo -e "${c_warning}WARNING: this project has already been built.$c_reset" @@ -180,11 +183,16 @@ generate_main(){ generate_operation_clean elif [[ $operation == "" ]] ; then echo -e "${c_error}ERROR: no operation was given.$c_reset" - exit 1 + generate_cleanup + return 1 else echo -e "${c_error}ERROR: the operation $c_notice$operation$c_error was not recognized.$c_reset" - exit 1 + generate_cleanup + return 1 fi + + generate_cleanup + return 0 } generate_handle_colors(){ @@ -633,4 +641,3 @@ generate_cleanup(){ } generate_main $* -generate_cleanup diff --git a/build/scripts/install.sh b/build/scripts/install.sh index 24eee69..9c2ab4e 100644 --- a/build/scripts/install.sh +++ b/build/scripts/install.sh @@ -146,22 +146,25 @@ install_main(){ if [[ $do_help == "yes" ]] ; then install_help install_cleanup - exit 0 + return 0 fi if [[ $operation_failure == "fail-unsupported" ]] ; then echo -e "${c_error}ERROR: the operation $c_notice$operation$c_error was not recognized.$c_reset" - exit 1 + install_cleanup + return 1 fi if [[ $prefix == "" && ! -d $path_build ]] ; then echo -e "${c_error}ERROR: the build path $c_notice$path_build$c_error is not a valid directory.$c_reset" - exit 1 + install_cleanup + return 1 fi if [[ $destination_prefix != "" && ! -d $destination_prefix ]] ; then echo -e "${c_error}ERROR: the destination prefix $c_notice$destination_prefix$c_error is not a valid directory.$c_reset" - exit 1 + install_cleanup + return 1 fi if [[ $destination_prefix != "" ]] ; then @@ -212,42 +215,52 @@ install_main(){ if [[ $work_directory != "" && ! -d $work_directory ]] ; then echo -e "${c_error}ERROR: the work directory $c_notice$work_directory$c_error is not a valid directory.$c_reset" - exit 1 + install_cleanup + return 1 fi if [[ ! -d $destination_programs ]] ; then echo -e "${c_error}ERROR: the destination bindir $c_notice$destination_programs$c_error is not a valid directory.$c_reset" - exit 1 + install_cleanup + return 1 fi if [[ ! -d $destination_programs_static ]] ; then echo -e "${c_error}ERROR: the destination (static) bindir $c_notice$destination_programs_static$c_error is not a valid directory.$c_reset" - exit 1 + install_cleanup + return 1 fi if [[ ! -d $destination_programs_shared ]] ; then echo -e "${c_error}ERROR: the destination (shared) bindir $c_notice$destination_programs_shared$c_error is not a valid directory.$c_reset" - exit 1 + install_cleanup + return 1 fi if [[ ! -d $destination_includes ]] ; then echo -e "${c_error}ERROR: the destination incluedir $c_notice$destination_includes$c_error is not a valid directory.$c_reset" - exit 1 + install_cleanup + return 1 fi if [[ ! -d $destination_libraries_static ]] ; then echo -e "${c_error}ERROR: the destination (static) libdir $c_notice$destination_libraries_static$c_error is not a valid directory.$c_reset" - exit 1 + install_cleanup + return 1 fi if [[ ! -d $destination_libraries_shared ]] ; then echo -e "${c_error}ERROR: the destination (shared) libdir $c_notice$destination_libraries_shared$c_error is not a valid directory.$c_reset" - exit 1 + install_cleanup + return 1 fi install_load_settings install_perform_install + + install_cleanup + return 0 } install_handle_colors(){ @@ -519,4 +532,3 @@ install_cleanup(){ } install_main $* -install_cleanup diff --git a/build/scripts/package.sh b/build/scripts/package.sh index c6f1820..339be16 100644 --- a/build/scripts/package.sh +++ b/build/scripts/package.sh @@ -104,49 +104,57 @@ package_main(){ if [[ $do_help == "yes" ]] ; then package_help package_cleanup - exit 0 + return 0 fi if [[ $operation_failure == "fail-multiple" ]] ; then echo -e "${c_error}ERROR: only one operation may be specified at a time.$c_reset" - exit 1 + package_cleanup + return 1 elif [[ $operation == "build" ]] ; then if [[ ! -d $path_build ]] ; then echo -e "${c_error}ERROR: build directory '$path_build' is invalid or missing.$c_reset" - exit 1 + package_cleanup + return 1 fi if [[ ! -d $path_destination ]] ; then mkdir -vp $path_destination if [[ $? -ne 0 ]] ; then echo -e "${c_error}ERROR: package directory '$path_destination' is invalid or could not be created.$c_reset" - exit 1 + package_cleanup + return 1 fi fi if [[ ! -d $path_sources ]] ; then echo -e "${c_error}ERROR: sources directory '$path_sources' is invalid or missing.$c_reset" - exit 1 + package_cleanup + return 1 fi if [[ ! -d ${path_sources}level_0/ ]] ; then echo -e "${c_error}ERROR: build sources directory '${path_sources}level_0/' is invalid or missing.$c_reset" - exit 1 + package_cleanup + return 1 fi if [[ ! -d ${path_sources}level_1/ ]] ; then echo -e "${c_error}ERROR: build sources directory '${path_sources}level_1/' is invalid or missing.$c_reset" - exit 1 + package_cleanup + return 1 fi if [[ ! -d ${path_sources}level_2/ ]] ; then echo -e "${c_error}ERROR: build sources directory '${path_sources}level_2/' is invalid or missing.$c_reset" - exit 1 + package_cleanup + return 1 fi if [[ ! -d ${path_sources}level_3/ ]] ; then echo -e "${c_error}ERROR: build sources directory '${path_sources}level_3/' is invalid or missing.$c_reset" - exit 1 + package_cleanup + return 1 fi if [[ $mode_individual == "" && $mode_level == "" && $mode_monolithic == "" && $mode_program == "" ]] ; then @@ -171,24 +179,31 @@ package_main(){ elif [[ $operation == "dependencies" ]] ; then if [[ ! -d $path_sources ]] ; then echo -e "${c_error}ERROR: sources directory '$path_sources' is invalid or missing.$c_reset" - exit 1 + package_cleanup + return 1 fi package_operation_dependencies elif [[ $operation == "clean" ]] ; then if [[ ! -d $path_destination ]] ; then echo -e "${c_warning}WARNING: package directory '$path_destination' does not exist, there is nothing to clean.$c_reset" - exit 0 + package_cleanup + return 0 fi package_operation_clean elif [[ $operation == "" ]] ; then echo -e "${c_error}ERROR: no operation was given.$c_reset" - exit 1 + package_cleanup + return 1 else echo -e "${c_error}ERROR: the operation $c_notice$operation$c_error was not recognized.$c_reset" - exit 1 + package_cleanup + return 1 fi + + package_cleanup + return 0 } package_handle_colors(){ @@ -512,7 +527,7 @@ package_operation_monolithic(){ if [[ ! -d ${path_build}monolithic ]] ; then echo -e "${c_error}ERROR: build settings directory $c_notice${path_build}monolithic$c_error is invalid or missing.$c_reset" package_cleanup - exit 1 + return 1 fi package_create_base_files @@ -523,7 +538,7 @@ package_operation_monolithic(){ if [[ $? -ne 0 ]] ; then echo -e "${c_error}ERROR: failed to create directory $c_notice${package}data$c_error.$c_reset" package_cleanup - exit 1 + return 1 fi fi @@ -536,7 +551,7 @@ package_operation_monolithic(){ if [[ $? -ne 0 ]] ; then echo -e "${c_error}ERROR: failed to move the directory $c_notice$path_build$level$c_error as $c_notice$path_build${level}build$c_error.$c_reset" package_cleanup - exit 1 + return 1 fi if [[ ! -d ${package}sources/ ]] ; then @@ -545,7 +560,7 @@ package_operation_monolithic(){ if [[ $? -ne 0 ]] ; then echo -e "${c_error}ERROR: failed to create directory $c_notice${package}sources$c_error.$c_reset" package_cleanup - exit 1 + return 1 fi fi @@ -1079,4 +1094,3 @@ package_cleanup(){ } package_main $* -package_cleanup -- 1.8.3.1