029ae1
# The RubyGems root folder.
029ae1
%gem_dir %{_datadir}/gems
029ae1
%gem_archdir %{_libdir}/gems
029ae1
029ae1
# Common gem locations and files.
029ae1
%gem_instdir %{gem_dir}/gems/%{gem_name}-%{version}%{?prerelease}
029ae1
%gem_extdir_mri %{gem_archdir}/%{name}/%{gem_name}-%{version}%{?prerelease}
029ae1
%gem_libdir %{gem_instdir}/lib
029ae1
%gem_cache %{gem_dir}/cache/%{gem_name}-%{version}%{?prerelease}.gem
029ae1
%gem_spec %{gem_dir}/specifications/%{gem_name}-%{version}%{?prerelease}.gemspec
029ae1
%gem_docdir %{gem_dir}/doc/%{gem_name}-%{version}%{?prerelease}
029ae1
029ae1
029ae1
# %gem_install - Install gem into appropriate directory.
029ae1
#
029ae1
# Usage: %gem_install [options]
029ae1
#
029ae1
# -n <gem_file>      Overrides gem file name for installation.
029ae1
# -d <install_dir>   Set installation directory.
029ae1
#
029ae1
%gem_install(d:n:) \
029ae1
mkdir -p %{-d*}%{!?-d:.%{gem_dir}} \
029ae1
\
029ae1
CONFIGURE_ARGS="--with-cflags='%{optflags}' --with-cxxflags='%{optflags}' $CONFIGURE_ARGS" \\\
029ae1
gem install \\\
029ae1
        -V \\\
029ae1
        --local \\\
029ae1
        --build-root %{-d*}%{!?-d:.} \\\
029ae1
        --force \\\
029ae1
        --document=ri,rdoc \\\
029ae1
        %{-n*}%{!?-n:%{gem_name}-%{version}%{?prerelease}.gem} \
029ae1
%{nil}
029ae1
029ae1
029ae1
# The 'read' command in %%gemspec_* macros is not essential, but it is usefull
029ae1
# to make the sript appear in build log.
029ae1
029ae1
029ae1
# %gemspec_add_dep - Add dependency into .gemspec.
029ae1
#
029ae1
# Usage: %gemspec_add_dep -g <gem> [options] [requirements]
029ae1
#
029ae1
# Add dependency named <gem> to .gemspec file. The macro adds runtime
029ae1
# dependency by default. The [requirements] argument can be used to specify
029ae1
# the dependency constraints more precisely. It is expected to be valid Ruby
029ae1
# code.
029ae1
#
029ae1
# -s <gemspec_file>   Overrides the default .gemspec location.
029ae1
# -d                  Add development dependecy.
029ae1
#
029ae1
%gemspec_add_dep(g:s:d) \
029ae1
read -d '' gemspec_add_dep_script << 'EOR' || : \
029ae1
  gemspec_file = '%{-s*}%{!?-s:%{_builddir}/%{gem_name}-%{version}%{?prerelease}.gemspec}' \
029ae1
  \
029ae1
  name = '%{-g*}' \
029ae1
  requirements = %{*}%{!?1:nil} \
029ae1
  \
029ae1
  type = :%{!?-d:runtime}%{?-d:development} \
029ae1
  \
029ae1
  spec = Gem::Specification.load(gemspec_file) \
029ae1
  abort("#{gemspec_file} is not accessible.") unless spec \
029ae1
  \
029ae1
  dep = spec.dependencies.detect { |d| d.type == type && d.name == name } \
029ae1
  if dep \
029ae1
    dep.requirement.concat requirements \
029ae1
  else \
029ae1
    spec.public_send "add_#{type}_dependency", name, requirements \
029ae1
  end \
029ae1
  File.write gemspec_file, spec.to_ruby \
029ae1
EOR\
029ae1
echo "$gemspec_add_dep_script" | ruby \
029ae1
unset -v gemspec_add_dep_script \
029ae1
%{nil}
029ae1
029ae1
029ae1
# %gemspec_remove_dep - Remove dependency from .gemspec.
029ae1
#
029ae1
# Usage: %gemspec_remove_dep -g <gem> [options] [requirements]
029ae1
#
029ae1
# Remove dependency named <gem> from .gemspec file. The macro removes runtime
029ae1
# dependency by default. The [requirements] argument can be used to specify
029ae1
# the dependency constraints more precisely. It is expected to be valid Ruby
029ae1
# code. The macro fails if these specific requirements can't be removed.
029ae1
#
029ae1
# -s <gemspec_file>   Overrides the default .gemspec location.
029ae1
# -d                  Remove development dependecy.
029ae1
#
029ae1
%gemspec_remove_dep(g:s:d) \
029ae1
read -d '' gemspec_remove_dep_script << 'EOR' || : \
029ae1
  gemspec_file = '%{-s*}%{!?-s:%{_builddir}/%{gem_name}-%{version}%{?prerelease}.gemspec}' \
029ae1
  \
029ae1
  name = '%{-g*}' \
