Browse Source

Update MKit to v0.0.34

Alois Mahdal 6 years ago
parent
commit
c839b88602

+ 66
- 41
utils/mkit/include/build.sh View File

@@ -26,45 +26,70 @@ __build1_ftype() {
26 26
     #
27 27
     local ftype=$1      # file/builder type
28 28
     case $ftype in
29
-        MKIT_COMMON)    __expand_tokens "tokens" ;;
30
-        rpmstuff)       __expand_tokens "tokens" "rpmstuff:tokens" ;;
31
-        debstuff)       __expand_tokens "tokens" "debstuff:tokens" ;;
29
+        MKIT_COMMON)    __expand_macros "macros" ;;
30
+        rpmstuff)       __expand_macros "macros" "rpmstuff:macros" ;;
31
+        debstuff)       __expand_macros "macros" "debstuff:macros" ;;
32 32
         *)              die "unknown file type: $ftype" ;;
33 33
     esac
34 34
 }
35 35
 
36
-__expand_tokens() {
37
-    #
38
-    # Read stdin, expanding tokens from sections $@
39
-    #
40
-    local script        # sed script cache
41
-    local section       # each section to expand tokens from
42
-    local varname       # each token name
43
-    local varvalue      # each token value
44
-    script=$(mktemp --tmpdir mkit-tmp.XXXXXXXXXX)
45
-    {
46
-        for section in "$@"; do
47
-            debug_var section
48
-            ini lskeys "$section" \
49
-              | while read -r varname; do
50
-                    varvalue="$(ini 1value "$section:$varname" | __qfs )"
51
-                    echo "s|$varname|$varvalue|g;"
52
-                    debug_var varname varvalue
53
-                done
36
+__expand_line() {
37
+    #
38
+    # Expand macro from $MacroMap in single line $1
39
+    #
40
+    # If macro value has multiple lines, repeat original line with
41
+    # different substitution.
42
+    #
43
+    # E.g. if macro value is "foo\nbar" and macro name is __FOO__,
44
+    # line `see: "__FOO__"` will expand to two lines: `see: "foo"`
45
+    # and `see: "bar"`.
46
+    #
47
+    local line=$1   # line to process
48
+    local mname     # macro name
49
+    local mvline    # line of macro value
50
+    local xline     # expanded line
51
+    xline=$line
52
+    for mname in "${!MacroMap[@]}"; do
53
+        if ! test "${line//$mname}" == "$line"; then
54
+            xline=$(
55
+                while IFS= read -r mvline; do
56
+                    echo "${line//$mname/$mvline}"
57
+                done <<<"${MacroMap[$mname]}"
58
+            )
59
+        fi
60
+        line=$xline
61
+    done
62
+    echo "$xline"
63
+    return 1
64
+}
65
+
66
+__expand_macros() {
67
+    #
68
+    # Read stdin, expanding macros from sections $@
69
+    #
70
+    local section       # each section to expand macros from
71
+    local line          # each line on stdin
72
+    local mname         # each macro name
73
+    local -A MacroMap   # macro value map
74
+    MacroMap[__MKIT_PROJ_NAME__]=$(ini 1value project:name)
75
+    MacroMap[__MKIT_PROJ_CODENAME__]=$(ini 1value project:codename)
76
+    MacroMap[__MKIT_PROJ_LICENSE__]=$(ini 1value project:license)
77
+    MacroMap[__MKIT_PROJ_PKGNAME__]=$(ini 1value project:pkgname)
78
+    MacroMap[__MKIT_PROJ_TAGLINE__]=$(ini 1value project:tagline)
79
+    MacroMap[__MKIT_PROJ_MAINTAINER__]=$(ini 1value project:maintainer)
80
+    MacroMap[__MKIT_PROJ_VCS_BROWSER__]=$(ini 1value project:vcs_browser)
81
+    MacroMap[__MKIT_PROJ_GIT_LASTHASH__]=$(__cached git_lasthash)
82
+    MacroMap[__MKIT_PROJ_VERSION__]=$(__cached semver)
83
+    MacroMap[__MKIT_SELF_VERSION__]=$MKIT_VERSION
84
+    for section in "$@"; do
85
+        for mname in $(ini lskeys "$section"); do
86
+            MacroMap[$mname]=$(ini values "$section:$mname")
54 87
         done
55
-        echo "s|__MKIT_PROJ_NAME__|$(ini 1value project:name | __qfs)|g;"
56
-        echo "s|__MKIT_PROJ_CODENAME__|$(ini 1value project:codename | __qfs)|g;"
57
-        echo "s|__MKIT_PROJ_LICENSE__|$(ini 1value project:license | __qfs)|g;"
58
-        echo "s|__MKIT_PROJ_PKGNAME__|$(ini 1value project:pkgname | __qfs)|g;"
59
-        echo "s|__MKIT_PROJ_TAGLINE__|$(ini 1value project:tagline | __qfs)|g;"
60
-        echo "s|__MKIT_PROJ_MAINTAINER__|$(ini 1value project:maintainer | __qfs)|g;"
61
-        echo "s|__MKIT_PROJ_VCS_BROWSER__|$(ini 1value project:vcs_browser | __qfs)|g;"
62
-        echo "s|__MKIT_PROJ_GIT_LASTHASH__|$(__cached git_lasthash | __qfs)|g;"
63
-        echo "s|__MKIT_PROJ_VERSION__|$(__cached semver | __qfs)|g;"
64
-        echo "s|__MKIT_SELF_VERSION__|$MKIT_VERSION|g;"
65
-    } >> "$script"
66
-    sed -f "$script" || die "__expand_tokens failed"
67
-    rm "$script"
88
+    done
89
+    debug_var MacroMap
90
+    while IFS= read -r line; do
91
+        __expand_line "$line"
92
+    done
68 93
 }
69 94
 
70 95
 __guess_ftype() {
@@ -141,14 +166,14 @@ __rec_built() {
141 166
 
142 167
 _mkit_data() {
143 168
     #
144
-    # Build sampler showing all token values
169
+    # Build sampler showing all macro values
145 170
     #
146
-    local token
171
+    local macro
147 172
     local section
148 173
     local sections
149 174
     sections=()
150
-    ini lskeys tokens | grep -q . && sections=(tokens)
151
-    sections+=( $(ini lssect | grep ':tokens$') )
175
+    ini lskeys macros | grep -q . && sections=(macros)
176
+    sections+=( $(ini lssect | grep ':macros$') )
152 177
     {
153 178
         echo "(builtin):"
154 179
         echo "  x_MKIT_PROJ_NAME__ => '__MKIT_PROJ_NAME__'"
@@ -163,12 +188,12 @@ _mkit_data() {
163 188
         echo "  x_MKIT_SELF_VERSION__ => '__MKIT_SELF_VERSION__'"
164 189
         for section in "${sections[@]}"; do
165 190
             echo "$section:"
166
-            for token in $(ini lskeys "$section"); do
167
-                echo "  x${token:1} => '$token'"
191
+            for macro in $(ini lskeys "$section"); do
192
+                echo "  x${macro:1} => '$macro'"
168 193
             done
169 194
         done
170 195
     } \
171
-      | __expand_tokens "MKIT_BUILTIN" "${sections[@]}" \
196
+      | __expand_macros "MKIT_BUILTIN" "${sections[@]}" \
172 197
       | sed '/^  x/ s|x|_|'
173 198
 }
174 199
 

+ 21
- 9
utils/mkit/include/facts.sh View File

@@ -54,6 +54,9 @@ git_fact() {
54 54
         latest_sha)
55 55
             git log -1 --pretty=format:%h HEAD
56 56
             ;;
57
+        latest_cdate)
58
+            git log -1 --format=%cd --date=unix HEAD
59
+            ;;
57 60
         *)
58 61
             warn "unknown git fact asked: $fact_name"
59 62
             ;;
@@ -139,9 +142,15 @@ semver() {
139 142
     local prerl         # pre-release keyword (from mkit.ini, eg. 'beta')
140 143
     local latest_tag    # latest git tag
141 144
     local commit        # commit indicator (CURRENT_BRANCH.gHASH)
142
-    local dirty         # 0 if dirty, 1 if clean
145
+    local dirty=F       # F if dirty, T if clean
146
+    local btime         # timestamp or nothing (see $MKIT_TTAG)
143 147
     local suffix        # version suffix
144 148
     prerl=$(ini 1value project:prerl)
149
+    case $MKIT_TTAG in
150
+        none)   btime= ;;
151
+        btime)  btime=$(date -u +%Y%m%d%H%M%S) ;;
152
+        ctime)  btime=$(date -d @"$(git_fact latest_cdate)" -u +%Y%m%d%H%M%S) ;;
153
+    esac
145 154
     grep ":" <<<"$prerl" \
