Class: Less::Command

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Command

Returns a new instance of Command.



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

def initialize options
  $verbose = options[:debug]
  @source = options[:source]
  @destination = (options[:destination] || options[:source]).gsub /\.(less|lss)/, '.css'
  @options = options
end

Instance Attribute Details

#destinationObject

Returns the value of attribute destination.



11
12
13
# File 'lib/less/command.rb', line 11

def destination
  @destination
end

#optionsObject

Returns the value of attribute options.



11
12
13
# File 'lib/less/command.rb', line 11

def options
  @options
end

#sourceObject

Returns the value of attribute source.



11
12
13
# File 'lib/less/command.rb', line 11

def source
  @source
end

Instance Method Details

#compress?Boolean

Returns:

  • (Boolean)


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

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

#debug?Boolean

Returns:

  • (Boolean)


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

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

#err(s = '', type = '') ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
# File 'lib/less/command.rb', line 94

def err s = '', type = ''
  type = type.strip + ' ' unless type.empty?
  print "#{RED["! #{type}Error"]}: #{s}"
  if @options[:growl]
    growl = Growl.new
    growl.title = "LESS"
    growl.message = "#{type}Error in #@source!"
    growl.run
    false
  end
end

#log(s = '') ⇒ Object

Just a logging function to avoid typing ‘*’



90
91
92
# File 'lib/less/command.rb', line 90

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

#parse(new = false) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/less/command.rb', line 61

def parse new = false
  begin
    # Create a new Less object with the contents of a file
    css = Less::Engine.new(File.new(@source)).to_css
    css = css.delete " \n" if compress?

    File.open( @destination, "w" ) do |file|
      file.write css
    end
    print "#{GREEN['* ' + (new ? 'Created'  : 'Updated')]} " + 
          "#{@destination.split('/').last}\n: " if watch?
  rescue Errno::ENOENT => e
    abort "#{e}"
  rescue SyntaxError => e
    err "#{e}\n", "Syntax"
  rescue MixedUnitsError => e
    err "`#{e}` you're  mixing units together! What do you expect?\n"
  rescue PathError => e
    err "`#{e}` was not found.\n", "Path"
  rescue VariableNameError => e
    err "#{YELLOW[e]} is undefined.\n", "Name"
  rescue MixinNameError => e
    err "#{YELLOW[e]} is undefined.\n", "Name"
  else
    true
  end
end

#run!Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/less/command.rb', line 35

def run!
  parse(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
        print "Change detected... "

        # Loop until error is fixed
        until parse
          log "Press [return] to continue..."
          watch { $stdin.gets }
        end
      end
    end
  else
    parse
  end
end

#watchObject

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



26
27
28
29
30
31
32
33
# File 'lib/less/command.rb', line 26

def watch
  begin
    yield
  rescue Interrupt
    puts
    exit 0
  end
end

#watch?Boolean

Returns:

  • (Boolean)


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

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