简体   繁体   English

是否可以从Thor脚本中调用Git或其他命令行工具?

[英]Is it possible to call Git or other command line tools from inside a Thor script?

I find that I'm often running a sequence of routine 'cleanup' tasks before and after I make a git commit for my Rails 3 app. 我发现在为我的Rails 3应用程序进行git提交之前和之后,我经常运行一系列例行的“清理”任务。

I was thinking about putting these things into a Thor script, but one thing I haven't been able to figure out is how to use Thor (or Rake) to call other tools on the system. 我正在考虑将这些东西放入Thor脚本,但有一点我无法弄清楚是如何使用Thor(或Rake)来调用系统上的其他工具。

Is it possible to call a command like git log from a Thor or Rake script, and if so what does that look like? 是否可以从Thor或Rake脚本中调用git log类的命令,如果是这样,那是什么样的?

Thanks! 谢谢!

Just shell out: 掏出来:

result = %x(git log)
puts result

or 要么

system('git log')

if you just want to pass the output to the terminal. 如果你只想将输出传递给终端。

There is also the grit gem that abstracts the Git tools into a Ruby library: 还有grit gem将Git工具抽象为Ruby库:

require 'grit'
repo = Grit::Repo.new("/path/to/repo")
repo.commits.each do |commit|
  puts "#{commit.id}: #{commit.message}"
end

不要忘记那些只是Ruby文件,所以你也可以在那里使用Ruby库中的所有东西,所以像%x [rm -rf /], system (“rm -rf /”)和`rm -rf这样的东西/`也可以在这些脚本中访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM