Class: DiscourseTheme::Watcher
- Inherits:
-
Object
- Object
- DiscourseTheme::Watcher
- Defined in:
- lib/discourse_theme/watcher.rb
Constant Summary collapse
- LISTEN_IGNORE_PATTERNS =
[%r{migrations/.+/.+\.js}]
Class Method Summary collapse
- .call_start_subscribers ⇒ Object
- .reset_start_subscribers ⇒ Object
- .return_immediately! ⇒ Object
- .return_immediately=(val) ⇒ Object
- .return_immediately? ⇒ Boolean
- .subscribe_start(&block) ⇒ Object
Instance Method Summary collapse
-
#initialize(dir:, uploader:) ⇒ Watcher
constructor
A new instance of Watcher.
- #watch ⇒ Object
Constructor Details
#initialize(dir:, uploader:) ⇒ Watcher
Returns a new instance of Watcher.
31 32 33 34 |
# File 'lib/discourse_theme/watcher.rb', line 31 def initialize(dir:, uploader:) @dir = dir @uploader = uploader end |
Class Method Details
.call_start_subscribers ⇒ Object
23 24 25 |
# File 'lib/discourse_theme/watcher.rb', line 23 def self.call_start_subscribers @subscribers&.each(&:call) end |
.reset_start_subscribers ⇒ Object
27 28 29 |
# File 'lib/discourse_theme/watcher.rb', line 27 def self.reset_start_subscribers @subscribers = [] end |
.return_immediately! ⇒ Object
6 7 8 |
# File 'lib/discourse_theme/watcher.rb', line 6 def self.return_immediately! @return_immediately = true end |
.return_immediately=(val) ⇒ Object
10 11 12 |
# File 'lib/discourse_theme/watcher.rb', line 10 def self.return_immediately=(val) @return_immediately = val end |
.return_immediately? ⇒ Boolean
14 15 16 |
# File 'lib/discourse_theme/watcher.rb', line 14 def self.return_immediately? !!@return_immediately end |
.subscribe_start(&block) ⇒ Object
18 19 20 21 |
# File 'lib/discourse_theme/watcher.rb', line 18 def self.subscribe_start(&block) @subscribers ||= [] @subscribers << block end |
Instance Method Details
#watch ⇒ Object
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 72 73 74 75 |
# File 'lib/discourse_theme/watcher.rb', line 36 def watch listener = Listen.to(@dir, ignore: LISTEN_IGNORE_PATTERNS) do |modified, added, removed| yield(modified, added, removed) if block_given? begin if modified.length == 1 && added.length == 0 && removed.length == 0 && (resolved = resolve_file(modified[0])) target, name, type_id = resolved UI.progress "Fast updating #{target}.scss" @uploader.upload_theme_field( target: target, name: name, value: File.read(modified[0]), type_id: type_id, ) else count = modified.length + added.length + removed.length if count > 1 UI.progress "Detected changes in #{count} files, uploading theme" else filename = modified[0] || added[0] || removed[0] UI.progress "Detected changes in #{filename.gsub(@dir, "")}, uploading theme" end @uploader.upload_full_theme end UI.success "Done! Watching for changes..." rescue DiscourseTheme::ThemeError => e UI.error "#{e.}" UI.progress "Watching for changes..." end end listener.start self.class.call_start_subscribers sleep 1 while !self.class.return_immediately? end |