Skip to content

Instantly share code, notes, and snippets.

@leejones
Last active December 20, 2015 06:09
Show Gist options
  • Save leejones/6083888 to your computer and use it in GitHub Desktop.
Save leejones/6083888 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
USAGE = <<-EOS
Run shell commands from vim in another terminal window.
Useful for triggering long-running tasks (tests, vagrant, test-kitchen, etc) without
blocking your vim session.
## Usage
### In a shell, start the command runner (from your project directory):
vrunner [name of project (defaults to current directory name)]
### In vim, send a command (ex. `bundle exec kitchen test`):
:silent ! echo "bundle exec kitchen test" > /tmp/vrunner-myproject<cr>
The `:silent` command sends the execution to the background so that vim is not blocked while
the the command is running.
EOS
require 'tempfile'
pipe_name = ARGV[0] || File.basename(Dir.pwd)
pipe_path = ['/tmp/vrunner', pipe_name].join('-')
@last_command = Tempfile.new('vrunner_last_run')
File.open(@last_command, 'w') { |f| f.write('') }
def last_command_path
File.read(@last_command).strip
end
def run_spec(command)
unless command.nil?
File.open(@last_command, 'w') { |f| f.write(command) }
system %Q{clear && echo "Running #{command} ..." && #{command}}
else
if File.exists?(last_command_path)
puts "Rerunning last task."
run_spec(last_command_path)
else
end
end
end
begin
system %Q{mkfifo #{pipe_path} && clear && echo "Vrunner is listening at: #{pipe_path}"}
loop do
@reader = File.new(pipe_path, 'r+')
input = @reader.gets
run_spec(input.strip)
end
rescue SystemExit, Interrupt
puts 'Exiting...'
ensure
@reader.close
File.delete(@reader.path)
@last_command.close
@last_command.delete
end
#!/usr/bin/env ruby
USAGE = <<-EOS
Run rspec on the current (or most recent) spec file.
## Usage
### Start the test runner:
vspec [name of project (defaults to current directory name)]
### Run a the current spec from vim:
:silent ! echo %:p > /tmp/vspec<cr>
Sends the current file path to vspec. If the current file is not a spec file,
vspec will run the previous spec file again.
The `:silent` command sends the execution to the background so that vim is not blocked while
the test is running.
## Thanks for the info and inspiration:
* http://www.pauldix.net/2009/07/using-named-pipes-in-ruby-for-interprocess-communication.html
* https://github.com/thoughtbot/vim-rspec
## TODO
- [] add ability to run all specs
EOS
require 'tempfile'
pipe_name = ARGV[0] || File.basename(Dir.pwd)
pipe_path = ['/tmp/vspec', pipe_name].join('-')
@rspec_args = ARGV[1] || ''
@last_spec = Tempfile.new('vspec_last_run')
File.open(@last_spec, 'w') { |f| f.write('') }
def last_spec_path
File.read(@last_spec).strip
end
def run_spec(spec_file)
if spec_file =~ /.*_spec\.rb/
File.open(@last_spec, 'w') { |f| f.write(spec_file) }
command = [
%Q{clear && echo "Running #{spec_file}..." &&},
'bundle exec rspec',
'--format documentation',
@rspec_args,
spec_file
].compact.join(' ')
system command
else
puts "#{spec_file} does not appear to be a spec file."
if File.exists?(last_spec_path)
puts "Running last spec instead."
run_spec(last_spec_path)
else
end
end
end
begin
system %Q{mkfifo #{pipe_path} && clear && echo "Vspec is listening at: #{pipe_path}"}
loop do
@reader = File.new(pipe_path, 'r+')
input = @reader.gets
run_spec(input.strip)
end
rescue SystemExit, Interrupt
puts 'Exiting...'
ensure
@reader.close
File.delete(@reader.path)
@last_spec.close
@last_spec.delete
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment