Skip to content

Commit 1045b91

Browse files
committed
Added the write_file() API function.
This function is added to provide a standard way of writing/overwriting previously generated files within a project target directory, or creating new files with custom/dynamic content, both when in form-based as well as in Quickstart mode. This allows init code to create new files with dynamically generated content, instead of having to use source templates with substitution variables. The function automatically performs some safety checks and lets the underlying initialization process fail in some cases. Therefore, by using this function a caller can use the same API irrespective of the underlying application mode context.
1 parent c285c4c commit 1045b91

File tree

2 files changed

+91
-2
lines changed

2 files changed

+91
-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: 90 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,92 @@ function proceed_next_level() {
55725572

55735573
source "$next_lvl_script";
55745574
}
5575+
5576+
# [API function]
5577+
# Writes text to a file within the project target directory.
5578+
#
5579+
# The file to write is specified by the first argument. The file path argument
5580+
# is interpreted as relative to the project target directory. The content to write
5581+
# is specified by the second argument.
5582+
#
5583+
# When in regular (form-based) application mode, the project target directory must have
5584+
# already been created by means of the project_init_copy() function before a file
5585+
# can be written. If the specified file already exists, its content is overwritten.
5586+
# If it does not already exist, it is created.
5587+
#
5588+
# When in Quickstart mode, the project target directory is the underlying Quickstart
5589+
# current working directory, i.e. where the Quickstart was initiated. If the specified
5590+
# file already exists, it is only overwritten if it was previously created by
5591+
# a Quickstart function, otherwise the entire Quickstart operation is cancelled
5592+
# by this function. The file is created if it does not already exists.
5593+
#
5594+
# Args:
5595+
# $1 - The relative path of the source file to write to in the project
5596+
# target directory. The path must not be absolute. This is a mandatory argument
5597+
# $2 - The file content to write to the specified file. Can be an empty string
5598+
# or omitted. This is an optional argument.
5599+
#
5600+
# Returns:
5601+
# 0 - If the write operation was successful.
5602+
# 1 - If the write operation to the specified file has failed.
5603+
#
5604+
# Examples:
5605+
# write_file "src/project_file.txt" "The file content";
5606+
#
5607+
function write_file() {
5608+
local arg_file_path="$1";
5609+
local arg_file_data="$2";
5610+
if [ -z "$arg_file_path" ]; then
5611+
_make_func_hl "write_file";
5612+
logE "Programming error: Illegal function call:";
5613+
logE "at: '${BASH_SOURCE[1]}' (line ${BASH_LINENO[0]})";
5614+
failure "Programming error: Invalid call to ${HYPERLINK_VALUE} function: " \
5615+
"No file argument specified";
5616+
fi
5617+
if _is_absolute_path "$arg_file_path"; then
5618+
_make_func_hl "write_file";
5619+
logE "Programming error: Illegal argument '${arg_target}'";
5620+
logE "at: '${BASH_SOURCE[1]}' (line ${BASH_LINENO[0]})";
5621+
failure "Programming error: Invalid call to ${HYPERLINK_VALUE} function: " \
5622+
"The file argument must not be absolute";
5623+
fi
5624+
5625+
local file_path="";
5626+
if [[ $PROJECT_INIT_QUICKSTART_REQUESTED == true ]]; then
5627+
file_path="${_PROJECT_INIT_QUICKSTART_OUTPUT_DIR}/${arg_file_path}";
5628+
if [ -e "$file_path" ]; then
5629+
if ! _array_contains "$file_path" "${CACHE_ALL_FILES[@]}"; then
5630+
_make_func_hl "write_file";
5631+
logW "Cannot write to file '${arg_file_path}'";
5632+
logW "A file may only be written to by the ${HYPERLINK_VALUE} function if it was";
5633+
logW "previously generated by a Quickstart function";
5634+
_cancel_quickstart $EXIT_FAILURE;
5635+
fi
5636+
if ! [ -f "$file_path" ]; then
5637+
logW "Cannot write to file '${arg_file_path}'";
5638+
logW "File already exists but is not a regular file";
5639+
_cancel_quickstart $EXIT_FAILURE;
5640+
fi
5641+
fi
5642+
else
5643+
if [[ ${_FLAG_PROJECT_FILES_COPIED} == false ]]; then
5644+
_make_func_hl "write_file";
5645+
local _hl_write_file="$HYPERLINK_VALUE";
5646+
_make_func_hl "project_init_copy";
5647+
local _hl_pic="$HYPERLINK_VALUE";
5648+
logE "Programming error in init script:";
5649+
logE "at: '${BASH_SOURCE[1]}' (line ${BASH_LINENO[0]})";
5650+
failure "Missing call to project_init_copy() function:" \
5651+
"When calling the ${_hl_write_file} function, the target project directory" \
5652+
"must already be created. " \
5653+
"Make sure you first call the ${_hl_pic} function in your init script";
5654+
fi
5655+
file_path="${var_project_dir}/${arg_file_path}";
5656+
fi
5657+
echo -ne "$arg_file_data" > "$file_path";
5658+
if (( $? != 0 )); then
5659+
logW "Could not write to file '${file_path}'";
5660+
return 1;
5661+
fi
5662+
return 0;
5663+
}

0 commit comments

Comments
 (0)