Browse Source

Revamp ffood() and add listing features

ffood now employs simpler algorithm to choose file to import and also
is able to list module contents.
Alois Mahdal 10 years ago
parent
commit
0fdb17dd27
1 changed files with 52 additions and 22 deletions
  1. 52
    22
      ffood.sh.in

+ 52
- 22
ffood.sh.in View File

@@ -1,38 +1,68 @@
1 1
 #!/bin/bash
2 2
 
3 3
 FFOOD_DATA_DIR=${FFOOD_DATA_DIR:-$HOME/.ffood}
4
+FFOOD_DIR=${FFOOD_DIR:-__FFOOD_DIR__}
4 5
 FFOOD_ARTIFACTS_DIR=${FFOOD_ARTIFACTS_DIR:-artifacts}
5 6
 FFOOD_DEBUG=${FFOOD_DEBUG:-false}
7
+FFOOD_INCLUDE=${FFOOD_INCLUDE:-$FFOOD_DIR/include}
6 8
 FFOOD_VERBOSE=${FFOOD_VERBOSE:-false}
7 9
 FFOOD_VERSION=__VERSION__
8 10
 
9
-die() {
10
-    echo "$@" >&2
11
-    exit 1
12
-}
13
-
14 11
 ffood() {
15
-    local ffood_dir="${FFOOD_DIR:-__FFOOD_DIR__}"
16 12
     case $1 in
13
+        __debug)
14
+            $FFOOD_DEBUG || return 0
15
+            shift
16
+            echo "debug: $@" >&2
17
+            ;;
18
+        __die)
19
+            shift
20
+            echo "$@" >&2
21
+            exit 1
22
+            ;;
23
+        doc)
24
+            ffood __die "not implemented (sorry...)"
25
+            ;;
17 26
         import)
18
-            if test -f "$ffood_dir/include/$2.sh";
19
-            then
20
-                . "$ffood_dir/include/$2.sh";
21
-                return $?
22
-            elif test -n "$FFOOD_INCLUDE" -a -f "$FFOOD_INCLUDE/$2.sh";
23
-            then
24
-                . "$FFOOD_INCLUDE/$2.sh"
25
-                return $?
26
-            elif test -f "./$2.sh";
27
-            then
28
-                . "./$2.sh"
29
-                return $?
30
-            else
31
-                die "cannot find library: $2"
32
-            fi
27
+            ffood __debug "importing module $2"
28
+            local mfile=$(ffood _list_mfiles_like $2 | head -1)
29
+            test -n "$mfile" || ffood __die "cannot find module $2"
30
+            ffood __debug "found module file: $mfile"
31
+            . $mfile
32
+            return $?
33
+            ;;
34
+        _list_all_functions)
35
+            local mfile
36
+            ffood _list_all_mfiles | while read mfile;
37
+            do
38
+                ffood _list_functions_in_mfile "$mfile"
39
+            done
40
+            ;;
41
+        _list_all_modules)
42
+            local mp mfn;
43
+            ffood _list_all_mfiles | while read mp;
44
+            do
45
+                mfn="${mp##*/}"
46
+                echo "${mfn%%.sh}"
47
+            done
48
+            ;;
49
+        _list_all_mfiles)
50
+            {
51
+                echo $FFOOD_INCLUDE/*.sh | tr ' ' '\n'
52
+                test -n "$FFOOD_PATH" && echo $FFOOD_PATH/*.sh
53
+            } | tr ' ' '\n'
54
+            ;;
55
+        _list_mfiles_like)
56
+            test -f "$FFOOD_INCLUDE/$2.sh"                  && echo "$FFOOD_INCLUDE/$2.sh"
57
+            test -n "$FFOOD_PATH" -a -f "$FFOOD_PATH/$2.sh" && echo "$FFOOD_PATH/$2.sh"
58
+            ;;
59
+        _list_functions_in_mfile)
60
+            local mfile="$2"
61
+            grep -HE '^[[:alnum:]_]+\(\) \{' "$mfile" \
62
+              | sed -e 's/^.*\///; s/\.sh:/./; s/(.*//'
33 63
             ;;
34 64
         *)
35
-            die "unknown ffood command: $1"
65
+            ffood __die "unknown ffood command: $1"
36 66
             ;;
37 67
     esac
38 68
 }