Skip to content

Instantly share code, notes, and snippets.

@igable
Last active December 31, 2015 16:29
Show Gist options
  • Select an option

  • Save igable/8013853 to your computer and use it in GitHub Desktop.

Select an option

Save igable/8013853 to your computer and use it in GitHub Desktop.

Revisions

  1. igable revised this gist Dec 17, 2013. 1 changed file with 11 additions and 3 deletions.
    14 changes: 11 additions & 3 deletions watch.rb
    100644 → 100755
    Original file line number Diff line number Diff line change
    @@ -18,9 +18,17 @@

    while true do
    files = []
    filetypes.each {|type|
    files += Dir.glob( File.join( watch_folder, "**", "*.#{type}" ) )
    }

    if File.directory?(watch_folder)
    filetypes.each {|type|
    files += Dir.glob( File.join( watch_folder, "**", "*.#{type}" ) )
    }
    else
    # watch a single file only. useful in cases like Jekyll where every file is
    # touched each time we generate the site
    files += Dir.glob(File.absolute_path(watch_folder))
    end

    new_hash = files.collect {|f| [ f, File.stat(f).mtime.to_i ] }
    hash ||= new_hash
    diff_hash = new_hash - hash
  2. igable created this gist Dec 17, 2013.
    51 changes: 51 additions & 0 deletions watch.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,51 @@
    #!/usr/bin/env ruby
    # watch.rb by Brett Terpstra, 2011 <http://brettterpstra.com>
    # with credit to Carlo Zottmann <https://github.com/carlo/haml-sass-file-watcher>

    trap("SIGINT") { exit }

    if ARGV.length < 2
    puts "Usage: #{$0} watch_folder keyword"
    puts "Example: #{$0} . mywebproject"
    exit
    end

    dev_extension = 'dev'
    filetypes = ['css','html','htm','php','rb','erb','less','js']
    watch_folder = ARGV[0]
    keyword = ARGV[1]
    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|
    puts "Detected change in #{df[0]}, refreshing"
    %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 do javascript "window.location.reload()"
    end if
    end repeat
    end repeat
    end tell
    ENDGAME
    }
    end
    end

    sleep 1
    end