class Pave::Reload

Public Class Methods

dev_extension() click to toggle source
# File lib/pave/reload.rb, line 73
def self.dev_extension
  "site"
end
filetypes() click to toggle source
# File lib/pave/reload.rb, line 77
def self.filetypes
  ['css','html','htm','php','rb','erb','less','js']
end
keyword() click to toggle source
# File lib/pave/reload.rb, line 85
def self.keyword
  File.basename(Dir.pwd)
end
live_reload(browser="chrome") click to toggle source
# File lib/pave/reload.rb, line 3
def self.live_reload(browser="chrome")
  # docs: http://brettterpstra.com/2011/03/07/watch-for-file-changes-and-refresh-your-browser-automatically/
  trap("SIGINT") { exit }

  puts "Watching #{watch_folder} and subfolders for changes in project files..."

  while true do
    files = []
    filetypes.each {|type|
      files += Dir.glob( File.join( watch_folder, "**", "*.#{type}" ) )
    }
    new_hash = files.collect {|f| [ f, File.stat(f).mtime.to_i ] }
    hash ||= new_hash
    diff_hash = new_hash - hash

    unless diff_hash.empty?
      hash = new_hash

      diff_hash.each do |df|
        print "Detected change in #{df[0]}, refreshing"
        if browser == "chrome"
          print " Chrome"
          %x{osascript<<ENDGAME
            tell application "Google Chrome"
              set windowList to every window
              repeat with aWindow in windowList
                set tabList to every tab of aWindow
                repeat with atab in tabList
                  if (URL of atab contains "#{keyword}") then
                    tell atab to reload
                  end if
                end repeat
              end repeat
            end tell
          }
        elsif browser == "safari"
          print " Safari"
          %x{osascript<<ENDGAME
            tell application "Safari"
              set windowList to every window
              repeat with aWindow in windowList
                set tabList to every tab of aWindow
                repeat with atab in tabList
                  if (URL of atab contains "#{keyword}") then
                    tell atab to set its URL to (get its URL)
                  end if
                end repeat
              end repeat
            end tell
          }
        elsif browser == "firefox"
          print " Firefox"
          %x{osascript<<ENDGAME
            tell app "Firefox" to activate
            tell app "System Events"
              keystroke "r" using command down
            end tell
          }
        else
          puts "#{browser} is not supported yet. Feel free to add it and create a pull request!"
        end
        puts ""
      end
    end

    sleep 1
  end

end
watch_folder() click to toggle source
# File lib/pave/reload.rb, line 81
def self.watch_folder
  Dir.pwd
end