Bläddra i källkod

Rewrite to meta-command and add update mode

'newstub' was not so far to being able to update meta-data based on
existing mkit.ini.  However, in order to keep the CLI sane, it seems
better to switch to meta-command scheme.
Alois Mahdal 7 år sedan
förälder
incheckning
c86e237053
2 ändrade filer med 141 tillägg och 47 borttagningar
  1. 1
    1
      mkit.ini
  2. 140
    46
      src/stub

+ 1
- 1
mkit.ini Visa fil

@@ -28,6 +28,6 @@
28 28
 
29 29
 [files]
30 30
     bin = src/make
31
-    bin = src/newstub
31
+    bin = src/stub
32 32
     rest = src/include
33 33
     rest = src/mkit.mk

src/newstub → src/stub Visa fil

@@ -21,6 +21,8 @@ MKIT_DIR=${MKIT_DIR:-$(dirname "$0")}
21 21
 
22 22
 init_core
23 23
 
24
+mkit_import ini
25
+
24 26
 declare -A MKIT_INIT_LICENSES
25 27
 MKIT_INIT_LICENSES[GPLv1]="http://www.gnu.org/licenses/old-licenses/gpl-1.0.md"
26 28
 MKIT_INIT_LICENSES[GPLv2]="http://www.gnu.org/licenses/old-licenses/gpl-2.0.md"
