如何使用红宝石宝石中的钩子 [英] How to use hooks in ruby gems

查看:101
本文介绍了如何使用红宝石宝石中的钩子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个gem,需要在安装gem后执行一些rake任务。安装后使用 http所描述的方法执行代码存在一种恶意的方式://blog.costan.us/2008/11/post-install-post-update-scripts-for.html



然而,宝石类现在有钩子,其中一个是post_install钩子。我试图添加一行,如

  Gem.post_install {puts'post hook example'} 

放到Rakefile中,它似乎不会在安装过程中执行。

解决方案

lib下创建一个文件/rubygems_plugin.rb



在此文件中,您可以定义自定义挂钩。例如:

$ p $ Gem.post_install do
putspost_install called for gem
end

不需要任何东西。

输出示例:

 成功构建RubyGem 
名称:post_install_test
版本:0.1.0
文件:post_install_test-0.1.0.gem
post_install要求gem
已成功安装post_install_test-0.1.0
1已安装gem

我只在源文件中找到相关文档。如果这不起作用,或者您对post install hook的更改似乎并不总是更新,请在重建之前彻底卸载gem。安装它。


I have a gem which needs some rake task to be executed after installation of the gem. There is a hackish way to execute a code after install using method described at http://blog.costan.us/2008/11/post-install-post-update-scripts-for.html.

However Gem class now has hooks, and one of them is a post_install hook. I am trying to add a line like

Gem.post_install { puts 'post hook example' } 

into Rakefile and it does not seem to be executed during install. Where should this line to be placed for the hook to be registered?

解决方案

Create a file at lib/rubygems_plugin.rb

In this file you can define your custom hooks. For example:

Gem.post_install do
  puts "post_install called for gem"
end

No need to require anything.
Example output:

  Successfully built RubyGem
  Name: post_install_test
  Version: 0.1.0
  File: post_install_test-0.1.0.gem
post_install called for gem
Successfully installed post_install_test-0.1.0
1 gem installed

I only found the documentation for this in the source.

If this doesn't work, or your changes to your post install hook don't always seem to update, uninstall the gem completely before rebuilding and installing it.

这篇关于如何使用红宝石宝石中的钩子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