1 |
-----BEGIN PGP SIGNED MESSAGE----- |
2 |
Hash: SHA1 |
3 |
|
4 |
Mike Frysinger wrote: |
5 |
> On Tuesday 30 October 2007, Marijn Schouten (hkBst) wrote: |
6 |
>> The purpose of this patch is to expose a generic function, namely _use, |
7 |
>> which can be used to build your own use* variant if you need that. I |
8 |
>> reimplemented all other current use function using _use (and _if) to cut |
9 |
>> out duplicate and verbose code. Comments expected. I didn't test this code. |
10 |
> |
11 |
> i guess i dont really see it ... there isnt that much duplicate code to begin |
12 |
> with, and the end result is kind of hard to understand at first glance which |
13 |
> is a bad thing ... |
14 |
> -mike |
15 |
|
16 |
I hope this is just an artifact of the patch being a bit opaque. The |
17 |
inconsistent indentation in the patch is a consequence of emacs bash mode |
18 |
using a different indentation style than (I guess) vi(m). I'm sure even in vi |
19 |
you can re-indent my code with one simple key-chord. |
20 |
|
21 |
The immediate motivation of my examining this code was a request on |
22 |
#gentoo-dev-help by lack for something which I could with my new code easily |
23 |
write like this: |
24 |
|
25 |
use_mime() { |
26 |
local WORD="$(_if $2 $2 $1)" |
27 |
|
28 |
_use $1 "${WORD};" |
29 |
} |
30 |
|
31 |
This is possible because besides being shorter, my code is more general and |
32 |
exposes utility functions to write your own use_* functions with. |
33 |
|
34 |
The explanation of this function is: |
35 |
|
36 |
#set WORD to argument 2 or if that is empty to argument 1 |
37 |
#output "${WORD};" if use flag $1 is set |
38 |
|
39 |
I don't think it gets any clearer/directer/shorter than that. Other existing |
40 |
functions that are trivial to re-implement: |
41 |
|
42 |
use() { |
43 |
_use ${1} |
44 |
} |
45 |
|
46 |
useq() { |
47 |
_use ${1} |
48 |
} |
49 |
|
50 |
usev() { |
51 |
_use ${1} ${1} |
52 |
} |
53 |
|
54 |
use_with() { |
55 |
local SUFFIX="$(_if $3 "=$3")" |
56 |
local WORD="$(_if $2 $2 $1)" |
57 |
|
58 |
_use $1 "--with-${WORD}${SUFFIX}" "--without-${WORD}" |
59 |
} |
60 |
|
61 |
use_enable() { |
62 |
local SUFFIX="$(_if $3 "=$3")" |
63 |
local WORD="$(_if $2 $2 $1)" |
64 |
|
65 |
_use $1 "--enable-${WORD}${SUFFIX}" "--disable-${WORD}" |
66 |
} |
67 |
|
68 |
All that is needed is: |
69 |
|
70 |
_if() { |
71 |
if $1; then echo $2; else echo $3; fi |
72 |
} |
73 |
|
74 |
and a function which is slightly extended from what is now useq to allow for |
75 |
choosing to echo strings. Please excuse some line-wrapping. |
76 |
|
77 |
_use() { |
78 |
local flag=$1 |
79 |
local string_success=$2 |
80 |
local string_failure=$3 |
81 |
local found=0 |
82 |
|
83 |
# invert the return value for "!blah" and strip the '!' |
84 |
[[ ${flag} = !* ]] && { found=1 ; flag=${flag:1} } |
85 |
|
86 |
# Make sure we have this USE flag in IUSE |
87 |
if ! hasq "${flag}" ${IUSE} ${E_IUSE} && ! hasq "${flag}" |
88 |
${PORTAGE_ARCHLIST} selinux; then |
89 |
eqawarn "QA Notice: USE Flag '${flag}' not in IUSE for ${CATEGORY}/${PF}" |
90 |
fi |
91 |
|
92 |
if hasq ${flag} ${USE} ; then |
93 |
echo ${string_success}; return ${found} |
94 |
else |
95 |
echo ${string_failure}; return $((!found)) |
96 |
fi |
97 |
} |
98 |
|
99 |
|
100 |
What's not to like? |
101 |
|
102 |
Marijn |
103 |
|
104 |
- -- |
105 |
Marijn Schouten (hkBst), Gentoo Lisp project |
106 |
<http://www.gentoo.org/proj/en/lisp/>, #gentoo-lisp on FreeNode |
107 |
-----BEGIN PGP SIGNATURE----- |
108 |
Version: GnuPG v2.0.7 (GNU/Linux) |
109 |
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org |
110 |
|
111 |
iD8DBQFHKHQ6p/VmCx0OL2wRAl4dAJ4ilITOLQapD2NXCenw+YOYMPyOxwCgunjt |
112 |
yKFi0LaXlEzAKQYnO2BS1SI= |
113 |
=XQvd |
114 |
-----END PGP SIGNATURE----- |
115 |
-- |
116 |
gentoo-portage-dev@g.o mailing list |