| Class | Haml::Exec::Sass |
| In: |
lib/haml/exec.rb
|
| Parent: | HamlSass |
The `sass` executable.
@param args [Array<String>] The command-line arguments
# File lib/haml/exec.rb, line 271
271: def initialize(args)
272: super
273: @name = "Sass"
274: @options[:for_engine][:load_paths] = ['.'] + (ENV['SASSPATH'] || '').split(File::PATH_SEPARATOR)
275: end
Processes the options set by the command-line arguments, and runs the Sass compiler appropriately.
# File lib/haml/exec.rb, line 340
340: def process_result
341: if !@options[:update] && !@options[:watch] &&
342: @args.first && colon_path?(@args.first)
343: if @args.size == 1
344: @args = split_colon_path(@args.first)
345: else
346: @options[:update] = true
347: end
348: end
349:
350: return interactive if @options[:interactive]
351: return watch_or_update if @options[:watch] || @options[:update]
352: super
353:
354: begin
355: input = @options[:input]
356: output = @options[:output]
357:
358: @options[:syntax] ||= :scss if input.is_a?(File) && input.path =~ /\.scss$/
359: tree =
360: if input.is_a?(File) && !@options[:check_syntax]
361: ::Sass::Files.tree_for(input.path, @options[:for_engine])
362: else
363: # We don't need to do any special handling of @options[:check_syntax] here,
364: # because the Sass syntax checking happens alongside evaluation
365: # and evaluation doesn't actually evaluate any code anyway.
366: ::Sass::Engine.new(input.read(), @options[:for_engine]).to_tree
367: end
368:
369: input.close() if input.is_a?(File)
370:
371: output.write(tree.render)
372: output.close() if output.is_a? File
373: rescue ::Sass::SyntaxError => e
374: raise e if @options[:trace]
375: raise e.sass_backtrace_str("standard input")
376: end
377: end
Tells optparse how to parse the arguments.
@param opts [OptionParser]
# File lib/haml/exec.rb, line 282
282: def set_opts(opts)
283: super
284:
285: opts.on('--scss',
286: 'Use the CSS-superset SCSS syntax.') do
287: @options[:for_engine][:syntax] = :scss
288: end
289: opts.on('--watch', 'Watch files or directories for changes.',
290: 'The location of the generated CSS can be set using a colon:',
291: ' sass --watch input.sass:output.css',
292: ' sass --watch input-dir:output-dir') do
293: @options[:watch] = true
294: end
295: opts.on('--update', 'Compile files or directories to CSS.',
296: 'Locations are set like --watch.') do
297: @options[:update] = true
298: end
299: opts.on('-t', '--style NAME',
300: 'Output style. Can be nested (default), compact, compressed, or expanded.') do |name|
301: @options[:for_engine][:style] = name.to_sym
302: end
303: opts.on('-q', '--quiet', 'Silence warnings during compilation.') do
304: @options[:for_engine][:quiet] = true
305: end
306: opts.on('-g', '--debug-info',
307: 'Emit extra information in the generated CSS that can be used by the FireSass Firebug plugin.') do
308: @options[:for_engine][:debug_info] = true
309: end
310: opts.on('-l', '--line-numbers', '--line-comments',
311: 'Emit comments in the generated CSS indicating the corresponding sass line.') do
312: @options[:for_engine][:line_numbers] = true
313: end
314: opts.on('-i', '--interactive',
315: 'Run an interactive SassScript shell.') do
316: @options[:interactive] = true
317: end
318: opts.on('-I', '--load-path PATH', 'Add a sass import path.') do |path|
319: @options[:for_engine][:load_paths] << path
320: end
321: opts.on('-r', '--require LIB', 'Require a Ruby library before running Sass.') do |lib|
322: require lib
323: end
324: opts.on('--cache-location PATH', 'The path to put cached Sass files. Defaults to .sass-cache.') do |loc|
325: @options[:for_engine][:cache_location] = loc
326: end
327: opts.on('-C', '--no-cache', "Don't cache to sassc files.") do
328: @options[:for_engine][:cache] = false
329: end
330:
331: unless ::Haml::Util.ruby1_8?
332: opts.on('-E encoding', 'Specify the default encoding for Sass files.') do |encoding|
333: Encoding.default_external = encoding
334: end
335: end
336: end
# File lib/haml/exec.rb, line 443
443: def colon_path?(path)
444: !split_colon_path(path)[1].nil?
445: end
# File lib/haml/exec.rb, line 381
381: def interactive
382: require 'sass'
383: require 'sass/repl'
384: ::Sass::Repl.new(@options).run
385: end
# File lib/haml/exec.rb, line 447
447: def split_colon_path(path)
448: one, two = path.split(':', 2)
449: if one && two && ::Haml::Util.windows? &&
450: one =~ /\A[A-Za-z]\Z/ && two =~ /\A[\/\\]/
451: # If we're on Windows and we were passed a drive letter path,
452: # don't split on that colon.
453: one2, two = two.split(':', 2)
454: one = one + ':' + one2
455: end
456: return one, two
457: end
# File lib/haml/exec.rb, line 387
387: def watch_or_update
388: require 'sass'
389: require 'sass/plugin'
390: ::Sass::Plugin.options.merge! @options[:for_engine]
391: ::Sass::Plugin.options[:unix_newlines] = @options[:unix_newlines]
392:
393: if @args[1] && !colon_path?(@args[0])
394: flag = @options[:update] ? "--update" : "--watch"
395: err =
396: if !File.exist?(@args[1])
397: "doesn't exist"
398: elsif @args[1] =~ /\.css$/
399: "is a CSS file"
400: end
401: raise "File \#{@args[1]} \#{err}.\n Did you mean: sass \#{flag} \#{@args[0]}:\#{@args[1]}\n" if err
402: end
403:
404: dirs, files = @args.map {|name| split_colon_path(name)}.
405: partition {|i, _| File.directory? i}
406: files.map! {|from, to| [from, to || from.gsub(/\..*?$/, '.css')]}
407: dirs.map! {|from, to| [from, to || from]}
408: ::Sass::Plugin.options[:template_location] = dirs
409:
410: ::Sass::Plugin.on_updating_stylesheet do |_, css|
411: if File.exists? css
412: puts_action :overwrite, :yellow, css
413: else
414: puts_action :create, :green, css
415: end
416: end
417:
418: ::Sass::Plugin.on_creating_directory {|dirname| puts_action :directory, :green, dirname}
419: ::Sass::Plugin.on_deleting_css {|filename| puts_action :delete, :yellow, filename}
420: ::Sass::Plugin.on_compilation_error do |error, _, _|
421: raise error unless error.is_a?(::Sass::SyntaxError)
422: puts_action :error, :red, "#{error.sass_filename} (Line #{error.sass_line}: #{error.message})"
423: end
424:
425: if @options[:update]
426: ::Sass::Plugin.update_stylesheets(files)
427: return
428: end
429:
430: puts ">>> Sass is watching for changes. Press Ctrl-C to stop."
431:
432: ::Sass::Plugin.on_template_modified {|template| puts ">>> Change detected to: #{template}"}
433: ::Sass::Plugin.on_template_created {|template| puts ">>> New template detected: #{template}"}
434: ::Sass::Plugin.on_template_deleted {|template| puts ">>> Deleted template detected: #{template}"}
435:
436: ::Sass::Plugin.watch(files)
437: end