TekWizely / bash-tpl

A smart, lightweight shell script templating engine, written in Bash
MIT License
67 stars 4 forks source link

Namespace the functions and change name of main to bash-tpl or btpl #24

Open dkebler opened 3 months ago

dkebler commented 3 months ago

I have need to source bash-tpl and then run it later via its function main. But to do that without a namespace issue bash-tpl would have to adopt a namespace prefix and main would need to be renamed to btpl or bash-tpl

i.e.

function btpl() {

(return 0 2> /dev/null) || btpl "$@"

and then for all functions and their calls

btpl_somefunctionname

all this would not affect directly executing the script

This would allow bash-tpl to play nice with other sourced code that may already be in a shell session.

as it is when I install the script I have to use sed to make those change those.

sed -i 's/\bmain\b/btpl/g'

using sed to prefix all the functions is a bit harder (at least where they are called)

dkebler commented 3 months ago

I can do a PR for this

TekWizely commented 3 months ago

Greetings @dkebler !

Although it seems interesting, and prepending all functions (and global variables) with something like bashtpl_ / BASHTPL_ would likely be easy and safe, you would be giving up .INCLUDE support by trying to use it this way ...

.INCLUDE purposefully invokes another copy of bash-tpl in order to protect the current state of the global variables.

bash-tpl is (currently) only written to support being sourced as a conceit required to support testing.

Q: What problem are you trying to solve by attempting to only execute the bash-tpl script once?

dkebler commented 3 months ago

I have written an extended bash shell ecosystem that allows me to lookup and source/load a "module" from a multiple libraries of bash modules (files) at will and run any function therein. This allows me to load modules within modules on the fly and then leverage those to make yet more complicated actions. I've even written a bundler so I can bundle a module's dependent modules and send the result over ssh to execute on a remote machine. I've taken care to avoid namespace issues in function names and yet I can use such for "overrides". Further in my modules I use local and any function called from the "main" function that doesn't get what it needs by "passing" I put inside the function so it has access to local variables. It's a hassle to have so many "local" variables always declared but at least I have few issues with one module stepping on another.

Many modules I have written are invoked the same as bash tpl that being execute a "main" function if executed otherwise just source it. I have a way to instead of loading a module to just get it's path back so I can execute it directly.

Anyway bash-tpl seemed like a super useful tool to treat as one of my modules. but only being able to execute it directly doesn't fit necessarily with my shell system as in if .INCLUDE won't work without being executed directly then that's bummer. So I guess can continue to use it as is but instead of loading/sourcing as a module in my system I can retrieve its path and then execute it directly. This of course won't work with my bundler.

Hmmmm, this explains why when I tried to source and then run the "main" function the .INCLUDE didn't work. :)

TekWizely commented 3 months ago

Thanks for the explanation ! Sounds like the quite the project - Now that you mention it, I think I remember checking out the project previously.

It may be possible to convert bash-tpl to use an Associative Array for the variables and then use a Stack push/pop for the include functionality ... Not sure if that would still allow support for older bash versions or not.

Lemme know if you give it a try :)