features.sh 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #!/bin/bash --norc
  2. set -e
  3. shopt -s nullglob
  4. nodefault() {
  5. # We define a function instead of a variable, as variables could
  6. # be predefined in the environment (in theory)
  7. eval "gluon_feature_nodefault_$1() {
  8. :
  9. }"
  10. }
  11. packages() {
  12. :
  13. }
  14. for f in package/features packages/*/features; do
  15. . "$f"
  16. done
  17. # Shell variables can't contain minus signs, so we escape them
  18. # using underscores (and also escape underscores to avoid mapping
  19. # multiple inputs to the same output)
  20. sanitize() {
  21. local v="$1"
  22. v="${v//_/_1}"
  23. v="${v//-/_2}"
  24. echo -n "$v"
  25. }
  26. vars=
  27. for feature in $1; do
  28. if [ "$(type -t gluon_feature_nodefault_${feature})" != 'function' ]; then
  29. echo "gluon-${feature}"
  30. fi
  31. vars="$vars $(sanitize "$feature")=1"
  32. done
  33. nodefault() {
  34. :
  35. }
  36. packages() {
  37. local cond="$(sanitize "$1")"
  38. shift
  39. # We only allow variable names, parentheses and the operators: & | !
  40. if [ "$(expr match "$cond" '.*[^A-Za-z0-9_()&|! ].*')" -gt 0 ]; then
  41. exit 1
  42. fi
  43. # Let will return false when the result of the passed expression is 0,
  44. # so we always add 1. This way false is only returned for syntax errors.
  45. local ret="$(env -i $vars bash --norc -ec "let _result_='1+($cond)'; echo -n \"\$_result_\"" 2>/dev/null)"
  46. case "$ret" in
  47. 2)
  48. for pkg in "$@"; do
  49. echo "$pkg"
  50. done
  51. ;;
  52. 1)
  53. ;;
  54. *)
  55. exit 1
  56. esac
  57. }
  58. for f in package/features packages/*/features; do
  59. . "$f"
  60. done