Skip to content

Extensions development

Alexandr edited this page Aug 3, 2020 · 4 revisions

Intro

With extensions, you can add new functionality to the script and change the existing one using script vars.

Support for extensions in the script is implemented thanks to hooks. The script looks for scripts in extensions folder, parses files headers and include files to hook locations.

Extensions files

Extension files must be placed in extensions directory. If extension consists of several files, it is recommended to place them in a separate directory (ex.: extensions/my_extension). The name of directories and files can be arbitrary and is not associated with the name of the extension.

Extensions files headers

Each extension file must contain the necessary headers - extension name and hook name:

# Extension: my_extension
# Hook: hook-name

Extension name must contain only numbers, upper and lower case characters, dash and underscore. Please note that lines in headers must start with a hash mark and there must be no extra spaces.

Hooks

You can use any hook from the script. Extensions are connected with the includeExtensions function. The name of the hook is passed to it.

Example:

# Hook: after-init-vars
includeExtensions after-init-vars

You can also add hooks to your extensions to connect other extensions to them. Extension names should not contain spaces or special characters. It is recommended to use lowercase characters and dashes for better readability.

You can see full list of hooks here.

Example

We want to completely remove gif support. Image types are defined in the IMG_TYPES_ARR array, the values of which are further used to check utilities, install dependencies, and search for images by file extension. We just need to remove the array element with the GIF key after definition IMG_TYPES_ARR array. We need use hook after-init-image-types or other next. Below is an example of our simple extension:

# Extension: disable_gif
# Hook: after-init-image-types

unset IMG_TYPES_ARR[GIF]

Now we can using this extension:

bash zImageOptimizer.sh -p /path/to/files -ext disable_gif
Clone this wiki locally