@@ -36,6 +38,8 @@ MKIT_INIT_LICENSES[FDLv1.1]="http://www.gnu.org/licenses/old-licenses/fdl-1.1.md
36 38
 deploy() {
37 39
     local file=$1       # which known file?
38 40
     local any_name=${NiceName:-$PackageName}
41
+    local tmp
42
+    tmp=$(mktemp -t newstub.deploy.XXXXXXX)
39 43
     mkdir -p "$(dirname "$file")"
40 44
     case $file in
41 45
 
@@ -73,37 +77,46 @@ deploy() {
73 77
             test -n "$VcsBrowser"   && echo "    vcs_browser = $VcsBrowser"
74 78
             test -n "$RelSrc"       && echo "    relsrc      = $RelSrc"
75 79
             test -n "$RelDst"       && echo "    reldst      = $RelDst"
76
-            echo ""
77
-            echo "[dist]"
78
-            $MkLicense  && echo "    tarball = LICENSE.md"
79
-            $MkMakefile && echo "    tarball = Makefile"
80
-            $MkReadme   && echo "    tarball = README.md"
81
-            echo "    tarball = mkit.ini"
82
-            $MkPackaging && echo "    tarball = packaging"
83
-            echo "    tarball = src"
84
-            echo "    tarball = tests"
85
-            echo "    tarball = utils"
86
-            $MkPackaging && echo "    rpmstuff = packaging/template.spec"
87
-            $MkPackaging && echo "    debstuff = packaging/debian"
88
-            echo ""
89
-            echo "[ENV]"
90
-            echo "    PREFIX = /usr/local"
91
-            echo ""
92
-            echo "[roots]"
93
-            echo "    bin     = [ENV:PREFIX]/bin"
94
-            echo "    doc     = [ENV:PREFIX]/share/doc/$PackageName"
95
-            echo ""
96
-            echo "[tokens]"
97
-            echo "    __BIN_DIR__ = [roots:bin]"
98
-            echo ""
99
-            echo "[modes]"
100
-            echo "    bin     = 755"
101
-            echo "    doc     = 644"
102
-            echo ""
103
-            echo "[files]"
104
-            echo "    bin      = src/$PackageName"
105
-            $MkLicense && echo "    doc      = LICENSE.md"
106
-            $MkReadme  && echo "    doc      = README.md"
80
+            if updating; then
81
+                remake_section dist
82
+                remake_section ENV
83
+                remake_section roots
84
+                remake_section tokens
85
+                remake_section modes
86
+                remake_section files
87
+            else
88
+                echo ""
89
+                echo "[dist]"
90
+                $MkLicense  && echo "    tarball = LICENSE.md"
91
+                $MkMakefile && echo "    tarball = Makefile"
92
+                $MkReadme   && echo "    tarball = README.md"
93
+                echo "    tarball = mkit.ini"
94
+                $MkPackaging && echo "    tarball = packaging"
95
+                echo "    tarball = src"
96
+                echo "    tarball = tests"
97
+                echo "    tarball = utils"
98
+                $MkPackaging && echo "    rpmstuff = packaging/template.spec"
99
+                $MkPackaging && echo "    debstuff = packaging/debian"
100
+                echo ""
101
+                echo "[ENV]"
102
+                echo "    PREFIX = /usr/local"
103
+                echo ""
104
+                echo "[roots]"
105
+                echo "    bin     = [ENV:PREFIX]/bin"
106
+                echo "    doc     = [ENV:PREFIX]/share/doc/$PackageName"
107
+                echo ""
108
+                echo "[tokens]"
109
+                echo "    __BIN_DIR__ = [roots:bin]"
110
+                echo ""
111
+                echo "[modes]"
112
+                echo "    bin     = 755"
113
+                echo "    doc     = 644"
114
+                echo ""
115
+                echo "[files]"
116
+                echo "    bin      = src/$PackageName"
117
+                $MkLicense && echo "    doc      = LICENSE.md"
118
+                $MkReadme  && echo "    doc      = README.md"
119
+            fi
107 120
             echo ""
108 121
             echo "#mkit version=$MKIT_VERSION"
109 122
             ;;
@@ -424,7 +437,9 @@ deploy() {
424 437
             echo "Once you have reviewed everything, just delete this file!"
425 438
             ;;
426 439
 
427
-    esac >"$file"
440
+    esac >"$tmp"
441
+    cat "$tmp" > "$file"
442
+    rm "$tmp"
428 443
 }
429 444
 
430 445
 known_licenses() {
@@ -439,7 +454,8 @@ known_licenses() {
439 454
 usage() {
440 455
     {
441 456
         echo "Usage:"
442
-        echo "   newstub [options] NAME"
457
+        echo "   newstub [options] new PKGNAME"
458
+        echo "   newstub [options] update"
443 459
         echo "   newstub -L"
444 460
         echo ""
445 461
         echo "Options:"
@@ -463,7 +479,7 @@ usage() {
463 479
         echo "    -P            skip creating packaging templates"
464 480
         echo "    -L            list know licenses and exit"
465 481
         echo ""
466
-        echo "NAME should be packaging-friendly name, ie. consist"
482
+        echo "PKGNAME should be packaging-friendly name, ie. consist"
467 483
         echo "only of small letters, numbers, underscore and dash."
468 484
         echo "For your 'real' name, use NICENAME, which can be any"
469 485
         echo "string."
@@ -511,7 +527,12 @@ mkcommit_mkit_code() {
511 527
 
512 528
 mkcommit_mkit_conf() {
513 529
     git add .                                                  || return
514
-    git commit -m "WIP [mkit/newstub] Add MKit configuration stub" || return
530
+    local msg       # commit message (the important art
531
+    case $Action in
532
+        new)    msg="Add MKit configuration stub" ;;
533
+        update) msg="Update MKit configuration"   ;;
534
+    esac
535
+    git commit -m "WIP [mkit/newstub] $msg" || return
515 536
 }
516 537
 
517 538
 deploy_packaging() {
@@ -527,6 +548,69 @@ deploy_packaging() {
527 548
     deploy packaging/template.spec
528 549
 }
529 550
 
551
+init_from_existing() {
552
+    #
553
+    # Initialize variables from old mkit.ini
554
+    #
555
+    test -f "$MKIT_INI" \
556
+     || die "mkit.ini not found; aborting update: $MKIT_INI"
557
+    user_gave Codename    || Codename=$(ini_raw1v project:codename)
558
+    user_gave License     || License=$(ini_raw1v project:license)
559
+    user_gave Maintainer  || Maintainer=$(ini_raw1v project:maintainer)
560
+    user_gave NiceName    || NiceName=$(ini_raw1v project:name)
561
+    user_gave PackageName || PackageName=$(ini_raw1v project:pkgname)
562
+    user_gave RelDst      || RelDst=$(ini_raw1v project:reldst)
563
+    user_gave RelSrc      || RelSrc=$(ini_raw1v project:relsrc)
564
+    user_gave Tagline     || Tagline=$(ini_raw1v project:tagline)
565
+    user_gave VcsBrowser  || VcsBrowser=$(ini_raw1v project:vcs_browser)
566
+    user_gave Version     || Version=$(ini_raw1v project:version)
567
+}
568
+
569
+ini_raw1v() {
570
+    #
571
+    # Read raw scalar from mkit.ini
572
+    #
573
+    local path=$1
574
+    MKIT_INI_EXPAND=0 ini 1value "$path"
575
+}
576
+
577
+user_gave() {
578
+    #
579
+    # True if user gave value to variable $1
580
+    #
581
+    local var=$1    # name of the variable
582
+    { test "${UserGave[$var]}" == 1; } 2>/dev/null
583
+}
584
+
585
+updating() {
586
+    #
587
+    # Are we updating?
588
+    #
589
+    test "$Action" == update
590
+}
591
+
592
+remake_section() {
593
+    #
594
+    # Re-compose mkit.ini section $1
595
+    #
596
+    local section=$1
597
+    local key
598
+    local value
599
+    ini lskeys "$section" | grep -q . \
600
+     || return 1
601
+    echo ""
602
+    echo "[$section]"
603
+    ini lskeys "$section" \
604
+      | while read -r key; do
605
+            MKIT_INI_EXPAND=0 ini values "$section:$key" \
606
+              | while read -r value; do
607
+                    echo "$key = $value"
608
+                done
609
+        done \
610
+      | column -to' ' \
611
+      | sed 's/^/    /'
612
+}
613
+
530 614
 main() {
531 615
     local NiceName          # human-readable project name
532 616
     local PackageName       # machine-safe project (package) name
@@ -544,16 +628,19 @@ main() {
544 628
     local MkMakefile=true   # create Makefile?
545 629
     local MkPackaging=true  # create packaging templates?
546 630
     local MkLicense=false   # create LICENSE.md file
631
+    local Action            # 'update' to respect existing, 'new' to force
632
+                            # rewrite incl. MKIT_NEWSTUB_* placeholders
633
+    declare -A UserGave
547 634
     while true; do case $1 in
548
-        -n) NiceName=$2;        shift 2 || usage ;;
549
-        -b) RelSrc=$2;          shift 2 || usage ;;
550
-        -B) RelDst=$2;          shift 2 || usage ;;
551
-        -c) Codename=$2;        shift 2 || usage ;;
552
-        -t) Tagline=$2;         shift 2 || usage ;;
553
-        -l) License=$2;         shift 2 || usage ;;
554
-        -m) Maintainer=$2;      shift 2 || usage ;;
555
-        -v) VcsBrowser=$2;      shift 2 || usage ;;
556
-        -V) Version=$2;         shift 2 || usage ;;
635
+        -n) NiceName=$2;   UserGave[NiceName]=1;   shift 2 || usage ;;
636
+        -b) RelSrc=$2;     UserGave[RelSrc]=1;     shift 2 || usage ;;
637
+        -B) RelDst=$2;     UserGave[RelDst]=1;     shift 2 || usage ;;
638
+        -c) Codename=$2;   UserGave[Codename]=1;   shift 2 || usage ;;
639
+        -t) Tagline=$2;    UserGave[Tagline]=1;    shift 2 || usage ;;
640
+        -l) License=$2;    UserGave[License]=1;    shift 2 || usage ;;
641
+        -m) Maintainer=$2; UserGave[Maintainer]=1; shift 2 || usage ;;
642
+        -v) VcsBrowser=$2; UserGave[VcsBrowser]=1; shift 2 || usage ;;
643
+        -V) Version=$2;    UserGave[Version]=1;    shift 2 || usage ;;
557 644
         -M) MkMakefile=false;   shift ;;
558 645
         -R) MkReadme=false;     shift ;;
559 646
         -a) AutoClean=true;     shift ;;
@@ -564,8 +651,15 @@ main() {
564 651
         -*) usage ;;
565 652
         *)  break ;;
566 653
     esac done
567
-    PackageName="$1"
568
-    test -n "$PackageName" || usage
654
+    Action=$1; PackageName=$2
655
+    case $Action:$PackageName in
656
+        new:)     usage ;;
657
+        new:*)    :     ;;
658
+        update:)  :     ;;
659
+        update:*) usage ;;
660
+        *)        usage ;;
661
+    esac
662
+    updating && init_from_existing
569 663
     if $MkLicense; then
570 664
         known_licenses | grep -qxF "$License" \
571 665
          || die "unknown license (use -L to get list): $License"