Skip to content

Commit

Permalink
Added the remove_file() API function.
Browse files Browse the repository at this point in the history
This function is added to provide a standard way of removing previously
generated files within a project target directory, both when in form-based
as well as in Quickstart mode. It automatically performs some safety checks
and lets the underlying initialization process fail if a file cannot
be removed, which should be the default anyway because otherwise that would
mean that the project cannot be initialized as the init code author intended.
Therefore, by using this function the call site becomes cleaner as the error
handling code can be omitted.
  • Loading branch information
kilo52 committed Dec 16, 2024
1 parent 27826c9 commit f4a3d5e
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 1 deletion.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.7.4-dev
1.8.0-dev
94 changes: 94 additions & 0 deletions libinit.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5572,3 +5572,97 @@ function proceed_next_level() {

source "$next_lvl_script";
}

# [API function]
# Removes a file or directory within the project target directory.
#
# The specified file path argument is interpreted as relative to the
# project target directory.
#
# When in regular (form-based) application mode, the project target directory must
# have already been created by means of the project_init_copy() function before a file
# can be removed.
#
# When in Quickstart mode, the project target directory is the underlying Quickstart
# current working directory, i.e. where the Quickstart was initiated. If the specified
# file or directory does not exist, or if it was not previously created by the same
# Quickstart function, then this function will cause the application to cancel the
# entire Quickstart operation.
#
# Since:
# 1.8.0
#
# Args:
# $1 - The relative path of the file or directory to remove in the project
# target directory. This is a mandatory argument.
#
# Examples:
# remove_file "subdirA/the_src_file.txt";
#
function remove_file() {
local arg_target="$1";
if [ -z "$arg_target" ]; then
_make_func_hl "remove_file";
logE "Programming error: Illegal function call:";
logE "at: '${BASH_SOURCE[1]}' (line ${BASH_LINENO[0]})";
failure "Programming error: Invalid call to ${HYPERLINK_VALUE} function: " \
"No file argument specified";
fi
if _is_absolute_path "$arg_target"; then
_make_func_hl "remove_file";
logE "Programming error: Illegal argument '${arg_target}'";
logE "at: '${BASH_SOURCE[1]}' (line ${BASH_LINENO[0]})";
failure "Programming error: Invalid call to ${HYPERLINK_VALUE} function: " \
"The file argument must not be absolute";
fi

local target_file="";
if [[ $PROJECT_INIT_QUICKSTART_REQUESTED == true ]]; then
target_file="${_PROJECT_INIT_QUICKSTART_OUTPUT_DIR}/${arg_target}";
if ! [ -e "$target_file" ]; then
logW "Cannot remove file '${arg_target}'";
logW "File or directory does not exist:";
logW "at: '${target_file}'";
_cancel_quickstart $EXIT_FAILURE;
fi
if ! _array_contains "$target_file" "${CACHE_ALL_FILES[@]}"; then
_make_func_hl "remove_file";
logW "Cannot remove file '${arg_target}'";
logW "A file may only be removed by the ${HYPERLINK_VALUE} function if it was";
logW "previously generated by a Quickstart function";
_cancel_quickstart $EXIT_FAILURE;
fi
else
if [[ ${_FLAG_PROJECT_FILES_COPIED} == false ]]; then
_make_func_hl "remove_file";
local _hl_remove_file="$HYPERLINK_VALUE";
_make_func_hl "project_init_copy";
local _hl_pic="$HYPERLINK_VALUE";
logE "Programming error in init script:";
logE "at: '${BASH_SOURCE[1]}' (line ${BASH_LINENO[0]})";
failure "Missing call to project_init_copy() function:" \
"When calling the ${_hl_remove_file} function, the target project directory" \
"must already be created. " \
"Make sure you first call the ${_hl_pic} function in your init script";
fi
target_file="${var_project_dir}/${arg_target}";
fi
local file_type="file";
if [ -d "$target_file" ]; then
file_type="directory";
rm -r "$target_file" 2>/dev/null;
else
rm "$target_file" 2>/dev/null;
fi

local cmd_rm_stat=$?;
if (( cmd_rm_stat != 0 )); then
logE "Failed to remove ${file_type} inside project directory";
logE "Command rm returned non-zero exit status ${cmd_rm_stat}";
logE "File: '${arg_target}'";
_cancel_quickstart $EXIT_FAILURE;
failure "Failed to remove source files";
fi
find_all_files;
return 0;
}

0 comments on commit f4a3d5e

Please sign in to comment.