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