Skip to content

Commit 5f12f65

Browse files
author
Daniel Ruthardt
authored
✨ Add support for nested package paths (#14)
* 🩹 Avoid including in comments Signed-off-by: Daniel Ruthardt <druthardt@linuxfoundation.org> * ✨ Add support for sub-packages Signed-off-by: Daniel Ruthardt <druthardt@linuxfoundation.org> * 📝 Add sub-packages to sample script/libs Signed-off-by: Daniel Ruthardt <druthardt@linuxfoundation.org> * 📝 Update documentation adding sub-packages Signed-off-by: Daniel Ruthardt <druthardt@linuxfoundation.org> --------- Signed-off-by: Daniel Ruthardt <druthardt@linuxfoundation.org>
1 parent 5a63706 commit 5f12f65

File tree

7 files changed

+72
-15
lines changed

7 files changed

+72
-15
lines changed

README.md

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,35 @@ test::hello
7373
# Rely on auto-include based on naming-convention "<package>::<function>"
7474

7575
test::hello_world
76+
77+
# Option 3
78+
# Use nested package paths for better organization
79+
80+
#include test/test/hello
81+
test::test::hello
82+
83+
test::test::hello_world
7684
```
7785

78-
This script demonstrates two ways to include and use functions from external libraries or packages
79-
in your Bash+ scripts.
86+
This script demonstrates three ways to include and use functions from external libraries or packages
87+
in your Bash+ scripts. The third option shows how to use nested package paths for better organization
88+
of your code.
89+
90+
### Package Path Resolution
91+
92+
Bash+ supports nested package paths, allowing for better organization of your code. For example:
93+
- `test::hello` - Basic package path
94+
- `test::test::hello` - Nested package path
95+
- `org::team::project::module::function` - Deeply nested package path
96+
97+
This hierarchical structure helps organize related functions and makes the codebase more maintainable.
98+
99+
### Git Integration
100+
101+
Bash+ automatically manages your `.gitignore` file:
102+
- Dynamically adds included functions to prevent unintended commits
103+
- Supports both GitHub and OCI package sources
104+
- Maintains a clean repository by tracking only necessary files
80105

81106
## Contributing
82107

bashpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ set -eo pipefail
1010
# - Local directories specified by $BASHP_INCLUDE_DIRS
1111
# - External sources via extension handlers (e.g., bashpp-oci, bashpp-gh)
1212
# This enables modular script development by allowing scripts to dynamically include and use
13-
# functions from external packages.
13+
# functions from external packages. It supports nested package paths for better organization
14+
# of functions (e.g., org::team::project::module::function).
1415
#
1516
# Usage:
1617
# bashpp [file]
@@ -41,7 +42,7 @@ set -eo pipefail
4142
#
4243
# Notes:
4344
# - Functions to be included should be placed in directories or repositories following the
44-
# structure <package>/<function>
45+
# structure <package>/<function> or <package>/<subpackage>/<function> for nested paths
4546
# - For OCI registries, packages should be named 'bashp-<package>:latest'
4647
# - For GitHub releases, assets should be named 'bashp-<package>.tar.gz'
4748
# - The script automatically adds included functions to .gitignore when pulled from external
@@ -50,6 +51,7 @@ set -eo pipefail
5051
# - GitHub releases are cached in $BASHP_CACHE_DIR to avoid repeated downloads
5152
# - Repository URLs can include explicit protocol handlers (oci://, gh://)
5253
# - If no protocol is specified in repository URL, OCI is assumed
54+
# - Nested package paths are supported (e.g., test::test::hello)
5355

5456
# include
5557
# Attempts to include a specified function from a given package.
@@ -140,9 +142,11 @@ function preprocess() {
140142
local line
141143
while IFS= read -r line; do
142144
case "$line" in
145+
*#*::*) echo "$line" 1;;
143146
*::*)
144147
local include
145-
grep -Eo '[a-zA-Z0-9_]+::[a-zA-Z0-9_]+' <<< "$line" |
148+
149+
grep -Eo '[a-zA-Z0-9_]+(::[a-zA-Z0-9_]+)+' <<< "$line" |
146150
while read include; do include_once ${include//::/\/}; done
147151
echo "$line"
148152
;;

bashpp-gh

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,17 @@
77
# This script serves as an extension handler for bashpp to pull packages from GitHub releases.
88
# When bashpp encounters a GitHub repository URL (gh:// prefix), it delegates the package
99
# retrieval to this handler. The handler downloads the latest release asset, extracts the
10-
# specified include file, and installs it to the appropriate directory.
10+
# specified include file, and installs it to the appropriate directory. It supports nested
11+
# package paths and maintains proper directory structure.
1112
#
1213
# Usage:
1314
# bashpp-gh <repository> <include>
1415
# Called automatically by bashpp when processing GitHub repository URLs
1516
#
1617
# Parameters:
1718
# repository - The GitHub repository in the format "owner/repo"
18-
# include - The path to the file to include, in the format "package/function"
19+
# include - The path to the file to include, in the format "package/function" or
20+
# "package/subpackage/function" for nested paths
1921
#
2022
# Outputs:
2123
# No direct output, but creates files in the cache directory and include directory.
@@ -26,6 +28,8 @@
2628
# Requires either GH_TOKEN or GITHUB_TOKEN environment variable for GitHub API access.
2729
# Will create cache directories as needed at BASHP_CACHE_DIR, XDG_CACHE_HOME/bashp or
2830
# ~/.cache/bashp.
31+
# Supports nested package paths and maintains proper directory structure.
32+
# Git integration has been improved to handle nested paths correctly.
2933

3034
declare -r INCLUDE_DIR=${INCLUDE_DIR:-${INCLUDE_DIR:?must be set}}
3135
declare -r repository=${1:-${repository:?must be set}}
@@ -59,7 +63,7 @@ mkdir -p "$INCLUDE_DIR/$package"
5963
cp "$cache/$include" "$INCLUDE_DIR/$include"
6064

6165
if command -v git &>/dev/null && git rev-parse --is-inside-work-tree &>/dev/null; then
62-
declare -r function=${include##*/}
66+
declare -r function=${include#*/}
6367
declare -r gitignore="$INCLUDE_DIR/$package/.gitignore"
6468
grep -q "^$function$" "$gitignore" 2>/dev/null || echo "$function" >> "$gitignore"
6569
fi

bashpp-oci

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,19 @@
55
#
66
# Description:
77
# This script serves as an extension handler for bashpp to pull packages from OCI container
8-
# images.
9-
# When bashpp encounters an OCI repository URL (default or oci:// prefix), it delegates the
10-
# package retrieval to this handler. The handler pulls the package, extracts the specified
11-
# include file, and installs it to the appropriate directory.
8+
# images. When bashpp encounters an OCI repository URL (default or oci:// prefix), it delegates
9+
# the package retrieval to this handler. The handler pulls the package, extracts the specified
10+
# include file, and installs it to the appropriate directory. It supports nested package paths
11+
# and maintains proper directory structure.
1212
#
1313
# Usage:
1414
# bashpp-oci <repository> <include>
1515
# Called automatically by bashpp when processing OCI repository URLs
1616
#
1717
# Parameters:
1818
# repository - The OCI repository containing the package image
19-
# include - The path to the file to include, in the format "package/function"
19+
# include - The path to the file to include, in the format "package/function" or
20+
# "package/subpackage/function" for nested paths
2021
#
2122
# Outputs:
2223
# No direct output, but creates files in the include directory and updates .gitignore
@@ -27,6 +28,8 @@
2728
# The OCI client can be configured using the OCI_CLIENT environment variable.
2829
# The image is expected to be tagged as 'latest' and follow the naming convention
2930
# 'repository/bashp-package:latest'.
31+
# Supports nested package paths and maintains proper directory structure.
32+
# Git integration has been improved to handle nested paths correctly.
3033

3134
declare -r INCLUDE_DIR=${INCLUDE_DIR:-${INCLUDE_DIR:?must be set}}
3235
declare -r repository=${1:-${repository:?must be set}}
@@ -38,7 +41,7 @@ command -v "$ociClient" &>/dev/null || exit 1
3841
declare -r package=${include%%/*}
3942
declare -r image="$repository/bashp-$package:latest"
4043
if "$ociClient" manifest inspect "$image" &>/dev/null; then
41-
mkdir -p "$INCLUDE_DIR/$package"
44+
mkdir -p "$INCLUDE_DIR/$(dirname "$include")"
4245

4346
id=$("$ociClient" create "$image" none)
4447
declare -r id
@@ -49,7 +52,7 @@ if "$ociClient" manifest inspect "$image" &>/dev/null; then
4952
"$ociClient" cp "$id:/$include" "$INCLUDE_DIR/$include"
5053

5154
if command -v git &>/dev/null && git rev-parse --is-inside-work-tree &>/dev/null; then
52-
declare -r function=${include##*/}
55+
declare -r function=${include#*/}
5356
declare -r gitignore="$INCLUDE_DIR/$package/.gitignore"
5457
grep -q "^$function$" "$INCLUDE_DIR/$package/.gitignore" 2>/dev/null ||
5558
echo "$function" >> "$gitignore"

hello-world.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,18 @@
66
#include test/hello
77
test::hello
88

9+
#include test/test/hello
10+
test::test::hello
11+
912
# Option 2
1013
# Rely on auto-include based on naming-convention "<package>::<function>"
1114

1215
test::hello_world
16+
17+
# Option 3
18+
# Use nested package paths for better organization
19+
20+
#include test/test/hello
21+
test::test::hello
22+
23+
test::test::hello_world

libs/test/test/hello

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# shellcheck shell=bash
2+
3+
function test::test::hello() {
4+
echo "Hey there, says the sub-package"
5+
}

libs/test/test/hello_world

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# shellcheck shell=bash
2+
3+
function test::test::hello_world() {
4+
echo "Hello World, says the sub-package"
5+
}

0 commit comments

Comments
 (0)