Class: Compass::SassCompiler

Inherits:
Object
  • Object
show all
Includes:
Actions
Defined in:
lib/compass/sass_compiler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Actions

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

Constructor Details

#initialize(options = {}, config = Compass.configuration) ⇒ SassCompiler

Returns a new instance of SassCompiler.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/compass/sass_compiler.rb', line 15

def initialize(options = {}, config = Compass.configuration)
  options = options.dup
  self.config = config
  self.display_compilation_times = options.delete(:time)
  self.working_path = options.delete(:working_path) || Dir.pwd
  self.only_sass_files = options.delete(:only_sass_files) || []
  @quiet = options[:quiet]
  plugin_options = config.to_sass_plugin_options.merge(options)
  if only_sass_files.any?
    plugin_options[:template_location] = []
    plugin_options[:load_paths] = config.sass_load_paths
  end
  plugin_options[:always_update] = true if options.delete(:force)
  plugin_options[:compass] ||= {}
  plugin_options[:compass][:logger] = logger
  @compiler = Sass::Plugin::Compiler.new(plugin_options)
  @start_times = {}
  @error_count = 0

  public_methods(true).grep(/^when_/).each do |callback|
    @compiler.send(callback.to_s.sub(/^when_/, 'on_')) {|*args| send(callback, *args) }
  end
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



10
11
12
# File 'lib/compass/sass_compiler.rb', line 10

def config
  @config
end

#display_compilation_timesObject

Returns the value of attribute display_compilation_times.



11
12
13
# File 'lib/compass/sass_compiler.rb', line 11

def display_compilation_times
  @display_compilation_times
end

#error_countObject (readonly)

Returns the value of attribute error_count.



9
10
11
# File 'lib/compass/sass_compiler.rb', line 9

def error_count
  @error_count
end

#loggerObject



128
129
130
# File 'lib/compass/sass_compiler.rb', line 128

def logger
  @logger ||= Compass::Logger.new(:quiet => quiet)
end

#only_sass_filesObject

Returns the value of attribute only_sass_files.



13
14
15
# File 'lib/compass/sass_compiler.rb', line 13

def only_sass_files
  @only_sass_files
end

#quietObject (readonly)

Returns the value of attribute quiet.



8
9
10
# File 'lib/compass/sass_compiler.rb', line 8

def quiet
  @quiet
end

#working_pathObject

Returns the value of attribute working_path.



12
13
14
# File 'lib/compass/sass_compiler.rb', line 12

def working_path
  @working_path
end

Instance Method Details

#clean!Object



57
58
59
# File 'lib/compass/sass_compiler.rb', line 57

def clean!
  @compiler.clean(individual_files)
end

#compile!Object



39
40
41
# File 'lib/compass/sass_compiler.rb', line 39

def compile!
  @compiler.update_stylesheets(individual_files)
end

#corresponding_css_file(sass_file) ⇒ Object



132
133
134
# File 'lib/compass/sass_compiler.rb', line 132

def corresponding_css_file(sass_file)
  "#{config.css_path}/#{stylesheet_name(sass_file)}.css"
end

#file_listObject



61
62
63
# File 'lib/compass/sass_compiler.rb', line 61

def file_list
  @compiler.file_list(individual_files)
end

#individual_filesObject



53
54
55
# File 'lib/compass/sass_compiler.rb', line 53

def individual_files
  only_sass_files.map {|sass_file| [sass_file, corresponding_css_file(sass_file)]}
end

#sass_files(options = {}) ⇒ Object



144
145
146
147
148
149
150
151
# File 'lib/compass/sass_compiler.rb', line 144

def sass_files(options = {})
  @compiler.template_location_array.map do |(sass_dir, css_dir)|
    glob = options[:include_partials] ?
             File.join("**","*.s[ac]ss*") :
             File.join("**","[^_]*.s[ac]ss*")
    Dir.glob(File.join(sass_dir, glob))
  end.flatten
end

#stylesheet_name(sass_file) ⇒ Object



136
137
138
139
140
141
142
# File 'lib/compass/sass_compiler.rb', line 136

def stylesheet_name(sass_file)
  if sass_file.index(config.sass_path) == 0
    sass_file[(config.sass_path.length + 1)..-6].sub(/\.css$/,'')
  else
    raise Compass::Error, "Individual stylesheets must be in the sass directory."
  end
end

#watch!(options = {}, &block) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/compass/sass_compiler.rb', line 43

def watch!(options = {}, &block)
  skip_initial_update = options.fetch(:skip_initial_update, false)
  begin
    @compiler.watch(individual_files, options.merge(:skip_initial_update => skip_initial_update), &block)
  rescue Sass::SyntaxError => e
    skip_initial_update = true
    retry
  end
end

#when_compilation_error(error, sass_file, css_file, sourcemap_file) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/compass/sass_compiler.rb', line 113

def when_compilation_error(error, sass_file, css_file, sourcemap_file)
  @error_count += 1
  if error.respond_to?(:sass_filename)
    error_filename = error.sass_filename || sass_file
    if relativize(error_filename) == relativize(sass_file)
      logger.record :error, "#{relativize(sass_file)} (Line #{error.sass_line}: #{error.message})"
    else
      logger.record :error, "#{relativize(sass_file)} (Line #{error.sass_line} of #{relativize(error_filename)}: #{error.message})"
    end
  else
    logger.record :error, "#{relativize(sass_file)} (#{error.backtrace.first}: #{error.message})"
  end
  config.run_stylesheet_error(sass_file, error.message)
end

#when_compilation_starting(sass_file, css, sourcemap) ⇒ Object



70
71
72
# File 'lib/compass/sass_compiler.rb', line 70

def when_compilation_starting(sass_file, css, sourcemap)
  @start_times[sass_file] = Time.now
end

#when_creating_directory(dirname) ⇒ Object



99
100
101
# File 'lib/compass/sass_compiler.rb', line 99

def when_creating_directory(dirname)
  logger.record :directory, relativize(dirname)
end

#when_deleting_css(filename) ⇒ Object



103
104
105
106
# File 'lib/compass/sass_compiler.rb', line 103

def when_deleting_css(filename)
  logger.record :delete, relativize(filename)
  config.run_stylesheet_removed(filename) if filename
end

#when_deleting_sourcemap(filename) ⇒ Object



108
109
110
111
# File 'lib/compass/sass_compiler.rb', line 108

def when_deleting_sourcemap(filename)
  logger.record :delete, relativize(filename)
  config.run_sourcemap_removed(filename) if filename
end

#when_template_created(sass_file) ⇒ Object



74
75
76
# File 'lib/compass/sass_compiler.rb', line 74

def when_template_created(sass_file)
  logger.record :created, relativize(sass_file)
end

#when_template_deleted(sass_file) ⇒ Object



78
79
80
# File 'lib/compass/sass_compiler.rb', line 78

def when_template_deleted(sass_file)
  logger.record :deleted, relativize(sass_file)
end

#when_template_modified(sass_file) ⇒ Object



82
83
84
# File 'lib/compass/sass_compiler.rb', line 82

def when_template_modified(sass_file)
  logger.record :modified, relativize(sass_file)
end

#when_updated_stylesheet(sass_file, css, sourcemap) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/compass/sass_compiler.rb', line 86

def when_updated_stylesheet(sass_file, css, sourcemap)
  if css && display_compilation_times && @start_times[sass_file]
    duration = ((Time.now - @start_times[sass_file]) * 1000).round / 1000.0
    logger.record :write, "#{relativize(css)} (#{duration}s)"
  else
    logger.record :write, relativize(css) if css
  end
  config.run_stylesheet_saved(css) if css

  logger.record :write, relativize(sourcemap) if sourcemap
  config.run_sourcemap_saved(sourcemap) if sourcemap
end

#when_updating_stylesheets(individual_files) ⇒ Object



65
66
67
68
# File 'lib/compass/sass_compiler.rb', line 65

def when_updating_stylesheets(individual_files)
  @start_times = {}
  @error_count = 0
end