Class: Hyla::Commands::Watch

Inherits:
Hyla::Command show all
Defined in:
lib/hyla/commands/watch.rb

Constant Summary collapse

WS_OPTIONS =
{
    :base_url => '/modules'
}

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Hyla::Command

check_mandatory_option?

Constructor Details

#initializeWatch

Returns a new instance of Watch.



9
10
# File 'lib/hyla/commands/watch.rb', line 9

def initialize
end

Class Method Details

.call_asciidoctor(f) ⇒ Object

listen



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/hyla/commands/watch.rb', line 72

def self.call_asciidoctor(f)
  dir_file = File.dirname(f)
  file_to_process = Pathname.new(f).basename
  @ext_name = File.extname(file_to_process)

  if [".adoc",".ad",".asciidoc",".txt",".index"].include? @ext_name

    Hyla.logger2.info ">> Directory containing file(s) to be processed : #{dir_file}"
    Hyla.logger2.info ">> File to be processed : #{file_to_process}"
    Hyla.logger2.info ">> Extension of the file : #{@ext_name}"

    # Generate File name
    # Rename xxx.adoc, xxx.asciidoc, xxx.ad, xxx.index to xxx.html
    to_file = file_to_process.to_s.gsub(/.adoc$|.ad$|.asciidoc$|.index$/, '.html')
    @opts[:to_file] = to_file

    # Use destination from original config
    @opts[:to_dir] = @opts_bk[:destination]

    # Calculate Asciidoc to_dir relative to the dir of the file to be processed
    # and create dir in watched dir
    rel_dir = substract_watch_dir(dir_file, @opts[:watch_dir])
    if !rel_dir.empty?
      calc_dir = File.expand_path @opts[:to_dir] + rel_dir
      FileUtils.makedirs calc_dir
    else
      calc_dir = File.expand_path @opts[:to_dir]
    end

    @opts[:to_dir] = calc_dir

    Hyla.logger2.info ">> Directory of the file to be generated : #{calc_dir}"
    Hyla.logger2.debug ">> Asciidoctor options : #{@opts}"

    # Render Asciidoc document
    Asciidoctor.render_file(f, @opts)

    # Refresh browser connected using LiveReload
    path = []
    path.push(calc_dir)
    @reload.reload_browser(path)
  end
end

.process(args, options = {}) ⇒ Object



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
# File 'lib/hyla/commands/watch.rb', line 17

def self.process(args, options = {})

  # Start LiveReload
  self.start_livereload

  @opts = options
  @opts_bk = @opts

  if options.has_key? :destination
    @opts[:to_dir] = File.expand_path options[:destination]
  end

  if options.has_key? :source
    @opts[:watch_dir] = File.expand_path options[:source]
  end

  #
  # Guard Listen Callback
  # Detect files modified, deleted or added
  #
  callback = Proc.new do |modified, added, removed|
    Hyla.logger2.info "modified absolute path: #{modified}"
    Hyla.logger2.info "added absolute path: #{added}"
    Hyla.logger2.info "removed absolute path: #{removed}"

    if !modified.nil? or !added.nil?
      modified.each do |modify|
        Hyla.logger2.info "File modified : #{modify}"
        call_asciidoctor(modify)
      end

      added.each do |add|
        Hyla.logger2.info "File added : #{add}"
        call_asciidoctor(add)
      end

    end
  end # callback

  Hyla.logger2.info ">> ... Starting\n"
  Hyla.logger2.info ">> Hyla has started to watch files in this dir : #{@opts[:watch_dir]}"
  Hyla.logger2.info ">> Results of rendering will be available here : #{@opts[:to_dir]}"

  # TODO : Investigate issue with Thread pool is not running (Celluloid::Error)
  # when using a more recent version of guard listen
  listener = Listen.to!(@opts[:watch_dir], &callback)

  trap(:INT) {
    Hyla.logger2.info "Interrupt intercepted"
    Thread.kill(@t)
  }
end

.start_livereloadObject



12
13
14
15
# File 'lib/hyla/commands/watch.rb', line 12

def self.start_livereload
  @reload = Hyla::Commands::Reload.new
  @t = Thread.new { @reload.process(WS_OPTIONS) }
end

.substract_watch_dir(file_dir, watched_dir) ⇒ Object



116
117
118
119
120
# File 'lib/hyla/commands/watch.rb', line 116

def self.substract_watch_dir(file_dir, watched_dir)
  s = file_dir.sub(watched_dir, '')
  Hyla.logger2.info ">> Relative directory : #{s}"
  s
end