Skip to content

Instantly share code, notes, and snippets.

@Deradon
Last active December 16, 2016 14:36
Show Gist options
  • Save Deradon/4709541 to your computer and use it in GitHub Desktop.
Save Deradon/4709541 to your computer and use it in GitHub Desktop.
Re-run a command whenever a file in directory is changed!
#!/usr/bin/env ruby
# Re-run a command whenever a file in directory is changed!
#
# Inspired by: http://ngauthier.com/2012/02/quick-tests-with-bash.html
# Thanks Nick!
#
# Patrick Helm (me@patrick-helm.de), 2013
#
#
# Requirements: sudo apt-get install inotify-tools
#
# Usage: "live command"
#
# Example:
# > live rspec spec
# > live rake test
#
# .live
# * Create a file named '.live' and specify which files/directories to watch
# * <filename> to watch
# * @<filename> to ignore
#
# Example .live:
# app/
# lib/
# spec/
# Just join args
args = ARGV.join(" ")
# Prepare the live command
puts "Reading from .live" if File.exists?(".live")
cmd = File.exists?(".live") ? "--fromfile .live" : "*"
cmd = "#{args}; while true; do inotifywait --exclude '.*\.swp' -r -e modify #{cmd}; #{args}; done"
puts "$: #{cmd}"
# Run the 'live' command
exec(cmd)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment