Class: Less::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/less/command.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Command

Returns a new instance of Command.



3
4
5
6
# File 'lib/less/command.rb', line 3

def initialize options
  @source, @destination = options[:source], options[:destination]
  @options = options
end

Instance Method Details

#compile(new = false) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/less/command.rb', line 49

def compile new = false
  begin
    # Create a new Less object with the contents of a file
    css = Less::Engine.new( File.read( @source ) ).to_css @options[:inheritance]
    css = css.delete " \n" if compress?
    
    File.open( @destination, "w" ) do |file|
      file.write css
    end
    puts "#{new ? '* [Created]' : ' [Updated]'} #{@destination.split('/').last}" if watch?
  rescue Errno::ENOENT => e
    abort "#{e}"
  rescue SyntaxError
    error = debug?? $! : $!.message.split("\n")[1..-1].collect {|e| 
      e.gsub(/\(eval\)\:(\d+)\:\s/, 'line \1: ') 
    } * "\n"
    err "errors were found in the .less file! \n#{error}\n"
  rescue MixedUnitsError => e
    err "`#{e}` you're  mixing units together! What do you expect?\n"
  rescue PathError => e
    err "`#{e}` was not found.\n"
  else
    true
  end
end

#compress?Boolean

Returns:



9
# File 'lib/less/command.rb', line 9

def compress?() @options[:compress] end

#debug?Boolean

Returns:



10
# File 'lib/less/command.rb', line 10

def debug?()    @options[:debug]    end

#err(s = '') ⇒ Object



80
81
82
# File 'lib/less/command.rb', line 80

def err s = ''
  print "!! #{s}"
end

#log(s = '') ⇒ Object

Just a logging function to avoid typing ‘}’



76
77
78
# File 'lib/less/command.rb', line 76

def log s = ''
  print '* ' + s.to_s
end

#run!Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/less/command.rb', line 23

def run!
  compile(true) unless File.exist? @destination
  
  if watch?
    log "Watching for changes in #@source ...Ctrl-C to abort.\n"
  
    # Main watch loop
    loop do
      watch { sleep 1 }
      
      # File has changed
      if File.stat( @source ).mtime > File.stat( @destination ).mtime
        log "Change detected... "
        
        # Loop until error is fixed
        until compile
          log "Press [enter] to continue..."
          watch { $stdin.gets }
        end
      end
    end
  else
    compile
  end
end

#watch(&block) ⇒ Object

little function which allows us to Ctrl-C exit inside the passed block



14
15
16
17
18
19
20
21
# File 'lib/less/command.rb', line 14

def watch &block
  begin
    block.call
  rescue Interrupt
    puts
    exit 0
  end
end

#watch?Boolean

Returns:



8
# File 'lib/less/command.rb', line 8

def watch?()    @options[:watch]    end