Class Haml::Exec::Sass
In: lib/haml/exec.rb
Parent: HamlSass

The `sass` executable.

Methods

Public Class methods

@param args [Array<String>] The command-line arguments

[Source]

     # File lib/haml/exec.rb, line 272
272:       def initialize(args)
273:         super
274:         @name = "Sass"
275:         @options[:for_engine][:load_paths] = ['.'] + (ENV['SASSPATH'] || '').split(File::PATH_SEPARATOR)
276:       end

Protected Instance methods

Processes the options set by the command-line arguments, and runs the Sass compiler appropriately.

[Source]

     # File lib/haml/exec.rb, line 338
338:       def process_result
339:         if !@options[:update] && !@options[:watch] &&
340:             @args.first && @args.first.include?(':')
341:           if @args.size == 1
342:             @args = @args.first.split(':', 2)
343:           else
344:             @options[:update] = true
345:           end
346:         end
347: 
348:         return interactive if @options[:interactive]
349:         return watch_or_update if @options[:watch] || @options[:update]
350:         super
351: 
352:         begin
353:           input = @options[:input]
354:           output = @options[:output]
355: 
356:           @options[:syntax] ||= :scss if input.is_a?(File) && input.path =~ /\.scss$/
357:           tree =
358:             if input.is_a?(File) && !@options[:check_syntax]
359:               ::Sass::Files.tree_for(input.path, @options[:for_engine])
360:             else
361:               # We don't need to do any special handling of @options[:check_syntax] here,
362:               # because the Sass syntax checking happens alongside evaluation
363:               # and evaluation doesn't actually evaluate any code anyway.
364:               ::Sass::Engine.new(input.read(), @options[:for_engine]).to_tree
365:             end
366: 
367:           input.close() if input.is_a?(File)
368: 
369:           output.write(tree.render)
370:           output.close() if output.is_a? File
371:         rescue ::Sass::SyntaxError => e
372:           raise e if @options[:trace]
373:           raise e.sass_backtrace_str("standard input")
374:         end
375:       end

Tells optparse how to parse the arguments.

@param opts [OptionParser]

[Source]

     # File lib/haml/exec.rb, line 283
283:       def set_opts(opts)
284:         super
285: 
286:         opts.on('--scss',
287:                 'Use the CSS-superset SCSS syntax.') do
288:           @options[:for_engine][:syntax] = :scss
289:         end
290:         opts.on('--watch', 'Watch files or directories for changes.',
291:                            'The location of the generated CSS can be set using a colon:',
292:                            '  sass --watch input.sass:output.css',
293:                            '  sass --watch input-dir:output-dir') do
294:           @options[:watch] = true
295:         end
296:         opts.on('--update', 'Compile files or directories to CSS.',
297:                             'Locations are set like --watch.') do
298:           @options[:update] = true
299:         end
300:         opts.on('-t', '--style NAME',
301:                 'Output style. Can be nested (default), compact, compressed, or expanded.') do |name|
302:           @options[:for_engine][:style] = name.to_sym
303:         end
304:         opts.on('-q', '--quiet', 'Silence warnings during compilation.') do
305:           @options[:for_engine][:quiet] = true
306:         end
307:         opts.on('-g', '--debug-info',
308:                 'Emit extra information in the generated CSS that can be used by the FireSass Firebug plugin.') do
309:           @options[:for_engine][:debug_info] = true
310:         end
311:         opts.on('-l', '--line-numbers', '--line-comments',
312:                 'Emit comments in the generated CSS indicating the corresponding sass line.') do
313:           @options[:for_engine][:line_numbers] = true
314:         end
315:         opts.on('-i', '--interactive',
316:                 'Run an interactive SassScript shell.') do
317:           @options[:interactive] = true
318:         end
319:         opts.on('-I', '--load-path PATH', 'Add a sass import path.') do |path|
320:           @options[:for_engine][:load_paths] << path
321:         end
322:         opts.on('--cache-location PATH', 'The path to put cached Sass files. Defaults to .sass-cache.') do |loc|
323:           @options[:for_engine][:cache_location] = loc
324:         end
325:         opts.on('-C', '--no-cache', "Don't cache to sassc files.") do
326:           @options[:for_engine][:cache] = false
327:         end
328: 
329:         unless ::Haml::Util.ruby1_8?
330:           opts.on('-E encoding', 'Specify the default encoding for Sass files.') do |encoding|
331:             Encoding.default_external = encoding
332:           end
333:         end
334:       end

Private Instance methods

[Source]

     # File lib/haml/exec.rb, line 379
379:       def interactive
380:         require 'sass'
381:         require 'sass/repl'
382:         ::Sass::Repl.new(@options).run
383:       end

[Source]

     # File lib/haml/exec.rb, line 385
385:       def watch_or_update
386:         require 'sass'
387:         require 'sass/plugin'
388:         ::Sass::Plugin.options.merge! @options[:for_engine]
389:         ::Sass::Plugin.options[:unix_newlines] = @options[:unix_newlines]
390: 
391:         if @args[1] && !@args[0].include?(':')
392:           flag = @options[:update] ? "--update" : "--watch"
393:           err =
394:             if !File.exist?(@args[1])
395:               "doesn't exist"
396:             elsif @args[1] =~ /\.css$/
397:               "is a CSS file"
398:             end
399:           raise "File \#{@args[1]} \#{err}.\n  Did you mean: sass \#{flag} \#{@args[0]}:\#{@args[1]}\n" if err
400:         end
401: 
402:         dirs, files = @args.map {|name| name.split(':', 2)}.
403:           partition {|i, _| File.directory? i}
404:         files.map! {|from, to| [from, to || from.gsub(/\..*?$/, '.css')]}
405:         dirs.map! {|from, to| [from, to || from]}
406:         ::Sass::Plugin.options[:template_location] = dirs
407: 
408:         ::Sass::Plugin.on_updating_stylesheet do |_, css|
409:           if File.exists? css
410:             puts_action :overwrite, :yellow, css
411:           else
412:             puts_action :create, :green, css
413:           end
414:         end
415: 
416:         ::Sass::Plugin.on_creating_directory {|dirname| puts_action :directory, :green, dirname}
417:         ::Sass::Plugin.on_deleting_css {|filename| puts_action :delete, :yellow, filename}
418:         ::Sass::Plugin.on_compilation_error do |error, _, _|
419:           raise error unless error.is_a?(::Sass::SyntaxError)
420:           puts_action :error, :red, "#{error.sass_filename} (Line #{error.sass_line}: #{error.message})"
421:         end
422: 
423:         if @options[:update]
424:           ::Sass::Plugin.update_stylesheets(files)
425:           return
426:         end
427: 
428:         puts ">>> Sass is watching for changes. Press Ctrl-C to stop."
429: 
430:         ::Sass::Plugin.on_template_modified {|template| puts ">>> Change detected to: #{template}"}
431:         ::Sass::Plugin.on_template_created {|template| puts ">>> New template detected: #{template}"}
432:         ::Sass::Plugin.on_template_deleted {|template| puts ">>> Deleted template detected: #{template}"}
433: 
434:         ::Sass::Plugin.watch(files)
435:       end

[Validate]