Skip to content

Commit

Permalink
Added capability to automatically load addon library script files.
Browse files Browse the repository at this point in the history
Adds code in libinit.sh to check for a property 'sys.addon.code.load', which
can be defined by an addon in its 'project.properties' file. The introduced
property defines a path to a script file, relative to the addon's source tree
root, which should be automatically loaded by the init system into the global
namespace. The loading must occur before any user-specific properties are
loaded in order to avoid redefinitions.
  • Loading branch information
kilo52 committed Jan 27, 2024
1 parent 017ff63 commit 8a28f1e
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
50 changes: 50 additions & 0 deletions libinit.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1035,6 +1035,40 @@ function _load_version_addons() {
return $ret_val;
}

# Loads the specified addon library script file.
#
# The specified script path must be valid and the file
# to be loaded must exist. If any condition is not satisfied, then
# this function will exit the program by means of the failure() function.
#
# Args:
# $1 - The name of the file to load, relative to the source tree root
# of the active addon. This is a mandatory argument.
#
function _load_addon_declared_libs() {
local addon_libs="$1";
if _is_absolute_path "$addon_libs"; then
logE "Addon code path is invalid.";
logE "Invalid property with key 'sys.addon.code.load': '${addon_libs}'";
logE "File path must be relative to addon directory.";
_show_helptext "E" "Addons#loading-common-code";
failure "An addon configuration has errors";
fi
if [[ "${addon_libs}" == *"../"* ]]; then
logE "Addon code path is invalid.";
failure "An addon configuration has errors";
fi
addon_libs="${PROJECT_INIT_ADDONS_DIR}/${addon_libs}";
if ! [ -r "${addon_libs}" ]; then
logE "Addon code file not found:";
logE "at: '${addon_libs}'";
logE "Invalid property with key 'sys.addon.code.load'";
_show_helptext "E" "Addons#loading-common-code";
failure "An addon configuration has errors";
fi
source "$addon_libs";
}

# Loads the quickstart function definitions.
#
# The path to the directory containing the quickstart script
Expand Down Expand Up @@ -2118,6 +2152,21 @@ function _fill_files_list_from() {
fi
}

# Handler function for when the addon-specific configuration file was loaded.
#
# This function can be used to perform checks for the addon-specific
# 'project.properties' configuration file after the base variant was loaded
# but before the user variant is loaded. It is only called if
# the addon-specific file was loaded.
#
function _after_addons_properties_loaded() {
get_property "sys.addon.code.load";
local addon_libs="$PROPERTY_VALUE";
if [ -n "$addon_libs" ]; then
_load_addon_declared_libs "$addon_libs";
fi
}

# Loads the base system configuration files and stores the
# data in the corresponding global variables.
#
Expand All @@ -2144,6 +2193,7 @@ function _load_configuration() {
if [ -n "$PROJECT_INIT_ADDONS_DIR" ]; then
if [ -f "$PROJECT_INIT_ADDONS_DIR/project.properties" ]; then
_read_properties "$PROJECT_INIT_ADDONS_DIR/project.properties";
_after_addons_properties_loaded;
fi
fi

Expand Down
9 changes: 9 additions & 0 deletions project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,15 @@ sys.starttext.show=true
# to be shown, the notify-send command must be available.
sys.notification.success.show=true

# A script file to source after an addon is loaded. This property must only be
# defined in a 'project.properties' file of an addon. It is a path to a
# script, relative to the addon's source tree, which gets automatically
# sourced after the addon has been successfully loaded. This can be used to
# put common code used throughout an addon into a separate file which acts like
# a library, and instruct Project Init to automatically load it once the addon
# is loaded. The code in the file is loaded into the global namespace.
# sys.addon.code.load=mylibs.sh

# ------------------------- Language-specific Settings ---------------------- #

# ##### C settings
Expand Down

0 comments on commit 8a28f1e

Please sign in to comment.