compass-rails 1.0.0.rc.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. data/.gitignore +18 -0
  2. data/.travis.yml +5 -0
  3. data/Appraisals +17 -0
  4. data/Gemfile +22 -0
  5. data/Guardfile +9 -0
  6. data/LICENSE +22 -0
  7. data/README.md +191 -0
  8. data/Rakefile +34 -0
  9. data/compass-rails.gemspec +21 -0
  10. data/gemfiles/rails2.gemfile +21 -0
  11. data/gemfiles/rails2.gemfile.lock +87 -0
  12. data/gemfiles/rails3.gemfile +21 -0
  13. data/gemfiles/rails3.gemfile.lock +134 -0
  14. data/gemfiles/rails31.gemfile +22 -0
  15. data/gemfiles/rails31.gemfile.lock +151 -0
  16. data/gemfiles/rails32.gemfile +22 -0
  17. data/gemfiles/rails32.gemfile.lock +148 -0
  18. data/lib/compass-rails.rb +207 -0
  19. data/lib/compass-rails/configuration.rb +1 -0
  20. data/lib/compass-rails/configuration/3_1.rb +51 -0
  21. data/lib/compass-rails/configuration/default.rb +75 -0
  22. data/lib/compass-rails/installer.rb +30 -0
  23. data/lib/compass-rails/patches.rb +6 -0
  24. data/lib/compass-rails/patches/3_1.rb +28 -0
  25. data/lib/compass-rails/patches/importer.rb +26 -0
  26. data/lib/compass-rails/patches/sprite_importer.rb +38 -0
  27. data/lib/compass-rails/railties.rb +11 -0
  28. data/lib/compass-rails/railties/2_3.rb +56 -0
  29. data/lib/compass-rails/railties/3_0.rb +48 -0
  30. data/lib/compass-rails/railties/3_1.rb +90 -0
  31. data/lib/compass-rails/version.rb +5 -0
  32. data/lib/compass/app_integration/rails.rb +10 -0
  33. data/test/fixtures/.gitkeep +0 -0
  34. data/test/helpers/command_helper.rb +120 -0
  35. data/test/helpers/debug_helper.rb +12 -0
  36. data/test/helpers/file_helper.rb +53 -0
  37. data/test/helpers/rails_helper.rb +65 -0
  38. data/test/helpers/rails_project.rb +186 -0
  39. data/test/integrations/.gitkeep +0 -0
  40. data/test/integrations/rails3_test.rb +37 -0
  41. data/test/integrations/rails_23_test.rb +28 -0
  42. data/test/integrations/rails_31_test.rb +47 -0
  43. data/test/integrations/rails_32_test.rb +46 -0
  44. data/test/test_helper.rb +15 -0
  45. data/test/units/.gitkeep +0 -0
  46. metadata +160 -0
