I believe the pkg_setup() sample in the development guide could be made better by using a case statement. I'm not a bash expert but I believe the patch below is working fine judging by the tests I made with a simpler version of the script. By using a series of ifs, ebuilds could become difficult to maintain if the package supports several different (as the example proposes) GUIs. I suggest using a case statement as patches for another compilation flag would be really simple to review. Hoping this will make gentoo documentation better, the proposed patch is available at "aditional information" below. Reproducible: Always Steps to Reproduce: 1. open the development guide 2. follow the links down to ebuild writing -> ebuild functions -> pkg_setup 3. read the sample Actual Results: pkg_setup() { # We need to know which GUI we're building in several # different places, so work it out here. if use gtk ; then if use gtk2 ; then export mypkg_gui="gtk2" else export mypkg_gui="gtk1" fi elif use motif then export mypkg_gui="motif" else export mypkg_gui="athena" fi } Expected Results: something like the following: 47 pkg_setup() { 48 # We need to know which GUI we're building in several 49 # different places, so work it out here. 50 useing="$USE --" 51 eval set -- $useing 52 while true; do 53 case $1 in 54 gtk2) 55 export mypkg_gui="gtk2"; 56 break;; 57 gtk1) 58 export mypkg_gui="gtk1"; 59 break;; 60 motif) 61 export mypkg_gui="motif" 62 break;; 63 athena) 64 export mypkg_gui="athena"; 65 break;; 66 --) 67 break;; 68 *) 69 shift 1;; 70 esac 71 done 72 } patch for the file: diff --git a/ebuild-writing/functions/pkg_setup/text.xml b/ebuild-writing/functi index a7fe9d2..b8849a3 100644 --- a/ebuild-writing/functions/pkg_setup/text.xml +++ b/ebuild-writing/functions/pkg_setup/text.xml @@ -47,17 +47,28 @@ pkg_setup() pkg_setup() { # We need to know which GUI we're building in several # different places, so work it out here. - if use gtk ; then - if use gtk2 ; then - export mypkg_gui="gtk2" - else - export mypkg_gui="gtk1" - fi - elif use motif then - export mypkg_gui="motif" - else - export mypkg_gui="athena" - fi + useing="$USE --" + eval set -- $useing + while true; do + case $1 in + gtk2) + export mypkg_gui="gtk2"; + break;; + gtk1) + export mypkg_gui="gtk1"; + break;; + motif) + export mypkg_gui="motif" + break;; + athena) + export mypkg_gui="athena"; + break;; + --) + break;; + *) + shift 1;; + esac + done } </codesample> </body>
ran more tests and the following also works. And looks better: diff --git a/ebuild-writing/functions/pkg_setup/text.xml b/ebuild-writing/functi index a7fe9d2..54c6318 100644 --- a/ebuild-writing/functions/pkg_setup/text.xml +++ b/ebuild-writing/functions/pkg_setup/text.xml @@ -47,17 +47,27 @@ pkg_setup() pkg_setup() { # We need to know which GUI we're building in several # different places, so work it out here. - if use gtk ; then - if use gtk2 ; then - export mypkg_gui="gtk2" - else - export mypkg_gui="gtk1" - fi - elif use motif then - export mypkg_gui="motif" - else - export mypkg_gui="athena" - fi + eval set -- "$USE --" + while true; do + case $1 in + gtk2) + export mypkg_gui="gtk2"; + break;; + gtk1) + export mypkg_gui="gtk1"; + break;; + motif) + export mypkg_gui="motif" + break;; + athena) + export mypkg_gui="athena"; + break;; + --) + break;; + *) + shift 1;; + esac + done } </codesample> </body>
The original code shows the usage of the use() function better than your proposed change (where the use() function is abstracted out). The use() function is used quite often and is beneficial to be exposed to, in my opinion.
(In reply to comment #2) > The original code shows the usage of the use() function better than your > proposed change (where the use() function is abstracted out). The use() > function is used quite often and is beneficial to be exposed to, in my opinion. ok, I'm new to gentoo so not really into its internals yet, does use() return the value of the USE variable ? If so, the case also applies. But judging from the original version use() is implemented so that it tests whether USE contains the argument we just passed, right ? would there be a way to use "case" statements since it does, IMHO, make the code a lot easier to follow and extend ? could use() be extended so that if we call it no arguments it returns the value of $USE ?
As stated on the front page of the devmanual: "If you have any corrections, suggestions or improvements please file a bug at bugs.gentoo.org and assign it to qa@gentoo.org." Not the GDP's problem. Reassigning to QA.
Part of the point of that code is that USE isn't in any particular order. The preference when multiple options are available is set by ebuilds, not by the user. Messing around with USE directly is generally a sign that you're doing something wrong.
Doesn't the Code sample on http://devmanual.gentoo.org/ebuild-writing/functions/pkg_setup/index.html works for you?
(In reply to comment #6) > Doesn't the Code sample on > > http://devmanual.gentoo.org/ebuild-writing/functions/pkg_setup/index.html > > works for you? It does, I was only thinking about ways of improving it :-)
Not sure if something is pending here. Reopen if needed