Browse Source

Revamp mkit

Make it

 *  more flexible,
 *  more readable,
 *  faster,
 *  take care of manpages (not fully done yet),
 *  take care of dist list,
 *  little bit more polished in various other things.
Alois Mahdal 9 years ago
parent
commit
38d7b93528
7 changed files with 251 additions and 179 deletions
  1. 1
    4
      Makefile
  2. 30
    12
      mkit/config.ini
  3. 30
    141
      mkit/include/build.sh
  4. 58
    0
      mkit/include/deploy.sh
  5. 90
    0
      mkit/include/ini.sh
  6. 40
    0
      mkit/include/mkit.sh
  7. 2
    22
      mkit/make

+ 1
- 4
Makefile View File

@@ -14,11 +14,8 @@ options:
14 14
 build:
15 15
 	@mkit/make build
16 16
 
17
-install_manpages: manpages
18
-	@mkit/make install_manpages
19
-
20 17
 manpages: build
21
-	@mkit/make manpages
18
+	@mkit/make build_manpages
22 19
 
23 20
 test:
24 21
 	@test/runtests

mkit/paths.ini → mkit/config.ini View File

@@ -1,32 +1,50 @@
1
-[roots]
2
-    bin     = /usr/bin
3
-    doc     = /usr/share/doc
4
-    man     = /usr/share/man
5
-    share   = /usr/share
6 1
 
7
-[groups]
2
+[main]
3
+
4
+    projname = ffoo
5
+
6
+    group = bin
7
+    group = doc
8
+    group = man
9
+    group = share
10
+
11
+    dist = config.mk
12
+    dist = LICENSE
13
+    dist = Makefile
14
+    dist = mkit
15
+    dist = README
16
+    dist = src
17
+    dist = test
18
+
19
+[modes]
8 20
     bin     = 755
9 21
     doc     = 644
10 22
     man     = 644
11 23
     share   = 644
12 24
 
13
-[bin]
25
+[roots]
26
+    bin     = /usr/bin
27
+    doc     = /usr/share/doc
28
+    man     = /usr/share/man
29
+    share   = /usr/share
30
+
31
+[files:bin]
14 32
     src/bin/ffdoc                       = ffdoc
15 33
     src/bin/fff                         = fff
16 34
     src/bin/ffoom                       = ffoom
17 35
 
18
-[doc]
36
+[files:doc]
19 37
     LICENSE                             = ffoo/LICENSE
20 38
     README                              = ffoo/README
21 39
     src/doc/examples/template.sh        = ffoo/examples/template.sh
22 40
     src/doc/examples/test_something.sh  = ffoo/examples/test_something.sh
23 41
     src/doc/ffoo.3.sh                   = ffoo/examples/ffoo.3.sh
24 42
 
25
-[man]
26
-    src/doc/fff.1.md                    = man1/fff.1
27
-    src/doc/ffoo.3.md                   = man1/ffoo.3
43
+[files:man]
44
+    src/doc/fff.1                       = man1/fff.1
45
+    src/doc/ffoo.3                      = man3/ffoo.3
28 46
 
29
-[share]
47
+[files:share]
30 48
     src/ffoo.sh                         = ffoo/ffoo.sh
31 49
     src/include/_pretty_color.sh        = ffoo/include/_pretty_color.sh
32 50
     src/include/_pretty_html.sh         = ffoo/include/_pretty_html.sh

mkit/include/common.sh → mkit/include/build.sh View File

@@ -1,8 +1,18 @@
1 1
 #!/bin/bash
2 2
 