@@ -0,0 +1,207 @@
1
+ require 'compass'
2
+ require "compass-rails/version"
3
+ require "compass-rails/configuration"
4
+
5
+ module CompassRails
6
+
7
+ RAILS_32 = %r{^3.2}
8
+ RAILS_31 = %r{^3.1}
9
+ RAILS_23 = %r{^2.3}
10
+ RAILS_3 = %r{^3.0}
11
+
12
+ extend self
13
+
14
+ def load_rails
15
+ return if defined?(::Rails) && !::Rails.application.nil?
16
+ rails_config_path = Dir.pwd
17
+ until File.exists?(File.join(rails_config_path, 'config', 'application.rb')) do
18
+ raise 'Rails application not found' if rails_config_path == '/'
19
+ rails_config_path = File.join(rails_config_path, '..')
20
+ end
21
+ #load the rails config
22
+ require "#{rails_config_path}/config/application.rb"
23
+ if rails31? || rails32?
24
+ require 'sass-rails'
25
+ require 'sprockets/railtie'
26
+ require 'rails/engine'
27
+ @app ||= ::Rails.application.initialize!(:assets)
28
+ end
29
+ end
30
+
31
+
32
+ def setup_fake_rails_env_paths(sprockets_env)
33
+ return unless rails_loaded?
34
+ keys = ['app/assets', 'lib/assets', 'vendor/assets']
35
+ local = keys.map {|path| ::Rails.root.join(path) }.map { |path| [File.join(path, 'images'), File.join(path, 'stylesheets')] }.flatten!
36
+ sprockets_env.send(:trail).paths.unshift(*local)
37
+ paths = []
38
+ ::Rails::Engine.subclasses.each do |subclass|
39
+ paths = subclass.paths
40
+ keys.each do |key|
41
+ sprockets_env.send(:trail).paths.unshift(*paths[key].existent_directories)
42
+ end
43
+ end
44
+ end
45
+
46
+ def sass_config
47
+ load_rails
48
+ ::Rails.application.config.sass
49
+ end
50
+
51
+ def sprockets
52
+ load_rails
53
+ @sprockets ||= ::Rails.application.assets
54
+ end
55
+
56
+ def context
57
+ load_rails
58
+ @context ||= begin
59
+ sprockets.version = ::Rails.env + "-#{sprockets.version}"
60
+ setup_fake_rails_env_paths(sprockets)
61
+ context = ::Rails.application.assets.context_class
62
+ context.extend(::Sprockets::Helpers::IsolatedHelper)
63
+ context.extend(::Sprockets::Helpers::RailsHelper)
64
+ context.extend(::Sass::Rails::Railtie::SassContext)
65
+ context.sass_config = sass_config
66
+
67
+ context
68
+ end
69
+ end
70
+
71
+ def installer(*args)
72
+ CompassRails::Installer.new(*args)
73
+ end
74
+
75
+ def rails_loaded?
76
+ defined?(::Rails)
77
+ end
78
+
79
+ def rails_version
80
+ Gem.loaded_specs["rails"].version.to_s
81
+ end
82
+
83
+ def rails3?
84
+ return false unless defined?(::Rails)
85
+ rails_version =~ RAILS_3
86
+ end
87
+
88
+ def rails31?
89
+ return false unless defined?(::Rails)
90
+ rails_version =~ RAILS_31
91
+ end
92
+
93
+ def rails32?
94
+ return false unless defined?(::Rails)
95
+ rails_version =~ RAILS_32
96
+ end
97
+
98
+ def rails2?
99
+ rails_version =~ RAILS_23
100
+ end
101
+
102
+ def booted!
103
+ CompassRails.const_set(:BOOTED, true)
104
+ end
105
+
106
+ def booted?
107
+ defined?(CompassRails::BOOTED) && CompassRails::BOOTED
108
+ end
109
+
110
+ def configuration
111
+ load_rails unless rails2?
112
+ config = Compass::Configuration::Data.new('rails')
113
+ config.extend(Configuration::Default)
114
+ if (rails31? || rails32?)
115
+ if asset_pipeline_enabled?
116
+ require "compass-rails/configuration/3_1"
117
+ config.extend(Configuration::Rails3_1)
118
+ end
119
+ end
120
+ config
121
+ end
122
+
123
+ def env
124
+ env_production? ? :production : :development
125
+ end
126
+
127
+ def prefix
128
+ ::Rails.application.config.assets.prefix
129
+ end
130
+
131
+ def env_production?
132
+ if defined?(::Rails) && ::Rails.respond_to?(:env)
133
+ ::Rails.env.production?
134
+ elsif defined?(RAILS_ENV)
135
+ RAILS_ENV == "production"
136
+ end
137
+ end
138
+
139
+ def root
140
+ @root ||= begin
141
+ if defined?(::Rails) && ::Rails.respond_to?(:root)
142
+ ::Rails.root
143
+ elsif defined?(RAILS_ROOT)
144
+ Pathname.new(RAILS_ROOT)
145
+ else
146
+ Pathname.new(Dir.pwd)
147
+ end
148
+ end
149
+ end
150
+
151
+ def check_for_double_boot!
152
+ if booted?
153
+ Compass::Util.compass_warn("Warning: Compass was booted twice. Compass-rails has got your back; please remove your compass initializer.")
154
+ else
155
+ booted!
156
+ end
157
+ end
158
+
159
+ def sass_plugin_enabled?
160
+ unless rails31?
161
+ defined?(Sass::Plugin) && !Sass::Plugin.options[:never_update]
162
+ end
163
+ end
164
+
165
+ # Rails 2.x projects use this in their compass initializer.
166
+ def initialize!(config = nil)
167
+ check_for_double_boot!
168
+ config ||= Compass.detect_configuration_file(root)
169
+ Compass.add_project_configuration(config, :project_type => :rails)
170
+ Compass.discover_extensions!
171
+ Compass.configure_sass_plugin!
172
+ Compass.handle_configuration_change! if sass_plugin_enabled?
173
+ end
174
+
175
+ def configure_rails!(app)
176
+ return unless app.config.respond_to?(:sass)
177
+ app.config.compass.to_sass_engine_options.each do |key, value|
178
+ app.config.sass.send(:"#{key}=", value)
179
+ end
180
+ end
181
+
182
+ def boot_config
183
+ config = if (config_file = Compass.detect_configuration_file) && (config_data = Compass.configuration_for(config_file))
184
+ config_data
185
+ else
186
+ Compass::Configuration::Data.new("compass_rails_boot")
187
+ end
188
+ config.top_level.project_type = :rails
189
+ config
190
+ end
191
+
192
+ def asset_pipeline_enabled?
193
+ return false unless rails_loaded?
194
+ rails_config = ::Rails.application.config
195
+ rails_config.respond_to?(:assets) && rails_config.assets.try(:enabled)
196
+ end
197
+
198
+ end
199
+
200
+ Compass::AppIntegration.register(:rails, "::CompassRails")
201
+ Compass.add_configuration(CompassRails.boot_config)
202
+
203
+ require "compass-rails/patches"
204
+ require "compass-rails/railties"
205
+ require "compass-rails/installer"
206
+
207
+
@@ -0,0 +1 @@
1
+ require "compass-rails/configuration/default"
@@ -0,0 +1,51 @@
1
+ module CompassRails
2
+ module Configuration
3
+ module Rails3_1
4
+
5
+ def default_images_dir
6
+ File.join("app", "assets", "images")
7
+ end
8
+
9
+ def default_fonts_dir
10
+ File.join("app", "assets", "fonts")
11
+ end
12
+
13
+ def default_javascripts_dir
14
+ File.join("app", "assets", "javascripts")
15
+ end
16
+
17
+ def default_css_dir
18
+ File.join('public', CompassRails.prefix)
19
+ end
20
+
21
+ def default_http_path
22
+ File.join(CompassRails.prefix)
23
+ end
24
+
25
+ def default_http_images_path
26
+ "#{top_level.http_path}"
27
+ end
28
+
29
+ def default_http_generated_images_path
30
+ "#{top_level.http_path}"
31
+ end
32
+
33
+ def default_http_javascripts_path
34
+ "#{top_level.http_path}"
35
+ end
36
+
37
+ def default_http_fonts_path
38
+ "#{top_level.http_path}"
39
+ end
40
+
41
+ def default_http_stylesheets_path
42
+ "#{top_level.http_path}"
43
+ end
44
+
45
+ def default_preferred_syntax
46
+ ::Rails.application.config.sass.preferred_syntax rescue nil
47
+ end
48
+
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,75 @@
1
+ module CompassRails
2
+ module Configuration
3
+ module Default
4
+
5
+ def project_type_without_default
6
+ :rails
7
+ end
8
+
9
+ def default_sass_dir
10
+ File.join("app", "assets", "stylesheets")
11
+ end
12
+
13
+ def default_css_dir
14
+ File.join("public", "stylesheets")
15
+ end
16
+
17
+ def default_images_dir
18
+ File.join("public", "images")
19
+ end
20
+
21
+ def default_fonts_dir
22
+ File.join("public", "fonts")
23
+ end
24
+
25
+ def default_javascripts_dir
26
+ File.join("public", "javascripts")
27
+ end
28
+
29
+ def default_http_images_path
30
+ # Relies on the fact that this will be loaded after the "normal"
31
+ # defaults, so that method_missing finds http_root_relative
32
+ http_root_relative "images"
33
+ end
34
+
35
+ def default_http_generated_images_path
36
+ # Relies on the fact that this will be loaded after the "normal"
37
+ # defaults, so that method_missing finds http_root_relative
38
+ http_root_relative "images"
39
+ end
40
+
41
+ def default_http_javascripts_path
42
+ http_root_relative "javascripts"
43
+ end
44
+
45
+ def default_http_fonts_path
46
+ http_root_relative "fonts"
47
+ end
48
+
49
+ def default_http_stylesheets_path
50
+ http_root_relative "stylesheets"
51
+ end
52
+
53
+ def default_extensions_dir
54
+ File.join("vendor", "plugins", "compass_extensions")
55
+ end
56
+
57
+ def default_cache_dir
58
+ File.join("tmp", "sass-cache")
59
+ end
60
+
61
+ def default_project_path
62
+ project_path = CompassRails.root
63
+ end
64
+
65
+ def default_http_path
66
+ "/" # XXX Where is/was this stored in the Rails config?
67
+ end
68
+
69
+ def default_environment
70
+ CompassRails.env
71
+ end
72
+
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,30 @@
1
+ module CompassRails
2
+ class Installer < Compass::Installers::ManifestInstaller
3
+
4
+ SASS_FILE_REGEX = %r{(.*)(?:\.css)?\.(sass|scss)}
5
+
6
+ def completed_configuration
7
+ @completed_configuration ||= CompassRails.configuration
8
+ end
9
+
10
+ def install_stylesheet(from, to, options)
11
+ if CompassRails.rails_loaded? && CompassRails.asset_pipeline_enabled?
12
+ _, name, ext = SASS_FILE_REGEX.match(to).to_a
13
+ to = "#{name}.css.#{ext}"
14
+ end
15
+ super(from, to, options)
16
+ end
17
+
18
+ def write_configuration_files
19
+ config_file = CompassRails.root.join('config', 'compass.rb')
20
+ unless config_file.exist?
21
+ write_file config_file.to_s, CompassRails.configuration.serialize
22
+ end
23
+ end
24
+
25
+ def prepare
26
+ write_configuration_files
27
+ end
28
+
29
+ end
30
+ end
@@ -0,0 +1,6 @@
1
+ if CompassRails.rails31? || CompassRails.rails32?
2
+ unless CompassRails.rails_loaded?
3
+ CompassRails.load_rails
4
+ end
5
+ require 'compass-rails/patches/importer'
6
+ end
@@ -0,0 +1,28 @@
1
+ module Sass::Script::Functions
2
+ def generated_image_url(path, only_path = nil)
3
+ asset_url(path, Sass::Script::String.new("image"))
4
+ end
5
+ end
6
+
7
+
8
+ module Compass::RailsImageFunctionPatch
9
+ private
10
+
11
+ def image_path_for_size(image_file)
12
+ begin
13
+ file = ::Rails.application.assets.find_asset(image_file)
14
+ return file
15
+ rescue ::Sprockets::FileOutsidePaths
16
+ return super(image_file)
17
+ end
18
+ end
19
+ end
20
+
21
+ module Sass::Script::Functions
22
+ include Compass::RailsImageFunctionPatch
23
+ end
24
+
25
+ # Wierd that this has to be re-included to pick up sub-modules. Ruby bug?
26
+ class Sass::Script::Functions::EvaluationContext
27
+ include Sass::Script::Functions
28
+ end
@@ -0,0 +1,26 @@
1
+ require 'compass/compiler'
2
+ module Compass
3
+ class Compiler
4
+ attr_accessor :sass_options
5
+ STYLESHEET = /stylesheet/
6
+ def sass_options
7
+ if CompassRails.asset_pipeline_enabled?
8
+ @sass_options[:custom] ||= {}
9
+ @sass_options[:custom] = {:resolver => ::Sass::Rails::Resolver.new(CompassRails.context)}
10
+ @sass_options[:load_paths] ||= []
11
+ unless @sass_options[:load_paths].any? {|k| k.is_a?(::Sass::Rails::Importer) }
12
+ ::Rails.application.assets.paths.each do |path|
13
+ next unless path.to_s =~ STYLESHEET
14
+ Dir["#{path}/**/*"].each do |pathname|
15
+ # args are: sprockets environment, the logical_path ex. 'stylesheets', and the full path name for the render
16
+ context = ::CompassRails.context.new(::Rails.application.assets, File.basename(path), Pathname.new(pathname))
17
+ @sass_options[:load_paths] << ::Sass::Rails::Importer.new(context)
18
+ end
19
+ end
20
+ end
21
+ end
22
+ @sass_options
23
+ end
24
+
25
+ end
26
+ end
@@ -0,0 +1,38 @@
1
+ require 'sprockets'
2
+ require 'compass/sprite_importer'
3
+
4
+ module Compass
5
+ class SpriteImporter < Sass::Importers::Base
6
+
7
+ alias :old_find :find
8
+
9
+ def find(uri, options)
10
+
11
+ if old = old_find(uri, options)
12
+ @_options = options
13
+ self.class.files(uri).each do |file|
14
+ if pathname = resolve(file)
15
+ context.depend_on(pathname)
16
+ end
17
+ end
18
+ end
19
+
20
+ old
21
+ end
22
+
23
+ private
24
+
25
+ def resolve(uri)
26
+ resolver.resolve(Pathname.new(uri))
27
+ end
28
+
29
+ def context
30
+ resolver.context
31
+ end
32
+
33
+ def resolver
34
+ @_options[:custom][:resolver]
35
+ end
36
+
37
+ end
38
+ end