Class: Compass::Commands::WatchProject

Inherits:
UpdateProject show all
Defined in:
lib/compass/commands/watch_project.rb

Instance Attribute Summary collapse

Attributes inherited from ProjectBase

#options, #project_name

Attributes inherited from Base

#options, #working_path

Attributes included from Actions

#logger

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from UpdateProject

absolutize, #check_for_sass_files!, #initialize, #new_compiler_instance, #new_config?, parse!, parse_arguments!, #prepare_project!, primary, #transfer_options, usage

Methods inherited from ProjectBase

#execute, #initialize

Methods inherited from Base

#execute, #failed!, #initialize, register, #successful?

Methods included from Actions

#basename, #copy, #directory, #log_action, #process_erb, #relativize, #remove, #separate, #strip_trailing_separator, #write_file

Constructor Details

This class inherits a constructor from Compass::Commands::UpdateProject

Instance Attribute Details

#last_sass_filesObject

Returns the value of attribute last_sass_files.



33
34
35
# File 'lib/compass/commands/watch_project.rb', line 33

def last_sass_files
  @last_sass_files
end

#last_update_timeObject

Returns the value of attribute last_update_time.



33
34
35
# File 'lib/compass/commands/watch_project.rb', line 33

def last_update_time
  @last_update_time
end

Class Method Details

.description(command) ⇒ Object



103
104
105
# File 'lib/compass/commands/watch_project.rb', line 103

def description(command)
  "Compile Sass stylesheets to CSS when they change"
end

.option_parser(arguments) ⇒ Object



107
108
109
110
111
112
113
# File 'lib/compass/commands/watch_project.rb', line 107

def option_parser(arguments)
  parser = Compass::Exec::CommandOptionParser.new(arguments)
  parser.extend(Compass::Exec::GlobalOptionsParser)
  parser.extend(Compass::Exec::ProjectOptionsParser)
  parser.extend(CompileProjectOptionsParser)
  parser.extend(WatchProjectOptionsParser)
end

Instance Method Details

#additional_watch_pathsObject



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/compass/commands/watch_project.rb', line 56

def additional_watch_paths
  Compass.configuration.watches.map do |watch|
    pathname = Pathname.new(File.join(Compass.configuration.project_path, watch.glob))
    real_path = nil
    pathname.ascend do |p|
      if p.exist?
        real_path = p
        break
      end
    end
    real_path
  end.compact.uniq
end

#compiler_optionsObject



52
53
54
# File 'lib/compass/commands/watch_project.rb', line 52

def compiler_options
  super.merge(:poll => options[:poll], :full_exception => true)
end

#happy_styling!(logger) ⇒ Object



48
49
50
# File 'lib/compass/commands/watch_project.rb', line 48

def happy_styling!(logger)
    logger.log "\n#{logger.color(:yellow)}★★★ #{logger.color(:blue)}Happy Styling!#{logger.color(:yellow)} ★★★#{logger.color(:clear)}"
end

#notify_watches(modified, added, removed) ⇒ Object



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
# File 'lib/compass/commands/watch_project.rb', line 70

def notify_watches(modified, added, removed)
  project_path = Compass.configuration.project_path
  files = {:modified => modified,
           :added    => added,
           :removed  => removed}

  run_once, run_each = Compass.configuration.watches.partition {|w| w.run_once_per_changeset?}

  run_once.each do |watcher|
    if file = files.values.flatten.detect{|f| watcher.match?(f) }
      action = files.keys.detect{|k| files[k].include?(file) }
      watcher.run_callback(project_path, relative_to(file, project_path), action)
    end
  end

  run_each.each do |watcher|
    files.each do |action, list|
      list.each do |file|
        if watcher.match?(file)
          watcher.run_callback(project_path, relative_to(file, project_path), action)
        end
      end
    end
  end
end

#performObject



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/compass/commands/watch_project.rb', line 35

def perform
  compiler = new_compiler_instance
  compiler.logger.time = true if options[:time]
  prepare_project!(compiler)
  compiler.logger.log ">>> #{compiler.logger.color(:green)}Compass is watching for changes.#{compiler.logger.color(:clear)} #{compiler.logger.color(:red)}Press Ctrl-C to Stop.#{compiler.logger.color(:clear)}"
  begin
    compiler.watch!(:additional_watch_paths => additional_watch_paths, &method(:notify_watches))
    happy_styling!(compiler.logger)
  rescue Interrupt
    happy_styling!(compiler.logger)
  end
end

#relative_to(f, dir) ⇒ Object



96
97
98
99
100
# File 'lib/compass/commands/watch_project.rb', line 96

def relative_to(f, dir)
  Pathname.new(f).relative_path_from(Pathname.new(dir))
rescue ArgumentError # does not share a common path.
  f
end