From 62828938624b1cb7b1e5d12e439f197976864527 Mon Sep 17 00:00:00 2001 From: mid-kid Date: Tue, 26 Jan 2016 16:47:45 +0100 Subject: [PATCH] Added support for options --- astronaut/astronaut | 52 +++++++++++++++++++++++++++++++++++++-------- astronaut/hello.sat | 8 +++++++ 2 files changed, 51 insertions(+), 9 deletions(-) diff --git a/astronaut/astronaut b/astronaut/astronaut index 09772da..2a1abf7 100755 --- a/astronaut/astronaut +++ b/astronaut/astronaut @@ -9,7 +9,8 @@ dir_prefix="$dir_sysroot/usr" # Prefix directory (Should always contain $dir_sy cmd_download="curl -# -L -o {dst} {src}" # Command to execute to download files cmd_extract="tar -x -C {dst} -f {src}" # Command to execute to extract files enable_check=true # Run the test suite of packages -unset dir_satellites # Directory where the satellite files are placed. +unset _dir_satellites # Directory where the satellite files are placed. +unset _options [ -f /etc/astronaut.conf ] && . /etc/astronaut.conf [ -f "$HOME/.astronaut.conf" ] && . "$HOME/.astronaut.conf" @@ -23,7 +24,7 @@ vcs_compile=false _show_help() { echo "This astronaut will help you build some satellites. Just describe what he's got to do in a satellite file. -Usage: $0 [-sbip ] [-IcCd] +Usage: $0 [-sbip ] [-IcCd] [-o ] -h/? Show this message -b Set build directory [WARNING: Will be deleted before build] @@ -34,7 +35,8 @@ Usage: $0 [-sbip ] [-IcCd] -I Nuke install directory before build -c/C Enable/Disable package checking -d Only download package files --v Only build vcs package if any updates have been made to it" +-v Only build vcs package if any updates have been made to it +-o Set package options (comma-separated)" } _msg() { @@ -47,7 +49,7 @@ _exiterr() { } # Gather info -while getopts "h?s:b:i:p:IcCdv" opt; do +while getopts "h?s:b:i:p:IcCdvo:" opt; do case "$opt" in h|\?) _show_help @@ -83,6 +85,9 @@ while getopts "h?s:b:i:p:IcCdv" opt; do v) _vcs_only=true ;; + o) + _options="$_options "$(printf "$OPTARG" | sed -e 's/,/ /g')"" + ;; esac done @@ -148,13 +153,42 @@ dlfile() { do_check() { if [ "$1" ]; then - [ "$enable_check" = true ] && $@ + [ "$enable_check" = true ] && eval $@ || true else - [ "$enable_check" = true ] - return $? + return $([ "$enable_check" = true ]) fi } +option() { + local check=$1 + shift + + local negate=false + case "$check" in + !*) + local negate=true + local check="$(printf "$check" | cut -c 2-)" + ;; + esac + + local enabled=false + local option + for option in $_options; do + if [ "$option" = "$check" ]; then + local enabled=true + break + fi + done + + if [ "$negate" = true ]; then + # Shitty way to do a simple "NOT" operation (come on, it's just one operation on most cpus). + [ "$enabled" = true ] && local enabled=false || local enabled=true + fi + + local enable_check=$enabled + do_check $@ # Reuse the code from do_check +} + import() { . "$(dirname "$_satellite")/functions/$1.sh" } @@ -183,8 +217,8 @@ _satname="$(basename "$_satellite" .sat)" # Try to find the satellite file if it can't be found. if [ ! -f "$_satellite" ]; then - if [ -d "$dir_satellites" ]; then - file="$(find "$dir_satellites" -type f \( -name "$_satname.sat" -o -name "$_satname" \) -print -quit)" + if [ -d "$_dir_satellites" ]; then + file="$(find "$_dir_satellites" -type f \( -name "$_satname.sat" -o -name "$_satname" \) -print -quit)" if [ -f "$file" ]; then _satellite="$(realpath "$file")" _satname="$(basename "$_satellite" .sat)" diff --git a/astronaut/hello.sat b/astronaut/hello.sat index da71f06..7ea9827 100644 --- a/astronaut/hello.sat +++ b/astronaut/hello.sat @@ -87,6 +87,14 @@ fi make DESTDIR="$dir_install" install rm "$dir_install/$dir_prefix/share/info/dir" # This file collides with some other packages. +# Check if an option is enabled +if option test; then + echo "Test option enabled" +fi +# Abbreviation of the above +#option !test echo "Test option disabled" +# You can use bangs ("!") to negate the operation. + # Call the function we imported. For more info see the top of the file, and functions/say_hello.sh say_hello