plugin.rb 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. require 'fileutils'
  2. require 'sass'
  3. require 'sass/plugin/compiler'
  4. module Sass
  5. # This module provides a single interface to the compilation of Sass/SCSS files
  6. # for an application. It provides global options and checks whether CSS files
  7. # need to be updated.
  8. #
  9. # This module is used as the primary interface with Sass
  10. # when it's used as a plugin for various frameworks.
  11. # All Rack-enabled frameworks are supported out of the box.
  12. # The plugin is {file:SASS_REFERENCE.md#rails_merb_plugin automatically activated for Rails and Merb}.
  13. # Other frameworks must enable it explicitly; see {Sass::Plugin::Rack}.
  14. #
  15. # This module has a large set of callbacks available
  16. # to allow users to run code (such as logging) when certain things happen.
  17. # All callback methods are of the form `on_#{name}`,
  18. # and they all take a block that's called when the given action occurs.
  19. #
  20. # Note that this class proxies almost all methods to its {Sass::Plugin::Compiler} instance.
  21. # See \{#compiler}.
  22. #
  23. # @example Using a callback
  24. # Sass::Plugin.on_updating_stylesheet do |template, css|
  25. # puts "Compiling #{template} to #{css}"
  26. # end
  27. # Sass::Plugin.update_stylesheets
  28. # #=> Compiling app/sass/screen.scss to public/stylesheets/screen.css
  29. # #=> Compiling app/sass/print.scss to public/stylesheets/print.css
  30. # #=> Compiling app/sass/ie.scss to public/stylesheets/ie.css
  31. # @see Sass::Plugin::Compiler
  32. module Plugin
  33. include Sass::Util
  34. extend self
  35. @checked_for_updates = false
  36. # Whether or not Sass has **ever** checked if the stylesheets need to be updated
  37. # (in this Ruby instance).
  38. #
  39. # @return [Boolean]
  40. attr_accessor :checked_for_updates
  41. # Same as \{#update\_stylesheets}, but respects \{#checked\_for\_updates}
  42. # and the {file:SASS_REFERENCE.md#always_update-option `:always_update`}
  43. # and {file:SASS_REFERENCE.md#always_check-option `:always_check`} options.
  44. #
  45. # @see #update_stylesheets
  46. def check_for_updates
  47. return unless !Sass::Plugin.checked_for_updates ||
  48. Sass::Plugin.options[:always_update] || Sass::Plugin.options[:always_check]
  49. update_stylesheets
  50. end
  51. # Returns the singleton compiler instance.
  52. # This compiler has been pre-configured according
  53. # to the plugin configuration.
  54. #
  55. # @return [Sass::Plugin::Compiler]
  56. def compiler
  57. @compiler ||= Compiler.new
  58. end
  59. # Updates out-of-date stylesheets.
  60. #
  61. # Checks each Sass/SCSS file in {file:SASS_REFERENCE.md#template_location-option `:template_location`}
  62. # to see if it's been modified more recently than the corresponding CSS file
  63. # in {file:SASS_REFERENCE.md#css_location-option `:css_location`}.
  64. # If it has, it updates the CSS file.
  65. #
  66. # @param individual_files [Array<(String, String)>]
  67. # A list of files to check for updates
  68. # **in addition to those specified by the
  69. # {file:SASS_REFERENCE.md#template_location-option `:template_location` option}.**
  70. # The first string in each pair is the location of the Sass/SCSS file,
  71. # the second is the location of the CSS file that it should be compiled to.
  72. def update_stylesheets(individual_files = [])
  73. return if options[:never_update]
  74. compiler.update_stylesheets(individual_files)
  75. end
  76. # Updates all stylesheets, even those that aren't out-of-date.
  77. # Ignores the cache.
  78. #
  79. # @param individual_files [Array<(String, String)>]
  80. # A list of files to check for updates
  81. # **in addition to those specified by the
  82. # {file:SASS_REFERENCE.md#template_location-option `:template_location` option}.**
  83. # The first string in each pair is the location of the Sass/SCSS file,
  84. # the second is the location of the CSS file that it should be compiled to.
  85. # @see #update_stylesheets
  86. def force_update_stylesheets(individual_files = [])
  87. Compiler.new(options.dup.merge(
  88. :never_update => false,
  89. :always_update => true,
  90. :cache => false)).update_stylesheets(individual_files)
  91. end
  92. # All other method invocations are proxied to the \{#compiler}.
  93. #
  94. # @see #compiler
  95. # @see Sass::Plugin::Compiler
  96. def method_missing(method, *args, &block)
  97. if compiler.respond_to?(method)
  98. compiler.send(method, *args, &block)
  99. else
  100. super
  101. end
  102. end
  103. # For parity with method_missing
  104. def respond_to?(method)
  105. super || compiler.respond_to?(method)
  106. end
  107. # There's a small speedup by not using method missing for frequently delegated methods.
  108. def options
  109. compiler.options
  110. end
  111. end
  112. end
  113. if defined?(ActionController)
  114. require 'sass/plugin/rails'
  115. elsif defined?(Merb::Plugins)
  116. require 'sass/plugin/merb'
  117. else
  118. require 'sass/plugin/generic'
  119. end