Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module LessCompiler
- class Middleware
- def initialize(app)
- @app = Rack::Static.new(app, :urls => %w(/stylesheets), :root => 'tmp')
- @compiler = Compiler.instance
- end
- def call(env)
- maybe_compile_less(env)
- @app.call(env)
- end
- private
- def maybe_compile_less(env)
- path = env['PATH_INFO']
- return unless path =~ %r(^/stylesheets/(.*)\.css$)
- @compiler.maybe_compile($1)
- end
- end
- class Compiler
- include Singleton
- #
- # path should be something like "global/nav" for "/global/nav.css"
- #
- def maybe_compile(path)
- less_path = File.join(Rails.root, 'app', 'stylesheets', "#{path}.less")
- css_path = File.join(Rails.root, 'tmp', 'stylesheets', "#{path}.css")
- unless !File.exist?(less_path) || File.exist?(css_path) && File.mtime(less_path) <= File.mtime(css_path)
- Rails.logger.debug "Recompiling #{less_path} => #{css_path}"
- FileUtils.mkdir_p(File.dirname(css_path))
- Less::Command.new(:source => less_path, :destination => css_path).run!
- end
- end
- end
- end
Add Comment
Please, Sign In to add comment