Class: Pave::Reload

Inherits:
Object
  • Object
show all
Defined in:
lib/pave/reload.rb

Class Method Summary collapse

Class Method Details

.dev_extensionObject



73
74
75
# File 'lib/pave/reload.rb', line 73

def self.dev_extension
  "site"
end

.filetypesObject



77
78
79
# File 'lib/pave/reload.rb', line 77

def self.filetypes
  ['css','html','htm','php','rb','erb','less','js']
end

.keywordObject



85
86
87
# File 'lib/pave/reload.rb', line 85

def self.keyword
  File.basename(Dir.pwd)
end

.live_reload(browser = "chrome") ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# 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_folderObject



81
82
83
# File 'lib/pave/reload.rb', line 81

def self.watch_folder
  Dir.pwd
end