The attached patch for ticket #705 [1] allows us to supply parameters to 
exlibs using the following syntax:

    require foo [ bar=baz blah=bleh ] monkey [ in=space ]

This requires foo.exlib with bar=baz and blah=bleh parameters, and 
monkey.exlib with the in=space parameter. Exlib parameters need to be declared 
as early as possible in the exlib using the `myexparam` function.

In the above example foo.exlib must contain something like e.g.:

    myexparam bar
    myexparam blah=bloh
    myexparam baz=

which declares the bar parameter as a required parameter, blah as an optional 
parameter that is set to bloh if not provided, and baz as an optional 
parameter that is set to an empty string if not provided.

To access the value of exlib parameters use the `exparam` function. This takes 
one argument, the name of the exlib parameter, and outputs the value. It must 
be used only in the exlib that declares the parameter and only after it has 
been declared.

Consequently `exparam bar` in foo.exlib outputs 'baz'. Calling `exparam bar` 
outside foo.exlib results in a fatal error.

[1] http://trac.pioto.org/paludis/ticket/705

-- 
Bo Andresen


From d8464e2735e01ce8e2f7ad2f00e2e17063be3e82 Mon Sep 17 00:00:00 2001
From: =?utf-8?q?Bo=20=C3=98rsted=20Andresen?= <[email protected]>
Date: Thu, 5 Feb 2009 15:54:24 +0100
Subject: [PATCH] Implement exlib parameters, including exparam and myexparam.

---
 .../e/ebuild/exheres-0/exlib_functions.bash        |   63 +++++++++++++++++++-
 1 files changed, 61 insertions(+), 2 deletions(-)

diff --git a/paludis/repositories/e/ebuild/exheres-0/exlib_functions.bash b/paludis/repositories/e/ebuild/exheres-0/exlib_functions.bash
index 2cae338..7429ba1 100644
--- a/paludis/repositories/e/ebuild/exheres-0/exlib_functions.bash
+++ b/paludis/repositories/e/ebuild/exheres-0/exlib_functions.bash
@@ -2,6 +2,7 @@
 # vim: set sw=4 sts=4 et :
 
 # Copyright (c) 2007, 2008 Ciaran McCreesh
+# Copyright (c) 2009 Bo Ørsted Andresen
 #
 # This file is part of the Paludis package manager. Paludis is free software;
 # you can redistribute it and/or modify it under the terms of the GNU General
@@ -29,17 +30,57 @@ export_exlib_phases()
     done
 }
 
+exparam() {
+    die "exparam is banned outside exlibs"
+}
+
+exparam_internal() {
+    local e v
+    e=${1//-/__dash__}
+    v=EXPARAMALLDECLS_${e}
+    has ${2} ${!v} || die "${e}.exlib has no ${2} parameter"
+    v=EXPARAMVAR_${e}_${2}
+    echo "${!v}"
+}
+
+myexparam() {
+    [[ -z "${CURRENT_EXLIB}" ]] && die "myexparam called but CURRENT_EXLIB undefined"
+
+    local v=EXPARAMVAR_${CURRENT_EXLIB//-/__dash__}_${1%%=*}
+    [[ -z ${!v+set} && ${1} == *=* ]] && export "${v}=${1#*=}"
+    export "EXPARAMALLDECLS_${CURRENT_EXLIB//-/__dash__}+= ${1%%=*}"
+}
+
 require()
 {
     ebuild_notice "debug" "Command 'require $...@}', using EXLIBSDIRS '${EXLIBSDIRS}'"
-    local e ee location v v_qa
-    for e in "$@" ; do
+    local exlibs e ee location v v_qa
+    # parse exlib parameters
+    while [[ -n $@ ]]; do
+        if [[ ${1} == "[" ]]; then
+            shift
+            while [[ -n ${1} && ${1} != "]" ]]; do
+                [[ -z ${e} ]] && die '"[" encountered with no preceding exlib'
+                export "EXPARAMALLVARS_${e//-/__dash__}+= ${1%%=*}"
+                export "EXPARAMVAR_${e//-/__dash__}_${1%%=*}=${1#*=}"
+                shift
+            done
+            [[ ${1} == "]" ]] || die '"[" encountered with no closing "]"'
+        else
+            e=${1}
+            exlibs+=" ${e}"
+        fi
+        shift
+    done
+    # source exlibs
+    for e in ${exlibs}; do
         location=
         for ee in ${EXLIBSDIRS} ; do
             [[ -f "${ee}/${e}.exlib" ]] && location="${ee}/${e}.exlib"
         done
         local old_CURRENT_EXLIB="${CURRENT_EXLIB}"
         export CURRENT_EXLIB="${e}"
+        alias exparam="exparam_internal ${CURRENT_EXLIB}"
 
         for v in ${PALUDIS_SOURCE_MERGED_VARIABLES} ${PALUDIS_BRACKET_MERGED_VARIABLES} ; do
             local c_v="current_${v}" u_v="unset_${v}"
@@ -88,7 +129,25 @@ require()
             fi
         done
 
+        # die on required exlib parameters that hasn't been supplied
+        local a_v=EXPARAMALLDECLS_${CURRENT_EXLIB//-/__dash__} c_v v
+        for v in ${!a_v}; do
+            c_v=EXPARAMVAR_${CURRENT_EXLIB//-/__dash__}_${v}
+            [[ -n ${!c_v+set} ]] || die "${CURRENT_EXLIB}.exlib requires a ${v} parameter"
+        done
+
+        # die on supplied exlib parameters which haven't been declared
+        v=EXPARAMALLVARS_${CURRENT_EXLIB//-/__dash__}
+        for v in ${!v}; do
+            has ${v} ${!a_v} || die "${CURRENT_EXLIB}.exlib takes no ${v} parameter"
+        done
+
         export CURRENT_EXLIB="${old_CURRENT_EXLIB}"
+        if [[ -n ${CURRENT_EXLIB} ]]; then
+            alias exparam="exparam_internal ${CURRENT_EXLIB}"
+        else
+            unalias exparam
+        fi
     done
 }
 
-- 
1.6.1.2

_______________________________________________
Exherbo-dev mailing list
[email protected]
http://lists.exherbo.org/mailman/listinfo/exherbo-dev

Reply via email to