使用Ruby在Git Hooks中执行Powershell命令

时间:2012-04-19 02:39:39

标签: ruby git powershell nunit

我在Windows上使用Git并尝试在工作流程中包含单元测试。如果提交消息包含关键字。 commit-msg 挂钩将触发Powershell命令以运行一些Nunit测试。

这是钩子中的ruby代码

#!/usr/bin/env ruby
message_file = ARGV[0]
message = File.read(message_file)

puts "The commit message is " + message

$regex = /(#runtest)/

if $regex.match(message)
    exec 'powershell.exe -ExecutionPolicy RemoteSigned -Command {RunNunitTestCase}'
end

但是当我提交更改时,结果如下所示。 exec行已运行但什么都不做。

PS D:\testfolder> git commit -am '#runtest'
The commit message is #runtest
[authorupdate b14878d] 123
 1 files changed, 1 insertions(+), 0 deletions(-)

我是ruby和powershell的新手。如果此工作流程可行或您有更好的方法,请随意发表评论。

谢谢。

2 个答案:

答案 0 :(得分:2)

需要对花括号进行转义。

exec "powershell.exe -ExecutionPolicy RemoteSigned -Command & \{RunNunitTestCase\}

答案 1 :(得分:1)

我尝试使用exec行的确切脚本:

exec 'powershell.exe -ExecutionPolicy RemoteSigned -Command gps'

当我执行gps

时,确实打印出了git commit -m "#runtest"的输出

所以它确实运行命令并且确实有效。

确保您执行的任何操作都有效。抓住powershell线并直接在命令行上试一试。