Skip to content

Instantly share code, notes, and snippets.

@fnando
Last active December 11, 2015 01:48
Show Gist options
  • Save fnando/4525701 to your computer and use it in GitHub Desktop.
Save fnando/4525701 to your computer and use it in GitHub Desktop.

He-he-hey!

I'm experimenting with running specs and other Ruby files inside the Vagrant box from the Sublime Text 2 editor, without the SSH connection delay (which was taking ~5s).

So I managed to run these files by using the Virtualbox API. The requirement for running specs for Rails apps is creating a project file. This will the the $project_path variable, which is the root directory.

Another thing I did is mapping my projects directory to the same path inside Vagrant. This can be done by using something like the following on your Vagrantfile:

Vagrant::Config.run do |config|
  config.vm.box = "hellobits"
  config.vm.network :hostonly, "192.168.33.2"
  config.vm.share_folder "v-root", "/Users/fnando/Projects", ".", :nfs => true
end

Also remember to put vagrant-run on your $PATH and make it executable.

The vagrant-run file requires you to set the name of your VirtualBox machine. Your best bet is getting the VM id with something like cat /path/to/.vagrant.

That's it! If you prefer you can assign these custom builders only for your project. See the Sublime Text 2 documentation for instructions.

{
"folders":
[
{
"path": "/Users/fnando/Projects/codeplane-api"
}
]
}
{
"cmd": ["vagrant-run", "rspec", "$project_path", "$file"],
"selector": "source.ruby.rspec"
}
{
"cmd": ["vagrant-run", "ruby", "$file_path", "$file"],
"selector": "source.ruby"
}
#!/usr/bin/env ruby
require "json"
VAGRANTFILE = File.expand_path("~/Projects/.vagrant")
USE_BUNDLER = false
VM_ID = JSON.load(File.read(VAGRANTFILE))["active"]["default"]
def find_working_dir(file)
working_dir = File.dirname(file)
while true
exists = ["spec", "test", "lib", "Gemfile", "node_modules"].find do |dir|
File.exist?("#{working_dir}/#{dir}") && working_dir !~ %r[/(spec|test)/]
end
return working_dir if exists
return if working_dir == "/"
working_dir = File.dirname(working_dir)
end
end
if ARGV.size == 2
command, file = *ARGV
else
fail "Invalid builder parameters"
end
working_dir = find_working_dir(file)
if USE_BUNDLER && working_dir && File.exist?("#{working_dir}/Gemfile")
bundle_exec = "bundle exec"
end
full_command = ""
full_command << "cd '#{working_dir}' && " if working_dir
full_command << "#{bundle_exec} #{command} '#{file}'"
system <<-SH
VBoxManage guestcontrol #{VM_ID} \
execute --image /bin/sh \
--username vagrant --password 'vagrant' \
--environment 'HOME=/home/vagrant PATH=./bin:/home/vagrant/local/bin:/home/vagrant/local/ruby/gems/bin:/usr/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin GEM_PATH=/home/vagrant/local/ruby/gems GEM_HOME=/home/vagrant/local/ruby/gems' \
--wait-stdout --wait-stderr --wait-exit \
-- -c "#{full_command}"
SH
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment