kde-plasma/breeze-gtk: x86 stable wrt bug #613144
[gentoo.git] / eclass / ruby-ng.eclass
1 # Copyright 1999-2017 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3
4 # @ECLASS: ruby-ng.eclass
5 # @MAINTAINER:
6 # Ruby herd <ruby@gentoo.org>
7 # @AUTHOR:
8 # Author: Diego E. Pettenò <flameeyes@gentoo.org>
9 # Author: Alex Legler <a3li@gentoo.org>
10 # Author: Hans de Graaff <graaff@gentoo.org>
11 # @BLURB: An eclass for installing Ruby packages with proper support for multiple Ruby slots.
12 # @DESCRIPTION:
13 # The Ruby eclass is designed to allow an easier installation of Ruby packages
14 # and their incorporation into the Gentoo Linux system.
15 #
16 # Currently available targets are:
17 #  * ruby19 - Ruby (MRI) 1.9.x
18 #  * ruby20 - Ruby (MRI) 2.0.x
19 #  * ruby21 - Ruby (MRI) 2.1.x
20 #  * ruby22 - Ruby (MRI) 2.2.x
21 #  * ruby23 - Ruby (MRI) 2.3.x
22 #  * ruby24 - Ruby (MRI) 2.4.x
23 #  * jruby  - JRuby
24 #  * rbx    - Rubinius
25 #
26 # This eclass does not define the implementation of the configure,
27 # compile, test, or install phases. Instead, the default phases are
28 # used.  Specific implementations of these phases can be provided in
29 # the ebuild either to be run for each Ruby implementation, or for all
30 # Ruby implementations, as follows:
31 #
32 #  * each_ruby_configure
33 #  * all_ruby_configure
34
35 # @ECLASS-VARIABLE: USE_RUBY
36 # @DEFAULT_UNSET
37 # @REQUIRED
38 # @DESCRIPTION:
39 # This variable contains a space separated list of targets (see above) a package
40 # is compatible to. It must be set before the `inherit' call. There is no
41 # default. All ebuilds are expected to set this variable.
42
43 # @ECLASS-VARIABLE: RUBY_PATCHES
44 # @DEFAULT_UNSET
45 # @DESCRIPTION:
46 # A String or Array of filenames of patches to apply to all implementations.
47
48 # @ECLASS-VARIABLE: RUBY_OPTIONAL
49 # @DESCRIPTION:
50 # Set the value to "yes" to make the dependency on a Ruby interpreter
51 # optional and then ruby_implementations_depend() to help populate
52 # DEPEND and RDEPEND.
53
54 # @ECLASS-VARIABLE: RUBY_S
55 # @DEFAULT_UNSET
56 # @DESCRIPTION:
57 # If defined this variable determines the source directory name after
58 # unpacking. This defaults to the name of the package. Note that this
59 # variable supports a wildcard mechanism to help with github tarballs
60 # that contain the commit hash as part of the directory name.
61
62 # @ECLASS-VARIABLE: RUBY_QA_ALLOWED_LIBS
63 # @DEFAULT_UNSET
64 # @DESCRIPTION:
65 # If defined this variable contains a whitelist of shared objects that
66 # are allowed to exist even if they don't link to libruby. This avoids
67 # the QA check that makes this mandatory. This is most likely not what
68 # you are looking for if you get the related "Missing links" QA warning,
69 # since the proper fix is almost always to make sure the shared object
70 # is linked against libruby. There are cases were this is not the case
71 # and the shared object is generic code to be used in some other way
72 # (e.g. selenium's firefox driver extension). When set this argument is
73 # passed to "grep -E" to remove reporting of these shared objects.
74
75 local inherits=""
76 case ${EAPI} in
77         2|3|4|5)
78                 inherits="eutils"
79                 ;;
80 esac
81
82 inherit ${inherits} java-utils-2 multilib toolchain-funcs ruby-utils
83
84 EXPORT_FUNCTIONS src_unpack src_prepare src_configure src_compile src_test src_install pkg_setup
85
86 case ${EAPI} in
87         0|1)
88                 die "Unsupported EAPI=${EAPI} (too old) for ruby-ng.eclass" ;;
89         2|3) ;;
90         4|5|6)
91                 # S is no longer automatically assigned when it doesn't exist.
92                 S="${WORKDIR}"
93                 ;;
94         *)
95                 die "Unknown EAPI=${EAPI} for ruby-ng.eclass"
96 esac
97
98 # @FUNCTION: ruby_implementation_depend
99 # @USAGE: target [comparator [version]]
100 # @RETURN: Package atom of a Ruby implementation to be used in dependencies.
101 # @DESCRIPTION:
102 # This function returns the formal package atom for a Ruby implementation.
103 #
104 # `target' has to be one of the valid values for USE_RUBY (see above)
105 #
106 # Set `comparator' and `version' to include a comparator (=, >=, etc.) and a
107 # version string to the returned string
108 ruby_implementation_depend() {
109         _ruby_implementation_depend $1
110 }
111
112 # @FUNCTION: ruby_samelib
113 # @RETURN: use flag string with current ruby implementations
114 # @DESCRIPTION:
115 # Convenience function to output the use dependency part of a
116 # dependency. Used as a building block for ruby_add_rdepend() and
117 # ruby_add_bdepend(), but may also be useful in an ebuild to specify
118 # more complex dependencies.
119 ruby_samelib() {
120         local res=
121         for _ruby_implementation in $USE_RUBY; do
122                 has -${_ruby_implementation} $@ || \
123                         res="${res}ruby_targets_${_ruby_implementation}?,"
124         done
125
126         echo "[${res%,}]"
127 }
128
129 _ruby_atoms_samelib_generic() {
130         eshopts_push -o noglob
131         echo "RUBYTARGET? ("
132         for token in $*; do
133                 case "$token" in
134                         "||" | "(" | ")" | *"?")
135                                 echo "${token}" ;;
136                         *])
137                                 echo "${token%[*}[RUBYTARGET,${token/*[}" ;;
138                         *)
139                                 echo "${token}[RUBYTARGET]" ;;
140                 esac
141         done
142         echo ")"
143         eshopts_pop
144 }
145
146 # @FUNCTION: ruby_implementation_command
147 # @RETURN: the path to the given ruby implementation
148 # @DESCRIPTION:
149 # Not all implementations have the same command basename as the
150 # target; This function translate between the two
151 ruby_implementation_command() {
152         local _ruby_name=$1
153
154         # Add all USE_RUBY values where the flag name diverts from the binary here
155
156         echo $(type -p ${_ruby_name} 2>/dev/null)
157 }
158
159 _ruby_atoms_samelib() {
160         local atoms=$(_ruby_atoms_samelib_generic "$*")
161
162         for _ruby_implementation in $USE_RUBY; do
163                 echo "${atoms//RUBYTARGET/ruby_targets_${_ruby_implementation}}"
164         done
165 }
166
167 _ruby_wrap_conditions() {
168         local conditions="$1"
169         local atoms="$2"
170
171         for condition in $conditions; do
172                 atoms="${condition}? ( ${atoms} )"
173         done
174
175         echo "$atoms"
176 }
177
178 # @FUNCTION: ruby_add_rdepend
179 # @USAGE: dependencies
180 # @DESCRIPTION:
181 # Adds the specified dependencies, with use condition(s) to RDEPEND,
182 # taking the current set of ruby targets into account. This makes sure
183 # that all ruby dependencies of the package are installed for the same
184 # ruby targets. Use this function for all ruby dependencies instead of
185 # setting RDEPEND yourself. The list of atoms uses the same syntax as
186 # normal dependencies.
187 #
188 # Note: runtime dependencies are also added as build-time test
189 # dependencies.
190 ruby_add_rdepend() {
191         case $# in
192                 1) ;;
193                 2)
194                         [[ "${GENTOO_DEV}" == "yes" ]] && eqawarn "You can now use the usual syntax in ruby_add_rdepend for $CATEGORY/$PF"
195                         ruby_add_rdepend "$(_ruby_wrap_conditions "$1" "$2")"
196                         return
197                         ;;
198                 *)
199                         die "bad number of arguments to $0"
200                         ;;
201         esac
202
203         local dependency=$(_ruby_atoms_samelib "$1")
204
205         RDEPEND="${RDEPEND} $dependency"
206
207         # Add the dependency as a test-dependency since we're going to
208         # execute the code during test phase.
209         DEPEND="${DEPEND} test? ( ${dependency} )"
210         has test "$IUSE" || IUSE="${IUSE} test"
211 }
212
213 # @FUNCTION: ruby_add_bdepend
214 # @USAGE: dependencies
215 # @DESCRIPTION:
216 # Adds the specified dependencies, with use condition(s) to DEPEND,
217 # taking the current set of ruby targets into account. This makes sure
218 # that all ruby dependencies of the package are installed for the same
219 # ruby targets. Use this function for all ruby dependencies instead of
220 # setting DEPEND yourself. The list of atoms uses the same syntax as
221 # normal dependencies.
222 ruby_add_bdepend() {
223         case $# in
224                 1) ;;
225                 2)
226                         [[ "${GENTOO_DEV}" == "yes" ]] && eqawarn "You can now use the usual syntax in ruby_add_bdepend for $CATEGORY/$PF"
227                         ruby_add_bdepend "$(_ruby_wrap_conditions "$1" "$2")"
228                         return
229                         ;;
230                 *)
231                         die "bad number of arguments to $0"
232                         ;;
233         esac
234
235         local dependency=$(_ruby_atoms_samelib "$1")
236
237         DEPEND="${DEPEND} $dependency"
238         RDEPEND="${RDEPEND}"
239 }
240
241 # @FUNCTION: ruby_get_use_implementations
242 # @DESCRIPTION:
243 # Gets an array of ruby use targets enabled by the user
244 ruby_get_use_implementations() {
245         local i implementation
246         for implementation in ${USE_RUBY}; do
247                 use ruby_targets_${implementation} && i+=" ${implementation}"
248         done
249         echo $i
250 }
251
252 # @FUNCTION: ruby_get_use_targets
253 # @DESCRIPTION:
254 # Gets an array of ruby use targets that the ebuild sets
255 ruby_get_use_targets() {
256         local t implementation
257         for implementation in ${USE_RUBY}; do
258                 t+=" ruby_targets_${implementation}"
259         done
260         echo $t
261 }
262
263 # @FUNCTION: ruby_implementations_depend
264 # @RETURN: Dependencies suitable for injection into DEPEND and RDEPEND.
265 # @DESCRIPTION:
266 # Produces the dependency string for the various implementations of ruby
267 # which the package is being built against. This should not be used when
268 # RUBY_OPTIONAL is unset but must be used if RUBY_OPTIONAL=yes. Do not
269 # confuse this function with ruby_implementation_depend().
270 #
271 # @EXAMPLE:
272 # EAPI=6
273 # RUBY_OPTIONAL=yes
274 #
275 # inherit ruby-ng
276 # ...
277 # DEPEND="ruby? ( $(ruby_implementations_depend) )"
278 # RDEPEND="${DEPEND}"
279 ruby_implementations_depend() {
280         local depend
281         for _ruby_implementation in ${USE_RUBY}; do
282                 depend="${depend}${depend+ }ruby_targets_${_ruby_implementation}? ( $(ruby_implementation_depend $_ruby_implementation) )"
283         done
284         echo "${depend}"
285 }
286
287 IUSE+=" $(ruby_get_use_targets)"
288 # If you specify RUBY_OPTIONAL you also need to take care of
289 # ruby useflag and dependency.
290 if [[ ${RUBY_OPTIONAL} != yes ]]; then
291         DEPEND="${DEPEND} $(ruby_implementations_depend)"
292         RDEPEND="${RDEPEND} $(ruby_implementations_depend)"
293
294         case ${EAPI:-0} in
295                 4|5|6)
296                         REQUIRED_USE+=" || ( $(ruby_get_use_targets) )"
297                         ;;
298         esac
299 fi
300
301 _ruby_invoke_environment() {
302         old_S=${S}
303         case ${EAPI} in
304                 4|5|6)
305                         if [ -z "${RUBY_S}" ]; then
306                                 sub_S=${P}
307                         else
308                                 sub_S=${RUBY_S}
309                         fi
310                         ;;
311                 *)
312                         sub_S=${S#${WORKDIR}/}
313                         ;;
314         esac
315
316         # Special case, for the always-lovely GitHub fetches. With this,
317         # we allow the star glob to just expand to whatever directory it's
318         # called.
319         if [[ "${sub_S}" = *"*"* ]]; then
320                 case ${EAPI} in
321                         2|3)
322                                 #The old method of setting S depends on undefined package
323                                 # manager behaviour, so encourage upgrading to EAPI=4.
324                                 eqawarn "Using * expansion of S is deprecated. Use EAPI and RUBY_S instead."
325                                 ;;
326                 esac
327                 pushd "${WORKDIR}"/all &>/dev/null || die
328                 # use an array to trigger filename expansion
329                 # fun fact: this expansion fails in src_unpack() but the original
330                 # code did not have any checks for failed expansion, so we can't
331                 # really add one now without redesigning stuff hard.
332                 sub_S=( ${sub_S} )
333                 if [[ ${#sub_S[@]} -gt 1 ]]; then
334                         die "sub_S did expand to multiple paths: ${sub_S[*]}"
335                 fi
336                 popd &>/dev/null || die
337         fi
338
339         environment=$1; shift
340
341         my_WORKDIR="${WORKDIR}"/${environment}
342         S="${my_WORKDIR}"/"${sub_S}"
343
344         if [[ -d "${S}" ]]; then
345                 pushd "$S" &>/dev/null || die
346         elif [[ -d "${my_WORKDIR}" ]]; then
347                 pushd "${my_WORKDIR}" &>/dev/null || die
348         else
349                 pushd "${WORKDIR}" &>/dev/null || die
350         fi
351
352         ebegin "Running ${_PHASE:-${EBUILD_PHASE}} phase for $environment"
353         "$@"
354         popd &>/dev/null || die
355
356         S=${old_S}
357 }
358
359 _ruby_each_implementation() {
360         local invoked=no
361         for _ruby_implementation in ${USE_RUBY}; do
362                 # only proceed if it's requested
363                 use ruby_targets_${_ruby_implementation} || continue
364
365                 RUBY=$(ruby_implementation_command ${_ruby_implementation})
366                 invoked=yes
367
368                 if [[ -n "$1" ]]; then
369                         _ruby_invoke_environment ${_ruby_implementation} "$@"
370                 fi
371
372                 unset RUBY
373         done
374
375         if [[ ${invoked} == "no" ]]; then
376                 eerror "You need to select at least one compatible Ruby installation target via RUBY_TARGETS in make.conf."
377                 eerror "Compatible targets for this package are: ${USE_RUBY}"
378                 eerror
379                 eerror "See https://www.gentoo.org/proj/en/prog_lang/ruby/index.xml#doc_chap3 for more information."
380                 eerror
381                 die "No compatible Ruby target selected."
382         fi
383 }
384
385 # @FUNCTION: ruby-ng_pkg_setup
386 # @DESCRIPTION:
387 # Check whether at least one ruby target implementation is present.
388 ruby-ng_pkg_setup() {
389         # This only checks that at least one implementation is present
390         # before doing anything; by leaving the parameters empty we know
391         # it's a special case.
392         _ruby_each_implementation
393
394         has ruby_targets_jruby ${IUSE} && use ruby_targets_jruby && java-pkg_setup-vm
395 }
396
397 # @FUNCTION: ruby-ng_src_unpack
398 # @DESCRIPTION:
399 # Unpack the source archive.
400 ruby-ng_src_unpack() {
401         mkdir "${WORKDIR}"/all
402         pushd "${WORKDIR}"/all &>/dev/null || die
403
404         # We don't support an each-unpack, it's either all or nothing!
405         if type all_ruby_unpack &>/dev/null; then
406                 _ruby_invoke_environment all all_ruby_unpack
407         else
408                 [[ -n ${A} ]] && unpack ${A}
409         fi
410
411         popd &>/dev/null || die
412 }
413
414 _ruby_apply_patches() {
415         case ${EAPI} in
416                 2|3|4|5)
417                         for patch in "${RUBY_PATCHES[@]}"; do
418                                 if [ -f "${patch}" ]; then
419                                         epatch "${patch}"
420                                 elif [ -f "${FILESDIR}/${patch}" ]; then
421                                         epatch "${FILESDIR}/${patch}"
422                                 else
423                                         die "Cannot find patch ${patch}"
424                                 fi
425                         done
426                         ;;
427                 6)
428                         if [[ -n ${RUBY_PATCHES[@]} ]]; then
429                            eqawarn "RUBY_PATCHES is no longer supported, use PATCHES instead"
430                         fi
431                         ;;
432         esac
433
434         # This is a special case: instead of executing just in the special
435         # "all" environment, this will actually copy the effects on _all_
436         # the other environments, and is thus executed before the copy
437         type all_ruby_prepare &>/dev/null && all_ruby_prepare
438 }
439
440 _ruby_source_copy() {
441         # Until we actually find a reason not to, we use hardlinks, this
442         # should reduce the amount of disk space that is wasted by this.
443         cp -prlP all ${_ruby_implementation} \
444                 || die "Unable to copy ${_ruby_implementation} environment"
445 }
446
447 # @FUNCTION: ruby-ng_src_prepare
448 # @DESCRIPTION:
449 # Apply patches and prepare versions for each ruby target
450 # implementation. Also carry out common clean up tasks.
451 ruby-ng_src_prepare() {
452         # Way too many Ruby packages are prepared on OSX without removing
453         # the extra data forks, we do it here to avoid repeating it for
454         # almost every other ebuild.
455         find . -name '._*' -delete
456
457         # Handle PATCHES and user supplied patches via the default phase
458         case ${EAPI} in
459                 6)
460                         _ruby_invoke_environment all default
461                         ;;
462         esac
463
464         _ruby_invoke_environment all _ruby_apply_patches
465
466         _PHASE="source copy" \
467                 _ruby_each_implementation _ruby_source_copy
468
469         if type each_ruby_prepare &>/dev/null; then
470                 _ruby_each_implementation each_ruby_prepare
471         fi
472 }
473
474 # @FUNCTION: ruby-ng_src_configure
475 # @DESCRIPTION:
476 # Configure the package.
477 ruby-ng_src_configure() {
478         if type each_ruby_configure &>/dev/null; then
479                 _ruby_each_implementation each_ruby_configure
480         fi
481
482         type all_ruby_configure &>/dev/null && \
483                 _ruby_invoke_environment all all_ruby_configure
484 }
485
486 # @FUNCTION: ruby-ng_src_compile
487 # @DESCRIPTION:
488 # Compile the package.
489 ruby-ng_src_compile() {
490         if type each_ruby_compile &>/dev/null; then
491                 _ruby_each_implementation each_ruby_compile
492         fi
493
494         type all_ruby_compile &>/dev/null && \
495                 _ruby_invoke_environment all all_ruby_compile
496 }
497
498 # @FUNCTION: ruby-ng_src_test
499 # @DESCRIPTION:
500 # Run tests for the package.
501 ruby-ng_src_test() {
502         if type each_ruby_test &>/dev/null; then
503                 _ruby_each_implementation each_ruby_test
504         fi
505
506         type all_ruby_test &>/dev/null && \
507                 _ruby_invoke_environment all all_ruby_test
508 }
509
510 _each_ruby_check_install() {
511         local scancmd=scanelf
512         # we have a Mach-O object here
513         [[ ${CHOST} == *-darwin ]] && scancmd=scanmacho
514
515         has "${EAPI}" 2 && ! use prefix && EPREFIX=
516
517         local libruby_basename=$(${RUBY} -rrbconfig -e 'puts RbConfig::CONFIG["LIBRUBY_SO"]')
518         local libruby_soname=$(basename $(${scancmd} -F "%S#F" -qS "${EPREFIX}/usr/$(get_libdir)/${libruby_basename}") 2>/dev/null)
519         local sitedir=$(${RUBY} -rrbconfig -e 'puts RbConfig::CONFIG["sitedir"]')
520         local sitelibdir=$(${RUBY} -rrbconfig -e 'puts RbConfig::CONFIG["sitelibdir"]')
521
522         # Look for wrong files in sitedir
523         # if [[ -d "${D}${sitedir}" ]]; then
524         #       local f=$(find "${D}${sitedir}" -mindepth 1 -maxdepth 1 -not -wholename "${D}${sitelibdir}")
525         #       if [[ -n ${f} ]]; then
526         #               eerror "Found files in sitedir, outsite sitelibdir:"
527         #               eerror "${f}"
528         #               die "Misplaced files in sitedir"
529         #       fi
530         # fi
531
532         # The current implementation lacks libruby (i.e.: jruby)
533         [[ -z ${libruby_soname} ]] && return 0
534
535         # Check also the gems directory, since we could be installing compiled
536         # extensions via ruby-fakegem; make sure to check only in sitelibdir, since
537         # that's what changes between two implementations (otherwise you'd get false
538         # positives now that Ruby 1.9.2 installs with the same sitedir as 1.8)
539         ${scancmd} -qnR "${D}${sitelibdir}" "${D}${sitelibdir/site_ruby/gems}" \
540                 | fgrep -v "${libruby_soname}" \
541                 | grep -E -v "${RUBY_QA_ALLOWED_LIBS}" \
542                 > "${T}"/ruby-ng-${_ruby_implementation}-mislink.log
543
544         if [[ -s "${T}"/ruby-ng-${_ruby_implementation}-mislink.log ]]; then
545                 ewarn "Extensions installed for ${_ruby_implementation} with missing links to ${libruby_soname}"
546                 ewarn $(< "${T}"/ruby-ng-${_ruby_implementation}-mislink.log )
547                 die "Missing links to ${libruby_soname}"
548         fi
549 }
550
551 # @FUNCTION: ruby-ng_src_install
552 # @DESCRIPTION:
553 # Install the package for each ruby target implementation.
554 ruby-ng_src_install() {
555         if type each_ruby_install &>/dev/null; then
556                 _ruby_each_implementation each_ruby_install
557         fi
558
559         type all_ruby_install &>/dev/null && \
560                 _ruby_invoke_environment all all_ruby_install
561
562         _PHASE="check install" \
563                 _ruby_each_implementation _each_ruby_check_install
564 }
565
566 # @FUNCTION: ruby_rbconfig_value
567 # @USAGE: rbconfig item
568 # @RETURN: Returns the value of the given rbconfig item of the Ruby interpreter in ${RUBY}.
569 ruby_rbconfig_value() {
570         echo $(${RUBY} -rrbconfig -e "puts RbConfig::CONFIG['$1']")
571 }
572
573 # @FUNCTION: doruby
574 # @USAGE: file [file...]
575 # @DESCRIPTION:
576 # Installs the specified file(s) into the sitelibdir of the Ruby interpreter in ${RUBY}.
577 doruby() {
578         [[ -z ${RUBY} ]] && die "\$RUBY is not set"
579         has "${EAPI}" 2 && ! use prefix && EPREFIX=
580         ( # don't want to pollute calling env
581                 sitelibdir=$(ruby_rbconfig_value 'sitelibdir')
582                 insinto ${sitelibdir#${EPREFIX}}
583                 insopts -m 0644
584                 doins "$@"
585         ) || die "failed to install $@"
586 }
587
588 # @FUNCTION: ruby_get_libruby
589 # @RETURN: The location of libruby*.so belonging to the Ruby interpreter in ${RUBY}.
590 ruby_get_libruby() {
591         ${RUBY} -rrbconfig -e 'puts File.join(RbConfig::CONFIG["libdir"], RbConfig::CONFIG["LIBRUBY"])'
592 }
593
594 # @FUNCTION: ruby_get_hdrdir
595 # @RETURN: The location of the header files belonging to the Ruby interpreter in ${RUBY}.
596 ruby_get_hdrdir() {
597         local rubyhdrdir=$(ruby_rbconfig_value 'rubyhdrdir')
598
599         if [[ "${rubyhdrdir}" = "nil" ]] ; then
600                 rubyhdrdir=$(ruby_rbconfig_value 'archdir')
601         fi
602
603         echo "${rubyhdrdir}"
604 }
605
606 # @FUNCTION: ruby_get_version
607 # @RETURN: The version of the Ruby interpreter in ${RUBY}, or what 'ruby' points to.
608 ruby_get_version() {
609         local ruby=${RUBY:-$(type -p ruby 2>/dev/null)}
610
611         echo $(${ruby} -e 'puts RUBY_VERSION')
612 }
613
614 # @FUNCTION: ruby_get_implementation
615 # @RETURN: The implementation of the Ruby interpreter in ${RUBY}, or what 'ruby' points to.
616 ruby_get_implementation() {
617         local ruby=${RUBY:-$(type -p ruby 2>/dev/null)}
618
619         case $(${ruby} --version) in
620                 *jruby*)
621                         echo "jruby"
622                         ;;
623                 *rubinius*)
624                         echo "rbx"
625                         ;;
626                 *)
627                         echo "mri"
628                         ;;
629         esac
630 }
631
632 # @FUNCTION: ruby-ng_rspec <arguments>
633 # @DESCRIPTION:
634 # This is simply a wrapper around the rspec command (executed by $RUBY})
635 # which also respects TEST_VERBOSE and NOCOLOR environment variables.
636 # Optionally takes arguments to pass on to the rspec invocation.  The
637 # environment variable RSPEC_VERSION can be used to control the specific
638 # rspec version that must be executed. It defaults to 2 for historical
639 # compatibility.
640 ruby-ng_rspec() {
641         local version=${RSPEC_VERSION-2}
642         local files="$@"
643
644         # Explicitly pass the expected spec directory since the versioned
645         # rspec wrappers don't handle this automatically.
646         if [ ${#@} -eq 0 ]; then
647                 files="spec"
648         fi
649
650         if [[ ${DEPEND} != *"dev-ruby/rspec"* ]]; then
651                 ewarn "Missing dev-ruby/rspec in \${DEPEND}"
652         fi
653
654         local rspec_params=
655         case ${NOCOLOR} in
656                 1|yes|true)
657                         rspec_params+=" --no-color"
658                         ;;
659                 *)
660                         rspec_params+=" --color"
661                         ;;
662         esac
663
664         case ${TEST_VERBOSE} in
665                 1|yes|true)
666                         rspec_params+=" --format documentation"
667                         ;;
668                 *)
669                         rspec_params+=" --format progress"
670                         ;;
671         esac
672
673         ${RUBY} -S rspec-${version} ${rspec_params} ${files} || die "rspec failed"
674 }
675
676 # @FUNCTION: ruby-ng_cucumber
677 # @DESCRIPTION:
678 # This is simply a wrapper around the cucumber command (executed by $RUBY})
679 # which also respects TEST_VERBOSE and NOCOLOR environment variables.
680 ruby-ng_cucumber() {
681         if [[ ${DEPEND} != *"dev-util/cucumber"* ]]; then
682                 ewarn "Missing dev-util/cucumber in \${DEPEND}"
683         fi
684
685         local cucumber_params=
686         case ${NOCOLOR} in
687                 1|yes|true)
688                         cucumber_params+=" --no-color"
689                         ;;
690                 *)
691                         cucumber_params+=" --color"
692                         ;;
693         esac
694
695         case ${TEST_VERBOSE} in
696                 1|yes|true)
697                         cucumber_params+=" --format pretty"
698                         ;;
699                 *)
700                         cucumber_params+=" --format progress"
701                         ;;
702         esac
703
704         if [[ ${RUBY} == *jruby ]]; then
705                 ewarn "Skipping cucumber tests on JRuby (unsupported)."
706                 return 0
707         fi
708
709         ${RUBY} -S cucumber ${cucumber_params} "$@" || die "cucumber failed"
710 }
711
712 # @FUNCTION: ruby-ng_testrb-2
713 # @DESCRIPTION:
714 # This is simply a replacement for the testrb command that load the test
715 # files and execute them, with test-unit 2.x. This actually requires
716 # either an old test-unit-2 version or 2.5.1-r1 or later, as they remove
717 # their script and we installed a broken wrapper for a while.
718 # This also respects TEST_VERBOSE and NOCOLOR environment variables.
719 ruby-ng_testrb-2() {
720         if [[ ${DEPEND} != *"dev-ruby/test-unit"* ]]; then
721                 ewarn "Missing dev-ruby/test-unit in \${DEPEND}"
722         fi
723
724         local testrb_params=
725         case ${NOCOLOR} in
726                 1|yes|true)
727                         testrb_params+=" --no-use-color"
728                         ;;
729                 *)
730                         testrb_params+=" --use-color=auto"
731                         ;;
732         esac
733
734         case ${TEST_VERBOSE} in
735                 1|yes|true)
736                         testrb_params+=" --verbose=verbose"
737                         ;;
738                 *)
739                         testrb_params+=" --verbose=normal"
740                         ;;
741         esac
742
743         ${RUBY} -S testrb-2 ${testrb_params} "$@" || die "testrb-2 failed"
744 }