3
-bailout() {
4
-    echo "$@" >&2
5
-    exit 3
3
+. mkit/include/ini.sh || die "cannot import ini.sh"
4
+
5
+
6
+build() {
7
+    #
8
+    # Add meat to all skeletons
9
+    #
10
+    local srcpath
11
+    find src -type f -name '*.skel' \
12
+     | while read srcpath;
13
+       do
14
+           build1 "$srcpath"
15
+       done
6 16
 }
7 17
 
8 18
 build1() {
@@ -24,16 +34,21 @@ build1() {
24 34
     echo $dstpath >> built.list
25 35
 }
26 36
 
27
-build() {
28
-    #
29
-    # Add meat to all skeletons
30
-    #
31
-    local srcpath
32
-    find -type f -name '*.skel' \
33
-     | while read srcpath;
34
-       do
35
-           build1 "$srcpath"
36
-       done
37
+build_manpages() {
38
+    local manfile mdfile
39
+    if command -v ronn >/dev/null;
40
+    then
41
+        ini lskeys "files:man" \
42
+          | while read manfile;
43
+            do
44
+                mdfile=$manfile.md
45
+                ronn -r $mdfile
46
+                echo $manfile >> built.list
47
+            done
48
+    else
49
+        echo "ronn is not installed"
50
+        return 1
51
+    fi
37 52
 }
38 53
 
39 54
 clean() {
@@ -46,34 +61,15 @@ clean() {
46 61
     } || :
47 62
 }
48 63
 
49
-configure() {
50
-    #
51
-    # Load variables fdom config.mk
52
-    #
53
-    tmp=$(mktemp)
54
-    sed -e 's/ = /=/' < config.mk > $tmp
55
-    . $tmp
56
-    rm -f $tmp
57
-    PREFIX="$(readlink -f "$PREFIX")"
58
-}
59
-
60
-die() {
61
-    #
62
-    # Exit with message and non-zero exit status
63
-    #
64
-    echo "$@" >&2
65
-    exit 4
66
-}
67
-
68 64
 dist() {
69 65
     #
70 66
     # Create distributable tarball
71 67
     #
72 68
     local version=$(get_version)
73
-    local dirname=ffoo-$version
69
+    local dirname=$MKIT_PROJNAME-$version
74 70
     mkdir -p $dirname
75 71
     local item
76
-    cp -R $(list_of_dist_items) $dirname
72
+    cp -R $(ini path "main:dist") $dirname
77 73
     sed -i -e "s/^VERSION = .*/VERSION = $version/" $dirname/config.mk
78 74
     tar -cf $dirname.tar $dirname
79 75
     gzip $dirname.tar
@@ -105,40 +101,6 @@ expand_includes() {
105 101
     '
106 102
 }
107 103
 
108
-expand_groups() {
109
-    #
110
-    # Read path groups from paths.ini and expand to install params
111
-    #
112
-    local group
113
-    local mode
114
-    cat "mkit/paths.ini" \
115
-      | read_section groups \
116
-      | while read line;
117
-        do
118
-            group=${line%=*}
119
-            mode=${line#$group=}
120
-            expand_paths "$group" "$mode"
121
-        done
122
-}
123
-
124
-expand_paths() {
125
-    #
126
-    # For each path of group $1, print all install params
127
-    #
128
-    local group=$1
129
-    local mode=$2
130
-    local root="$(get_root $group)"
131
-    local src dst
132
-    cat mkit/paths.ini \
133
-      | read_section $group \
134
-      | while read line;
135
-        do
136
-            src=${line%=*}
137
-            dst=${line#$src=}
138
-            echo "$src:$mode:$root/$dst"
139
-        done
140
-}
141
-
142 104
 expand_variables() {
143 105
     #
144 106
     # Expand variable values
@@ -150,16 +112,6 @@ expand_variables() {
150 112
     "
151 113
 }
152 114
 
153
-get_root() {
154
-    #
155
-    # Find out target rooot for group $1
156
-    #
157
-    local group="$1"
158
-    local varname="${group^^*}_DIR"
159
-    test -n "${!varname}" || die "missing in config.mk: $varname"
160
-    echo "${!varname}"
161
-}
162
-
163 115
 get_version() {
164 116
     #
165 117
     # Build semver version string with build metadata
@@ -234,66 +186,3 @@ get_version() {
234 186
     fi
235 187
     echo $version
236 188
 }
237
-
238
-install() {
239
-    #
240
-    # Install product
241
-    #
242
-    expand_groups \
243
-      | while read line;
244
-        do
245
-            src="$(cut -d: -f1 <<<"$line")"
246
-            mode="$(cut -d: -f2 <<<"$line")"
247
-            dst="$(cut -d: -f3- <<<"$line")"
248
-            command -p install -DTvm $mode $src "$dst"
249
-        done
250
-    test -f .autoclean && clean || :
251
-}
252
-
253
-read_section() {
254
-    #
255
-    # Read one INI section
256
-    #
257
-    local wnt=$1
258
-    local ok=false
259
-    grep '.' \
260
-      | grep -v '\s*#' \
261
-      | while read line;
262
-        do
263
-            case "$line" in
264
-                \[$wnt\]) ok=true;  continue ;;
265
-                \[*\])    ok=false; continue ;;
266
-            esac
267
-            $ok || continue
268
-            printf -- "%s\n" "$line"
269
-        done \
270
-      | sed -e 's/ *= */=/; s/ +$//; s/^//;'
271
-}
272
-
273
-route() {
274
-    #
275
-    # Call correct function based on $1
276
-    #
277
-    case $1 in
278
-        build|clean|dist|install|install_manpages|manpages|uninstall)
279
-            $1
280
-            ;;
281
-        *)
282
-            echo "usage: $(basename $0) build|clean|dist|install|uninstall" >&2
283
-    esac
284
-}
285
-
286
-uninstall() {
287
-    #
288
-    # Uninstall product
289
-    #
290
-    expand_groups \
291
-      | while read line;
292
-        do
293
-            src="$(cut -d: -f1 <<<"$line")"
294
-            mode="$(cut -d: -f2 <<<"$line")"
295
-            dst="$(cut -d: -f3- <<<"$line")"
296
-            rm -vf "$dst"
297
-        done
298
-
299
-}

+ 58
- 0
mkit/include/deploy.sh View File

@@ -0,0 +1,58 @@
1
+#!/bin/bash
2
+
3
+
4
+get_dst() {
5
+    #
6
+    # Find out target path for src file $2 of group $1
7
+    #
8
+    local grp=$1
9
+    local src=$2
10
+    echo "$(get_root $grp)/$(ini path "files:$group:$src")"
11
+}
12
+
13
+get_root() {
14
+    #
15
+    # Find out target rooot for group $1
16
+    #
17
+    local group="$1"
18
+    local varname="${group^^*}_DIR"
19
+    test -n "${!varname}" || die "missing in config.mk: $varname"
20
+    echo "${!varname}"
21
+}
22
+
23
+install() {
24
+    #
25
+    # Install product
26
+    #
27
+    local dst group mode src
28
+    ini path "main:group" \
29
+      | while read group;
30
+        do
31
+            mode=$(ini path "modes:$group")
32
+            ini lskeys "files:$group" \
33
+              | while read src;
34
+                do
35
+                    dst=$(get_dst "$group" "$src")
36
+                    command -p install -DTvm $mode $src "$dst"
37
+                done
38
+        done
39
+    test -f .autoclean && clean || :
40
+}
41
+
42
+uninstall() {
43
+    #
44
+    # Uninstall product
45
+    #
46
+    local dst group src
47
+    ini path "main:group" \
48
+      | while read group;
49
+        do
50
+            ini lskeys "files:$group" \
51
+              | while read src;
52
+                do
53
+                    dst=$(get_dst "$group" "$src")
54
+                    rm -vf "$dst"
55
+                done
56
+        done
57
+
58
+}

+ 90
- 0
mkit/include/ini.sh View File

@@ -0,0 +1,90 @@
1
+#!/bin/bash
2
+
3
+_ini_cat() {
4
+    #
5
+    # Load ini file to our cache (RAM disk) and cat it
6
+    #
7
+    # User should set MKIT_INI_CACHE to a ram-based file
8
+    # if mktemp cannot be relied upon.
9
+    #
10
+    if test -z "$_MKIT_INI_CACHED";
11
+    then
12
+        MKIT_INI_CACHE=${MKIT_INI_CACHE:-$(mktemp)}
13
+        _MKIT_INI_CACHED=$MKIT_INI_CACHE
14
+        test -r $MKIT_INI || die "cannot read MKIT_INI: $MKIT_INI"
15
+        cp $MKIT_INI $_MKIT_INI_CACHED
16
+    fi
17
+    cat $_MKIT_INI_CACHED
18
+}
19
+
20
+ini() {
21
+    #
22
+    # do ini operation
23
+    #
24
+    local op=$1
25
+    local arg=$2
26
+    local fn
27
+    case $op in
28
+        lskeys) fn=_ini_lskeys   ;;
29
+        sec)    fn=_ini_grepsec  ;;
30
+        key)    fn=_ini_grepkey  ;;
31
+        path)   fn=_ini_greppath ;;
32
+        *)      die "incorrect use of \`ini()\`"
33
+    esac
34
+    _ini_cat | $fn $arg
35
+}
36
+
37
+_ini_grepkey() {
38
+    #
39
+    # Read key from a section
40
+    #
41
+    local wnt=$1
42
+    grep '.' \
43
+      | grep -v '\s*#' \
44
+      | sed -e 's/ *= */=/; s/ +$//; s/^//;' \
45
+      | grep -e "^$wnt=" \
46
+      | cut -d= -f2-
47
+}
48
+
49
+_ini_greppath() {
50
+    #
51
+    # Read key from the right section
52
+    #
53
+    # E.g. `files:share:my/lib.sh` should read
54
+    #
55
+    #     [files:share]
56
+    #         my/lib.sh   = proj/my/lib.sh
57
+    #
58
+    local wnt=$1
59
+    local wntkey=${wnt##*:}
60
+    local wntsec=${wnt%:$wntkey}
61
+    _ini_grepsec $wntsec | _ini_grepkey $wntkey
62
+}
63
+
64
+_ini_grepsec() {
65
+    #
66
+    # Read one INI section
67
+    #
68
+    local wnt=$1
69
+    local ok=false
70
+    grep '.' \
71
+      | grep -v '\s*#' \
72
+      | while read line;
73
+        do
74
+            case "$line" in
75
+                \[$wnt\]) ok=true;  continue ;;
76
+                \[*\])    ok=false; continue ;;
77
+            esac
78
+            $ok || continue
79
+            printf -- "%s\n" "$line"
80
+        done \
81
+      | sed -e 's/ *= */=/; s/ +$//; s/^//;'
82
+}
83
+
84
+_ini_lskeys() {
85
+    #
86
+    # List keys from a section
87
+    #
88
+    local sct=$1
89
+    _ini_grepsec $sct | cut -d= -f1 | sort | uniq
90
+}

