Class: LiveReload::Project

Inherits:
Object show all
Defined in:
lib/livereload.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(directory, explicit_config = nil) ⇒ Project

Returns a new instance of Project.



116
117
118
119
120
# File 'lib/livereload.rb', line 116

def initialize directory, explicit_config=nil
  @directory = directory
  @explicit_config = explicit_config
  read_config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



114
115
116
# File 'lib/livereload.rb', line 114

def config
  @config
end

Instance Method Details

#is_excluded?(path) ⇒ Boolean

Returns:

  • (Boolean)


148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/livereload.rb', line 148

def is_excluded? path
  basename = File.basename(path)
  @config.exclusions.any? do |exclusion|
    if Regexp === exclusion
      path =~ exclusion
    elsif exclusion.include? '/'
      File.fnmatch?(File.join(@directory, exclusion), path)
    else
      File.fnmatch?(exclusion, basename)
    end
  end
end


133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/livereload.rb', line 133

def print_config
  puts "Watching: #{@directory}"
  puts "  - extensions: " + @config.exts.collect {|e| ".#{e}"}.join(" ")
  if !@config.apply_js_live && !@config.apply_css_live
    puts "  - live refreshing disabled for .css & .js: will reload the whole page on every change"
  elsif !@config.apply_js_live
    puts "  - live refreshing disabled for .js: will reload the whole page when .js is changed"
  elsif !@config.apply_css_live
    puts "  - live refreshing disabled for .css: will reload the whole page when .css is changed"
  end
  if @config.exclusions.size > 0
    puts "  - excluding changes in: " + @config.exclusions.join(" ")
  end
end

#read_configObject



122
123
124
125
126
127
128
129
130
131
# File 'lib/livereload.rb', line 122

def read_config
  project_config_file = File.join(@directory, '.livereload')
  unless File.file? project_config_file
    File.open(project_config_file, 'w') do |file|
      file.write PROJECT_CONFIG_FILE_TEMPLATE
    end
  end
  project_config = Config.load_from project_config_file
  @config = Config.merge(DEFAULT_CONFIG, USER_CONFIG, project_config, @explicit_config)
end

#restart_watchingObject



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/livereload.rb', line 165

def restart_watching
  if @dw
    @dw.stop
  end
  @dw = DirectoryWatcher.new @directory, :glob => "{.livereload,**/*.{#{@config.exts.join(',')}}}", :scanner => :em, :pre_load => true
  @dw.add_observer do |*args|
    begin
      args.each do |event|
        path = event[:path]
        if File.basename(path) == '.livereload'
          @when_changes_detected.call [:config_changed, path]
        elsif event[:type] == :modified
          @when_changes_detected.call [if is_excluded?(path) then :excluded else :modified end, path]
        end
      end
    rescue
      puts $!
      puts $!.backtrace
    end
  end
  @dw.start
end

#when_changes_detected(&block) ⇒ Object



161
162
163
# File 'lib/livereload.rb', line 161

def when_changes_detected &block
  @when_changes_detected = block
end