Class: Hyla::Commands::Watch
- Inherits:
-
Hyla::Command
- Object
- Hyla::Command
- Hyla::Commands::Watch
- Defined in:
- lib/hyla/commands/watch.rb
Constant Summary collapse
- WS_OPTIONS =
{ :base_url => '/modules' }
Class Method Summary collapse
-
.call_asciidoctor(f) ⇒ Object
listen.
- .process(args, options = {}) ⇒ Object
- .start_livereload ⇒ Object
- .substract_watch_dir(file_dir, watched_dir) ⇒ Object
Instance Method Summary collapse
-
#initialize ⇒ Watch
constructor
A new instance of Watch.
Methods inherited from Hyla::Command
Constructor Details
#initialize ⇒ Watch
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.logger.info ">> Directory containing file(s) to be processed : #{dir_file}" Hyla.logger.info ">> File to be processed : #{file_to_process}" Hyla.logger.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. @opts[:to_dir] + rel_dir FileUtils.makedirs calc_dir else calc_dir = File. @opts[:to_dir] end @opts[:to_dir] = calc_dir Hyla.logger.info ">> Directory of the file to be generated : #{calc_dir}" Hyla.logger.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, = {}) # Start LiveReload self.start_livereload @opts = @opts_bk = @opts if .has_key? :destination @opts[:to_dir] = File. [:destination] end if .has_key? :source @opts[:watch_dir] = File. [:source] end # # Guard Listen Callback # Detect files modified, deleted or added # callback = Proc.new do |modified, added, removed| Hyla.logger.info "modified absolute path: #{modified}" Hyla.logger.info "added absolute path: #{added}" Hyla.logger.info "removed absolute path: #{removed}" if !modified.nil? or !added.nil? modified.each do |modify| Hyla.logger.info "File modified : #{modify}" call_asciidoctor(modify) end added.each do |add| Hyla.logger.info "File added : #{add}" call_asciidoctor(add) end end end # callback Hyla.logger.info ">> ... Starting\n" Hyla.logger.info ">> Hyla has started to watch files in this dir : #{@opts[:watch_dir]}" Hyla.logger.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.logger.info "Interrupt intercepted" Thread.kill(@t) } end |
.start_livereload ⇒ Object
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 |