Browse Source

Support multi-line macros

Alois Mahdal 6 years ago
parent
commit
6cb6149590
1 changed files with 47 additions and 26 deletions
  1. 47
    26
      src/include/build.sh

+ 47
- 26
src/include/build.sh View File

33
     esac
33
     esac
34
 }
34
 }
35
 
35
 
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
+    for mname in "${!MacroMap[@]}"; do
51
+        if ! test "${line//$mname}" == "$line"; then
52
+            while IFS= read -r mvline; do
53
+                echo "${line//$mname/$mvline}"
54
+            done <<<"${MacroMap[$mname]}"
55
+            return 0
56
+        fi
57
+    done
58
+    echo "$line"
59
+    return 1
60
+}
61
+
36
 __expand_macros() {
62
 __expand_macros() {
37
     #
63
     #
38
     # Read stdin, expanding macros from sections $@
64
     # Read stdin, expanding macros from sections $@
39
     #
65
     #
40
-    local script        # sed script cache
41
     local section       # each section to expand macros from
66
     local section       # each section to expand macros from
42
-    local varname       # each macro name
43
-    local varvalue      # each macro 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
67
+    local line          # each line on stdin
68
+    local mname         # each macro name
69
+    local -A MacroMap   # macro value map
70
+    MacroMap[__MKIT_PROJ_NAME__]=$(ini 1value project:name)
71
+    MacroMap[__MKIT_PROJ_CODENAME__]=$(ini 1value project:codename)
72
+    MacroMap[__MKIT_PROJ_LICENSE__]=$(ini 1value project:license)
73
+    MacroMap[__MKIT_PROJ_PKGNAME__]=$(ini 1value project:pkgname)
74
+    MacroMap[__MKIT_PROJ_TAGLINE__]=$(ini 1value project:tagline)
75
+    MacroMap[__MKIT_PROJ_MAINTAINER__]=$(ini 1value project:maintainer)
76
+    MacroMap[__MKIT_PROJ_VCS_BROWSER__]=$(ini 1value project:vcs_browser)
77
+    MacroMap[__MKIT_PROJ_GIT_LASTHASH__]=$(__cached git_lasthash)
78
+    MacroMap[__MKIT_PROJ_VERSION__]=$(__cached semver)
79
+    MacroMap[__MKIT_SELF_VERSION__]=$MKIT_VERSION
80
+    for section in "$@"; do
81
+        for mname in $(ini lskeys "$section"); do
82
+            MacroMap[$mname]=$(ini values "$section:$mname")
54
         done
83
         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_macros failed"
67
-    rm "$script"
84
+    done
85
+    debug_var MacroMap
86
+    while IFS= read -r line; do
87
+        __expand_line "$line"
88
+    done
68
 }
89
 }
69
 
90
 
70
 __guess_ftype() {
91
 __guess_ftype() {