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