How to add (before / post) _install_hook to ruby ​​gems

I want to execute some code during preinstallation when installing gem from rubygems.org using a command like gem install some-gem.

The documentation states that you can use http://docs.ruby-lang.org/en/2.2.0/Gem.html hook, which looks like this:

# File lib/rubygems.rb, line 724
def self.pre_install(&hook)
  @pre_install_hooks << hook
end

The rest of the document says:

By default, RubyGems values ​​are stored in rubygems / defaults.rb. If you are wrapping RubyGems or implementing Ruby, you can change the settings of RubyGems. For RubyGems packages, specify lib / rubygems / defaults / operating_system.rb and override any default values ​​from lib / rubygems / defaults.rb. For Ruby developers, specify lib / rubygems / defaults / # {RUBY_ENGINE} .rb and override any default values ​​from lib / rubygems / defaults.rb. If you need RubyGems to do additional installation or removal work , the default override file can install pre and post install and remove the hooks. See :: pre_install, :: pre_uninstall, :: post_install, :: post_uninstall.

Sounds like what I want. So I created the files

  • Library / RubyGems / default /defaults.rb
  • /RubyGems/ /operating _system.rb
  • RubyGems/defaults.rb

Gem.pre_install { puts 'pre install hook called!' }

. require_paths gemspec, :

s.require_paths = ["lib", "test", "rubygems"]

.

?

+4
2

(2015-11-11) . . , , RubyGem . : Ruby gem ? .

:

lib/rubygems/defaults/defaults.rb
lib/rubygems/defaults/operating_system.rb
rubygems/defaults.rb

gem. RubyGems.

, , pre_install, /usr/lib64/ruby/2.2.0/rubygems/defaults.rb Ruby ​​ . operating_system.rb .

+2

.

gemspec: s.extensions << 'path/to/extconf.rb'

gem install Makefile .

, extconf.rb Makefile:

dummy_make_content = "make:\n" \
                     "\t:\n" \
                     "install:\n" \
                     "\t:\n" \
                     "clean:\n" \
                     "\t:\n"
File.write('Makefile', dummy_make_content)

mkmf :

require 'mkmf'
create_makefile ''
0

Source: https://habr.com/ru/post/1614716/


All Articles