You are not logged in.

#1 2008-12-23 17:53:51

hbekel
Member
Registered: 2008-10-04
Posts: 311

Prompt for and cache configure options in PKGBUILD

Today I helped out FreakGuard on irc with a PKGBUILD in which he prompted the user for configure options and cached the results for later. I generalized his approach and came up with the following code:

# Prompt user for configure options and cache them.
# Intended for use in a PKGBUILD. Adjust to your needs.
#
# _options contains exactly five strings per option.
#
# 1. Question to ask
# 2. Default answer ("y" or "n")
# 3. Help text (available via "?" if not empty)
# 3. Hint to print if non-default answer was given
# 4. Option to add to _configure_options if non-default answer was given

# just for testing...
pkgname=test

# example: some conky configure options 
_options=(
    "Do you want audacious support?" "n" 
    "Enables support for the audacious media player,\nsee http://audacious-media-player.org/"
    "Please remember to install audacious before compiling conky."
    "--enable-audacious=yes"
    
    "Do you want bmpx support?" "n" "" "" "--enable-bmpx"
    "Do you want mpd support?" "y" "" "" "--disable-mpd"
    )

_configure_options=""

# assuming we're in $srcdir...
_configure_cache="../$pkgname-configure-options"

if [ -f $_configure_cache ]; then
    source $_configure_cache
else
    for (( _i=0; _i<${#_options[@]}; _i+=5 )); do
    _question=${_options[_i]}
    _default=${_options[_i+1]}
    _help=${_options[_i+2]}
    _comment=${_options[_i+3]}
    _option=${_options[_i+4]}
    
    _other="y";
    _choice="[y/N"
    
    if [ $_default == "y" ]; then
        _other="n"
        _choice="[Y/n"
    fi

    if [ "$_help" != "" ]; then
        _choice="$_choice/?"
    fi
    _choice="$_choice]"

    _answer="empty"
    until [[ $_answer =~ ^(Y|y|N|n) || $_answer == "" ]]; do
        echo -e -n "\e[1;32m==> \e[0m $_question $_choice "
        read -n 1 _answer      
        if [ "$_answer" != "" ]; then
        echo
        fi
        if [[ "$_answer" == "?" && "$_help" != "" ]]; then
        echo -e $_help
        fi
    done
    
    _pattern="^($_other|$(echo $_other | tr '[:lower:]' '[:upper:]'))"
    
    if [[ $_answer =~ $_pattern ]]; then
        if [[ "$_comment" != "" ]]; then
        echo -e $_comment
        fi
        _configure_options="$_configure_options $_option"
    fi
    done
    echo "_configure_options='$_configure_options'" > $_configure_cache 
fi

echo "./configure $_configure_options"

As you can see, the options will end up in _configure_options. I don't know how useful the caching part is, but you can easily remove it if you don't need it.

Offline

#2 2008-12-23 18:06:54

FreakGuard
Member
Registered: 2008-04-27
Posts: 103

Re: Prompt for and cache configure options in PKGBUILD

Overall: Great work, thanks big_smile

hbekel wrote:

I don't know how useful the caching part is, but you can easily remove it if you don't need it.

That was just because of git, which you might want to recompile later.

Offline

Board footer

Powered by FluxBB