How do I run a rake script automatically every time I save a file in Visual Studio? I can create a batch file that wraps the command. And I'd like to trigger it on CTRL + S
. But, Visual Studio 2012 doesn't have macros.
JP Boodhoo has done it in many of his screen casts, but hasn't shared the implementation.
FYI, my rakefile
looks like this
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]
There may be some options that integrate with the command line that are ignorant of Visual Studio.
The Ruby/Guard Way
I was playing with the Guard gem last night. You basically install guard and the Guard rake plugin.
You can create a Guard "template", a
Guardfile
with a plain Rake task in itYou can edit it to watch
.cs
files in thesource
directory, for example. (andThen start Guard
You may need to use
-i
to turn off this "interactive" mode, which may generate errors on Windows!Guard runs like a little local server, showing logs
And if you force a file change (I'm going to
touch
a fake file I set up in my test directory), you'll get the output of your rake task!The PowerShell Way
There's nothing fancy out there that wraps up filesystem polling triggered actions, but that doesn't mean you can't build your own! I wrote a
.\guard.ps1
file that sits in my solution root. It has aFileSystemWatcher
and waits for changes in a loop.You can see it working and printing when you
touch
or create (New-Item <name> -Type File
) files. But, we can also execute rake very simplyPowerShell will just go ahead and execute that as a native command. You could get fancy and make this script look and feel more like Guard (ok, not a lot more, but a little!)
And you'd execute it like this
I used the External Tools to run a batch file that executes the default Rake task.
I assigned the shortcut key
CTRL + S
to the tool, so that, when I save, the rake task is triggered! I hope this helps someone looking to do the same thing.