Skip to content

Commit 764ef01

Browse files
authored
Merge pull request #264 from rails/3.x-deprecations
Deprecation Branch for 3.X
2 parents 5988d7a + 5a0b19d commit 764ef01

16 files changed

+271
-55
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ doc
66
docs
77
pkg
88
tmp
9+
.DS_Store

lib/sprockets.rb

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
require 'sprockets/environment'
55
require 'sprockets/errors'
66
require 'sprockets/manifest'
7+
require 'sprockets/deprecation'
78

89
module Sprockets
910
require 'sprockets/processor_utils'
@@ -122,25 +123,31 @@ module Sprockets
122123

123124
# Mmm, CoffeeScript
124125
require 'sprockets/coffee_script_processor'
125-
register_engine '.coffee', CoffeeScriptProcessor, mime_type: 'application/javascript'
126+
Deprecation.silence do
127+
register_engine '.coffee', CoffeeScriptProcessor, mime_type: 'application/javascript', silence_deprecation: true
128+
end
126129

127130
# JST engines
128131
require 'sprockets/eco_processor'
129132
require 'sprockets/ejs_processor'
130133
require 'sprockets/jst_processor'
131-
register_engine '.jst', JstProcessor, mime_type: 'application/javascript'
132-
register_engine '.eco', EcoProcessor, mime_type: 'application/javascript'
133-
register_engine '.ejs', EjsProcessor, mime_type: 'application/javascript'
134+
Deprecation.silence do
135+
register_engine '.jst', JstProcessor, mime_type: 'application/javascript', silence_deprecation: true
136+
register_engine '.eco', EcoProcessor, mime_type: 'application/javascript', silence_deprecation: true
137+
register_engine '.ejs', EjsProcessor, mime_type: 'application/javascript', silence_deprecation: true
138+
end
134139

135140
# CSS engines
136141
require 'sprockets/sass_processor'
137-
register_engine '.sass', SassProcessor, mime_type: 'text/css'
138-
register_engine '.scss', ScssProcessor, mime_type: 'text/css'
142+
Deprecation.silence do
143+
register_engine '.sass', SassProcessor, mime_type: 'text/css', silence_deprecation: true
144+
register_engine '.scss', ScssProcessor, mime_type: 'text/css', silence_deprecation: true
145+
end
139146
register_bundle_metadata_reducer 'text/css', :sass_dependencies, Set.new, :+
140147

141148
# Other
142149
require 'sprockets/erb_processor'
143-
register_engine '.erb', ERBProcessor, mime_type: 'text/plain'
150+
register_engine '.erb', ERBProcessor, mime_type: 'text/plain', silence_deprecation: true
144151

145152
register_dependency_resolver 'environment-version' do |env|
146153
env.version

lib/sprockets/coffee_script_template.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,16 @@
22

33
module Sprockets
44
# Deprecated
5-
CoffeeScriptTemplate = CoffeeScriptProcessor
5+
module CoffeeScriptTemplate
6+
VERSION = CoffeeScriptProcessor::VERSION
7+
8+
def self.cache_key
9+
CoffeeScriptProcessor.cache_key
10+
end
11+
12+
def self.call(*args)
13+
Deprecation.new.warn "CoffeeScriptTemplate is deprecated please use CoffeeScriptProcessor instead"
14+
CoffeeScriptProcessor.call(*args)
15+
end
16+
end
617
end

