Skip to content

Commit 8a28f1e

Browse files
committed
Added capability to automatically load addon library script files.
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.
1 parent 017ff63 commit 8a28f1e

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

libinit.sh

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,6 +1035,40 @@ function _load_version_addons() {
10351035
return $ret_val;
10361036
}
10371037

1038+
# Loads the specified addon library script file.
1039+
#
1040+
# The specified script path must be valid and the file
1041+
# to be loaded must exist. If any condition is not satisfied, then
1042+
# this function will exit the program by means of the failure() function.
1043+
#
1044+
# Args:
1045+
# $1 - The name of the file to load, relative to the source tree root
1046+
# of the active addon. This is a mandatory argument.
1047+
#
1048+
function _load_addon_declared_libs() {
1049+
local addon_libs="$1";
1050+
if _is_absolute_path "$addon_libs"; then
1051+
logE "Addon code path is invalid.";
1052+
logE "Invalid property with key 'sys.addon.code.load': '${addon_libs}'";
1053+
logE "File path must be relative to addon directory.";
1054+
_show_helptext "E" "Addons#loading-common-code";
1055+
failure "An addon configuration has errors";
1056+
fi
1057+
if [[ "${addon_libs}" == *"../"* ]]; then
1058+
logE "Addon code path is invalid.";
1059+
failure "An addon configuration has errors";
1060+
fi
1061+
addon_libs="${PROJECT_INIT_ADDONS_DIR}/${addon_libs}";
1062+
if ! [ -r "${addon_libs}" ]; then
1063+
logE "Addon code file not found:";
1064+
logE "at: '${addon_libs}'";
1065+
logE "Invalid property with key 'sys.addon.code.load'";
1066+
_show_helptext "E" "Addons#loading-common-code";
1067+
failure "An addon configuration has errors";
1068+
fi
1069+
source "$addon_libs";
1070+
}
1071+
10381072
# Loads the quickstart function definitions.
10391073
#
10401074
# The path to the directory containing the quickstart script
@@ -2118,6 +2152,21 @@ function _fill_files_list_from() {
21182152
fi
21192153
}
21202154

2155+
# Handler function for when the addon-specific configuration file was loaded.
2156+
#
2157+
# This function can be used to perform checks for the addon-specific
2158+
# 'project.properties' configuration file after the base variant was loaded
2159+
# but before the user variant is loaded. It is only called if
2160+
# the addon-specific file was loaded.
2161+
#
2162+
function _after_addons_properties_loaded() {
2163+
get_property "sys.addon.code.load";
2164+
local addon_libs="$PROPERTY_VALUE";
2165+
if [ -n "$addon_libs" ]; then
2166+
_load_addon_declared_libs "$addon_libs";
2167+
fi
2168+
}
2169+
21212170
# Loads the base system configuration files and stores the
21222171
# data in the corresponding global variables.
21232172
#
@@ -2144,6 +2193,7 @@ function _load_configuration() {
21442193
if [ -n "$PROJECT_INIT_ADDONS_DIR" ]; then
21452194
if [ -f "$PROJECT_INIT_ADDONS_DIR/project.properties" ]; then
21462195
_read_properties "$PROJECT_INIT_ADDONS_DIR/project.properties";
2196+
_after_addons_properties_loaded;
21472197
fi
21482198
fi
21492199

project.properties

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,15 @@ sys.starttext.show=true
129129
# to be shown, the notify-send command must be available.
130130
sys.notification.success.show=true
131131

132+
# A script file to source after an addon is loaded. This property must only be
133+
# defined in a 'project.properties' file of an addon. It is a path to a
134+
# script, relative to the addon's source tree, which gets automatically
135+
# sourced after the addon has been successfully loaded. This can be used to
136+
# put common code used throughout an addon into a separate file which acts like
137+
# a library, and instruct Project Init to automatically load it once the addon
138+
# is loaded. The code in the file is loaded into the global namespace.
139+
# sys.addon.code.load=mylibs.sh
140+
132141
# ------------------------- Language-specific Settings ---------------------- #
133142

134143
# ##### C settings

0 commit comments

Comments
 (0)