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