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