146 155
      && warn "colon in project:prerl may corrupt version data: $prerl"
147 156
     git_present || {
@@ -161,14 +170,17 @@ semver() {
161 170
     then    # we are at a later commit than the last tag
162 171
         commit="$(git_fact current_branch).g$(git_fact latest_sha)"
163 172
     fi
164
-    git_bool dirty; dirty=$?
165
-    case "$dirty:$commit" in
166
-        1:)  suffix=""               ;;
167
-        0:)  suffix="+dirty"         ;;
168
-        1:*) suffix="+$commit"       ;;
169
-        0:*) suffix="+$commit.dirty" ;;
170
-        *)   suffix=MKIT_BUG
171
-             warn "MKIT_BUG: bad dirt/commit detection" ;;
173
+    git_bool dirty && dirty=T
174
+    case "$dirty:$btime:$commit" in
175
+        F:*:)   suffix=""                       ;;
176
+        T::)    suffix="+dirty"                 ;;
177
+        T:*:)   suffix="+t$btime.dirty"           ;;
178
+        F::*)   suffix="+$commit"               ;;
179
+        F:*:*)  suffix="+t$btime.$commit"         ;;
180
+        T::*)   suffix="+$commit.dirty"         ;;
181
+        T:*:*)  suffix="+t$btime.$commit.dirty"   ;;
182
+        *)      suffix=MKIT_BUG
183
+                warn "MKIT_BUG: bad dirt/commit detection" ;;
172 184
     esac
