Skip to content

Commit da1cda4

Browse files
committed
implement glob importing
1 parent 980a80b commit da1cda4

File tree

5 files changed

+100
-44
lines changed

5 files changed

+100
-44
lines changed

lib/sassc/rails/importer.rb

Lines changed: 58 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def initialize(postfix=nil)
1010
@postfix = postfix
1111
end
1212

13-
def import_for(original_path, parent_path, full_path, options)
13+
def import_for(full_path, parent_dir, options)
1414
SassC::Importer::Import.new(full_path)
1515
end
1616
end
@@ -20,7 +20,7 @@ def postfix
2020
".css"
2121
end
2222

23-
def import_for(original_path, parent_path, full_path, options)
23+
def import_for(full_path, parent_dir, options)
2424
import_path = full_path.gsub(/\.css$/,"")
2525
SassC::Importer::Import.new(import_path)
2626
end
@@ -31,7 +31,7 @@ def postfix
3131
".css.scss"
3232
end
3333

34-
def import_for(original_path, parent_path, full_path, options)
34+
def import_for(full_path, parent_dir, options)
3535
source = File.open(full_path, 'rb') { |f| f.read }
3636
SassC::Importer::Import.new(full_path, source: source)
3737
end
@@ -42,7 +42,7 @@ def postfix
4242
".css.sass"
4343
end
4444

45-
def import_for(original_path, parent_path, full_path, options)
45+
def import_for(full_path, parent_dir, options)
4646
sass = File.open(full_path, 'rb') { |f| f.read }
4747
parsed_scss = SassC::Sass2Scss.convert(sass)
4848
SassC::Importer::Import.new(full_path, source: parsed_scss)
@@ -54,7 +54,7 @@ def postfix
5454
".sass.erb"
5555
end
5656

57-
def import_for(original_path, parent_path, full_path, options)
57+
def import_for(full_path, parent_dir, options)
5858
template = Tilt::ERBTemplate.new(full_path)
5959
parsed_erb = template.render(options[:sprockets][:context], {})
6060
parsed_scss = SassC::Sass2Scss.convert(parsed_erb)
@@ -63,30 +63,37 @@ def import_for(original_path, parent_path, full_path, options)
6363
end
6464

6565
class ERBExtension < Extension
66-
def import_for(original_path, parent_path, full_path, options)
66+
def import_for(full_path, parent_dir, options)
6767
template = Tilt::ERBTemplate.new(full_path)
6868
parsed_erb = template.render(options[:sprockets][:context], {})
6969
SassC::Importer::Import.new(full_path, source: parsed_erb)
7070
end
7171
end
7272

7373
EXTENSIONS = [
74+
CssScssExtension.new,
75+
CssSassExtension.new,
7476
Extension.new(".scss"),
7577
Extension.new(".sass"),
7678
CSSExtension.new,
7779
ERBExtension.new(".scss.erb"),
7880
ERBExtension.new(".css.erb"),
79-
SassERBExtension.new,
80-
CssScssExtension.new,
81-
CssSassExtension.new
81+
SassERBExtension.new
8282
]
8383

8484
PREFIXS = [ "", "_" ]
85+
GLOB = /(\A|\/)(\*|\*\*\/\*)\z/
8586

8687
def imports(path, parent_path)
8788
parent_dir, _ = File.split(parent_path)
8889
specified_dir, specified_file = File.split(path)
8990

91+
if m = path.match(GLOB)
92+
path = path.sub(m[0], "")
93+
base = File.expand_path(path, File.dirname(parent_path))
94+
return glob_imports(base, m[2], parent_path)
95+
end
96+
9097
search_paths = ([parent_dir] + load_paths).uniq
9198

9299
if specified_dir != "."
@@ -103,17 +110,24 @@ def imports(path, parent_path)
103110
try_path = File.join(search_path, file_name + extension.postfix)
104111
if File.exists?(try_path)
105112
record_import_as_dependency try_path
106-
return extension.import_for(path, parent_path, try_path, options)
113+
return extension.import_for(try_path, parent_dir, options)
107114
end
108115
end
109116
end
110117
end
111118

112-
Import.new(path)
119+
# TODO: Raise an error from SassC here
120+
raise ArgumentError.new("file not found: #{path}")
113121
end
114122

115123
private
116124

125+
def extension_for_file(file)
126+
EXTENSIONS.detect do |extension|
127+
file.include? extension.postfix
128+
end
129+
end
130+
117131
def record_import_as_dependency(path)
118132
context.depend_on path
119133
end
@@ -125,6 +139,39 @@ def context
125139
def load_paths
126140
options[:load_paths]
127141
end
142+
143+
def glob_imports(base, glob, current_file)
144+
files = globbed_files(base, glob)
145+
files = files.reject { |f| f == current_file }
146+
147+
files.map do |filename|
148+
record_import_as_dependency(filename)
149+
extension = extension_for_file(filename)
150+
extension.import_for(filename, base, options)
151+
end
152+
end
153+
154+
def globbed_files(base, glob)
155+
# TODO: Raise an error from SassC here
156+
raise ArgumentError unless glob == "*" || glob == "**/*"
157+
158+
extensions = EXTENSIONS.map(&:postfix)
159+
exts = extensions.map { |ext| Regexp.escape("#{ext}") }.join("|")
160+
sass_re = Regexp.compile("(#{exts})$")
161+
162+
record_import_as_dependency(base)
163+
164+
files = Dir["#{base}/#{glob}"].sort.map do |path|
165+
if File.directory?(path)
166+
record_import_as_dependency(path)
167+
nil
168+
elsif sass_re =~ path
169+
path
170+
end
171+
end
172+
173+
files.compact
174+
end
128175
end
129176
end
130177
end

