tl;dr

I updated the macros and here are some highlights:

* The macro names were shorten according to František's suggestion and I
added the -d flag in case you want to work with development dependencies.
* By default, they work on ./%{gem_name}.spec, i.e. the unpacked
.gemspec or you can specify custom path via -s parameter
* The -g parameter now specifies the name of the dependency
* The remaining options should be something which can be handled by Ruby
and they should specify the version(s) (see the examples bellow).
* When removing the dependency, the -g parameter specifying the
dependency name must always be found in the .gemspec otherwise the macro
fails. This makes sure that if something is changed in the .gemspec, it
will be brought to your attention.
* For similar reasons, if you optionally specify the precise versions,
they have to be available in .gemspec, otherwise the remove macro fails.



So here is the latest version of the macros:

```
# The 'read' command not essential, but it is usefull to make the sript
# appear in build log.

# Add dependency named gem with requirements to .gemspec. It adds runtime
# dependency by default.
# -g<gem>            Specifies name of the gem dependency.
# -s<gemspec_file>   Overrides the default .gemspec location.
# -d                 Add development dependecy.
#
# The remaining arguments are expected to by requirements and should be
# valid Ruby code.
%define gemspec_add_dep(g:s:d) \
read -d '' gemspec_add_dep_script << 'EOR' || : \
  gemspec_file = '%{-s*}%{!?-s:./%{gem_name}.spec}' \
  \
  name = '%{-g*}' \
  requirements = %{*}%{!?1:nil} \
  \
  type = :%{!?-d:runtime}%{?-d:development} \
  \
  spec = Gem::Specification.load(gemspec_file) \
  abort("#{gemspec_file} is not accessible.") unless spec \
  \
  dep = spec.dependencies.detect { |d| d.type == type && d.name == name } \
  if dep \
    dep.requirement.concat requirements \
  else \
    spec.public_send "add_#{type}_dependency", name, requirements \
  end \
  File.write gemspec_file, spec.to_ruby \
EOR\
echo "$gemspec_add_dep_script" | ruby \
unset -v gemspec_add_dep_script \
%{nil}

# Remove  dependency named gem with requirements to .gemspec. It adds
runtime
# dependency by default.
# -g<gem>            Specifies name of the gem dependency.
# -s<gemspec_file>   Overrides the default .gemspec location.
# -d                 Remove development dependecy.
#
# If remaing arguments specify some version requirements, the macro fails if
# these specific requirements can't be removed.
%define gemspec_remove_dep(g:s:d) \
read -d '' gemspec_remove_dep_script << 'EOR' || : \
  gemspec_file = '%{-s*}%{!?-s:./%{gem_name}.spec}' \
  \
  name = '%{-g*}' \
  requirements = %{*}%{!?1:nil} \
  \
  type = :%{!?-d:runtime}%{?-d:development} \
  \
  spec = Gem::Specification.load(gemspec_file) \
  abort("#{gemspec_file} is not accessible.") unless spec \
  \
  dep = spec.dependencies.detect { |d| d.type == type && d.name == name } \
  if dep \
    if requirements \
      requirements = Gem::Requirement.create(requirements).requirements \
      requirements.each do |r| \
        unless dep.requirement.requirements.reject! {
|dependency_requirements| dependency_requirements == r } \
          abort("Requirement '#{r.first} #{r.last}' for dependency
'#{name}' was not found!") \
        end \
      end \
    else \
      dep = spec.dependencies.delete dep \
    end \
  else \
    abort("Dependency '#{name}' was not found!") \
  end \
  File.write '.%{gem_spec}', spec.to_ruby \
EOR\
echo "$gemspec_remove_dep_script" | ruby \
unset -v gemspec_remove_dep_script \
%{nil}

```

You can used them as follows:


```
%gemspec_add_dep -s ./usr/share/gems/specifications/fog-1.38.0.gemspec
-g fog-dynect ['~> 10.0', '>= 10.1.1']
%gemspec_remove_dep -s
./usr/share/gems/specifications/fog-1.38.0.gemspec -g fog-dynect '~> 10.0'
%gemspec_add_dep -s ./usr/share/gems/specifications/fog-1.38.0.gemspec
-g fog-dynect '~> 10.0'
%gemspec_remove_dep -s
./usr/share/gems/specifications/fog-1.38.0.gemspec -g fog-dynect
```


Vít
_______________________________________________
ruby-sig mailing list -- ruby-sig@lists.fedoraproject.org
To unsubscribe send an email to ruby-sig-le...@lists.fedoraproject.org

Reply via email to