是否可以从Thor脚本中调用Git或其他命令行工具? [英] Is it possible to call Git or other command line tools from inside a Thor script?
问题描述
我发现在我为Rails 3应用程序进行git commit之前和之后,我经常运行一系列例行的清理任务。
我是考虑将这些东西放到Thor脚本中,但是我还没有弄清楚的是如何使用Thor(或Rake)来调用系统上的其他工具。
是否可以从Thor或Rake脚本中调用像 git log
这样的命令,如果是这样的话,它看起来像什么?
谢谢!
result =%x(git log)
puts结果
或
系统('git log')
如果您只是想将输出传递给终端。
还有 grit gem将Git工具抽象为Ruby库:
需要'gri t'
repo = Grit :: Repo.new(/ path / to / repo)
repo.commits.each do | commit |
puts#{commit.id}:#{commit.message}
end
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.
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.
Is it possible to call a command like git log
from a Thor or Rake script, and if so what does that look like?
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:
require 'grit'
repo = Grit::Repo.new("/path/to/repo")
repo.commits.each do |commit|
puts "#{commit.id}: #{commit.message}"
end
这篇关于是否可以从Thor脚本中调用Git或其他命令行工具?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!