Re: [gentoo-portage-dev] use* cleanup

2007-10-31 Thread Mike Frysinger
On Wednesday 31 October 2007, Marijn Schouten (hkBst) wrote:
> I hope this is just an artifact of the patch being a bit opaque. The
> inconsistent indentation in the patch is a consequence of emacs bash mode
> using a different indentation style than (I guess) vi(m). I'm sure even in
> vi you can re-indent my code with one simple key-chord.

no idea, i dont use vi ... but you're writing the patch which means you get to 
fix it ;)

> The immediate motivation of my examining this code was a request on
> #gentoo-dev-help by lack for something which I could with my new code
> easily write like this:
>
> use_mime() {
> local WORD="$(_if $2 $2 $1)"
>
> _use $1 "${WORD};"
> }

write where ?  in eclasses/ebuilds ?

> local SUFFIX="$(_if $3 "=$3")"
> local WORD="$(_if $2 $2 $1)"

local FOO=$(...)
extraneous quoting makes eyes bleed

> _if() {
> if $1; then echo $2; else echo $3; fi
> }

$1 && echo $2 || echo $3

> if hasq ${flag} ${USE} ; then
>   echo ${string_success}; return ${found}
> else
>   echo ${string_failure}; return $((!found))
> fi

no point in cuddling those lines

> What's not to like?

when you've written it out, it does look much nicer ... but i'd like to 
understand the motivation behind it first ...
-mike


signature.asc
Description: This is a digitally signed message part.


Re: [gentoo-portage-dev] use* cleanup

2007-10-31 Thread Marijn Schouten (hkBst)
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

Mike Frysinger wrote:
> On Tuesday 30 October 2007, Marijn Schouten (hkBst) wrote:
>> The purpose of this patch is to expose a generic function, namely _use,
>> which can be used to build your own use* variant if you need that. I
>> reimplemented all other current use function using _use (and _if) to cut
>> out duplicate and verbose code. Comments expected. I didn't test this code.
> 
> i guess i dont really see it ... there isnt that much duplicate code to begin 
> with, and the end result is kind of hard to understand at first glance which 
> is a bad thing ...
> -mike

I hope this is just an artifact of the patch being a bit opaque. The
inconsistent indentation in the patch is a consequence of emacs bash mode
using a different indentation style than (I guess) vi(m). I'm sure even in vi
you can re-indent my code with one simple key-chord.

The immediate motivation of my examining this code was a request on
#gentoo-dev-help by lack for something which I could with my new code easily
write like this:

use_mime() {
local WORD="$(_if $2 $2 $1)"

_use $1 "${WORD};"
}

This is possible because besides being shorter, my code is more general and
exposes utility functions to write your own use_* functions with.

The explanation of this function is:

#set WORD to argument 2 or if that is empty to argument 1
#output "${WORD};" if use flag $1 is set

I don't think it gets any clearer/directer/shorter than that. Other existing
functions that are trivial to re-implement:

use() {
_use ${1}
}

useq() {
_use ${1}
}

usev() {
_use ${1} ${1}
}

use_with() {
local SUFFIX="$(_if $3 "=$3")"
local WORD="$(_if $2 $2 $1)"

_use $1 "--with-${WORD}${SUFFIX}" "--without-${WORD}"
}

use_enable() {
local SUFFIX="$(_if $3 "=$3")"
local WORD="$(_if $2 $2 $1)"

_use $1 "--enable-${WORD}${SUFFIX}" "--disable-${WORD}"
}

All that is needed is:

_if() {
if $1; then echo $2; else echo $3; fi
}

and a function which is slightly extended from what is now useq to allow for
choosing to echo strings. Please excuse some line-wrapping.

_use() {
local flag=$1
local string_success=$2
local string_failure=$3
local found=0

# invert the return value for "!blah" and strip the '!'
[[ ${flag} = !* ]] && { found=1 ; flag=${flag:1} }

# Make sure we have this USE flag in IUSE
if ! hasq "${flag}" ${IUSE} ${E_IUSE} && ! hasq "${flag}"
${PORTAGE_ARCHLIST} selinux; then
eqawarn "QA Notice: USE Flag '${flag}' not in IUSE for 
${CATEGORY}/${PF}"
fi

if hasq ${flag} ${USE} ; then
echo ${string_success}; return ${found}
else
echo ${string_failure}; return $((!found))
fi
}


What's not to like?

Marijn

- --
Marijn Schouten (hkBst), Gentoo Lisp project
, #gentoo-lisp on FreeNode
-BEGIN PGP SIGNATURE-
Version: GnuPG v2.0.7 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFHKHQ6p/VmCx0OL2wRAl4dAJ4ilITOLQapD2NXCenw+YOYMPyOxwCgunjt
yKFi0LaXlEzAKQYnO2BS1SI=
=XQvd
-END PGP SIGNATURE-
-- 
[EMAIL PROTECTED] mailing list