Dne 15.9.2016 v 10:06 Vít Ondruch napsal(a):
> BTW this is first draft of the macros. I am going to try second
> implementation, which will probably need to use a bit different syntax
> for macro calls, but it might replace the 3 lines usage example above
> with 2 lines version:
>
>
> ```
>
> %gemspec_remove_runtime_dependency -n fog-dynect
> %gemspec_add_runtime_dependency -n fog-dynect ['~> 10.0', '>= 10.1']
>
> ```
>
>

As I promised, this is implementation with the '-n' parameter:

```
%define gemspec_add_runtime_dependency(n:) \
read -d '' add_runtime_dependency_script << 'EOR' || : \
  name = '%{-n*}' \
  version = %{*} \
  spec = Gem::Specification.load('.%{gem_spec}') \
  dep = spec.dependencies.detect { |d| d.type == :runtime && d.name ==
name } \
  if dep \
    dep.requirement.concat version \
  else \
    spec.add_runtime_dependency name, version \
  end \
  File.write '.%{gem_spec}', spec.to_ruby \
EOR\
echo "$add_runtime_dependency_script" | ruby \
%{nil}

```

It is a bit more flexible, since what is following behind the name of
the dependency is pasted directly into Ruby code, so either ```['~>
10.0', '>= 10.1']``` or '~> 10.0' are viable options. IOW, it must be
valid ruby code, otherwise the script bails out with error. E.g. for

```
%gemspec_add_runtime_dependency -n fog-dynect ~> 10.0
```

the following error is reported:

```
+ read -d '' add_runtime_dependency_script
+ :
+ echo 'name = '\''fog-dynect'\''
  version = ~> 10.0
  spec =
Gem::Specification.load('\''./usr/share/gems/specifications/fog-1.38.0.gemspec'\'')

  dep = spec.dependencies.detect { |d| d.type == :runtime && d.name ==
name }
  if dep
    dep.requirement.concat version
  else
    spec.add_runtime_dependency name, version
  end
  File.write '\''./usr/share/gems/specifications/fog-1.38.0.gemspec'\'',
spec.to_ruby'
+ ruby
-:2: syntax error, unexpected '>'
  version = ~> 10.0
              ^
```


Vít
_______________________________________________
ruby-sig mailing list
ruby-sig@lists.fedoraproject.org
https://lists.fedoraproject.org/admin/lists/ruby-sig@lists.fedoraproject.org

Reply via email to