Class: Awestruct::CLI::Auto

Inherits:
Object
  • Object
show all
Defined in:
lib/awestruct/cli/auto.rb

Instance Method Summary collapse

Constructor Details

#initialize(config, base_url) ⇒ Auto

Returns a new instance of Auto



11
12
13
14
# File 'lib/awestruct/cli/auto.rb', line 11

def initialize(config, base_url)
  @config = config
  @base_url = base_url
end

Instance Method Details

#runObject



16
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
91
92
93
94
95
96
97
98
99
# File 'lib/awestruct/cli/auto.rb', line 16

def run()
  generate_thread = nil
  current_path = nil

  begin
    guard = if ( @config.options.livereload )
      Guard.init({})
      guard = Guard::LiveReload.new
      guard.start
      guard
    else
      nil
    end
  rescue => e
    puts e
    puts e.backtrace
  end

  force_polling = ( RUBY_PLATFORM =~ /mingw/ ? true : false )
  listener = Listen.to( @config.dir, :latency=>0.5, :force_polling=>force_polling ) do |modified, added, removed|
    modified.each do |path| # path is absolute path
      engine = ::Awestruct::Engine.instance

      begin
        $LOG.info "Change detected for file #{path}" if $LOG.info?
        if path.eql? current_path
          unless generate_thread.nil?
            $LOG.info "Same path triggered, stopping previous generation" if generate_thread.alive? && $LOG.info?
            generate_thread.kill
          end
        else
          generate_thread.join unless generate_thread.nil?
          current_path = path
        end

        generate_thread = Thread.new {
          begin
            page = engine.page_by_source_path(path)
            pages = []
            if ( page )
              unless ( guard )
                pages = engine.generate_page_and_dependencies( page )
              else
                pages = engine.page_dependencies( page )
              end
            else
              if File.exist? path
              # chances are this is an extension or yaml file
                pages = engine.run_auto_for_non_page(path, !@config.options.generate_on_access)
              end
            end

            unless ( guard )
              $LOG.info "Regeneration finished." if $LOG.info?
            end

            if ( guard )
              urls = pages.map do |p|
                @base_url + p.url.to_s
              end

              guard.run_on_modifications(urls)
            end

          rescue => e
            ExceptionHelper.log_building_error e, path
          end
        }
      rescue => e
        ExceptionHelper.log_building_error e, path
      end
    end
  end
  listener.ignore( %r(\.awestruct) )
  listener.ignore( %r(^#{File.basename( @config.tmp_dir )}) )
  listener.ignore( %r(\.sass-cache) )
  listener.ignore( %r(^#{File.basename( @config.output_dir )}) )

  @config.ignore.each do |i|
    listener.ignore( %r(^#{i}) )
  end

  listener.start
end