1 |
Gilles Dartiguelongue posted on Sun, 18 Nov 2012 20:06:30 +0100 as |
2 |
excerpted: |
3 |
|
4 |
It's admittedly a style thing thus pretty much up to the author, purely |
5 |
bikeshedding in the original sense of the essay's trivial color choice, |
6 |
but... |
7 |
|
8 |
> # Even though xz-utils are in @system, they must still be added to DEPEND; see |
9 |
> # http://archives.gentoo.org/gentoo-dev/msg_a0d4833eb314d1be5d5802a3b710e0a4.xml |
10 |
> if [[ ${GST_TARBALL_SUFFIX} == "xz" ]]; then |
11 |
> DEPEND="${DEPEND} app-arch/xz-utils" |
12 |
> fi |
13 |
|
14 |
Single-clause conditional tests (without an else clause) such as the above |
15 |
can be trivially rewritten like so: |
16 |
|
17 |
[[ ${GST_TARBALL_SUFFIX} == "xz" ]] && \ |
18 |
DEPEND="${DEPEND} app-arch/xz-utils" |
19 |
|
20 |
Two lines (or one if short enough) instead of three and more concise (fi |
21 |
line omitted entirely, "if" omitted, "; then" replaced with &&). |
22 |
|
23 |
It's possible to do similar with if/then/else -> &&/||, but that may |
24 |
require {} for clarity if not bash parsing, and IMO is not nearly as |
25 |
clear as the more straightforward no-else-clause case. So if there's |
26 |
an else clause, I prefer if/then/else; if not, I prefer the && or || logic. |
27 |
|
28 |
> if [[ ${PN} != ${GST_ORG_MODULE} ]]; then |
29 |
> # Do not run test phase for invididual plugin ebuilds. |
30 |
> RESTRICT="test" |
31 |
> fi |
32 |
|
33 |
Another example, this one definitely a one-liner (plus comment, |
34 |
"invididual" typo left intact)... |
35 |
|
36 |
# Do not run test phase for invididual plugin ebuilds. |
37 |
[[ ${PN} != ${GST_ORG_MODULE} ]] && RESTRICT="test" |
38 |
|
39 |
Or, avoiding that ! in the test and changing the && to ||... |
40 |
|
41 |
[[ ${PN} == ${GST_ORG_MODULE} ]] || RESTRICT="test" |
42 |
|
43 |
Also, while we're here, "test" is a string-literal with no possibility of |
44 |
spaces or anything else that could otherwise confuse bash, so needs no |
45 |
quotes. And I'm not sure if the != pattern matching trigger was |
46 |
deliberate or not (see bash's "help [[" output). Thus... |
47 |
|
48 |
[[ ${PN} = ${GST_ORG_MODULE} ]] || RESTRICT=test |
49 |
|
50 |
... or... |
51 |
|
52 |
[[ ${PN} == ${GST_ORG_MODULE} ]] || RESTRICT=test |
53 |
|
54 |
... with the single vs. double equals making the string match (single) vs |
55 |
pattern match (double) explicit. If the test-internal-not form is |
56 |
retained, the string match form would be... |
57 |
|
58 |
[[ ${PN} ! = ${GST_ORG_MODULE} ]] && RESTRICT=test |
59 |
|
60 |
... while the pattern match form would retain the != that was used |
61 |
(the distinction being the separating space, != is a pattern match |
62 |
as is ==, ! = is a string match as is a single = ). |
63 |
|
64 |
|
65 |
> # added to remove circular deps # 6/2/2006 - zaheerm |
66 |
if [[ ${PN} != ${GST_ORG_MODULE} ]]; then |
67 |
> RDEPEND="${RDEPEND} media-libs/${GST_ORG_MODULE}:${SLOT}" |
68 |
> fi |
69 |
|
70 |
Ditto... Further examples skipped. |
71 |
|
72 |
> # @FUNCTION: gst-plugins10_src_install gst-plugins10_src_install() { |
73 |
> gst-plugins10_find_plugin_dir |
74 |
> |
75 |
> if has "${EAPI:-0}" 0 1 2 3 ; then |
76 |
> emake install DESTDIR="${D}" || die |
77 |
> [[ -e README ]] && dodoc README |
78 |
> else |
79 |
> default |
80 |
> fi |
81 |
> |
82 |
> [[ ${GST_LA_PUNT} = "yes" ]] && prune_libtool_files --modules |
83 |
> } |
84 |
|
85 |
Here (last line before the function-closing "}", as I said above, I prefer |
86 |
if/then/else where there's an else clause, so wouldn't change the upper |
87 |
conditional) we see a counter-example, doing it just as I suggested. |
88 |
The if/then version (keeping function indent) would look like this. |
89 |
|
90 |
if [[ ${GST_LA_PUNT} = "yes" ]]; then |
91 |
prune_libtool_files --modules |
92 |
fi |
93 |
|
94 |
Of course regardless of that, the quotes around "yes" may be omitted as |
95 |
it's a string literal. (And here, the explicit single-equals string- |
96 |
literal match is used, as opposed to the double-equals pattern match. Of |
97 |
course either one would work here as it's a trivial pattern, just noting |
98 |
it for consistency.) |
99 |
|
100 |
[[ ${GST_LA_PUNT} = yes ]] && prune_libtool_files --modules |
101 |
|
102 |
|
103 |
But as I said up top, that's (mostly, the pattern matching vs string |
104 |
matching will occasionally bite if you're not on the lookout for it) |
105 |
trivial style stuff. You may well prefer the way it is now. But in that |
106 |
case, why the counter-example, omitting if/then? (You may of course |
107 |
fairly point out that consistency is a trivial style thing too. =:^) |
108 |
|
109 |
-- |
110 |
Duncan - List replies preferred. No HTML msgs. |
111 |
"Every nonfree program has a lord, a master -- |
112 |
and if you use the program, he is your master." Richard Stallman |