173 185
     test -n "$prerl" && suffix="-$prerl$suffix"
174 186
     echo "$xyz$suffix"

+ 26
- 3
utils/mkit/include/ini.sh View File

@@ -30,13 +30,19 @@ __ini_expand() {
30 30
     done
31 31
 }
32 32
 
33
+__ini_grepcmt() {
34
+    #
35
+    # Remove comments from INI file on stdin
36
+    #
37
+    grep -v '^[[:space:]]*#'
38
+}
39
+
33 40
 __ini_grepkey() {
34 41
     #
35 42
     # Read key from a section
36 43
     #
37 44
     local wnt=$1    # wanted key
38 45
     grep '.' \
39
-      | grep -v '\s*#' \
40 46
       | sed -e 's/ *= */=/; s/ +$//; s/^//;' \
41 47
       | grep -e "^$wnt=" \
42 48
       | cut -d= -f2- \
@@ -73,7 +79,6 @@ __ini_grepsec() {
73 79
     local ok=false      # are we in the section?
74 80
     local line          # each input line
75 81
     grep '.' \
76
-      | grep -v '\s*#' \
77 82
       | while read -r line; do
78 83
             case "$line" in
79 84
                 \[$wnt\]) ok=true;  continue ;;
@@ -112,6 +117,24 @@ __ini_maybe_expand() {
112 117
     fi
113 118
 }
114 119
 
120
+__ini_body() {
121
+    #
122
+    # Produce mkit.ini body including INCLUDE
123
+    #
124
+    # Note: recursive includes are not supported.
125
+    #
126
+    local inc                       # file to include
127
+    local incre='\[INCLUDE:.*\]'    # include directive regex
128
+    local iline                     # include directive line
129
+    if iline=$(grep -m1 -x "$incre" "$MKIT_INI"); then
130
+        inc=${iline#*:}; inc=${inc%]}
131
+        grep -vx "$incre" "$inc"
132
+        grep -vx "$incre" "$MKIT_INI"
133
+    else
134
+        cat "$MKIT_INI"
135
+    fi | __ini_grepcmt
136
+}
137
+
115 138
 ini() {
116 139
     #
117 140
     # do ini operation
@@ -128,7 +151,7 @@ ini() {
128 151
         1value) fn=__ini_greppath; limit="tail -1" ;;
129 152
         *)      die "incorrect use of \`ini()\`"
130 153
     esac
131
-    <"$MKIT_INI" $fn "$arg" | $limit
154
+    __ini_body | $fn "$arg" | $limit
132 155
 }
133 156
 
134 157
 update_version() {

+ 4
- 4
utils/mkit/include/mkit.sh View File

@@ -66,12 +66,12 @@ debug_var() {
66 66
     #
67 67
     $MKIT_DEBUG || return 0
68 68
     local __mkit_debug_var_name__       # variable name to debug
69
+    local decl                          # declare string
69 70
     for __mkit_debug_var_name__ in "$@"; do
70 71
         {
71
-            echo -n "MKIT_DEBUG: ${FUNCNAME[1]}():"
72
-            echo -n " $__mkit_debug_var_name__"
73
-            echo -n "='${!__mkit_debug_var_name__}'"
74
-            echo
72
+            decl=$(declare -p "$__mkit_debug_var_name__")
73
+            decl=${decl#declare ?? }
74
+            echo "MKIT_DEBUG: ${FUNCNAME[1]}(): $decl"
75 75
         } >&2
76 76
     done
77 77
 }

+ 30
- 2
utils/mkit/include/vars.sh View File

@@ -51,11 +51,39 @@ MKIT_LOCAL=${MKIT_LOCAL:-.mkit}
51 51
 #
52 52
 # Package name
53 53
 #
54
-# Used as base for tarball and in some default tokens.
54
+# Used as base for tarball and in some default macros.
55 55
 #
56 56
 MKIT_PROJ_PKGNAME=""
57 57
 
58
+#
59
+# Add time-based ordinal tag to SemVer build data?
60
+#
61
+# Can be 'none', 'ctime' or 'btime'.
62
+#
63
+# If 'ctime', devel builds have also timestamp-based tag in format of
64
+# `t%Y%m%d%H%M%S`, that is, a small character 't' followed by timestamp
65
+# without non-digit characters.  The timestamps are in UTC, ie. timezones
66
+# need not apply.  'btime' has the same format, except that it's derived
67
+# from build time, while 'ctime' is from last commit's commit date.
68
+#
69
+# This helps with deploying development builds where packaging system
70
+# is not SemVer-compliant and makes it hard to install arbitrary version.
71
+# For example, old yum version (as of RHEL-6) will not let you install
72
+# version that it deems older than is installed, making it hard to
73
+# continually upgrade during active development.  While packaging
74
+# systems have their own rukes (and SemVer says both versions should be
75
+# considered same) this tag will make it more likely to "win" the build
76
+# you made later.
77
+#
78
+# Note that this does not affect clean builds (ie. builds from clean
79
+# repo with HEAD corresponding to latest version tag.).
80
+#
81
+# Also note that 'btime' makes the version non-deterministic: merely
82
+# initiating the build a second later will result in different version.
83
+#
84
+MKIT_TTAG=${MKIT_TTAG:-ctime}
85
+
58 86
 #
59 87
 # This MKit version
60 88
 #
61
-MKIT_VERSION=0.0.29
89
+MKIT_VERSION=0.0.34

+ 8
- 8
utils/mkit/stub View File

@@ -82,7 +82,7 @@ deploy() {
82 82
                 remake_section dist
83 83
                 remake_section ENV
84 84
                 remake_section roots
85
-                remake_section tokens
85
+                remake_section macros
86 86
                 remake_section modes
87 87
                 remake_section files
88 88
             else
@@ -112,7 +112,7 @@ deploy() {
112 112
                     echo "doc = [ENV:PREFIX]/share/doc/$PackageName"
113 113
                 } | reformat_section
114 114
                 echo ""
115
-                echo "[tokens]"
115
+                echo "[macros]"
116 116
                 {
117 117
                     echo "__${PackageName^^}_FOO__ = Barr.."
118 118
                 } | reformat_section
@@ -258,7 +258,7 @@ deploy() {
258 258
             echo "    machine should be uder 'src'."
259 259
             echo ""
260 260
             echo "    Note that during build time, files named ending with"
261
-            echo "    '.skel' are subject to token expansion, see mkit.ini"
261
+            echo "    '.skel' are subject to macro expansion, see mkit.ini"
262 262
             echo "    section below for details."
263 263
             echo ""
264 264
             echo " *  *notes* directory - here you shall store notes"
@@ -279,7 +279,7 @@ deploy() {
279 279
             echo " *  *packaging* directory contains templates that enable"
280 280
             echo "    MKit create raw stuffs used to create DEB or RPM"
281 281
             echo "    packages.  Similar to '.skel' files in 'src', all files"
282
-            echo "    here are automatically considered for token expansion,"
282
+            echo "    here are automatically considered for macro expansion,"
283 283
             echo "    no matter how they are named (see mkit.ini section"
284 284
             echo "    below)."
285 285
             echo ""
@@ -382,13 +382,13 @@ deploy() {
382 382
             echo "    \`[modes]\` section is your friend.  Permissions here"
383 383
             echo "    should be in UNIX octal format."
384 384
             echo ""
385
-            echo " 5. Next, \`[tokens]\` section allows you to define own"
385
+            echo " 5. Next, \`[macros]\` section allows you to define own"
386 386
             echo "    placeholders that will be replaced when your scripts are"
387 387
             echo "    built.  Each file in 'src' directory that is named with"
388 388
             echo "    '.skel' suffix, and each file from 'packaging' directory"
389
-            echo "    (no matter its name), can contain one or more of tokens"
390
-            echo "    defined here, plus range of tokens automatically defined"
391
-            echo "    by MKit.  During build, these tokens are replaced with"
389
+            echo "    (no matter its name), can contain one or more of macros"
390
+            echo "    defined here, plus range of macros automatically defined"
391
+            echo "    by MKit.  During build, these macros are replaced with"
392 392
             echo "    their actual values."
393 393
             echo ""
394 394
             echo " 6. Less interesting, but important section is \`[dist]\`,"