029ae1
  requirements = %{*}%{!?1:nil} \
029ae1
  \
029ae1
  type = :%{!?-d:runtime}%{?-d:development} \
029ae1
  \
029ae1
  spec = Gem::Specification.load(gemspec_file) \
029ae1
  abort("#{gemspec_file} is not accessible.") unless spec \
029ae1
  \
029ae1
  dep = spec.dependencies.detect { |d| d.type == type && d.name == name } \
029ae1
  if dep \
029ae1
    if requirements \
029ae1
      requirements = Gem::Requirement.create(requirements).requirements \
029ae1
      requirements.each do |r| \
029ae1
        unless dep.requirement.requirements.reject! { |dependency_requirements| dependency_requirements == r } \
029ae1
          abort("Requirement '#{r.first} #{r.last}' was not possible to remove for dependency '#{dep}'!") \
029ae1
        end \
029ae1
      end \
029ae1
      spec.dependencies.delete dep if dep.requirement.requirements.empty? \
029ae1
    else \
029ae1
      spec.dependencies.delete dep \
029ae1
    end \
029ae1
  else \
029ae1
    abort("Dependency '#{name}' was not found!") \
029ae1
  end \
029ae1
  File.write gemspec_file, spec.to_ruby \
029ae1
EOR\
029ae1
echo "$gemspec_remove_dep_script" | ruby \
029ae1
unset -v gemspec_remove_dep_script \
029ae1
%{nil}
029ae1
029ae1
029ae1
# %%gemspec_add_file - Add files to various files lists in .gemspec.
029ae1
#
029ae1
# Usage: %%gemspec_add_file [options] <file>
029ae1
#
029ae1
# Add files to .gemspec file. <file> is expected to be valid Ruby code.
029ae1
# Path to file is expected. Does not check real files in any way.
029ae1
# By default, `files` list is edited.
029ae1
#
029ae1
# -s <gemspec_file>   Overrides the default .gemspec location.
029ae1
# -t                  Edit test_files only.
029ae1
# -r                  Edit extra_rdoc_files only.
029ae1
#
029ae1
%gemspec_add_file(s:tr) \
029ae1
read -d '' gemspec_add_file_script << 'EOR' || : \
029ae1
  gemspec_file = '%{-s*}%{!?-s:%{_builddir}/%{gem_name}-%{version}%{?prerelease}.gemspec}' \
029ae1
  \
029ae1
  abort("gemspec_add_file: Use only one '-t' or '-r' at a time.") if "%{?-t}%{?-r}" == "-t-r" \
029ae1
  \
029ae1
  filenames = %{*}%{!?1:nil} \
029ae1
  filenames = Array(filenames) \
029ae1
  \
029ae1
  spec = Gem::Specification.load(gemspec_file) \
029ae1
  abort("#{gemspec_file} is not accessible.") unless spec \
029ae1
  \
029ae1
  spec.%{?-t:test_}%{?-r:extra_rdoc_}files += filenames \
029ae1
  File.write gemspec_file, spec.to_ruby \
029ae1
EOR\
029ae1
echo "$gemspec_add_file_script" | ruby \
029ae1
unset -v gemspec_add_file_script \
029ae1
%{nil}
029ae1
029ae1
029ae1
# %%gemspec_remove_file - Remove files from various files lists in .gemspec.
029ae1
#
029ae1
# Usage: %%gemspec_remove_file [options] <file>
029ae1
#
029ae1
# Remove files from .gemspec file. <file> is expected to be valid Ruby code.
029ae1
# Path to file is expected. Does not check/remove real files in any way.
029ae1
# By default, `files` list is edited. File has to be removed from `test_files`
029ae1
# first in order to be removable from `files`.
029ae1
#
029ae1
# -s <gemspec_file>   Overrides the default .gemspec location.
029ae1
# -t                  Edit test_files only.
029ae1
# -r                  Edit extra_rdoc_files only.
029ae1
#
029ae1
%gemspec_remove_file(s:tr) \
029ae1
read -d '' gemspec_remove_file_script << 'EOR' || : \
029ae1
  gemspec_file = '%{-s*}%{!?-s:%{_builddir}/%{gem_name}-%{version}%{?prerelease}.gemspec}' \
029ae1
  \
029ae1
  abort("gemspec_remove_file: Use only one '-t' or '-r' at a time.") if "%{?-t}%{?-r}" == "-t-r" \
029ae1
  \
029ae1
  filenames = %{*}%{!?1:nil} \
029ae1
  filenames = Array(filenames) \
029ae1
  \
029ae1
  spec = Gem::Specification.load(gemspec_file) \
029ae1
  abort("#{gemspec_file} is not accessible.") unless spec \
029ae1
  \
029ae1
  spec.%{?-t:test_}%{?-r:extra_rdoc_}files -= filenames \
029ae1
  File.write gemspec_file, spec.to_ruby \
029ae1
EOR\
029ae1
echo "$gemspec_remove_file_script" | ruby \
029ae1
unset -v gemspec_remove_file_script \
029ae1
%{nil}