Skip to content

Commit 98ce0d1

Browse files
committed
feat: Automatically call 'core.init' if it has not been called
1 parent 64d0662 commit 98ce0d1

File tree

6 files changed

+31
-16
lines changed

6 files changed

+31
-16
lines changed

.hooks/pre-commit

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ source "${0%/*}/.hookah/lib.sh"
44
hookah.init
55

66
hookah.run ./bake test
7+
hookah.run ./bake docs
8+
git add './docs/api.md'

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ The following are functions available for use. See [api.md](./docs/api.md) for m
88

99
### `init`
1010

11-
- `core.init`
11+
- `core.util.init`
1212

1313
Initialize global variables used by other functions
1414

docs/api.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Core functions for any Bash program
1919
### core.init()
2020

2121
Initiates global variables used by other functions
22+
@deprecated
2223

2324
_Function has no arguments._
2425

@@ -110,7 +111,7 @@ Sets an error.
110111
### core.err_clear()
111112

112113
Clears any of the global error state (sets to empty string).
113-
This means any `core.err_exists` calls after this _will_ return `true`
114+
This means any `core.err_exists` calls after this _will_ `return 1`
114115

115116
_Function has no arguments._
116117

pkg/src/public/bash-core.sh

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,10 @@
44
# @description Core functions for any Bash program
55

66
# @description Initiates global variables used by other functions
7+
# @deprecated
78
# @noargs
89
core.init() {
9-
if [ ${___global_bash_core_has_init__+x} ]; then
10-
return
11-
fi
12-
13-
___global_bash_core_has_init__=
14-
declare -Ag ___global_trap_table___=()
15-
declare -ag ___global_shopt_stack___=()
10+
core.util.init
1611
}
1712

1813
# @description Adds a handler for a particular `trap` signal or event. Noticably,
@@ -25,6 +20,9 @@ core.init() {
2520
# kill -USR1 $$
2621
# core.trap_remove 'some_handler' 'USR1'
2722
core.trap_add() {
23+
if ! [ ${___global_bash_core_has_init__+x} ]; then
24+
core.util.init
25+
fi
2826
local function="$1"
2927

3028
# validation
@@ -82,6 +80,9 @@ core.trap_add() {
8280
# kill -USR1 $$
8381
# core.trap_remove 'some_handler' 'USR1'
8482
core.trap_remove() {
83+
if ! [ ${___global_bash_core_has_init__+x} ]; then
84+
core.util.init
85+
fi
8586
local function="$1"
8687

8788
# validation
@@ -146,6 +147,9 @@ core.trap_remove() {
146147
# [[ 'variable' == @(foxtrot|golf|echo|variable) ]] && printf '%s\n' 'Woof!'
147148
# core.shopt_pop
148149
core.shopt_push() {
150+
if ! [ ${___global_bash_core_has_init__+x} ]; then
151+
core.util.init
152+
fi
149153
local shopt_action="$1"
150154
local shopt_name="$2"
151155

@@ -195,6 +199,10 @@ core.shopt_push() {
195199
# [[ 'variable' == @(foxtrot|golf|echo|variable) ]] && printf '%s\n' 'Woof!'
196200
# core.shopt_pop
197201
core.shopt_pop() {
202+
if ! [ ${___global_bash_core_has_init__+x} ]; then
203+
core.util.init
204+
fi
205+
198206
if (( ${#___global_shopt_stack___[@]} == 0 )); then
199207
printf '%s\n' "Error: core.shopt_pop: Unable to pop as nothing is in the shopt stack"
200208
return 1
@@ -330,6 +338,7 @@ core.should_color_output() {
330338
# @set REPLY The full path to the directory
331339
# @internal
332340
core.get_package_dir() {
341+
333342
# local start_dir="${1:-"${BASH_SOURCE[1]}"}"
334343

335344
# while [ ! -f 'basalt.toml' ] && [ "$PWD" != / ]; do

pkg/src/util/util.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
# shellcheck shell=bash
22

3+
core.util.init() {
4+
if [ ${___global_bash_core_has_init__+x} ]; then
5+
return
6+
fi
7+
8+
___global_bash_core_has_init__=
9+
declare -gA ___global_trap_table___=()
10+
declare -ga ___global_shopt_stack___=()
11+
}
12+
313
core.util.trap_handler_common() {
414
local signal_spec="$1"
515

tests/trap.bats

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ load './util/init.sh'
4646

4747
@test "core.trap_add adds trap function properly" {
4848
somefunction() { :; }
49-
core.init
5049
core.trap_add 'somefunction' 'USR1'
5150

5251
[ "${___global_trap_table___[nokey]}" != $'\x1Csomefunction' ]
@@ -56,7 +55,6 @@ load './util/init.sh'
5655
@test "core.trap_add adds function properly 2" {
5756
somefunction() { :; }
5857
somefunction2() { :; }
59-
core.init
6058
core.trap_add 'somefunction' 'USR1'
6159
core.trap_add 'somefunction2' 'USR1'
6260

@@ -66,7 +64,6 @@ load './util/init.sh'
6664

6765
@test "core.trap_add adds function properly 3" {
6866
somefunction() { :; }
69-
core.init
7067
core.trap_add 'somefunction' 'USR1' 'USR2'
7168

7269
[ "${___global_trap_table___[nokey]}" != $'\x1Csomefunction' ]
@@ -104,7 +101,6 @@ load './util/init.sh'
104101

105102
@test "core.trap_remove removes trap function properly" {
106103
somefunction() { :; }
107-
core.init
108104
core.trap_add 'somefunction' 'USR1'
109105
core.trap_remove 'somefunction' 'USR1'
110106

@@ -114,7 +110,6 @@ load './util/init.sh'
114110
@test "core.trap_remove removes trap function properly 2" {
115111
somefunction() { :; }
116112
somefunction2() { :; }
117-
core.init
118113
core.trap_add 'somefunction' 'USR1'
119114
core.trap_add 'somefunction2' 'USR1'
120115
core.trap_remove 'somefunction' 'USR1'
@@ -125,7 +120,6 @@ load './util/init.sh'
125120

126121
@test "core.trap_add removes function properly 3" {
127122
somefunction() { :; }
128-
core.init
129123
core.trap_add 'somefunction' 'USR1'
130124
core.trap_add 'somefunction' 'USR2'
131125
core.trap_remove 'somefunction' 'USR1' 'USR2'
@@ -137,7 +131,6 @@ load './util/init.sh'
137131
@test "core.trap_remove removes trap function properly 4" {
138132
somefunction() { :; }
139133
somefunction2() { :; }
140-
core.init
141134
core.trap_add 'somefunction' 'USR1'
142135
core.trap_add 'somefunction2' 'USR1'
143136
core.trap_remove 'somefunction2' 'USR1'

0 commit comments

Comments
 (0)