+ 40
- 0
mkit/include/mkit.sh View File

@@ -0,0 +1,40 @@
1
+#!/bin/bash
2
+
3
+. mkit/include/build.sh  || die "cannot import ini.sh"
4
+. mkit/include/deploy.sh || die "cannot import ini.sh"
5
+. mkit/include/ini.sh    || die "cannot import ini.sh"
6
+
7
+MKIT_INI=${MKIT_INI:-mkit/config.ini}
8
+MKIT_PROJNAME=$(ini path main:projname)
9
+
10
+configure() {
11
+    #
12
+    # Load variables fdom config.mk
13
+    #
14
+    tmp=$(mktemp)
15
+    sed -e 's/ = /=/' < config.mk > $tmp
16
+    . $tmp
17
+    rm -f $tmp
18
+    PREFIX="$(readlink -f "$PREFIX")"
19
+}
20
+
21
+die() {
22
+    #
23
+    # Exit with message and non-zero exit status
24
+    #
25
+    echo "$@" >&2
26
+    exit 4
27
+}
28
+
29
+route() {
30
+    #
31
+    # Call correct function based on $1
32
+    #
33
+    case $1 in
34
+        build|build_manpages|clean|dist|install|uninstall)
35
+            $1
36
+            ;;
37
+        *)
38
+            echo "usage: $(basename $0) build|clean|dist|install|uninstall" >&2
39
+    esac
40
+}

