2

每次在 Visual Studio 中保存文件时,如何自动运行 rake 脚本?我可以创建一个包装命令的批处理文件。我想触发它CTRL + S​​。但是,Visual Studio 2012 没有宏。

JP Boodhoo在他的许多屏幕截图中都这样做了,但没有分享实现。


仅供参考,我的rakefile长相是这样的

require 'albacore'

desc 'Build Solution'
msbuild :Build do |msb| 
  msb.properties :configuration => :Release 
  msb.targets :Clean, :Build 
  msb.solution = 'ProjoBot.sln' 
end 

desc 'Run Unit Tests' 
mspec :Test do |mspec| 
  mspec.command = 'Lib/Tools/MSpec/mspec-clr4.exe' 
  mspec.assemblies 'Src/Tests/ProjoBot.UnitSpecifications/bin/Release/ProjoBot.UnitSpecifications.dll'
end 

task :default => [:Build, :Test]
4

2 回答 2

1

可能有一些与命令行集成的选项不了解 Visual Studio。

红宝石/守卫方式

昨晚我在玩Guard gem。您基本上安装了 Guard 和Guard rake 插件

gem install guard
gem install guard-rake

您可以创建一个 Guard “模板”,其中Guardfile包含一个普通的 Rake 任务

guard init rake

例如,您可以对其进行编辑以查看.cs目录中的文件source。(和

guard 'rake', :task => 'default', :run_on_start => false do
  watch(%r{^source/.+\.cs$})
end

然后启动Guard

guard

您可能需要使用-i关闭此“交互”模式,这可能会在 Windows 上产生错误!

guard -i

Guard 像一个小型本地服务器一样运行,显示日志

12:07:08 - INFO - Guard uses TerminalTitle to send notifications.
12:07:08 - INFO - Starting guard-rake default
12:07:08 - INFO - Guard is now watching at 'D:/temp'
[1] guard(main)>

如果您强制更改文件(我将转到touch我在测试目录中设置的假文件),您将获得 rake 任务的输出!

12:07:08 - INFO - Guard uses TerminalTitle to send notifications.
12:07:08 - INFO - Starting guard-rake default
12:07:08 - INFO - Guard is now watching at 'D:/temp'
12:07:54 - INFO - running default
building!...
[1] guard(main)>

PowerShell 方式

没有什么花哨的东西可以包含文件系统轮询触发的操作,但这并不意味着您不能构建自己的!我写了一个.\guard.ps1位于我的解决方案根目录中的文件。它有一个FileSystemWatcher并等待循环中的变化。

$here = Split-Path $MyInvocation.MyCommand.Definition

$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = "$here\source"
$watcher.IncludeSubdirectories = $true

while($true) {
  # timeout here lets the console process kill commands and such
  $result = $watcher.WaitForChanged('All', 3000)

  if($result.TimedOut) {
    continue
  }

  # this is where you'd put your rake command
  Write-Host "$($result.ChangeType):: $($result.Name)"

  # and a delay here is good for the CPU :)
  Start-Sleep -Seconds 3
}

当您touch或创建 ( New-Item <name> -Type File) 文件时,您可以看到它正在工作和打印。但是,我们也可以非常简单地执行 rake

rake

PowerShell 将继续并将其作为本机命令执行。你可以花点心思让这个脚本看起来和感觉更像 Guard(好吧,不是更多,而是一点点!)

param(
  [string] $path = (Split-Path $MyInvocation.MyCommand.Definition),
  [string] $task = 'default'
)

$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = $path
$watcher.IncludeSubdirectories = $true

while($true) {
  $result = $watcher.WaitForChanged('All', 3000)

  if($result.TimedOut) {
    continue
  }

  rake $task

  Start-Sleep -Seconds 3
}

你会像这样执行它

.\guard.ps1 -Path "$pwd\source" -Task 'build'
于 2013-02-12T17:23:02.310 回答
1

我使用外部工具来运行一个执行默认 Rake 任务的批处理文件。

@ECHO OFF
rake

我为工具分配了快捷键CTRL + S,这样,当我保存时,就会触发 rake 任务!我希望这对希望做同样事情的人有所帮助。

于 2013-01-24T14:13:57.907 回答