1 # Copyright 1999-2015 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
5 # @ECLASS: ruby-fakegem.eclass
7 # Ruby herd <ruby@gentoo.org>
9 # Author: Diego E. Pettenò <flameeyes@gentoo.org>
10 # Author: Alex Legler <a3li@gentoo.org>
11 # @BLURB: An eclass for installing Ruby packages to behave like RubyGems.
13 # This eclass allows to install arbitrary Ruby libraries (including Gems),
14 # providing integration into the RubyGems system even for "regular" packages.
18 # @ECLASS-VARIABLE: RUBY_FAKEGEM_NAME
20 # Sets the Gem name for the generated fake gemspec.
21 # This variable MUST be set before inheriting the eclass.
22 # RUBY_FAKEGEM_NAME="${PN}"
24 # @ECLASS-VARIABLE: RUBY_FAKEGEM_VERSION
26 # Sets the Gem version for the generated fake gemspec.
27 # This variable MUST be set before inheriting the eclass.
28 # RUBY_FAKEGEM_VERSION="${PV}"
30 # @ECLASS-VARIABLE: RUBY_FAKEGEM_TASK_DOC
32 # Specify the rake(1) task to run to generate documentation.
33 # RUBY_FAKEGEM_TASK_DOC="rdoc"
35 # @ECLASS-VARIABLE: RUBY_FAKEGEM_RECIPE_TEST
37 # Specify one of the default testing function for ruby-fakegem:
38 # - rake (default; see also RUBY_FAKEGEM_TASK_TEST)
39 # - rspec (calls ruby-ng_rspec, adds dev-ruby/rspec:2 to the dependencies)
40 # - rspec3 (calls ruby-ng_rspec, adds dev-ruby/rspec:3 to the dependencies)
41 # - cucumber (calls ruby-ng_cucumber, adds dev-util/cucumber to the
42 # dependencies; does not work on JRuby).
44 # RUBY_FAKEGEM_RECIPE_TEST="rake"
46 # @ECLASS-VARIABLE: RUBY_FAKEGEM_TASK_TEST
48 # Specify the rake(1) task used for executing tests. Only valid
49 # if RUBY_FAKEGEM_RECIPE_TEST is set to "rake" (the default).
50 # RUBY_FAKEGEM_TASK_TEST="test"
52 # @ECLASS-VARIABLE: RUBY_FAKEGEM_RECIPE_DOC
54 # Specify one of the default API doc building function for ruby-fakegem:
55 # - rake (default; see also RUBY_FAKEGEM_TASK_DOC)
56 # - rdoc (calls `rdoc-2`, adds dev-ruby/rdoc to the dependencies);
57 # - yard (calls `yard`, adds dev-ruby/yard to the dependencies);
59 # RUBY_FAKEGEM_RECIPE_DOC="rake"
61 # @ECLASS-VARIABLE: RUBY_FAKEGEM_DOCDIR
63 # Specify the directory under which the documentation is built;
64 # if empty no documentation will be installed automatically.
65 # Note: if RUBY_FAKEGEM_RECIPE_DOC is set to `rdoc`, this variable is
67 # RUBY_FAKEGEM_DOCDIR=""
69 # @ECLASS-VARIABLE: RUBY_FAKEGEM_EXTRADOC
71 # Extra documentation to install (readme, changelogs, …).
72 # RUBY_FAKEGEM_EXTRADOC=""
74 # @ECLASS-VARIABLE: RUBY_FAKEGEM_DOC_SOURCES
76 # Allow settings defined sources to scan for documentation.
77 # This only applies if RUBY_FAKEGEM_DOC_TASK is set to `rdoc`.
78 # RUBY_FAKEGEM_DOC_SOURCES="lib"
80 # @ECLASS-VARIABLE: RUBY_FAKEGEM_BINWRAP
82 # Binaries to wrap around (relative to the bin/ directory)
83 # RUBY_FAKEGEM_BINWRAP="*"
85 # @ECLASS-VARIABLE: RUBY_FAKEGEM_BINDIR
87 # Path that contains binaries to be binwrapped. Equivalent to the
88 # gemspec bindir option.
89 # RUBY_FAKEGEM_BINDIR="bin"
91 # @ECLASS-VARIABLE: RUBY_FAKEGEM_REQUIRE_PATHS
93 # Extra require paths (beside lib) to add to the specification
94 # RUBY_FAKEGEM_REQUIRE_PATHS=""
96 # @ECLASS-VARIABLE: RUBY_FAKEGEM_GEMSPEC
98 # Filename of .gemspec file to install instead of generating a generic one.
99 # RUBY_FAKEGEM_GEMSPEC=""
101 # @ECLASS-VARIABLE: RUBY_FAKEGEM_EXTRAINSTALL
103 # List of files and directories relative to the top directory that also
104 # get installed. Some gems provide extra files such as version information,
105 # Rails generators, or data that needs to be installed as well.
106 # RUBY_FAKEGEM_EXTRAINSTALL=""
108 RUBY_FAKEGEM_NAME="${RUBY_FAKEGEM_NAME:-${PN}}"
109 RUBY_FAKEGEM_VERSION="${RUBY_FAKEGEM_VERSION:-${PV/_pre/.pre}}"
110 RUBY_FAKEGEM_SUFFIX="${RUBY_FAKEGEM_SUFFIX:-}"
112 RUBY_FAKEGEM_RECIPE_DOC="${RUBY_FAKEGEM_RECIPE_DOC-rake}"
113 RUBY_FAKEGEM_TASK_DOC="${RUBY_FAKEGEM_TASK_DOC-rdoc}"
114 RUBY_FAKEGEM_DOC_SOURCES="${RUBY_FAKEGEM_DOC_SOURCES-lib}"
116 RUBY_FAKEGEM_RECIPE_TEST="${RUBY_FAKEGEM_RECIPE_TEST-rake}"
117 RUBY_FAKEGEM_TASK_TEST="${RUBY_FAKEGEM_TASK_TEST-test}"
119 RUBY_FAKEGEM_BINWRAP="${RUBY_FAKEGEM_BINWRAP-*}"
120 RUBY_FAKEGEM_BINDIR="${RUBY_FAKEGEM_BINDIR-bin}"
122 [[ ${RUBY_FAKEGEM_TASK_DOC} == "" ]] && RUBY_FAKEGEM_RECIPE_DOC="none"
124 case ${RUBY_FAKEGEM_RECIPE_DOC} in
127 ruby_add_bdepend "doc? ( dev-ruby/rake )"
128 RUBY_FAKEGEM_DOCDIR="doc"
132 ruby_add_bdepend "doc? ( dev-ruby/rdoc )"
133 RUBY_FAKEGEM_DOCDIR="doc"
137 ruby_add_bdepend "doc? ( dev-ruby/yard )"
138 RUBY_FAKEGEM_DOCDIR="doc"
141 [[ -n ${RUBY_FAKEGEM_DOCDIR} ]] && IUSE+=" doc"
145 [[ ${RUBY_FAKEGEM_TASK_TEST} == "" ]] && RUBY_FAKEGEM_RECIPE_TEST="none"
147 case ${RUBY_FAKEGEM_RECIPE_TEST} in
150 ruby_add_bdepend "test? ( dev-ruby/rake )"
154 # Also require a new enough rspec-core version that installs the
156 ruby_add_bdepend "test? ( dev-ruby/rspec:2 >=dev-ruby/rspec-core-2.14.8-r2 )"
160 ruby_add_bdepend "test? ( dev-ruby/rspec:3 )"
164 # Unfortunately as of August 2012, cucumber is not supported on
165 # JRuby. We work it around here to avoid repeating the same
166 # code over and over again.
167 USE_RUBY="${USE_RUBY/jruby/}" ruby_add_bdepend "test? ( dev-util/cucumber )"
170 RUBY_FAKEGEM_RECIPE_TEST="none"
174 SRC_URI="mirror://rubygems/${RUBY_FAKEGEM_NAME}-${RUBY_FAKEGEM_VERSION}${RUBY_FAKEGEM_SUFFIX:+-${RUBY_FAKEGEM_SUFFIX}}.gem"
176 ruby_add_bdepend virtual/rubygems
177 ruby_add_rdepend virtual/rubygems
179 # @FUNCTION: ruby_fakegem_gemsdir
180 # @RETURN: Returns the gem data directory
182 # This function returns the gems data directory for the ruby
183 # implementation in question.
184 ruby_fakegem_gemsdir() {
185 has "${EAPI}" 2 && ! use prefix && EPREFIX=
187 local _gemsitedir=$(ruby_rbconfig_value 'sitelibdir')
188 _gemsitedir=${_gemsitedir//site_ruby/gems}
189 _gemsitedir=${_gemsitedir#${EPREFIX}}
191 [[ -z ${_gemsitedir} ]] && {
192 eerror "Unable to find the gems dir"
193 die "Unable to find the gems dir"
196 echo "${_gemsitedir}"
199 # @FUNCTION: ruby_fakegem_doins
200 # @USAGE: file [file...]
202 # Installs the specified file(s) into the gems directory.
203 ruby_fakegem_doins() {
205 insinto $(ruby_fakegem_gemsdir)/gems/${RUBY_FAKEGEM_NAME}-${RUBY_FAKEGEM_VERSION}
207 ) || die "failed $0 $@"
210 # @FUNCTION: ruby_fakegem_newsins()
211 # @USAGE: file filename
213 # Installs the specified file into the gems directory using the provided filename.
214 ruby_fakegem_newins() {
216 # Since newins does not accept full paths but just basenames
217 # for the target file, we want to extend it here.
218 local newdirname=/$(dirname "$2")
219 [[ ${newdirname} == "/." ]] && newdirname=
221 local newbasename=$(basename "$2")
223 insinto $(ruby_fakegem_gemsdir)/gems/${RUBY_FAKEGEM_NAME}-${RUBY_FAKEGEM_VERSION}${newdirname}
224 newins "$1" ${newbasename}
225 ) || die "failed $0 $@"
228 # @FUNCTION: ruby_fakegem_install_gemspec
230 # Install a .gemspec file for this package. Either use the file indicated
231 # by the RUBY_FAKEGEM_GEMSPEC variable, or generate one using
232 # ruby_fakegem_genspec.
233 ruby_fakegem_install_gemspec() {
234 local gemspec="${T}"/${RUBY_FAKEGEM_NAME}-${_ruby_implementation}
237 if [[ ${RUBY_FAKEGEM_GEMSPEC} != "" ]]; then
238 ruby_fakegem_gemspec_gemspec ${RUBY_FAKEGEM_GEMSPEC} ${gemspec}
240 local metadata="${WORKDIR}"/${_ruby_implementation}/metadata
242 if [[ -e ${metadata} ]]; then
243 ruby_fakegem_metadata_gemspec ${metadata} ${gemspec}
245 ruby_fakegem_genspec ${gemspec}
248 ) || die "Unable to generate gemspec file."
250 insinto $(ruby_fakegem_gemsdir)/specifications
251 newins ${gemspec} ${RUBY_FAKEGEM_NAME}-${RUBY_FAKEGEM_VERSION}.gemspec || die "Unable to install gemspec file."
254 # @FUNCTION: ruby_fakegem_gemspec_gemspec
255 # @USAGE: gemspec-input gemspec-output
257 # Generates an installable version of the specification indicated by
258 # RUBY_FAKEGEM_GEMSPEC. This file is eval'ed to produce a final specification
259 # in a way similar to packaging the gemspec file.
260 ruby_fakegem_gemspec_gemspec() {
261 ${RUBY} -e "puts eval(File::open('$1').read).to_ruby" > $2
264 # @FUNCTION: ruby_fakegem_metadata_gemspec
265 # @USAGE: gemspec-metadata gemspec-output
267 # Generates an installable version of the specification indicated by
268 # the metadata distributed by the gem itself. This is similar to how
269 # rubygems creates an installation from a .gem file.
270 ruby_fakegem_metadata_gemspec() {
273 ${RUBY} -r yaml -e "puts Gem::Specification.from_yaml(File::open('$1').read).to_ruby" > $2
276 ${RUBY} -r yaml -e "puts Gem::Specification.from_yaml(File::open('$1', :encoding => 'UTF-8').read).to_ruby" > $2
281 # @FUNCTION: ruby_fakegem_genspec
282 # @USAGE: output-gemspec
284 # Generates a gemspec for the package and places it into the "specifications"
285 # directory of RubyGems.
286 # If the metadata normally distributed with a gem is present then that is
287 # used to generate the gemspec file.
289 # As a fallback we can generate our own version.
290 # In the gemspec, the following values are set: name, version, summary,
291 # homepage, and require_paths=["lib"].
292 # See RUBY_FAKEGEM_NAME and RUBY_FAKEGEM_VERSION for setting name and version.
293 # See RUBY_FAKEGEM_REQUIRE_PATHS for setting extra require paths.
294 ruby_fakegem_genspec() {
295 local required_paths="'lib'"
296 for path in ${RUBY_FAKEGEM_REQUIRE_PATHS}; do
297 required_paths="${required_paths}, '${path}'"
300 # We use the _ruby_implementation variable to avoid having stray
301 # copies with different implementations; while for now we're using
302 # the same exact content, we might have differences in the future,
303 # so better taking this into consideration.
304 local quoted_description=${DESCRIPTION//\"/\\\"}
306 # generated by ruby-fakegem.eclass $Revision: 1.45 $
307 Gem::Specification.new do |s|
308 s.name = "${RUBY_FAKEGEM_NAME}"
309 s.version = "${RUBY_FAKEGEM_VERSION}"
310 s.summary = "${quoted_description}"
311 s.homepage = "${HOMEPAGE}"
312 s.require_paths = [${required_paths}]
317 # @FUNCTION: ruby_fakegem_binwrapper
318 # @USAGE: command [path] [content]
320 # Creates a new binary wrapper for a command installed by the RubyGem.
321 # path defaults to /usr/bin/$command content is optional and can be used
322 # to inject additional ruby code into the wrapper. This may be useful to
323 # e.g. force a specific version using the gem command.
324 ruby_fakegem_binwrapper() {
327 local newbinary=${2:-/usr/bin/$gembinary}
329 local relativegembinary=${RUBY_FAKEGEM_NAME}-${RUBY_FAKEGEM_VERSION}/${RUBY_FAKEGEM_BINDIR}/${gembinary}
330 local binpath=$(dirname $newbinary)
331 [[ ${binpath} = . ]] && binpath=/usr/bin
333 # Try to find out whether the package is going to install for
334 # one or multiple implementations; if we're installing for a
335 # *single* implementation, no need to use “/usr/bin/env ruby”
336 # in the shebang, and we can actually avoid errors when
337 # calling the script by default (see for instance the
338 # JRuby-specific commands).
340 for implementation in ${USE_RUBY}; do
341 # ignore non-enabled implementations
342 use ruby_targets_${implementation} || continue
343 if [ -z $rubycmd ]; then
344 # if no other implementation was set before, set it.
345 rubycmd="$(ruby_implementation_command ${implementation})"
347 # if another implementation already arrived, then make
348 # it generic and break out of the loop. This ensures
349 # that we do at most two iterations.
350 rubycmd="/usr/bin/env ruby"
355 cat - > "${T}"/gembin-wrapper-${gembinary} <<EOF
357 # This is a simplified version of the RubyGems wrapper
359 # Generated by ruby-fakegem.eclass $Revision: 1.45 $
364 load Gem::default_path[-1] + "/gems/${relativegembinary}"
368 exeinto ${binpath:-/usr/bin}
369 newexe "${T}"/gembin-wrapper-${gembinary} $(basename $newbinary)
370 ) || die "Unable to create fakegem wrapper"
373 # @FUNCTION: all_fakegem_compile
375 # Build documentation for the package if indicated by the doc USE flag
376 # and if there is a documetation task defined.
377 all_fakegem_compile() {
378 if [[ -n ${RUBY_FAKEGEM_DOCDIR} ]] && use doc; then
379 case ${RUBY_FAKEGEM_RECIPE_DOC} in
381 rake ${RUBY_FAKEGEM_TASK_DOC} || die "failed to (re)build documentation"
384 rdoc ${RUBY_FAKEGEM_DOC_SOURCES} || die "failed to (re)build documentation"
387 yard doc ${RUBY_FAKEGEM_DOC_SOURCES} || die "failed to (re)build documentation"
393 # @FUNCTION: all_ruby_unpack
395 # Unpack the source archive, including support for unpacking gems.
397 # Special support for extracting .gem files; the file need to be
398 # extracted twice and the mtime from the archive _has_ to be
399 # ignored (it's always set to epoch 0).
400 for archive in ${A}; do
403 # Make sure that we're not running unpack for more than
404 # one .gem file, since we won't support that at all.
405 [[ -d "${S}" ]] && die "Unable to unpack ${archive}, ${S} exists"
407 ebegin "Unpacking .gem file..."
408 tar -mxf "${DISTDIR}"/${archive} || die
411 ebegin "Uncompressing metadata"
412 gunzip metadata.gz || die
416 pushd "${S}" &>/dev/null || die
418 ebegin "Unpacking data.tar.gz"
419 tar -mxf "${my_WORKDIR}"/data.tar.gz || die
422 popd &>/dev/null || die
425 # We apply the patches with RUBY_PATCHES directly from DISTDIR,
426 # as the WORKDIR variable changes value between the global-scope
427 # and the time all_ruby_unpack/_prepare are called. Since we can
428 # simply decompress them when applying, this is much easier to
430 einfo "Keeping ${archive} as-is"
439 # @FUNCTION: all_ruby_compile
441 # Compile the package.
446 # @FUNCTION: each_fakegem_test
448 # Run tests for the package for each ruby target if the test task is defined.
449 each_fakegem_test() {
450 case ${RUBY_FAKEGEM_RECIPE_TEST} in
452 ${RUBY} -S rake ${RUBY_FAKEGEM_TASK_TEST} || die "tests failed"
455 RSPEC_VERSION=2 ruby-ng_rspec
458 RSPEC_VERSION=3 ruby-ng_rspec
464 ewarn "each_fakegem_test called, but \${RUBY_FAKEGEM_RECIPE_TEST} is 'none'"
469 if [[ ${RUBY_FAKEGEM_RECIPE_TEST} != none ]]; then
470 # @FUNCTION: each_ruby_test
472 # Run the tests for this package.
478 # @FUNCTION: each_fakegem_install
480 # Install the package for each ruby target.
481 each_fakegem_install() {
482 ruby_fakegem_install_gemspec
484 local _gemlibdirs="${RUBY_FAKEGEM_EXTRAINSTALL}"
485 for directory in "${RUBY_FAKEGEM_BINDIR}" lib; do
486 [[ -d ${directory} ]] && _gemlibdirs="${_gemlibdirs} ${directory}"
489 [[ -n ${_gemlibdirs} ]] && \
490 ruby_fakegem_doins -r ${_gemlibdirs}
493 # @FUNCTION: each_ruby_install
495 # Install the package for each target.
496 each_ruby_install() {
500 # @FUNCTION: all_fakegem_install
502 # Install files common to all ruby targets.
503 all_fakegem_install() {
504 if [[ -n ${RUBY_FAKEGEM_DOCDIR} ]] && use doc; then
505 for dir in ${RUBY_FAKEGEM_DOCDIR}; do
506 [[ -d ${dir} ]] || continue
508 pushd ${dir} &>/dev/null || die
509 dohtml -r * || die "failed to install documentation"
510 popd &>/dev/null || die
514 if [[ -n ${RUBY_FAKEGEM_EXTRADOC} ]]; then
515 dodoc ${RUBY_FAKEGEM_EXTRADOC} || die "failed to install further documentation"
518 # binary wrappers; we assume that all the implementations get the
519 # same binaries, or something is wrong anyway, so...
520 if [[ -n ${RUBY_FAKEGEM_BINWRAP} ]]; then
521 local bindir=$(find "${D}" -type d -path "*/gems/${RUBY_FAKEGEM_NAME}-${RUBY_FAKEGEM_VERSION}/${RUBY_FAKEGEM_BINDIR}" -print -quit)
523 if [[ -d "${bindir}" ]]; then
524 pushd "${bindir}" &>/dev/null || die
525 local binaries=$(eval ls ${RUBY_FAKEGEM_BINWRAP})
526 for binary in $binaries; do
527 ruby_fakegem_binwrapper $binary
529 popd &>/dev/null || die
534 # @FUNCTION: all_ruby_install
536 # Install files common to all ruby targets.