+ 2
- 22
mkit/make View File

@@ -6,7 +6,7 @@ die() {
6 6
     echo "$@" && exit 9
7 7
 }
8 8
 
9
-. mkit/include/common.sh || die "this must be run from project root"
9
+. mkit/include/mkit.sh || die "this must be run from project root"
10 10
 
11 11
 configure
12 12
 
@@ -14,26 +14,6 @@ BIN_DIR=${DESTDIR}${PREFIX}/bin
14 14
 MAN_DIR=${DESTDIR}${MANPREFIX}
15 15
 DOC_DIR=${DESTDIR}${PREFIX}/share/doc
16 16
 SHARE_DIR=${DESTDIR}${PREFIX}/share
17
-
18
-list_of_dist_items() {
19
-    echo config.mk
20
-    echo LICENSE
21
-    echo Makefile
22
-    echo mkit
23
-    echo README
24
-    echo src
25
-    echo test
26
-}
27
-
28
-manpages() {
29
-    ronn -r src/doc/*.[1-9].md
30
-    ls src/doc/*.[1-9] >> built.list
31
-}
32
-
33
-install_manpages() {
34
-    cp -vr src/doc/*.1 $MAN_DIR/man1
35
-    cp -vr src/doc/*.3 $MAN_DIR/man3
36
-    test -f .autoclean && clean || :
37
-}
17
+LIBEXEC_DIR=${DESTDIR}${PREFIX}/libexec
38 18
 
39 19
 route "$@"