lib/sprockets/deprecation.rb

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
module Sprockets
2+
class Deprecation
3+
THREAD_LOCAL__SILENCE_KEY = "_sprockets_deprecation_silence".freeze
4+
DEFAULT_BEHAVIORS = {
5+
raise: ->(message, callstack) {
6+
e = DeprecationException.new(message)
7+
e.set_backtrace(callstack.map(&:to_s))
8+
raise e
9+
},
10+
11+
stderr: ->(message, callstack) {
12+
$stderr.puts(message)
13+
},
14+
}
15+
16+
attr_reader :callstack
17+
18+
def self.silence(&block)
19+
Thread.current[THREAD_LOCAL__SILENCE_KEY] = true
20+
block.call
21+
ensure
22+
Thread.current[THREAD_LOCAL__SILENCE_KEY] = false
23+
end
24+
25+
def initialize(callstack = nil)
26+
@callstack = callstack || caller(2)
27+
end
28+
29+
def warn(message)
30+
return if Thread.current[THREAD_LOCAL__SILENCE_KEY]
31+
deprecation_message(message).tap do |m|
32+
behavior.each { |b| b.call(m, callstack) }
33+
end
34+
end
35+
36+
private
37+
def behavior
38+
@behavior ||= [DEFAULT_BEHAVIORS[:stderr]]
39+
end
40+
41+
def behavior=(behavior)
42+
@behavior = Array(behavior).map { |b| DEFAULT_BEHAVIORS[b] || b }
43+
end
44+
45+
def deprecation_message(message = nil)
46+
message ||= "You are using deprecated behavior which will be removed from the next major or minor release."
47+
"DEPRECATION WARNING: #{message} #{ deprecation_caller_message }"
48+
end
49+
50+
def deprecation_caller_message
51+
file, line, method = extract_callstack
52+
if file
53+
if line && method
54+
"(called from #{method} at #{file}:#{line})"
55+
else
56+
"(called from #{file}:#{line})"
57+
end
58+
end
59+
end
60+
61+
SPROCKETS_GEM_ROOT = File.expand_path("../../../../..", __FILE__) + "/"
62+
63+
def ignored_callstack(path)
64+
path.start_with?(SPROCKETS_GEM_ROOT) || path.start_with?(RbConfig::CONFIG['rubylibdir'])
65+
end
66+
67+
def extract_callstack
68+
return _extract_callstack if callstack.first.is_a? String
69+
70+
offending_line = callstack.find { |frame|
71+
frame.absolute_path && !ignored_callstack(frame.absolute_path)
72+
} || callstack.first
73+
74+
[offending_line.path, offending_line.lineno, offending_line.label]
75+
end
76+
77+
def _extract_callstack
78+
offending_line = callstack.find { |line| !ignored_callstack(line) } || callstack.first
79+
80+
if offending_line
81+
if md = offending_line.match(/^(.+?):(\d+)(?::in `(.*?)')?/)
82+
md.captures
83+
else
84+
offending_line
85+
end
86+
end
87+
end
88+
end
89+
private_constant :Deprecation
90+
end

lib/sprockets/eco_template.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,16 @@
22

33
module Sprockets
44
# Deprecated
5-
EcoTemplate = EcoProcessor
5+
module EcoTemplate
6+
VERSION = EcoProcessor::VERSION
7+
8+
def self.cache_key
9+
EcoProcessor.cache_key
10+
end
11+
12+
def self.call(*args)
13+
Deprecation.new.warn "EcoTemplate is deprecated please use EcoProcessor instead"
14+
EcoProcessor.call(*args)
15+
end
16+
end
617
end

lib/sprockets/ejs_template.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,16 @@
22

33
module Sprockets
44
# Deprecated
5-
EjsTemplate = EjsProcessor
5+
module EjsTemplate
6+
VERSION = EjsProcessor::VERSION
7+
8+
def self.cache_key
9+
EjsProcessor.cache_key
10+
end
11+
12+
def self.call(*args)
13+
Deprecation.new.warn "EjsTemplate is deprecated please use EjsProcessor instead"
14+
EjsProcessor.call(*args)
15+
end
16+
end
617
end

lib/sprockets/engines.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,17 @@ def engine_mime_types
5151
# environment.register_engine '.coffee', CoffeeScriptProcessor
5252
#
5353
def register_engine(ext, klass, options = {})
54+
unless options[:silence_deprecation]
55+
msg = <<-MSG
56+
Sprockets method `register_engine` is deprecated.
57+
Please register a mime type using `register_mime_type` then
58+
use `register_compressor` or `register_transformer`.
59+
https://github.com/rails/sprockets/blob/master/guides/extending_sprockets.md#supporting-all-versions-of-sprockets-in-processors
60+
MSG
61+
62+
Deprecation.new([caller.first]).warn(msg)
63+
end
64+
5465
ext = Sprockets::Utils.normalize_extension(ext)
5566

5667
self.computed_config = {}

lib/sprockets/erb_template.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,10 @@
22

33
module Sprockets
44
# Deprecated
5-
ERBTemplate = ERBProcessor
5+
class ERBTemplate < ERBProcessor
6+
def call(*args)
7+
Deprecation.new.warn "ERBTemplate is deprecated please use ERBProcessor instead"
8+
super
9+
end
10+
end
611
end

lib/sprockets/processing.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,14 +231,24 @@ def unregister_config_processor(type, mime_type, klass)
231231
compute_transformers!
232232
end
233233

234+
def deprecate_legacy_processor_interface(interface)
235+
msg = "You are using the a deprecated processor interface #{ interface.inspect }.\n" +
236+
"Please update your processor interface:\n" +
237+
"https://github.com/rails/sprockets/blob/master/guides/extending_sprockets.md#supporting-all-versions-of-sprockets-in-processors\n"
238+
239+
Deprecation.new([caller[3]]).warn msg
240+
end
241+
234242
def wrap_processor(klass, proc)
235243
if !proc
236244
if klass.respond_to?(:call)
237245
klass
238246
else
247+
deprecate_legacy_processor_interface(klass)
239248
LegacyTiltProcessor.new(klass)
240249
end
241250
elsif proc.respond_to?(:arity) && proc.arity == 2
251+
deprecate_legacy_processor_interface(proc)
242252
LegacyProcProcessor.new(klass.to_s, proc)
243253
else
244254
proc

lib/sprockets/sass_cache_store.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,10 @@ def path_to(key)
2525
end
2626

2727
# Deprecated: Use Sprockets::SassProcessor::CacheStore instead.
28-
SassCacheStore = SassProcessor::CacheStore
28+
class SassCacheStore < SassProcessor::CacheStore
29+
def initialize(*args)
30+
Deprecation.new.warn "SassCacheStore is deprecated please use SassProcessor::CacheStore instead"
31+
super
32+
end
33+
end
2934
end

0 commit comments

Comments
 (0)