Class: Nanoc3::CLI::Commands::Watch

Inherits:
Nanoc3::CLI::Command show all
Defined in:
lib/nanoc3/cli/commands/watch.rb

Defined Under Namespace

Classes: Notifier

Instance Attribute Summary

Attributes inherited from Nanoc3::CLI::Command

#arguments, #command, #options

Instance Method Summary collapse

Methods inherited from Nanoc3::CLI::Command

#call, call, #initialize, #site

Constructor Details

This class inherits a constructor from Nanoc3::CLI::Command

Instance Method Details

#runObject



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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/nanoc3/cli/commands/watch.rb', line 17

def run
  require 'fssm'
  require 'pathname'

  @notifier = Notifier.new

  # Define rebuilder
  rebuilder = lambda do |base, relative|
    # Determine filename
    if base.nil? || relative.nil?
      filename = nil
    else
      filename = ::Pathname.new(File.join([ base, relative ])).relative_path_from(::Pathname.new(Dir.getwd)).to_s
    end

    # Notify
    if filename
      print "Change detected to #{filename}; recompiling… ".make_compatible_with_env
    else
      print "Watcher started; compiling the entire site… ".make_compatible_with_env
    end

    # Recompile
    start = Time.now
    site = Nanoc3::Site.new('.')
    begin
      site.compile

      # TODO include icon (--image misc/success-icon.png)
      notify_on_compilation_success = site.config.has_key?(:notify_on_compilation_success) ?
        site.config[:notify_on_compilation_success] :
        true
      if notify_on_compilation_success
        @notifier.notify('Compilation complete')
      end

      time_spent = ((Time.now - start)*1000.0).round
      puts "done in #{format '%is %ims', *(time_spent.divmod(1000))}"
    rescue Exception => e
      # TODO include icon (--image misc/error-icon.png)
      notify_on_compilation_failure = site.config.has_key?(:notify_on_compilation_failure) ?
        site.config[:notify_on_compilation_failure] :
        true
      if notify_on_compilation_failure
        @notifier.notify('Compilation failed')
      end

      puts
      Nanoc3::CLI::ErrorHandler.print_error(e)
      puts
    end
  end

  # Rebuild once
  rebuilder.call(nil, nil)

  # Get directories to watch
  watcher_config = self.site.config[:watcher] || {}
  dirs_to_watch  = watcher_config[:dirs_to_watch]  || %w( content layouts lib )
  files_to_watch = watcher_config[:files_to_watch] || %w( config.yaml Rules rules Rules.rb rules.rb' )
  files_to_watch.delete_if { |f| !File.file?(f) }

  # Watch
  puts "Watching for changes…".make_compatible_with_env
  watcher = lambda do |*args|
    update(&rebuilder)
    delete(&rebuilder)
    create(&rebuilder)
  end
  monitor = FSSM::Monitor.new
  dirs_to_watch.each  { |dir|      monitor.path(dir, '**/*', &watcher) }
  files_to_watch.each { |filename| monitor.file(filename, &watcher)    }
  monitor.run
end