Sha256: 1af26e2a6d50f4ab64cf2035c7d297594899cf5a5c372280d119d1cc94175d3b

Contents?: true

Size: 1.87 KB

Versions: 3

Compression:

Stored size: 1.87 KB

Contents

module Less
  class Command
    def initialize options
      @source, @destination = options[:source], options[:destination]
      @options = options
    end
    
    def watch?()    @options[:watch]    end
    def compress?() @options[:compress] end
    
    # little function which allows us to 
    # Ctrl-C exit inside the passed block
    def watch &block
      begin
        block.call
      rescue Interrupt
        puts
        exit 0
      end
    end
      
    def run!
      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
            while not compile
              log "Press [enter] to continue..."
              watch { $stdin.gets }
            end
          end
        end
      else
        compile
      end
    end
    
    def compile
      begin
        # Create a new Less object with the contents of a file
        css = Less::Engine.new( File.read( @source ) ).to_css
        css = css.delete " \n" if compress?
        
        File.open( @destination, "w" ) do |file|
          file.write css
        end
        puts " [Updated] #{@destination.split('/').last}" if watch?
      rescue Errno::ENOENT => e
        abort "#{e}"
      rescue SyntaxError
        error = $!.message.split("\n")[1..-1].collect {|e| e.gsub(/\(eval\)\:\d+\:\s/, '') } * "\n"
        log " !! errors were found in the .less file! \n#{error}\n"
      rescue MixedUnitsError
        log "!! You're  mixing units together! what do you expect?\n"
      else
        true
      end
    end
    
    # Just a logging function to avoid typing '}'
    def log s = ''
      print '} ' + s.to_s
    end
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
cloudhead-less-0.8.0 lib/less/command.rb
less-0.8.0 lib/less/command.rb
less-0.8.1 lib/less/command.rb