gabr 2.0.13 → 2.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/example/usage.sh CHANGED
@@ -2,24 +2,13 @@
2
2
  # @file usage.sh
3
3
  #
4
4
  # @brief Usage.sh contains a example on how to reimplement the usage behavior of gabr.
5
+
5
6
  set +x
6
7
  declare stack="$(declare -f -F)" # start keeping count of stack (usage.sh will do difference check)
7
8
  if [ -n "${GABR_DEBUG_MODE:-}" ]; then
8
9
  set -x
9
10
  fi
10
11
 
11
-
12
- usage() {
13
- set +x
14
- if [ $# -eq 0 ]; then
15
- set -- ${prevArgs[@]:-}
16
- fi
17
- local stack="${stack:-$(declare -F)}"
18
- local -a bashsource=(${BASH_SOURCE[@]} ${@})
19
- local fullCommand="$(IFS=' '; echo ${@##*usage})" # strip 'usage' of $@
20
- echo "Usage: ${fullCommand}$(_usageFiles)$(_usageScope) ${example:-}" >&2
21
- }
22
-
23
12
  function _filename(){
24
13
  local filename=${1:-$BASH_SOURCE}
25
14
  local pathJuggle=${filename##*/};
@@ -41,12 +30,13 @@ function _usageScope(){ # Prints all functions added to scope by gabr
41
30
  set -x
42
31
  fi
43
32
  if [ -n "${usageScope:-}" ] && [ ${#usageScope} -gt 1 ]; then
44
- usageScope=" [${usageScope:0: -1}]" # strip last |
33
+ usageScope=" [${usageScope%|}]" # strip last |
45
34
  fi
46
35
  fi
47
36
  echo "${usageScope:-}"
48
37
  }
49
38
 
39
+
50
40
  function _usageFiles(){
51
41
  if ! [ "${usageFiles+set}" = "set" ]; then
52
42
  local findString=""
@@ -56,17 +46,28 @@ function _usageFiles(){
56
46
  done
57
47
  local usageFiles=$(
58
48
  IFS=' '
59
- ! find . -maxdepth 1 ${findString} -name '*'${ext:-.sh} |
49
+ ! find . -maxdepth 1 ${findString} -type f \( -iname "*.sh" ! -iname ".*" \) |
60
50
  cut -c3- | # cut ./
61
51
  rev |
62
52
  cut -c4- | # cut .sh
63
53
  rev |
64
- awk '!/^\./{ print $0 }' | # hide dot prefixed
65
54
  tr '\n' "|"
66
55
  );
67
56
  if [ -n "${usageFiles:-}" ] && [ ${#usageFiles} -gt 1 ]; then
68
- usageFiles=" [${usageFiles:0: -1}]" # strip last |
57
+ usageFiles=" [${usageFiles%|}]" # strip last |
69
58
  fi
70
59
  fi
71
60
  echo "${usageFiles:-}"
72
- }
61
+ }
62
+
63
+
64
+ usage() {
65
+ set +x
66
+ if [ $# -eq 0 ]; then
67
+ set -- ${prevArgs[@]:-}
68
+ fi
69
+ local stack="${stack:-$(declare -F)}"
70
+ local -a bashsource=(${BASH_SOURCE[@]} ${@})
71
+ local fullCommand="$(IFS=' '; echo ${@##*usage})" # strip 'usage' of $@
72
+ echo "Usage: ${fullCommand}$(_usageFiles)$(_usageScope) ${example:-}" >&2
73
+ }
package/gabr.linux.sh CHANGED
@@ -15,7 +15,7 @@
15
15
  # @exitcode 0 If successfull
16
16
  # @exitcode >0 On failure
17
17
  #
18
- if [ ${BASH_VERSION:0:1} -ge 4 ] && [ ${BASH_VERSION:2:1} -ge 4 ]
18
+ if [ ${BASH_VERSION:0:1} -ge 4 ]
19
19
  then
20
20
  function gabr() { # A function to run other functions
21
21
  local fn file
@@ -56,8 +56,17 @@ ${prevArgs[@]} [directory | file] function [arguments] -- A function to call oth
56
56
  ! [[ -v prevArgs ]] && local -a prevArgs=()
57
57
  # helpers
58
58
  _isFn(){ [[ $(type -t ${1:-$fn}) = function ]]; }
59
- _canSource(){ ! [[ -v files[$file] ]] || ! [[ ${files[$file]} = $file ]]; }
60
59
  _isDebug(){ [[ -v GABR_DEBUG_MODE ]]; }
60
+ _listFiles(){
61
+ find . -maxdepth 3 -type f \( -iname "*${ext:-'.sh'}" ! -iname ".*" \) ! -path "*/.*"; # hide dot prefix
62
+ }
63
+ _listFunctions(){
64
+ declare -f -F |
65
+ tr ' ' '\n' |
66
+ sort |
67
+ uniq -u |
68
+ awk '! /^_/{print $0}' # hide underscore prefix
69
+ }
61
70
  # begin processing arguments
62
71
  if [ $# -eq 0 ]; then
63
72
  set -- $default
@@ -82,30 +91,28 @@ ${prevArgs[@]} [directory | file] function [arguments] -- A function to call oth
82
91
  ${fn} ${@:-}; # call the function
83
92
  _isDebug && set +x
84
93
  break
85
- elif [[ -f ./${fn}${ext} ]]; then # allow a file with extension
94
+ elif [[ ${#fn} -ge 2 && ${fn:0:2} = './' ]]; then # allow redundant ./ in arguments
95
+ shift
96
+ set - ${fn:2} ${@:-}
97
+ elif [[ ${#fn} -ge ${#ext} && ${fn:$(( ${#fn}-${#ext} ))} = ${ext} ]]; then # allow redundant .sh in arguments
98
+ shift
99
+ set - ${fn:: $(( - ${#ext} ))} ${@:-}
100
+ elif [[ -r ./${fn}${ext} ]]; then # allow a file with extension and read permissions
86
101
  file=${fn}${ext}
87
- if ! _canSource; then
88
- set -- '-' "'$file' is already sourced by argument $fn" 1>&2
89
- continue
90
- fi
91
102
  files+=([$file]=$file)
92
- if ! [ -r "${file:-}" ]; then
93
- set -- '-' "'$file' is not a readable file" 1>&2
94
- continue
95
- fi
96
103
  prevArgs+=($1)
97
104
  shift
98
105
  args=(${@:-})
99
- _isDebug && set -x
100
- . ${fn}${ext} # source the file
106
+ _isDebug && set -x
107
+ . ./${fn}${ext} # source the file
101
108
  _isDebug && set +x
102
109
  _isFn $fn && set -- $fn ${@:-}
103
110
  [ $# -eq 0 ] && _isFn $default && set -- ${default} ${prevArgs[@]}
104
- elif [[ -f ./${fn} ]]; then # allow a file without extension
111
+ elif [[ -f ./${fn} && -x ./${fn} ]]; then # allow a file without extension and executable permissions
105
112
  prevArgs+=($1)
106
113
  shift
107
114
  args=(${@:-})
108
- exec ${fn} $@ # execute the file
115
+ exec ./${fn} $@ # execute the file
109
116
  break
110
117
  elif [[ -d ./${fn} ]]; then # allow a directory
111
118
  cd ./$fn
@@ -122,19 +129,23 @@ $default() {
122
129
  EOF
123
130
  _isFn && continue
124
131
  set -- '-' "Could not generate default function"
125
- elif [ $# -gt 1 ]; then # shift arguments
132
+ elif [[ $# -gt 1 ]]; then # shift arguments
126
133
  prevArgs+=($1)
127
134
  shift
128
- elif [[ -f ./${default}${ext} ]] && [[ ${dir: $(( -${#fn}-1 ))} = /$fn ]]; then
135
+ elif [[ -r ./${default}${ext} && ${dir: $(( -${#fn}-1 ))} = /$fn ]]; then # allow a default fallback in a directory
129
136
  prevArgs+=($1)
130
137
  set -- ${default:-usage} ${prevArgs[@]}
131
- elif [ $# -eq 1 ] && ! [[ ${#files[@]} -eq 0 ]]; then
132
- set -- '-' "\
133
- '$fn' could not be used to call a function in file(s): $(echo ${!files[@]})"
134
- elif [ $# -eq 1 ] && [[ -v dir ]]; then
135
- set -- '-' "'$fn' could not be used to call a function in directory $dir"
138
+ elif [[ $# -eq 1 && ! ${#files[@]} -eq 0 ]]; then
139
+ set -- '-' \
140
+ "Argument '$fn' could not be used to call a function in file(s):" \
141
+ ${!files[@]} \
142
+ "Below options are valid:" \
143
+ $(_listFunctions)
136
144
  else
137
- set -- '-' "'$fn' could not be used as file, function or directory"
145
+ set -- '-' \
146
+ "Argument '$fn' could not be used as file, function or directory." \
147
+ "Below options are valid:" \
148
+ $(printf ' %s\n' $(_listFiles))
138
149
  fi
139
150
  done
140
151
  return;
package/gabr.sh CHANGED
@@ -16,7 +16,7 @@
16
16
  # @exitcode >0 On failure
17
17
  #
18
18
  # we can source linux version if available (has minor benefits like file checking)
19
- if [ ${BASH_VERSION:0:1} -ge 4 ] && [ ${BASH_VERSION:2:1} -ge 4 ] && [[ -r "${BASH_SOURCE}.linux" || -r "${BASH_SOURCE%\.sh*}.linux.sh" ]]; then
19
+ if { [ ${BASH_VERSION:0:1} -ge 5 ] || { [ ${BASH_VERSION:0:1} -eq 4 ] && [ ${BASH_VERSION:2:1} -ge 4 ]; }; } && [[ -r "${BASH_SOURCE}.linux" || -r "${BASH_SOURCE%\.sh*}.linux.sh" ]]; then
20
20
  . "${BASH_SOURCE%\.sh*}.linux$([ "${BASH_SOURCE}" != "${BASH_SOURCE%\.sh*}" ] && echo .sh)"
21
21
  else
22
22
  function gabr() { # A function to run other functions
@@ -57,16 +57,18 @@ ${prevArgs[@]} [directory | file] function [arguments] -- A function to call oth
57
57
  ! [ ${#prevArgs[@]} -eq 0 ] && local -a prevArgs=()
58
58
  # helpers
59
59
  _isFn(){ [ "$(type -t ${1:-$fn})" = 'function' ]; }
60
- _canSource(){
61
- for _file in ${files[@]:-}; do
62
- if [ $_file = $file ]; then
63
- [ $_file = $file ]
64
- fi
65
- done
66
- }
67
60
  _isDebug(){ [ -n "${GABR_DEBUG_MODE:-}" ]; }
68
61
  _isRoot()( [ "${dir:0:${#root}}" = "$root" ] || [ "$root" = "$PWD" ] && [ "." = "." ]; )
69
-
62
+ _listFiles(){
63
+ find . -maxdepth 3 -type f \( -iname "*${ext:-'.sh'}" ! -iname ".*" \) ! -path "*/.*"; # hide dot prefix
64
+ }
65
+ _listFunctions(){
66
+ declare -f -F |
67
+ tr ' ' '\n' |
68
+ sort |
69
+ uniq -u |
70
+ awk '! /^_/{print $0}' # hide underscore prefix
71
+ }
70
72
  # begin processing arguments
71
73
  if [ $# -eq 0 ]; then
72
74
  set -- $default
@@ -91,17 +93,15 @@ ${prevArgs[@]} [directory | file] function [arguments] -- A function to call oth
91
93
  ${fn} ${@:-}; # call the function
92
94
  _isDebug && set +x
93
95
  break
94
- elif [ -f "./${fn}${ext}" ]; then # allow a file with extension
96
+ elif [ ${#fn} -ge 2 ] && [ "${fn:0:2}" = './' ]; then # allow redundant ./ in arguments
97
+ shift
98
+ set - ${fn:2} ${@:-}
99
+ elif [ ${#fn} -ge ${#ext} ] && [ "${fn:$(( ${#fn}-${#ext} ))}" = "${ext}" ]; then # allow redundant .sh in arguments
100
+ shift
101
+ set - ${fn:0:$(( ${#fn}-${#ext} ))} ${@:-}
102
+ elif [ -r "./${fn}${ext}" ]; then # allow a file with extension and read permissions
95
103
  file=${fn}${ext}
96
- if ! _canSource; then
97
- set -- '-' "'$file' is already sourced by argument $fn" 1>&2
98
- continue
99
- fi
100
104
  files+=($file)
101
- if ! [ -r "${file:-}" ]; then
102
- set -- '-' "'$file' is not a readable file" 1>&2
103
- continue
104
- fi
105
105
  prevArgs+=($1)
106
106
  shift
107
107
  args=(${@:-})
@@ -110,11 +110,11 @@ ${prevArgs[@]} [directory | file] function [arguments] -- A function to call oth
110
110
  _isDebug && set +x
111
111
  _isFn && set -- $fn ${@:-}
112
112
  [ $# -eq 0 ] && set -- ${default} ${prevArgs[@]}
113
- elif [ -f "./${fn}" ]; then # allow a file without extension
113
+ elif [ -f "./${fn}" ] && [ -x "./${fn}" ]; then # allow a file without extension and executable permissions
114
114
  prevArgs+=($1)
115
115
  shift
116
116
  args=(${@:-})
117
- exec ${fn} $@ # execute the file
117
+ exec ./${fn} $@ # execute the file
118
118
  break
119
119
  elif [ -d "./${fn}" ]; then # allow a directory
120
120
  cd ./$fn
@@ -135,16 +135,20 @@ EOF
135
135
  elif [ $# -gt 1 ]; then # shift arguments
136
136
  prevArgs+=($1)
137
137
  shift
138
- elif [ $# -eq 1 ] && [ -f "./${default}${ext}" ] && [ "${dir: $(( -${#fn}-1 ))}" = "/$fn" ]; then
138
+ elif [ $# -eq 1 ] && [ -r "./${default}${ext}" ] && [ "${dir: $(( -${#fn}-1 ))}" = "/$fn" ]; then
139
139
  prevArgs+=($1)
140
140
  set -- ${default:-usage} ${prevArgs[@]}
141
141
  elif [ $# -eq 1 ] && ! [ ${#files[@]} -eq 0 ]; then
142
- set -- '-' "\
143
- '$fn' could not be used to call a function in file(s): $(echo ${!files[@]})"
144
- elif [ $# -eq 1 ] && [ -n "${dir:-}" ]; then
145
- set -- '-' "'$fn' could not be used to call a function in directory $dir"
142
+ set -- '-' \
143
+ "Argument '$fn' could not be used to call a function in file(s):" \
144
+ ${files[@]} \
145
+ "Below options are valid:" \
146
+ $(_listFunctions)
146
147
  else
147
- set -- '-' "'$fn' could not be used as file, function or directory"
148
+ set -- '-' \
149
+ "Argument '$fn' could not be used as file, function or directory." \
150
+ "Below options are valid:" \
151
+ $(printf ' %s\n' $(_listFiles))
148
152
  fi
149
153
  done
150
154
  return;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gabr",
3
- "version": "2.0.13",
3
+ "version": "2.1.1",
4
4
  "description": "Make Bash functions accessible with gabr.sh",
5
5
  "main": "gabr.sh",
6
6
  "scripts": {
@@ -32,7 +32,6 @@
32
32
  },
33
33
  "homepage": "https://github.com/nicobrinkkemper/gabr.sh#readme",
34
34
  "devDependencies": {
35
- "release-it": "^12.3.0"
36
- },
37
- "dependencies": {}
35
+ "release-it": "^19.2.4"
36
+ }
38
37
  }
package/say.sh ADDED
@@ -0,0 +1,10 @@
1
+ printf '%s' 'saying '
2
+ function hi(){
3
+ printf '%s' 'hi'
4
+ }
5
+ function greet(){
6
+ printf '%s' 'greetings'
7
+ }
8
+ function bye(){
9
+ printf '%s' 'bye'
10
+ }
package/start/start.sh ADDED
@@ -0,0 +1,11 @@
1
+ printf '%s' 'saying '
2
+ function hi(){
3
+ printf '%s' 'hi'
4
+ }
5
+ function greet(){
6
+ printf '%s' 'greetings'
7
+ }
8
+
9
+ function start(){
10
+ printf '%s' 'start'
11
+ }
package/test/gabr.bats CHANGED
@@ -20,20 +20,23 @@ if ! [ -f "./gabr.sh" ]; then
20
20
  fi
21
21
 
22
22
 
23
- @test "Gabr can find a file and run it's functions" {
23
+ @test "Gabr can find a file and run its functions" {
24
24
  echo "\
25
- printf %s sourced
26
- function sayhi(){
27
- printf %s hi
25
+ printf '%s' 'saying '
26
+ function hi(){
27
+ printf '%s' 'hi'
28
28
  }
29
- function saybye(){
30
- printf %s bye
31
- }" > ./sayhi.sh
29
+ function greet(){
30
+ printf '%s' 'greetings'
31
+ }
32
+ function bye(){
33
+ printf '%s' 'bye'
34
+ }" > ./say.sh
32
35
  source ./gabr.sh
33
- local result="$(gabr ./sayhi sayhi) $(gabr ./sayhi) $(gabr sayhi) $(gabr ./sayhi saybye)"
36
+ local result="$(gabr say hi) $(gabr ./say greet) $(gabr ./say.sh bye)"
34
37
  echo failed-result="\"${result}\"" 1>&2
35
38
  trap 'rm -f ./sayhi.sh' RETURN
36
- [ "$result" = 'sourcedhi sourced sourcedhi sourcedbye' ]
39
+ [ "$result" = 'saying hi saying greetings saying bye' ]
37
40
  }
38
41
 
39
42
  @test "Gabr errors the same return code" {
@@ -139,7 +142,7 @@ function baa()(
139
142
  [ "${output}" = "${hack}" ]
140
143
  }
141
144
 
142
- @test "gabr does not walk over a error" {
145
+ @test "gabr does not walk over an error" {
143
146
  function dontwalkover()(
144
147
  return1
145
148
  echo nowido
@@ -163,10 +166,21 @@ function whatdidisay(){
163
166
  source ./gabr.sh
164
167
  run gabr whatdidisay ' jim ' " has long " " cheeks "
165
168
  debug
166
- trap 'rm -f ./whatdidisay.sh' RETURN
167
169
  [ "$output" = ' jim has long cheeks ' ]
168
170
  }
169
171
 
172
+ @test "Gabr allows for redundant but more precise alternatives" {
173
+ source ./gabr.sh
174
+ run gabr whatdidisay.sh '.sh is redundant'
175
+ debug
176
+ [ "$output" = '.sh is redundant' ]
177
+ run gabr ./whatdidisay.sh './ and .sh is redundant'
178
+ debug
179
+ trap 'rm -f ./whatdidisay.sh' RETURN
180
+ [ "$output" = './ and .sh is redundant' ]
181
+ }
182
+
183
+
170
184
  @test "Gabr sees tabs as separator" {
171
185
  echo "\
172
186
  function spectabular(){
package/usage.sh CHANGED
@@ -8,9 +8,5 @@ Try out:
8
8
  - gabr test
9
9
  - gabr example docker test 3.2
10
10
  Usage: ${usage:-'not set'}" >&2
11
- if [ -n "${GABR_ROOT:-}" ]; then
12
- printf '%s\n' "\
13
- # You might be seeing this message because GABR_ROOT=$GABR_ROOT. It doesn't have to be set
14
- to the location of this repository, but your own." >&2
15
- fi
11
+
16
12
  }
@@ -1,53 +0,0 @@
1
- Copyright (c) 2017 bats-core contributors
2
-
3
- Permission is hereby granted, free of charge, to any person obtaining
4
- a copy of this software and associated documentation files (the
5
- "Software"), to deal in the Software without restriction, including
6
- without limitation the rights to use, copy, modify, merge, publish,
7
- distribute, sublicense, and/or sell copies of the Software, and to
8
- permit persons to whom the Software is furnished to do so, subject to
9
- the following conditions:
10
-
11
- The above copyright notice and this permission notice shall be
12
- included in all copies or substantial portions of the Software.
13
-
14
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
-
22
- ---
23
-
24
- * [bats-core] is a continuation of [bats]. Copyright for portions of the
25
- bats-core project are held by Sam Stephenson, 2014 as part of the project
26
- [bats], licensed under MIT:
27
-
28
- Copyright (c) 2014 Sam Stephenson
29
-
30
- Permission is hereby granted, free of charge, to any person obtaining
31
- a copy of this software and associated documentation files (the
32
- "Software"), to deal in the Software without restriction, including
33
- without limitation the rights to use, copy, modify, merge, publish,
34
- distribute, sublicense, and/or sell copies of the Software, and to
35
- permit persons to whom the Software is furnished to do so, subject to
36
- the following conditions:
37
-
38
- The above copyright notice and this permission notice shall be
39
- included in all copies or substantial portions of the Software.
40
-
41
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
42
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
43
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
44
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
45
- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
46
- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
47
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
48
-
49
- For details, please see the [version control history][commits].
50
-
51
- [bats-core]: https://github.com/bats-core/bats-core
52
- [bats]:https://github.com/sstephenson/bats
53
- [commits]:https://github.com/bats-core/bats-core/commits/master