class Hyla::Commands::Watch

Constants

WS_OPTIONS

Public Class Methods

call_asciidoctor(f) click to toggle source

listen

# 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
new() click to toggle source
# File lib/hyla/commands/watch.rb, line 9
def initialize
end
process(args, options = {}) click to toggle source
# 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_livereload() click to toggle source
# 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) click to toggle source
# 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