Guest User

Untitled

a guest
Jun 21st, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. module LessCompiler
  2. class Middleware
  3. def initialize(app)
  4. @app = Rack::Static.new(app, :urls => %w(/stylesheets), :root => 'tmp')
  5. @compiler = Compiler.instance
  6. end
  7.  
  8. def call(env)
  9. maybe_compile_less(env)
  10. @app.call(env)
  11. end
  12.  
  13. private
  14.  
  15. def maybe_compile_less(env)
  16. path = env['PATH_INFO']
  17. return unless path =~ %r(^/stylesheets/(.*)\.css$)
  18. @compiler.maybe_compile($1)
  19. end
  20. end
  21.  
  22. class Compiler
  23. include Singleton
  24.  
  25. #
  26. # path should be something like "global/nav" for "/global/nav.css"
  27. #
  28. def maybe_compile(path)
  29. less_path = File.join(Rails.root, 'app', 'stylesheets', "#{path}.less")
  30. css_path = File.join(Rails.root, 'tmp', 'stylesheets', "#{path}.css")
  31. unless !File.exist?(less_path) || File.exist?(css_path) && File.mtime(less_path) <= File.mtime(css_path)
  32. Rails.logger.debug "Recompiling #{less_path} => #{css_path}"
  33. FileUtils.mkdir_p(File.dirname(css_path))
  34. Less::Command.new(:source => less_path, :destination => css_path).run!
  35. end
  36. end
  37. end
  38. end
Add Comment
Please, Sign In to add comment