Skip to content

Commit eb18d3b

Browse files
committed
Added the file_exists() and directory_exists() API functions.
These functions are added to provide a standard way of checking for the existence of previously generated files within a project target directory, both when in form-based as well as in Quickstart mode. By using these functions a caller can use the same API irrespective of the underlying application mode context and project directory paths.
1 parent 3441130 commit eb18d3b

File tree

2 files changed

+146
-2
lines changed

2 files changed

+146
-2
lines changed

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.7.6-dev
1+
1.8.0-dev

libinit.sh

Lines changed: 145 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/bin/bash
2-
# Copyright (C) 2024 Raven Computing
2+
# Copyright (C) 2025 Raven Computing
33
#
44
# Licensed under the Apache License, Version 2.0 (the "License");
55
# you may not use this file except in compliance with the License.
@@ -5572,3 +5572,147 @@ function proceed_next_level() {
55725572

55735573
source "$next_lvl_script";
55745574
}
5575+
5576+
# [API function]
5577+
# Checks whether a regular file exists within the project target directory.
5578+
#
5579+
# The file path argument is interpreted as relative to the project target directory.
5580+
# When in regular (form-based) application mode, the project target directory must have
5581+
# already been created by means of the project_init_copy() function before this check
5582+
# can be done. When in Quickstart mode, the project target directory is the underlying
5583+
# Quickstart current working directory, i.e. where the Quickstart was initiated.
5584+
# This function only checks for regular files and will report nonexistence for a file
5585+
# of any other file type.
5586+
#
5587+
# Since:
5588+
# 1.8.0
5589+
#
5590+
# Args:
5591+
# $1 - The relative path of the file to check in the project target directory.
5592+
# The path must not be absolute. This is a mandatory argument.
5593+
#
5594+
# Returns:
5595+
# 0 - If the specified regular file exists.
5596+
# 1 - If the specified regular file does not exist.
5597+
#
5598+
# Examples:
5599+
# if file_exists "myfolder/my_src_file.txt"; then
5600+
# echo "File does exist";
5601+
# fi
5602+
#
5603+
# if ! file_exists "main.py"; then
5604+
# echo "Main file does not exist";
5605+
# fi
5606+
#
5607+
function file_exists() {
5608+
local arg_file_path="$1";
5609+
if [ -z "$arg_file_path" ]; then
5610+
_make_func_hl "file_exists";
5611+
logE "Programming error: Illegal function call:";
5612+
logE "at: '${BASH_SOURCE[1]}' (line ${BASH_LINENO[0]})";
5613+
failure "Programming error: Invalid call to ${HYPERLINK_VALUE} function: " \
5614+
"No file argument specified";
5615+
fi
5616+
if _is_absolute_path "$arg_file_path"; then
5617+
_make_func_hl "file_exists";
5618+
logE "Programming error: Illegal argument '${arg_file_path}'";
5619+
logE "at: '${BASH_SOURCE[1]}' (line ${BASH_LINENO[0]})";
5620+
failure "Programming error: Invalid call to ${HYPERLINK_VALUE} function: " \
5621+
"The file argument must not be absolute";
5622+
fi
5623+
5624+
local file_path="";
5625+
if [[ $PROJECT_INIT_QUICKSTART_REQUESTED == true ]]; then
5626+
file_path="${_PROJECT_INIT_QUICKSTART_OUTPUT_DIR}/${arg_file_path}";
5627+
else
5628+
if [[ ${_FLAG_PROJECT_FILES_COPIED} == false ]]; then
5629+
_make_func_hl "file_exists";
5630+
local _hl_file_exists="$HYPERLINK_VALUE";
5631+
_make_func_hl "project_init_copy";
5632+
local _hl_pic="$HYPERLINK_VALUE";
5633+
logE "Programming error in init script:";
5634+
logE "at: '${BASH_SOURCE[1]}' (line ${BASH_LINENO[0]})";
5635+
failure "Missing call to project_init_copy() function:" \
5636+
"When calling the ${_hl_file_exists} function, the target project directory" \
5637+
"must already be created. " \
5638+
"Make sure you first call the ${_hl_pic} function in your init script";
5639+
fi
5640+
file_path="${var_project_dir}/${arg_file_path}";
5641+
fi
5642+
if [ -f "$file_path" ]; then
5643+
return 0;
5644+
fi
5645+
return 1;
5646+
}
5647+
5648+
# [API function]
5649+
# Checks whether a directory exists within the project target directory.
5650+
#
5651+
# The file path argument is interpreted as relative to the project target directory.
5652+
# When in regular (form-based) application mode, the project target directory must have
5653+
# already been created by means of the project_init_copy() function before this check
5654+
# can be done. When in Quickstart mode, the project target directory is the underlying
5655+
# Quickstart current working directory, i.e. where the Quickstart was initiated.
5656+
# This function only checks for directory files and will report nonexistence for a file
5657+
# of any other file type.
5658+
#
5659+
# Since:
5660+
# 1.8.0
5661+
#
5662+
# Args:
5663+
# $1 - The relative path of the file to check in the project target directory.
5664+
# The path must not be absolute. This is a mandatory argument.
5665+
#
5666+
# Returns:
5667+
# 0 - If the specified directory exists.
5668+
# 1 - If the specified directory does not exist.
5669+
#
5670+
# Examples:
5671+
# if directory_exists "myfolder/other_subfolder"; then
5672+
# echo "Directory does exist";
5673+
# fi
5674+
#
5675+
# if ! directory_exists "tests"; then
5676+
# echo "Tests directory not found";
5677+
# fi
5678+
#
5679+
function directory_exists() {
5680+
local arg_file_path="$1";
5681+
if [ -z "$arg_file_path" ]; then
5682+
_make_func_hl "directory_exists";
5683+
logE "Programming error: Illegal function call:";
5684+
logE "at: '${BASH_SOURCE[1]}' (line ${BASH_LINENO[0]})";
5685+
failure "Programming error: Invalid call to ${HYPERLINK_VALUE} function: " \
5686+
"No file argument specified";
5687+
fi
5688+
if _is_absolute_path "$arg_file_path"; then
5689+
_make_func_hl "directory_exists";
5690+
logE "Programming error: Illegal argument '${arg_file_path}'";
5691+
logE "at: '${BASH_SOURCE[1]}' (line ${BASH_LINENO[0]})";
5692+
failure "Programming error: Invalid call to ${HYPERLINK_VALUE} function: " \
5693+
"The file argument must not be absolute";
5694+
fi
5695+
5696+
local file_path="";
5697+
if [[ $PROJECT_INIT_QUICKSTART_REQUESTED == true ]]; then
5698+
file_path="${_PROJECT_INIT_QUICKSTART_OUTPUT_DIR}/${arg_file_path}";
5699+
else
5700+
if [[ ${_FLAG_PROJECT_FILES_COPIED} == false ]]; then
5701+
_make_func_hl "directory_exists";
5702+
local _hl_directory_exists="$HYPERLINK_VALUE";
5703+
_make_func_hl "project_init_copy";
5704+
local _hl_pic="$HYPERLINK_VALUE";
5705+
logE "Programming error in init script:";
5706+
logE "at: '${BASH_SOURCE[1]}' (line ${BASH_LINENO[0]})";
5707+
failure "Missing call to project_init_copy() function:" \
5708+
"When calling the ${_hl_directory_exists} function, the target project directory" \
5709+
"must already be created. " \
5710+
"Make sure you first call the ${_hl_pic} function in your init script";
5711+
fi
5712+
file_path="${var_project_dir}/${arg_file_path}";
5713+
fi
5714+
if [ -d "$file_path" ]; then
5715+
return 0;
5716+
fi
5717+
return 1;
5718+
}

0 commit comments

Comments
 (0)