Skip to content

Instantly share code, notes, and snippets.

@johnbintz
Created November 23, 2010 20:27
Show Gist options
  • Save johnbintz/712463 to your computer and use it in GitHub Desktop.
Save johnbintz/712463 to your computer and use it in GitHub Desktop.
Rebuild PHP files in a directory, showing warnings when they fail
require 'open3'
require 'rainbow'
require 'fileutils'
static_dir = 'static'
def growl(title, message)
system %{growlnotify -m "#{message}" "#{title}"}
end
puts "Watching for changes...".foreground(:yellow)
growl("Watching...", "Watching for changes...")
def rebuild_file(match)
target = File.join(static_dir, match[1]) + ".html"
print "Rebuilding #{target.bright}:".foreground(:yellow)
STDOUT.flush
output = nil
errors = nil
Open3.popen3(%{php #{match[0]}}) do |stdin, stdout, stderr|
output = stdout.read
errors = stderr.read
end
if !errors.empty?
growl("FAILURE", errors)
print " [FAILURE]".foreground(:red)
puts
puts "PHP errors below:".foreground(:red).bright
puts errors
else
growl("SUCCESS", target)
print " [success]".foreground(:green)
FileUtils.mkdir_p File.split(target).first
File.open(target, 'w') { |fh| fh.print output }
end
puts
STDOUT.flush
end
php_match = '(.*)\.php$'
watch('php.watchr') { p exec %{watchr php.watchr} }
watch(php_match) { |match| rebuild_file(match) }
watch('(.*)\.inc') do |match|
Dir["**/*.php"].each { |file| FileUtils.touch(php_match.match(file)) }
end
Signal.trap('QUIT') { FileUtils.touch('php.watchr') } # Ctrl-\
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment