sass4 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (147) hide show
  1. checksums.yaml +7 -0
  2. data/.yardopts +13 -0
  3. data/AGENTS.md +534 -0
  4. data/CODE_OF_CONDUCT.md +10 -0
  5. data/CONTRIBUTING.md +148 -0
  6. data/MIT-LICENSE +20 -0
  7. data/README.md +242 -0
  8. data/VERSION +1 -0
  9. data/VERSION_NAME +1 -0
  10. data/bin/sass +13 -0
  11. data/bin/sass-convert +12 -0
  12. data/bin/scss +13 -0
  13. data/extra/sass-spec-ref.sh +40 -0
  14. data/extra/update_watch.rb +13 -0
  15. data/init.rb +18 -0
  16. data/lib/sass/cache_stores/base.rb +88 -0
  17. data/lib/sass/cache_stores/chain.rb +34 -0
  18. data/lib/sass/cache_stores/filesystem.rb +60 -0
  19. data/lib/sass/cache_stores/memory.rb +46 -0
  20. data/lib/sass/cache_stores/null.rb +25 -0
  21. data/lib/sass/cache_stores.rb +15 -0
  22. data/lib/sass/callbacks.rb +67 -0
  23. data/lib/sass/css.rb +407 -0
  24. data/lib/sass/deprecation.rb +55 -0
  25. data/lib/sass/engine.rb +1236 -0
  26. data/lib/sass/environment.rb +236 -0
  27. data/lib/sass/error.rb +198 -0
  28. data/lib/sass/exec/base.rb +188 -0
  29. data/lib/sass/exec/sass_convert.rb +283 -0
  30. data/lib/sass/exec/sass_scss.rb +436 -0
  31. data/lib/sass/exec.rb +9 -0
  32. data/lib/sass/features.rb +48 -0
  33. data/lib/sass/importers/base.rb +182 -0
  34. data/lib/sass/importers/deprecated_path.rb +51 -0
  35. data/lib/sass/importers/filesystem.rb +221 -0
  36. data/lib/sass/importers.rb +23 -0
  37. data/lib/sass/logger/base.rb +47 -0
  38. data/lib/sass/logger/delayed.rb +50 -0
  39. data/lib/sass/logger/log_level.rb +45 -0
  40. data/lib/sass/logger.rb +17 -0
  41. data/lib/sass/media.rb +210 -0
  42. data/lib/sass/plugin/compiler.rb +552 -0
  43. data/lib/sass/plugin/configuration.rb +134 -0
  44. data/lib/sass/plugin/generic.rb +15 -0
  45. data/lib/sass/plugin/merb.rb +48 -0
  46. data/lib/sass/plugin/rack.rb +60 -0
  47. data/lib/sass/plugin/rails.rb +47 -0
  48. data/lib/sass/plugin/staleness_checker.rb +199 -0
  49. data/lib/sass/plugin.rb +134 -0
  50. data/lib/sass/railtie.rb +10 -0
  51. data/lib/sass/repl.rb +57 -0
  52. data/lib/sass/root.rb +7 -0
  53. data/lib/sass/script/css_lexer.rb +33 -0
  54. data/lib/sass/script/css_parser.rb +36 -0
  55. data/lib/sass/script/functions.rb +3103 -0
  56. data/lib/sass/script/lexer.rb +518 -0
  57. data/lib/sass/script/parser.rb +1164 -0
  58. data/lib/sass/script/tree/funcall.rb +314 -0
  59. data/lib/sass/script/tree/interpolation.rb +220 -0
  60. data/lib/sass/script/tree/list_literal.rb +119 -0
  61. data/lib/sass/script/tree/literal.rb +49 -0
  62. data/lib/sass/script/tree/map_literal.rb +64 -0
  63. data/lib/sass/script/tree/node.rb +119 -0
  64. data/lib/sass/script/tree/operation.rb +149 -0
  65. data/lib/sass/script/tree/selector.rb +26 -0
  66. data/lib/sass/script/tree/string_interpolation.rb +125 -0
  67. data/lib/sass/script/tree/unary_operation.rb +69 -0
  68. data/lib/sass/script/tree/variable.rb +57 -0
  69. data/lib/sass/script/tree.rb +16 -0
  70. data/lib/sass/script/value/arg_list.rb +36 -0
  71. data/lib/sass/script/value/base.rb +258 -0
  72. data/lib/sass/script/value/bool.rb +35 -0
  73. data/lib/sass/script/value/callable.rb +25 -0
  74. data/lib/sass/script/value/color.rb +704 -0
  75. data/lib/sass/script/value/function.rb +19 -0
  76. data/lib/sass/script/value/helpers.rb +298 -0
  77. data/lib/sass/script/value/list.rb +135 -0
  78. data/lib/sass/script/value/map.rb +70 -0
  79. data/lib/sass/script/value/null.rb +44 -0
  80. data/lib/sass/script/value/number.rb +564 -0
  81. data/lib/sass/script/value/string.rb +138 -0
  82. data/lib/sass/script/value.rb +13 -0
  83. data/lib/sass/script.rb +66 -0
  84. data/lib/sass/scss/css_parser.rb +61 -0
  85. data/lib/sass/scss/parser.rb +1343 -0
  86. data/lib/sass/scss/rx.rb +134 -0
  87. data/lib/sass/scss/static_parser.rb +351 -0
  88. data/lib/sass/scss.rb +14 -0
  89. data/lib/sass/selector/abstract_sequence.rb +112 -0
  90. data/lib/sass/selector/comma_sequence.rb +195 -0
  91. data/lib/sass/selector/pseudo.rb +291 -0
  92. data/lib/sass/selector/sequence.rb +661 -0
  93. data/lib/sass/selector/simple.rb +124 -0
  94. data/lib/sass/selector/simple_sequence.rb +348 -0
  95. data/lib/sass/selector.rb +327 -0
  96. data/lib/sass/shared.rb +76 -0
  97. data/lib/sass/source/map.rb +209 -0
  98. data/lib/sass/source/position.rb +39 -0
  99. data/lib/sass/source/range.rb +41 -0
  100. data/lib/sass/stack.rb +140 -0
  101. data/lib/sass/supports.rb +225 -0
  102. data/lib/sass/tree/at_root_node.rb +83 -0
  103. data/lib/sass/tree/charset_node.rb +22 -0
  104. data/lib/sass/tree/comment_node.rb +82 -0
  105. data/lib/sass/tree/content_node.rb +9 -0
  106. data/lib/sass/tree/css_import_node.rb +68 -0
  107. data/lib/sass/tree/debug_node.rb +18 -0
  108. data/lib/sass/tree/directive_node.rb +59 -0
  109. data/lib/sass/tree/each_node.rb +24 -0
  110. data/lib/sass/tree/error_node.rb +18 -0
  111. data/lib/sass/tree/extend_node.rb +43 -0
  112. data/lib/sass/tree/for_node.rb +36 -0
  113. data/lib/sass/tree/function_node.rb +44 -0
  114. data/lib/sass/tree/if_node.rb +52 -0
  115. data/lib/sass/tree/import_node.rb +75 -0
  116. data/lib/sass/tree/keyframe_rule_node.rb +15 -0
  117. data/lib/sass/tree/media_node.rb +48 -0
  118. data/lib/sass/tree/mixin_def_node.rb +38 -0
  119. data/lib/sass/tree/mixin_node.rb +52 -0
  120. data/lib/sass/tree/node.rb +240 -0
  121. data/lib/sass/tree/prop_node.rb +162 -0
  122. data/lib/sass/tree/return_node.rb +19 -0
  123. data/lib/sass/tree/root_node.rb +44 -0
  124. data/lib/sass/tree/rule_node.rb +153 -0
  125. data/lib/sass/tree/supports_node.rb +38 -0
  126. data/lib/sass/tree/trace_node.rb +33 -0
  127. data/lib/sass/tree/variable_node.rb +36 -0
  128. data/lib/sass/tree/visitors/base.rb +72 -0
  129. data/lib/sass/tree/visitors/check_nesting.rb +173 -0
  130. data/lib/sass/tree/visitors/convert.rb +350 -0
  131. data/lib/sass/tree/visitors/cssize.rb +362 -0
  132. data/lib/sass/tree/visitors/deep_copy.rb +107 -0
  133. data/lib/sass/tree/visitors/extend.rb +64 -0
  134. data/lib/sass/tree/visitors/perform.rb +572 -0
  135. data/lib/sass/tree/visitors/set_options.rb +139 -0
  136. data/lib/sass/tree/visitors/to_css.rb +440 -0
  137. data/lib/sass/tree/warn_node.rb +18 -0
  138. data/lib/sass/tree/while_node.rb +18 -0
  139. data/lib/sass/util/multibyte_string_scanner.rb +151 -0
  140. data/lib/sass/util/normalized_map.rb +122 -0
  141. data/lib/sass/util/subset_map.rb +109 -0
  142. data/lib/sass/util/test.rb +9 -0
  143. data/lib/sass/util.rb +1137 -0
  144. data/lib/sass/version.rb +120 -0
  145. data/lib/sass.rb +102 -0
  146. data/rails/init.rb +1 -0
  147. metadata +283 -0
@@ -0,0 +1,552 @@
1
+ require 'fileutils'
2
+
3
+ require 'sass'
4
+ # XXX CE: is this still necessary now that we have the compiler class?
5
+ require 'sass/callbacks'
6
+ require 'sass/plugin/configuration'
7
+ require 'sass/plugin/staleness_checker'
8
+
9
+ module Sass::Plugin
10
+ # The Compiler class handles compilation of multiple files and/or directories,
11
+ # including checking which CSS files are out-of-date and need to be updated
12
+ # and calling Sass to perform the compilation on those files.
13
+ #
14
+ # {Sass::Plugin} uses this class to update stylesheets for a single application.
15
+ # Unlike {Sass::Plugin}, though, the Compiler class has no global state,
16
+ # and so multiple instances may be created and used independently.
17
+ #
18
+ # If you need to compile a Sass string into CSS,
19
+ # please see the {Sass::Engine} class.
20
+ #
21
+ # Unlike {Sass::Plugin}, this class doesn't keep track of
22
+ # whether or how many times a stylesheet should be updated.
23
+ # Therefore, the following `Sass::Plugin` options are ignored by the Compiler:
24
+ #
25
+ # * `:never_update`
26
+ # * `:always_check`
27
+ class Compiler
28
+ include Configuration
29
+ extend Sass::Callbacks
30
+
31
+ # Creates a new compiler.
32
+ #
33
+ # @param opts [{Symbol => Object}]
34
+ # See {file:SASS_REFERENCE.md#Options the Sass options documentation}.
35
+ def initialize(opts = {})
36
+ @watched_files = Set.new
37
+ options.merge!(opts)
38
+ end
39
+
40
+ # Register a callback to be run before stylesheets are mass-updated.
41
+ # This is run whenever \{#update\_stylesheets} is called,
42
+ # unless the \{file:SASS_REFERENCE.md#never_update-option `:never_update` option}
43
+ # is enabled.
44
+ #
45
+ # @yield [files]
46
+ # @yieldparam files [<(String, String, String)>]
47
+ # Individual files to be updated. Files in directories specified are included in this list.
48
+ # The first element of each pair is the source file,
49
+ # the second is the target CSS file,
50
+ # the third is the target sourcemap file.
51
+ define_callback :updating_stylesheets
52
+
53
+ # Register a callback to be run after stylesheets are mass-updated.
54
+ # This is run whenever \{#update\_stylesheets} is called,
55
+ # unless the \{file:SASS_REFERENCE.md#never_update-option `:never_update` option}
56
+ # is enabled.
57
+ #
58
+ # @yield [updated_files]
59
+ # @yieldparam updated_files [<(String, String)>]
60
+ # Individual files that were updated.
61
+ # The first element of each pair is the source file, the second is the target CSS file.
62
+ define_callback :updated_stylesheets
63
+
64
+ # Register a callback to be run after a single stylesheet is updated.
65
+ # The callback is only run if the stylesheet is really updated;
66
+ # if the CSS file is fresh, this won't be run.
67
+ #
68
+ # Even if the \{file:SASS_REFERENCE.md#full_exception-option `:full_exception` option}
69
+ # is enabled, this callback won't be run
70
+ # when an exception CSS file is being written.
71
+ # To run an action for those files, use \{#on\_compilation\_error}.
72
+ #
73
+ # @yield [template, css, sourcemap]
74
+ # @yieldparam template [String]
75
+ # The location of the Sass/SCSS file being updated.
76
+ # @yieldparam css [String]
77
+ # The location of the CSS file being generated.
78
+ # @yieldparam sourcemap [String]
79
+ # The location of the sourcemap being generated, if any.
80
+ define_callback :updated_stylesheet
81
+
82
+ # Register a callback to be run when compilation starts.
83
+ #
84
+ # In combination with on_updated_stylesheet, this could be used
85
+ # to collect compilation statistics like timing or to take a
86
+ # diff of the changes to the output file.
87
+ #
88
+ # @yield [template, css, sourcemap]
89
+ # @yieldparam template [String]
90
+ # The location of the Sass/SCSS file being updated.
91
+ # @yieldparam css [String]
92
+ # The location of the CSS file being generated.
93
+ # @yieldparam sourcemap [String]
94
+ # The location of the sourcemap being generated, if any.
95
+ define_callback :compilation_starting
96
+
97
+ # Register a callback to be run when Sass decides not to update a stylesheet.
98
+ # In particular, the callback is run when Sass finds that
99
+ # the template file and none of its dependencies
100
+ # have been modified since the last compilation.
101
+ #
102
+ # Note that this is **not** run when the
103
+ # \{file:SASS_REFERENCE.md#never-update_option `:never_update` option} is set,
104
+ # nor when Sass decides not to compile a partial.
105
+ #
106
+ # @yield [template, css]
107
+ # @yieldparam template [String]
108
+ # The location of the Sass/SCSS file not being updated.
109
+ # @yieldparam css [String]
110
+ # The location of the CSS file not being generated.
111
+ define_callback :not_updating_stylesheet
112
+
113
+ # Register a callback to be run when there's an error
114
+ # compiling a Sass file.
115
+ # This could include not only errors in the Sass document,
116
+ # but also errors accessing the file at all.
117
+ #
118
+ # @yield [error, template, css]
119
+ # @yieldparam error [Exception] The exception that was raised.
120
+ # @yieldparam template [String]
121
+ # The location of the Sass/SCSS file being updated.
122
+ # @yieldparam css [String]
123
+ # The location of the CSS file being generated.
124
+ define_callback :compilation_error
125
+
126
+ # Register a callback to be run when Sass creates a directory
127
+ # into which to put CSS files.
128
+ #
129
+ # Note that even if multiple levels of directories need to be created,
130
+ # the callback may only be run once.
131
+ # For example, if "foo/" exists and "foo/bar/baz/" needs to be created,
132
+ # this may only be run for "foo/bar/baz/".
133
+ # This is not a guarantee, however;
134
+ # it may also be run for "foo/bar/".
135
+ #
136
+ # @yield [dirname]
137
+ # @yieldparam dirname [String]
138
+ # The location of the directory that was created.
139
+ define_callback :creating_directory
140
+
141
+ # Register a callback to be run when Sass detects
142
+ # that a template has been modified.
143
+ # This is only run when using \{#watch}.
144
+ #
145
+ # @yield [template]
146
+ # @yieldparam template [String]
147
+ # The location of the template that was modified.
148
+ define_callback :template_modified
149
+
150
+ # Register a callback to be run when Sass detects
151
+ # that a new template has been created.
152
+ # This is only run when using \{#watch}.
153
+ #
154
+ # @yield [template]
155
+ # @yieldparam template [String]
156
+ # The location of the template that was created.
157
+ define_callback :template_created
158
+
159
+ # Register a callback to be run when Sass detects
160
+ # that a template has been deleted.
161
+ # This is only run when using \{#watch}.
162
+ #
163
+ # @yield [template]
164
+ # @yieldparam template [String]
165
+ # The location of the template that was deleted.
166
+ define_callback :template_deleted
167
+
168
+ # Register a callback to be run when Sass deletes a CSS file.
169
+ # This happens when the corresponding Sass/SCSS file has been deleted
170
+ # and when the compiler cleans the output files.
171
+ #
172
+ # @yield [filename]
173
+ # @yieldparam filename [String]
174
+ # The location of the CSS file that was deleted.
175
+ define_callback :deleting_css
176
+
177
+ # Register a callback to be run when Sass deletes a sourcemap file.
178
+ # This happens when the corresponding Sass/SCSS file has been deleted
179
+ # and when the compiler cleans the output files.
180
+ #
181
+ # @yield [filename]
182
+ # @yieldparam filename [String]
183
+ # The location of the sourcemap file that was deleted.
184
+ define_callback :deleting_sourcemap
185
+
186
+ # Updates out-of-date stylesheets.
187
+ #
188
+ # Checks each Sass/SCSS file in
189
+ # {file:SASS_REFERENCE.md#template_location-option `:template_location`}
190
+ # to see if it's been modified more recently than the corresponding CSS file
191
+ # in {file:SASS_REFERENCE.md#css_location-option `:css_location`}.
192
+ # If it has, it updates the CSS file.
193
+ #
194
+ # @param individual_files [Array<(String, String[, String])>]
195
+ # A list of files to check for updates
196
+ # **in addition to those specified by the
197
+ # {file:SASS_REFERENCE.md#template_location-option `:template_location` option}.**
198
+ # The first string in each pair is the location of the Sass/SCSS file,
199
+ # the second is the location of the CSS file that it should be compiled to.
200
+ # The third string, if provided, is the location of the Sourcemap file.
201
+ def update_stylesheets(individual_files = [])
202
+ Sass::Plugin.checked_for_updates = true
203
+ staleness_checker = StalenessChecker.new(engine_options)
204
+
205
+ files = file_list(individual_files)
206
+ run_updating_stylesheets(files)
207
+
208
+ updated_stylesheets = []
209
+ files.each do |file, css, sourcemap|
210
+ # TODO: Does staleness_checker need to check the sourcemap file as well?
211
+ if options[:always_update] || staleness_checker.stylesheet_needs_update?(css, file)
212
+ # XXX For consistency, this should return the sourcemap too, but it would
213
+ # XXX be an API change.
214
+ updated_stylesheets << [file, css]
215
+ update_stylesheet(file, css, sourcemap)
216
+ else
217
+ run_not_updating_stylesheet(file, css, sourcemap)
218
+ end
219
+ end
220
+ run_updated_stylesheets(updated_stylesheets)
221
+ end
222
+
223
+ # Construct a list of files that might need to be compiled
224
+ # from the provided individual_files and the template_locations.
225
+ #
226
+ # Note: this method does not cache the results as they can change
227
+ # across invocations when sass files are added or removed.
228
+ #
229
+ # @param individual_files [Array<(String, String[, String])>]
230
+ # A list of files to check for updates
231
+ # **in addition to those specified by the
232
+ # {file:SASS_REFERENCE.md#template_location-option `:template_location` option}.**
233
+ # The first string in each pair is the location of the Sass/SCSS file,
234
+ # the second is the location of the CSS file that it should be compiled to.
235
+ # The third string, if provided, is the location of the Sourcemap file.
236
+ # @return [Array<(String, String, String)>]
237
+ # A list of [sass_file, css_file, sourcemap_file] tuples similar
238
+ # to what was passed in, but expanded to include the current state
239
+ # of the directories being updated.
240
+ def file_list(individual_files = [])
241
+ files = individual_files.map do |tuple|
242
+ if engine_options[:sourcemap] == :none
243
+ tuple[0..1]
244
+ elsif tuple.size < 3
245
+ [tuple[0], tuple[1], Sass::Util.sourcemap_name(tuple[1])]
246
+ else
247
+ tuple.dup
248
+ end
249
+ end
250
+
251
+ template_location_array.each do |template_location, css_location|
252
+ Sass::Util.glob(File.join(template_location, "**", "[^_]*.s[ca]ss")).sort.each do |file|
253
+ # Get the relative path to the file
254
+ name = Sass::Util.relative_path_from(file, template_location).to_s
255
+ css = css_filename(name, css_location)
256
+ sourcemap = Sass::Util.sourcemap_name(css) unless engine_options[:sourcemap] == :none
257
+ files << [file, css, sourcemap]
258
+ end
259
+ end
260
+ files
261
+ end
262
+
263
+ # Watches the template directory (or directories)
264
+ # and updates the CSS files whenever the related Sass/SCSS files change.
265
+ # `watch` never returns.
266
+ #
267
+ # Whenever a change is detected to a Sass/SCSS file in
268
+ # {file:SASS_REFERENCE.md#template_location-option `:template_location`},
269
+ # the corresponding CSS file in {file:SASS_REFERENCE.md#css_location-option `:css_location`}
270
+ # will be recompiled.
271
+ # The CSS files of any Sass/SCSS files that import the changed file will also be recompiled.
272
+ #
273
+ # Before the watching starts in earnest, `watch` calls \{#update\_stylesheets}.
274
+ #
275
+ # Note that `watch` uses the [Listen](http://github.com/guard/listen) library
276
+ # to monitor the filesystem for changes.
277
+ # Listen isn't loaded until `watch` is run.
278
+ # The version of Listen distributed with Sass is loaded by default,
279
+ # but if another version has already been loaded that will be used instead.
280
+ #
281
+ # @param individual_files [Array<(String, String[, String])>]
282
+ # A list of files to check for updates
283
+ # **in addition to those specified by the
284
+ # {file:SASS_REFERENCE.md#template_location-option `:template_location` option}.**
285
+ # The first string in each pair is the location of the Sass/SCSS file,
286
+ # the second is the location of the CSS file that it should be compiled to.
287
+ # The third string, if provided, is the location of the Sourcemap file.
288
+ # @param options [Hash] The options that control how watching works.
289
+ # @option options [Boolean] :skip_initial_update
290
+ # Don't do an initial update when starting the watcher when true
291
+ def watch(individual_files = [], options = {})
292
+ @inferred_directories = []
293
+ options, individual_files = individual_files, [] if individual_files.is_a?(Hash)
294
+ update_stylesheets(individual_files) unless options[:skip_initial_update]
295
+
296
+ directories = watched_paths
297
+ individual_files.each do |(source, _, _)|
298
+ source = File.expand_path(source)
299
+ @watched_files << Sass::Util.realpath(source).to_s
300
+ @inferred_directories << File.dirname(source)
301
+ end
302
+
303
+ directories += @inferred_directories
304
+ directories = remove_redundant_directories(directories)
305
+
306
+ # TODO: Keep better track of what depends on what
307
+ # so we don't have to run a global update every time anything changes.
308
+ # XXX The :additional_watch_paths option exists for Compass to use until
309
+ # a deprecated feature is removed. It may be removed without warning.
310
+ directories += Array(options[:additional_watch_paths])
311
+
312
+ options = {
313
+ :relative_paths => false,
314
+ # The native windows listener is much slower than the polling option, according to
315
+ # https://github.com/nex3/sass/commit/a3031856b22bc834a5417dedecb038b7be9b9e3e
316
+ :force_polling => @options[:poll] || Sass::Util.windows?
317
+ }
318
+
319
+ listener = create_listener(*directories, options) do |modified, added, removed|
320
+ on_file_changed(individual_files, modified, added, removed)
321
+ yield(modified, added, removed) if block_given?
322
+ end
323
+
324
+ begin
325
+ listener.start
326
+ sleep
327
+ rescue Interrupt
328
+ # Squelch Interrupt for clean exit from Listen::Listener
329
+ end
330
+ end
331
+
332
+ # Non-destructively modifies \{#options} so that default values are properly set,
333
+ # and returns the result.
334
+ #
335
+ # @param additional_options [{Symbol => Object}] An options hash with which to merge \{#options}
336
+ # @return [{Symbol => Object}] The modified options hash
337
+ def engine_options(additional_options = {})
338
+ opts = options.merge(additional_options)
339
+ opts[:load_paths] = load_paths(opts)
340
+ options[:sourcemap] = :auto if options[:sourcemap] == true
341
+ options[:sourcemap] = :none if options[:sourcemap] == false
342
+ opts
343
+ end
344
+
345
+ # Compass expects this to exist
346
+ def stylesheet_needs_update?(css_file, template_file)
347
+ StalenessChecker.stylesheet_needs_update?(css_file, template_file)
348
+ end
349
+
350
+ # Remove all output files that would be created by calling update_stylesheets, if they exist.
351
+ #
352
+ # This method runs the deleting_css and deleting_sourcemap callbacks for
353
+ # the files that are deleted.
354
+ #
355
+ # @param individual_files [Array<(String, String[, String])>]
356
+ # A list of files to check for updates
357
+ # **in addition to those specified by the
358
+ # {file:SASS_REFERENCE.md#template_location-option `:template_location` option}.**
359
+ # The first string in each pair is the location of the Sass/SCSS file,
360
+ # the second is the location of the CSS file that it should be compiled to.
361
+ # The third string, if provided, is the location of the Sourcemap file.
362
+ def clean(individual_files = [])
363
+ file_list(individual_files).each do |(_, css_file, sourcemap_file)|
364
+ if File.exist?(css_file)
365
+ run_deleting_css css_file
366
+ File.delete(css_file)
367
+ end
368
+ if sourcemap_file && File.exist?(sourcemap_file)
369
+ run_deleting_sourcemap sourcemap_file
370
+ File.delete(sourcemap_file)
371
+ end
372
+ end
373
+ nil
374
+ end
375
+
376
+ private
377
+
378
+ # This is mocked out in compiler_test.rb.
379
+ def create_listener(*args, &block)
380
+ require 'sass-listen'
381
+ SassListen.to(*args, &block)
382
+ end
383
+
384
+ def remove_redundant_directories(directories)
385
+ dedupped = []
386
+ directories.each do |new_directory|
387
+ # no need to add a directory that is already watched.
388
+ next if dedupped.any? do |existing_directory|
389
+ child_of_directory?(existing_directory, new_directory)
390
+ end
391
+ # get rid of any sub directories of this new directory
392
+ dedupped.reject! do |existing_directory|
393
+ child_of_directory?(new_directory, existing_directory)
394
+ end
395
+ dedupped << new_directory
396
+ end
397
+ dedupped
398
+ end
399
+
400
+ def on_file_changed(individual_files, modified, added, removed)
401
+ recompile_required = false
402
+
403
+ modified.uniq.each do |f|
404
+ next unless watched_file?(f)
405
+ recompile_required = true
406
+ run_template_modified(relative_to_pwd(f))
407
+ end
408
+
409
+ added.uniq.each do |f|
410
+ next unless watched_file?(f)
411
+ recompile_required = true
412
+ run_template_created(relative_to_pwd(f))
413
+ end
414
+
415
+ removed.uniq.each do |f|
416
+ next unless watched_file?(f)
417
+ run_template_deleted(relative_to_pwd(f))
418
+ if (files = individual_files.find {|(source, _, _)| File.expand_path(source) == f})
419
+ recompile_required = true
420
+ # This was a file we were watching explicitly and compiling to a particular location.
421
+ # Delete the corresponding file.
422
+ try_delete_css files[1]
423
+ else
424
+ next unless watched_file?(f)
425
+ recompile_required = true
426
+ # Look for the sass directory that contained the sass file
427
+ # And try to remove the css file that corresponds to it
428
+ template_location_array.each do |(sass_dir, css_dir)|
429
+ sass_dir = File.expand_path(sass_dir)
430
+ next unless child_of_directory?(sass_dir, f)
431
+ remainder = f[(sass_dir.size + 1)..-1]
432
+ try_delete_css(css_filename(remainder, css_dir))
433
+ break
434
+ end
435
+ end
436
+ end
437
+
438
+ return unless recompile_required
439
+
440
+ # In case a file we're watching is removed and then recreated we
441
+ # prune out the non-existant files here.
442
+ watched_files_remaining = individual_files.select {|(source, _, _)| File.exist?(source)}
443
+ update_stylesheets(watched_files_remaining)
444
+ end
445
+
446
+ def update_stylesheet(filename, css, sourcemap)
447
+ dir = File.dirname(css)
448
+ unless File.exist?(dir)
449
+ run_creating_directory dir
450
+ FileUtils.mkdir_p dir
451
+ end
452
+
453
+ begin
454
+ File.read(filename) unless File.readable?(filename) # triggers an error for handling
455
+ engine_opts = engine_options(:css_filename => css,
456
+ :filename => filename,
457
+ :sourcemap_filename => sourcemap)
458
+ mapping = nil
459
+ run_compilation_starting(filename, css, sourcemap)
460
+ engine = Sass::Engine.for_file(filename, engine_opts)
461
+ if sourcemap
462
+ rendered, mapping = engine.render_with_sourcemap(File.basename(sourcemap))
463
+ else
464
+ rendered = engine.render
465
+ end
466
+ rescue StandardError => e
467
+ compilation_error_occurred = true
468
+ run_compilation_error e, filename, css, sourcemap
469
+ raise e unless options[:full_exception]
470
+ rendered = Sass::SyntaxError.exception_to_css(e, options[:line] || 1)
471
+ end
472
+
473
+ write_file(css, rendered)
474
+ if mapping
475
+ write_file(
476
+ sourcemap,
477
+ mapping.to_json(
478
+ :css_path => css, :sourcemap_path => sourcemap, :type => options[:sourcemap]))
479
+ end
480
+ run_updated_stylesheet(filename, css, sourcemap) unless compilation_error_occurred
481
+ end
482
+
483
+ def write_file(fileName, content)
484
+ flag = 'w'
485
+ flag = 'wb' if Sass::Util.windows? && options[:unix_newlines]
486
+ File.open(fileName, flag) do |file|
487
+ file.set_encoding(content.encoding)
488
+ file.print(content)
489
+ end
490
+ end
491
+
492
+ def try_delete_css(css)
493
+ if File.exist?(css)
494
+ run_deleting_css css
495
+ File.delete css
496
+ end
497
+ map = Sass::Util.sourcemap_name(css)
498
+
499
+ return unless File.exist?(map)
500
+
501
+ run_deleting_sourcemap map
502
+ File.delete map
503
+ end
504
+
505
+ def watched_file?(file)
506
+ @watched_files.include?(file) ||
507
+ normalized_load_paths.any? {|lp| lp.watched_file?(file)} ||
508
+ @inferred_directories.any? {|d| sass_file_in_directory?(d, file)}
509
+ end
510
+
511
+ def sass_file_in_directory?(directory, filename)
512
+ filename =~ /\.s[ac]ss$/ && filename.start_with?(directory + File::SEPARATOR)
513
+ end
514
+
515
+ def watched_paths
516
+ @watched_paths ||= normalized_load_paths.map {|lp| lp.directories_to_watch}.compact.flatten
517
+ end
518
+
519
+ def normalized_load_paths
520
+ @normalized_load_paths ||=
521
+ Sass::Engine.normalize_options(:load_paths => load_paths)[:load_paths]
522
+ end
523
+
524
+ def load_paths(opts = options)
525
+ (opts[:load_paths] || []) + template_locations
526
+ end
527
+
528
+ def template_locations
529
+ template_location_array.to_a.map {|l| l.first}
530
+ end
531
+
532
+ def css_locations
533
+ template_location_array.to_a.map {|l| l.last}
534
+ end
535
+
536
+ def css_filename(name, path)
537
+ "#{path}#{File::SEPARATOR unless path.end_with?(File::SEPARATOR)}#{name}".
538
+ gsub(/\.s[ac]ss$/, '.css')
539
+ end
540
+
541
+ def relative_to_pwd(f)
542
+ Sass::Util.relative_path_from(f, Dir.pwd).to_s
543
+ rescue ArgumentError # when a relative path cannot be computed
544
+ f
545
+ end
546
+
547
+ def child_of_directory?(parent, child)
548
+ parent_dir = parent.end_with?(File::SEPARATOR) ? parent : (parent + File::SEPARATOR)
549
+ child.start_with?(parent_dir) || parent == child
550
+ end
551
+ end
552
+ end
@@ -0,0 +1,134 @@
1
+ module Sass
2
+ module Plugin
3
+ # We keep configuration in its own self-contained file so that we can load
4
+ # it independently in Rails 3, where the full plugin stuff is lazy-loaded.
5
+ #
6
+ # Note that this is not guaranteed to be thread-safe. For guaranteed thread
7
+ # safety, use a separate {Sass::Plugin} for each thread.
8
+ module Configuration
9
+ # Returns the default options for a {Sass::Plugin::Compiler}.
10
+ #
11
+ # @return [{Symbol => Object}]
12
+ def default_options
13
+ @default_options ||= {
14
+ :css_location => './public/stylesheets',
15
+ :always_update => false,
16
+ :always_check => true,
17
+ :full_exception => true,
18
+ :cache_location => ".sass-cache"
19
+ }.freeze
20
+ end
21
+
22
+ # Resets the options and
23
+ # {Sass::Callbacks::InstanceMethods#clear_callbacks! clears all callbacks}.
24
+ def reset!
25
+ @options = nil
26
+ clear_callbacks!
27
+ end
28
+
29
+ # An options hash. See {file:SASS_REFERENCE.md#Options the Sass options
30
+ # documentation}.
31
+ #
32
+ # @return [{Symbol => Object}]
33
+ def options
34
+ @options ||= default_options.dup
35
+ end
36
+
37
+ # Adds a new template-location/css-location mapping.
38
+ # This means that Sass/SCSS files in `template_location`
39
+ # will be compiled to CSS files in `css_location`.
40
+ #
41
+ # This is preferred over manually manipulating the
42
+ # {file:SASS_REFERENCE.md#template_location-option `:template_location` option}
43
+ # since the option can be in multiple formats.
44
+ #
45
+ # Note that this method will change `options[:template_location]`
46
+ # to be in the Array format.
47
+ # This means that even if `options[:template_location]`
48
+ # had previously been a Hash or a String,
49
+ # it will now be an Array.
50
+ #
51
+ # @param template_location [String] The location where Sass/SCSS files will be.
52
+ # @param css_location [String] The location where compiled CSS files will go.
53
+ def add_template_location(template_location, css_location = options[:css_location])
54
+ normalize_template_location!
55
+ template_location_array << [template_location, css_location]
56
+ end
57
+
58
+ # Removes a template-location/css-location mapping.
59
+ # This means that Sass/SCSS files in `template_location`
60
+ # will no longer be compiled to CSS files in `css_location`.
61
+ #
62
+ # This is preferred over manually manipulating the
63
+ # {file:SASS_REFERENCE.md#template_location-option `:template_location` option}
64
+ # since the option can be in multiple formats.
65
+ #
66
+ # Note that this method will change `options[:template_location]`
67
+ # to be in the Array format.
68
+ # This means that even if `options[:template_location]`
69
+ # had previously been a Hash or a String,
70
+ # it will now be an Array.
71
+ #
72
+ # @param template_location [String]
73
+ # The location where Sass/SCSS files were,
74
+ # which is now going to be ignored.
75
+ # @param css_location [String]
76
+ # The location where compiled CSS files went, but will no longer go.
77
+ # @return [Boolean]
78
+ # Non-`nil` if the given mapping already existed and was removed,
79
+ # or `nil` if nothing was changed.
80
+ def remove_template_location(template_location, css_location = options[:css_location])
81
+ normalize_template_location!
82
+ template_location_array.delete([template_location, css_location])
83
+ end
84
+
85
+ # Returns the template locations configured for Sass
86
+ # as an array of `[template_location, css_location]` pairs.
87
+ # See the {file:SASS_REFERENCE.md#template_location-option `:template_location` option}
88
+ # for details.
89
+ #
90
+ # Modifications to the returned array may not be persistent. Use {#add_template_location}
91
+ # and {#remove_template_location} instead.
92
+ #
93
+ # @return [Array<(String, String)>]
94
+ # An array of `[template_location, css_location]` pairs.
95
+ def template_location_array
96
+ convert_template_location(options[:template_location], options[:css_location])
97
+ end
98
+
99
+ private
100
+
101
+ # Returns the given template location, as an array. If it's already an array,
102
+ # it is returned unmodified. Otherwise, a new array is created and returned.
103
+ #
104
+ # @param template_location [String, Array<(String, String)>]
105
+ # A single template location, or a pre-normalized array of template
106
+ # locations and CSS locations.
107
+ # @param css_location [String?]
108
+ # The location for compiled CSS files.
109
+ # @return [Array<(String, String)>]
110
+ # An array of `[template_location, css_location]` pairs.
111
+ def convert_template_location(template_location, css_location)
112
+ return template_location if template_location.is_a?(Array)
113
+
114
+ case template_location
115
+ when nil
116
+ if css_location
117
+ [[File.join(css_location, 'sass'), css_location]]
118
+ else
119
+ []
120
+ end
121
+ when String
122
+ [[template_location, css_location]]
123
+ else
124
+ template_location.to_a
125
+ end
126
+ end
127
+
128
+ def normalize_template_location!
129
+ options[:template_location] = convert_template_location(
130
+ options[:template_location], options[:css_location])
131
+ end
132
+ end
133
+ end
134
+ end
@@ -0,0 +1,15 @@
1
+ # The reason some options are declared here rather than in sass/plugin/configuration.rb
2
+ # is that otherwise they'd clobber the Rails-specific options.
3
+ # Since Rails' options are lazy-loaded in Rails 3,
4
+ # they're reverse-merged with the default options
5
+ # so that user configuration is preserved.
6
+ # This means that defaults that differ from Rails'
7
+ # must be declared here.
8
+
9
+ unless defined?(Sass::GENERIC_LOADED)
10
+ Sass::GENERIC_LOADED = true
11
+
12
+ Sass::Plugin.options.merge!(:css_location => './public/stylesheets',
13
+ :always_update => false,
14
+ :always_check => true)
15
+ end