sassc-rails.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Gem::Specification.new do |spec|
2626
# unfortunately we require sass for now, so that we can
2727
# reuse portions of the sprockets template
2828
spec.add_dependency 'sass'
29-
spec.add_dependency "sassc", "1.1.1"
29+
spec.add_dependency "sassc", "~> 1.1.2"
3030

3131
spec.add_dependency "tilt"
3232

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@import "globbed/**/*";

test/dummy/app/assets/stylesheets/imports_test.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
@import "partials/css_sass_import";
22
@import "partials/sass_import";
33
@import "partials/scss_import";
4-
// //@import "globbed/**/*";
4+
@import "globbed/**/*";
55
@import "subfolder/plain";
66
@import "subfolder/second_level";
77
@import "partials/without_css_ext";

test/sassc_rails_test.rb

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,8 @@ def test_sass_imports_work_correctly
133133

134134
assert_match /default-old-css/, css_output
135135

136-
# skip for now
137-
# assert_match /globbed/, css_output
138-
# assert_match /nested-glob/, css_output
136+
assert_match /globbed/, css_output
137+
assert_match /nested-glob/, css_output
139138
end
140139

141140
def test_style_config_item_is_defaulted_to_expanded_in_development_mode
@@ -198,41 +197,50 @@ def test_compression_works
198197
# assert_match /\.import-css-application/, css_output
199198
#end
200199

201-
#test 'globbed imports work when new file is added' do
202-
# skip
200+
def test_globbed_imports_work_when_globbed_file_is_changed
201+
skip "This seems to work in practice, possible test setup problem"
203202

204-
# project = 'scss_project'
205-
# filename = 'application.scss'
203+
begin
204+
initialize!
206205

207-
# within_rails_app(project) do |tmpdir|
208-
# asset_output(filename)
206+
new_file = File.join(File.dirname(__FILE__), 'dummy', 'app', 'assets', 'stylesheets', 'globbed', 'new_glob.scss')
209207

210-
# new_file = File.join(tmpdir, 'app', 'assets', 'stylesheets', 'globbed', 'new.scss')
211-
# File.open(new_file, 'w') do |file|
212-
# file.puts '.new-file-test { color: #000; }'
213-
# end
208+
File.open(new_file, 'w') do |file|
209+
file.puts '.new-file-test { color: #000; }'
210+
end
214211

215-
# css_output = asset_output(filename)
216-
# assert_match /new-file-test/, css_output
217-
# end
218-
#end
212+
css_output = render_asset("glob_test.scss")
213+
assert_match /new-file-test/, css_output
219214

220-
#test 'globbed imports work when globbed file is changed' do
221-
# skip
215+
File.open(new_file, 'w') do |file|
216+
file.puts '.changed-file-test { color: #000; }'
217+
end
222218

223-
# project = 'scss_project'
224-
# filename = 'application.scss'
219+
new_css_output = render_asset("glob_test.scss")
220+
assert_match /changed-file-test/, new_css_output
221+
refute_equal css_output, new_css_output
222+
ensure
223+
File.delete(new_file)
224+
end
225+
end
225226

226-
# within_rails_app(project) do |tmpdir|
227-
# asset_output(filename)
227+
def test_globbed_imports_work_when_globbed_file_is_added
228+
begin
229+
initialize!
228230

229-
# new_file = File.join(tmpdir, 'app', 'assets', 'stylesheets', 'globbed', 'globbed.scss')
230-
# File.open(new_file, 'w') do |file|
231-
# file.puts '.changed-file-test { color: #000; }'
232-
# end
231+
css_output = render_asset("glob_test.scss")
232+
refute_match /changed-file-test/, css_output
233+
new_file = File.join(File.dirname(__FILE__), 'dummy', 'app', 'assets', 'stylesheets', 'globbed', 'new_glob.scss')
233234

234-
# css_output = asset_output(filename)
235-
# assert_match /changed-file-test/, css_output
236-
# end
237-
#end
235+
File.open(new_file, 'w') do |file|
236+
file.puts '.changed-file-test { color: #000; }'
237+
end
238+
239+
new_css_output = render_asset("glob_test.scss")
240+
assert_match /changed-file-test/, new_css_output
241+
refute_equal css_output, new_css_output
242+
ensure
243+
File.delete(new_file)
244+
end
245+
end
238246
end

0 commit comments

Comments
 (0)