Class: KBuilder::Watch::ActionWatcher

Inherits:
BaseAction
  • Object
show all
Defined in:
lib/k_builder/watch/action_watcher.rb

Overview

Watcher does the actual file watching and run the processor

Instance Attribute Summary collapse

Attributes inherited from BaseAction

#options

Instance Method Summary collapse

Methods inherited from BaseAction

#initialize

Constructor Details

This class inherits a constructor from KBuilder::Watch::BaseAction

Instance Attribute Details

#directoryObject

Returns the value of attribute directory.



14
15
16
# File 'lib/k_builder/watch/action_watcher.rb', line 14

def directory
  @directory
end

Instance Method Details

#process_updated_file(filename) ⇒ Object

rubocop:disable Lint/RescueException



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/k_builder/watch/action_watcher.rb', line 46

def process_updated_file(filename)
  clear_screen
  update_load_path(filename)

  puts "File updated: #{filename}"

  content = File.read(filename)
  Object.class_eval(content, filename)
rescue Exception => e
  puts e.message
  puts e.backtrace
        .select { |ex| ex.start_with?(filename) }
        .map { |m| m.delete_suffix(":in `process_updated_file'") }
        .join("\n")
  # puts '-' * 70
  # puts e.backtrace.join("\n")
end

#runObject

TODO: Need to support folder exclusions so that I can place .templates, in with .builders



18
19
20
21
# File 'lib/k_builder/watch/action_watcher.rb', line 18

def run
  @directory = options.watch_path
  start
end

#startObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/k_builder/watch/action_watcher.rb', line 23

def start
  # clear_screen
  puts "KBuilder-Watch: v#{KBuilder::Watch::VERSION} is watching for file changes"
  puts "Directory: #{directory}"
  # puts "Watch File: #{watch_file}"

  # { filename: '/Users/davidcruwys/dev/kgems/k_manager/spec/samples/.builder/a/b/c/d/e/bob.png'         , event: :created    },
  # { filename: '/Users/davidcruwys/dev/kgems/k_manager/spec/samples/.builder/a/b/c/d/e/bob.rb'          , event: :created    },
  # { filename: '/Users/davidcruwys/dev/kgems/k_manager/spec/samples/.builder/a/b/c/d/e/bob_builder.txt' , event: :updated    },
  # { filename: '/Users/davidcruwys/dev/kgems/k_manager/spec/samples/.builder/a/b/c/d/e/kill_bill.txt'   , event: :deleted    },

  Filewatcher.new(directory).watch do |changes|
    changes.each do |filename, event|
      puts "File #{event}: #{filename}"

      # process_created_file(filename) if event == :created
      process_updated_file(filename) if event == :updated # || event == :created
      # process_deleted_file(filename) if event == :deleted
    end
  end
end