sprockets-foo 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +1 -0
- data/VERSION +1 -1
- data/ext/sprockets_with_directives/lib/sprockets.rb +46 -0
- data/ext/sprockets_with_directives/lib/sprockets/concatenation.rb +38 -0
- data/ext/sprockets_with_directives/lib/sprockets/directive.rb +56 -0
- data/ext/sprockets_with_directives/lib/sprockets/directives.rb +15 -0
- data/ext/sprockets_with_directives/lib/sprockets/directives/provide_directive.rb +25 -0
- data/ext/sprockets_with_directives/lib/sprockets/directives/relative_require_directive.rb +18 -0
- data/ext/sprockets_with_directives/lib/sprockets/directives/require_directive.rb +34 -0
- data/ext/sprockets_with_directives/lib/sprockets/environment.rb +52 -0
- data/ext/sprockets_with_directives/lib/sprockets/error.rb +5 -0
- data/ext/sprockets_with_directives/lib/sprockets/pathname.rb +37 -0
- data/ext/sprockets_with_directives/lib/sprockets/preprocessor.rb +138 -0
- data/ext/sprockets_with_directives/lib/sprockets/secretary.rb +107 -0
- data/ext/sprockets_with_directives/lib/sprockets/source_file.rb +32 -0
- data/ext/sprockets_with_directives/lib/sprockets/version.rb +9 -0
- data/ext/sprockets_with_directives/test/test_concatenation.rb +28 -0
- data/ext/sprockets_with_directives/test/test_environment.rb +64 -0
- data/ext/sprockets_with_directives/test/test_helper.rb +51 -0
- data/ext/sprockets_with_directives/test/test_pathname.rb +43 -0
- data/ext/sprockets_with_directives/test/test_preprocessor.rb +107 -0
- data/ext/sprockets_with_directives/test/test_secretary.rb +89 -0
- data/ext/sprockets_with_directives/test/test_source_file.rb +33 -0
- data/ext/sprockets_with_directives/test/test_source_line.rb +89 -0
- data/sprockets-foo.gemspec +84 -0
- data/test/{test_preprocessor.rb → test_template_directive.rb} +0 -0
- metadata +27 -4
data/Rakefile
CHANGED
@@ -12,6 +12,7 @@ begin
|
|
12
12
|
gem.authors = ["Ben Curren"]
|
13
13
|
gem.add_dependency "ejs-rcompiler", ">= 0.1.1"
|
14
14
|
# gem.add_dependency "sprockets", ">= " # use when sprockets directive branch is merged
|
15
|
+
Dir.glob('ext/**/*.rb').each { |f| gem.files << f }
|
15
16
|
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
16
17
|
end
|
17
18
|
Jeweler::GemcutterTasks.new
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.2
|
@@ -0,0 +1,46 @@
|
|
1
|
+
$:.unshift File.dirname(__FILE__)
|
2
|
+
|
3
|
+
require "yaml"
|
4
|
+
require "fileutils"
|
5
|
+
|
6
|
+
module Sprockets
|
7
|
+
class << self
|
8
|
+
def running_on_windows?
|
9
|
+
RUBY_PLATFORM =~ /(win|w)32$/
|
10
|
+
end
|
11
|
+
|
12
|
+
def absolute?(location)
|
13
|
+
same_when_expanded?(location) || platform_absolute_path?(location)
|
14
|
+
end
|
15
|
+
|
16
|
+
protected
|
17
|
+
def same_when_expanded?(location)
|
18
|
+
location[0, 1] == File.expand_path(location)[0, 1]
|
19
|
+
end
|
20
|
+
|
21
|
+
def platform_absolute_path?(location)
|
22
|
+
false
|
23
|
+
end
|
24
|
+
|
25
|
+
if Sprockets.running_on_windows?
|
26
|
+
def platform_absolute_path?(location)
|
27
|
+
location[0, 1] == File::SEPARATOR && File.expand_path(location) =~ /[A-Za-z]:[\/\\]/
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
require "sprockets/concatenation"
|
34
|
+
require "sprockets/directive"
|
35
|
+
require "sprockets/directives"
|
36
|
+
require "sprockets/directives/provide_directive"
|
37
|
+
require "sprockets/directives/require_directive"
|
38
|
+
require "sprockets/directives/relative_require_directive"
|
39
|
+
require "sprockets/error"
|
40
|
+
require "sprockets/environment"
|
41
|
+
require "sprockets/pathname"
|
42
|
+
require "sprockets/preprocessor"
|
43
|
+
require "sprockets/source_file"
|
44
|
+
require "sprockets/secretary"
|
45
|
+
require "sprockets/version"
|
46
|
+
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module Sprockets
|
2
|
+
class Concatenation
|
3
|
+
attr_reader :lines
|
4
|
+
|
5
|
+
def initialize
|
6
|
+
@lines = []
|
7
|
+
@source_files_and_numbers = []
|
8
|
+
@source_file_mtimes = {}
|
9
|
+
end
|
10
|
+
|
11
|
+
def record(source_file, line, number)
|
12
|
+
@lines << line
|
13
|
+
@source_files_and_numbers << [source_file, number]
|
14
|
+
record_mtime_for(source_file)
|
15
|
+
line
|
16
|
+
end
|
17
|
+
|
18
|
+
def to_s
|
19
|
+
lines.join
|
20
|
+
end
|
21
|
+
|
22
|
+
def mtime
|
23
|
+
@source_file_mtimes.values.max
|
24
|
+
end
|
25
|
+
|
26
|
+
def save_to(filename)
|
27
|
+
timestamp = mtime
|
28
|
+
File.open(filename, "w") { |file| file.write(to_s) }
|
29
|
+
File.utime(timestamp, timestamp, filename)
|
30
|
+
true
|
31
|
+
end
|
32
|
+
|
33
|
+
protected
|
34
|
+
def record_mtime_for(source_file)
|
35
|
+
@source_file_mtimes[source_file] ||= source_file.mtime
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
module Sprockets
|
2
|
+
class Directive
|
3
|
+
attr_reader :source_file, :line, :number, :name, :argument
|
4
|
+
|
5
|
+
QUOTED_STRING = /(?:"([^"]+|\\"+)*")/
|
6
|
+
ANGLED_STRING = /(?:<([^>]+|\\>+)*>)/
|
7
|
+
|
8
|
+
class << self
|
9
|
+
def for(source_file, line, number)
|
10
|
+
if directive_klass = Directives.find(line)
|
11
|
+
directive_klass.new(source_file, line, number)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def parse(line)
|
16
|
+
if matches = full_pattern.match(line)
|
17
|
+
matches.captures
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
protected
|
22
|
+
def full_pattern
|
23
|
+
@full_pattern ||= /\s*\/\/=\s+#{pattern}\s*$/
|
24
|
+
end
|
25
|
+
|
26
|
+
def pattern
|
27
|
+
/^$/
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def initialize(source_file, line, number)
|
32
|
+
@source_file = source_file
|
33
|
+
@line, @number = line, number
|
34
|
+
@name, @argument = self.class.parse(line)
|
35
|
+
end
|
36
|
+
|
37
|
+
protected
|
38
|
+
def environment
|
39
|
+
source_file.environment
|
40
|
+
end
|
41
|
+
|
42
|
+
def parse_quoted_string(string)
|
43
|
+
QUOTED_STRING.match(string)[1].gsub(/\\"/, "\"")
|
44
|
+
end
|
45
|
+
|
46
|
+
def parse_angled_string(string)
|
47
|
+
ANGLED_STRING.match(string)[1].gsub(/\\>/, "\>")
|
48
|
+
end
|
49
|
+
|
50
|
+
def raise_load_error
|
51
|
+
raise LoadError,
|
52
|
+
"can't find file for #{name} `#{require_location}' " +
|
53
|
+
"(line #{number} of #{source_file.pathname})"
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module Sprockets
|
2
|
+
module Directives
|
3
|
+
def self.find(line)
|
4
|
+
directives.find do |directive_klass|
|
5
|
+
directive_klass.parse(line)
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.directives
|
10
|
+
constants.grep(/Directive$/).map do |directive_name|
|
11
|
+
const_get(directive_name)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Sprockets
|
2
|
+
module Directives
|
3
|
+
class ProvideDirective < Directive
|
4
|
+
def self.pattern
|
5
|
+
/(provide)\s+(#{QUOTED_STRING})/
|
6
|
+
end
|
7
|
+
|
8
|
+
def evaluate_in(preprocessor)
|
9
|
+
if asset_path
|
10
|
+
preprocessor.provide(asset_path)
|
11
|
+
else
|
12
|
+
raise_load_error
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def asset_path
|
17
|
+
source_file.find(provide_location, :directory)
|
18
|
+
end
|
19
|
+
|
20
|
+
def provide_location
|
21
|
+
parse_quoted_string(argument)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Sprockets
|
2
|
+
module Directives
|
3
|
+
class RelativeRequireDirective < RequireDirective
|
4
|
+
def self.pattern
|
5
|
+
/(require)\s+(#{QUOTED_STRING})/
|
6
|
+
end
|
7
|
+
|
8
|
+
def require_location
|
9
|
+
parse_quoted_string(argument)
|
10
|
+
end
|
11
|
+
|
12
|
+
protected
|
13
|
+
def location_finder
|
14
|
+
source_file
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module Sprockets
|
2
|
+
module Directives
|
3
|
+
class RequireDirective < Directive
|
4
|
+
def self.pattern
|
5
|
+
/(require)\s+(#{ANGLED_STRING})/
|
6
|
+
end
|
7
|
+
|
8
|
+
def evaluate_in(preprocessor)
|
9
|
+
if source_file_path
|
10
|
+
preprocessor.require(source_file_path.source_file)
|
11
|
+
else
|
12
|
+
raise_load_error
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def source_file_path
|
17
|
+
@source_file_path ||= location_finder.find(normalize(require_location))
|
18
|
+
end
|
19
|
+
|
20
|
+
def require_location
|
21
|
+
parse_angled_string(argument)
|
22
|
+
end
|
23
|
+
|
24
|
+
protected
|
25
|
+
def location_finder
|
26
|
+
environment
|
27
|
+
end
|
28
|
+
|
29
|
+
def normalize(location)
|
30
|
+
File.join(File.dirname(location), File.basename(location, ".js") + ".js")
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
module Sprockets
|
2
|
+
class Environment
|
3
|
+
attr_reader :root, :load_path
|
4
|
+
|
5
|
+
def initialize(root, load_path = [])
|
6
|
+
@load_path = [@root = Pathname.new(self, root)]
|
7
|
+
|
8
|
+
load_path.reverse_each do |location|
|
9
|
+
register_load_location(location)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def pathname_from(location)
|
14
|
+
Pathname.new(self, absolute_location_from(location))
|
15
|
+
end
|
16
|
+
|
17
|
+
def register_load_location(location)
|
18
|
+
pathname = pathname_from(location)
|
19
|
+
load_path.delete(pathname)
|
20
|
+
load_path.unshift(pathname)
|
21
|
+
location
|
22
|
+
end
|
23
|
+
|
24
|
+
def find(location)
|
25
|
+
if Sprockets.absolute?(location) && File.exists?(location)
|
26
|
+
pathname_from(location)
|
27
|
+
else
|
28
|
+
find_all(location).first
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def constants(reload = false)
|
33
|
+
@constants = nil if reload
|
34
|
+
@constants ||= find_all("constants.yml").inject({}) do |constants, pathname|
|
35
|
+
contents = YAML.load(pathname.contents) rescue nil
|
36
|
+
contents = {} unless contents.is_a?(Hash)
|
37
|
+
constants.merge(contents)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
protected
|
42
|
+
def absolute_location_from(location)
|
43
|
+
location = location.to_s
|
44
|
+
location = File.join(root.absolute_location, location) unless Sprockets.absolute?(location)
|
45
|
+
File.expand_path(location)
|
46
|
+
end
|
47
|
+
|
48
|
+
def find_all(location)
|
49
|
+
load_path.map { |pathname| pathname.find(location) }.compact
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module Sprockets
|
2
|
+
class Pathname
|
3
|
+
attr_reader :environment, :absolute_location
|
4
|
+
|
5
|
+
def initialize(environment, absolute_location)
|
6
|
+
@environment = environment
|
7
|
+
@absolute_location = File.expand_path(absolute_location)
|
8
|
+
end
|
9
|
+
|
10
|
+
# Returns a Pathname for the location relative to this pathname's absolute location.
|
11
|
+
def find(location, kind = :file)
|
12
|
+
location = File.join(absolute_location, location)
|
13
|
+
File.send("#{kind}?", location) ? Pathname.new(environment, location) : nil
|
14
|
+
end
|
15
|
+
|
16
|
+
def parent_pathname
|
17
|
+
Pathname.new(environment, File.dirname(absolute_location))
|
18
|
+
end
|
19
|
+
|
20
|
+
def source_file
|
21
|
+
SourceFile.new(environment, self)
|
22
|
+
end
|
23
|
+
|
24
|
+
def contents
|
25
|
+
IO.read(absolute_location)
|
26
|
+
end
|
27
|
+
|
28
|
+
def ==(pathname)
|
29
|
+
environment == pathname.environment &&
|
30
|
+
absolute_location == pathname.absolute_location
|
31
|
+
end
|
32
|
+
|
33
|
+
def to_s
|
34
|
+
absolute_location
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,138 @@
|
|
1
|
+
module Sprockets
|
2
|
+
class Preprocessor
|
3
|
+
attr_reader :environment, :concatenation, :source_files, :pdoc_lines, :asset_paths
|
4
|
+
|
5
|
+
def initialize(environment, options = {})
|
6
|
+
@environment = environment
|
7
|
+
@concatenation = Concatenation.new
|
8
|
+
@source_files = []
|
9
|
+
@comment_lines = []
|
10
|
+
@pdoc_lines = []
|
11
|
+
@asset_paths = []
|
12
|
+
@options = options
|
13
|
+
end
|
14
|
+
|
15
|
+
def require(source_file)
|
16
|
+
return if source_files.include?(source_file)
|
17
|
+
source_files << source_file
|
18
|
+
|
19
|
+
source_file.each_line do |line, number|
|
20
|
+
process(source_file, line, number)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def provide(asset_path)
|
25
|
+
return if !asset_path || asset_paths.include?(asset_path)
|
26
|
+
asset_paths << asset_path
|
27
|
+
end
|
28
|
+
|
29
|
+
protected
|
30
|
+
attr_reader :options, :comment_lines
|
31
|
+
|
32
|
+
def process(source_file, line, number)
|
33
|
+
if inside_multi_line_comment?
|
34
|
+
comment_lines.push([source_file, line, number])
|
35
|
+
|
36
|
+
if line_closes_multi_line_pdoc_comment?(line)
|
37
|
+
record_comment_lines_as(:pdoc)
|
38
|
+
elsif line_closes_multi_line_comment?(line)
|
39
|
+
record_comment_lines_as(:source)
|
40
|
+
end
|
41
|
+
|
42
|
+
elsif line_opens_multi_line_pdoc_comment?(line)
|
43
|
+
comment_lines.push([source_file, line, number])
|
44
|
+
|
45
|
+
elsif line_is_single_line_comment?(line)
|
46
|
+
if directive = Directive.for(source_file, line, number)
|
47
|
+
directive.evaluate_in(self)
|
48
|
+
else
|
49
|
+
record_single_comment_line(source_file, line, number)
|
50
|
+
end
|
51
|
+
|
52
|
+
else
|
53
|
+
record_source_line(source_file, line, number)
|
54
|
+
end
|
55
|
+
|
56
|
+
rescue UndefinedConstantError => constant
|
57
|
+
raise UndefinedConstantError,
|
58
|
+
"couldn't find constant `#{constant}' in line #{number} of #{source_file.pathname}"
|
59
|
+
end
|
60
|
+
|
61
|
+
private
|
62
|
+
def record_source_line(source_file, line, number)
|
63
|
+
concatenation.record(source_file, format(line), number)
|
64
|
+
end
|
65
|
+
|
66
|
+
def record_pdoc_line(source_file, line, number)
|
67
|
+
pdoc_lines.push([source_file, line, number])
|
68
|
+
record_source_line(source_file, line, number) unless stripping_comments?
|
69
|
+
end
|
70
|
+
|
71
|
+
def record_single_comment_line(source_file, line, number)
|
72
|
+
record_source_line(source_file, line, number) unless stripping_comments?
|
73
|
+
end
|
74
|
+
|
75
|
+
def record_comment_lines_as(kind)
|
76
|
+
comment_lines.each do |comment_line|
|
77
|
+
send(:"record_#{kind}_line", *comment_line)
|
78
|
+
end
|
79
|
+
comment_lines.clear
|
80
|
+
end
|
81
|
+
|
82
|
+
def format(line)
|
83
|
+
result = line.chomp
|
84
|
+
interpolate_constants!(result, environment.constants)
|
85
|
+
strip_trailing_whitespace!(result)
|
86
|
+
result << $/
|
87
|
+
end
|
88
|
+
|
89
|
+
def interpolate_constants!(result, constants)
|
90
|
+
result.gsub!(/<%=(.*?)%>/) do
|
91
|
+
constant = $1.strip
|
92
|
+
if value = constants[constant]
|
93
|
+
value
|
94
|
+
else
|
95
|
+
raise UndefinedConstantError, constant
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
def strip_trailing_whitespace!(result)
|
101
|
+
result.gsub!(/\s+$/, "")
|
102
|
+
end
|
103
|
+
|
104
|
+
def inside_multi_line_comment?
|
105
|
+
comment_lines.any?
|
106
|
+
end
|
107
|
+
|
108
|
+
def line_opens_multi_line_pdoc_comment?(line)
|
109
|
+
if rest = line[/^\s*\/\*\*(.*)/, 1]
|
110
|
+
!line_closes_multi_line_comment?(rest)
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
def line_closes_multi_line_pdoc_comment?(line)
|
115
|
+
line =~ /\*\*\/\s*$/
|
116
|
+
end
|
117
|
+
|
118
|
+
def line_opens_multi_line_comment?(line)
|
119
|
+
if rest = line[/^\s*\/\*(.*)/, 1]
|
120
|
+
!line_closes_multi_line_comment?(rest)
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
def line_closes_multi_line_comment?(line)
|
125
|
+
if rest = line[/\*\/(.*)/, 1]
|
126
|
+
!line_opens_multi_line_comment?(rest)
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
def line_is_single_line_comment?(line)
|
131
|
+
line =~ /^\s*\/\//
|
132
|
+
end
|
133
|
+
|
134
|
+
def stripping_comments?
|
135
|
+
options[:strip_comments] != false
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|
@@ -0,0 +1,107 @@
|
|
1
|
+
module Sprockets
|
2
|
+
class Secretary
|
3
|
+
DEFAULT_OPTIONS = {
|
4
|
+
:root => ".",
|
5
|
+
:load_path => [],
|
6
|
+
:source_files => [],
|
7
|
+
:expand_paths => true,
|
8
|
+
:strip_comments => true
|
9
|
+
}
|
10
|
+
|
11
|
+
attr_reader :environment, :preprocessor
|
12
|
+
|
13
|
+
def initialize(options = {})
|
14
|
+
reset!(options)
|
15
|
+
end
|
16
|
+
|
17
|
+
def reset!(options = @options)
|
18
|
+
@options = DEFAULT_OPTIONS.merge(options)
|
19
|
+
@environment = Sprockets::Environment.new(@options[:root])
|
20
|
+
@preprocessor = Sprockets::Preprocessor.new(@environment, :strip_comments => @options[:strip_comments])
|
21
|
+
|
22
|
+
add_load_locations(@options[:load_path])
|
23
|
+
add_source_files(@options[:source_files])
|
24
|
+
end
|
25
|
+
|
26
|
+
def add_load_location(load_location, options = {})
|
27
|
+
add_load_locations([load_location], options)
|
28
|
+
end
|
29
|
+
|
30
|
+
def add_load_locations(load_locations, options = {})
|
31
|
+
expand_paths(load_locations, options).each do |load_location|
|
32
|
+
environment.register_load_location(load_location)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def add_source_file(source_file, options = {})
|
37
|
+
add_source_files([source_file], options)
|
38
|
+
end
|
39
|
+
|
40
|
+
def add_source_files(source_files, options = {})
|
41
|
+
expand_paths(source_files, options).each do |source_file|
|
42
|
+
if pathname = environment.find(source_file)
|
43
|
+
preprocessor.require(pathname.source_file)
|
44
|
+
else
|
45
|
+
raise Sprockets::LoadError, "no such file `#{source_file}'"
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def concatenation
|
51
|
+
preprocessor.concatenation
|
52
|
+
end
|
53
|
+
|
54
|
+
def install_assets
|
55
|
+
if @options[:asset_root]
|
56
|
+
preprocessor.asset_paths.each do |asset_path|
|
57
|
+
copy_assets_from(asset_path.absolute_location)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def source_last_modified
|
63
|
+
preprocessor.source_files.map { |s| s.mtime }.max
|
64
|
+
end
|
65
|
+
|
66
|
+
protected
|
67
|
+
def expand_paths(paths, options = {})
|
68
|
+
if options.has_key?(:expand_paths) ? options[:expand_paths] : @options[:expand_paths]
|
69
|
+
paths.map { |path| Dir[from_root(path)].sort }.flatten.compact
|
70
|
+
else
|
71
|
+
paths.map { |path| from_root(path) }
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def from_root(path)
|
76
|
+
if Sprockets.absolute?(path)
|
77
|
+
path
|
78
|
+
else
|
79
|
+
File.join(@options[:root], path)
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
def copy_assets_from(asset_path)
|
84
|
+
relative_file_paths_beneath(asset_path).each do |filename|
|
85
|
+
source, destination = File.join(asset_path, filename), File.join(asset_root, File.dirname(filename))
|
86
|
+
if !File.directory?(source)
|
87
|
+
FileUtils.mkdir_p(destination)
|
88
|
+
FileUtils.cp(source, destination)
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
def relative_file_paths_beneath(path)
|
94
|
+
Dir[File.join(path, "**", "*")].map do |filename|
|
95
|
+
File.join(*path_pieces(filename)[path_pieces(path).length..-1])
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
def asset_root
|
100
|
+
from_root(@options[:asset_root])
|
101
|
+
end
|
102
|
+
|
103
|
+
def path_pieces(path)
|
104
|
+
path.split(File::SEPARATOR)
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module Sprockets
|
2
|
+
class SourceFile
|
3
|
+
attr_reader :environment, :pathname
|
4
|
+
|
5
|
+
def initialize(environment, pathname)
|
6
|
+
@environment = environment
|
7
|
+
@pathname = pathname
|
8
|
+
end
|
9
|
+
|
10
|
+
def find(location, kind = :file)
|
11
|
+
pathname.parent_pathname.find(location, kind)
|
12
|
+
end
|
13
|
+
|
14
|
+
def ==(source_file)
|
15
|
+
pathname == source_file.pathname
|
16
|
+
end
|
17
|
+
|
18
|
+
def mtime
|
19
|
+
File.mtime(pathname.absolute_location)
|
20
|
+
rescue Errno::ENOENT
|
21
|
+
Time.now
|
22
|
+
end
|
23
|
+
|
24
|
+
def each_line
|
25
|
+
File.open(pathname.absolute_location) do |file|
|
26
|
+
file.each_line do |line|
|
27
|
+
yield line, file.lineno
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class ConcatenationTest < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
@concatenation = Sprockets::Concatenation.new
|
6
|
+
@environment = environment_for_fixtures
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_record
|
10
|
+
assert_equal [], @concatenation.lines
|
11
|
+
assert_equal "hello\n", @concatenation.record(source_file, "hello\n", 1).to_s
|
12
|
+
assert_equal "world\n", @concatenation.record(source_file, "world\n", 2).to_s
|
13
|
+
assert_equal ["hello\n", "world\n"], @concatenation.lines
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_to_s
|
17
|
+
@concatenation.record(source_file, "hello\n", 1)
|
18
|
+
@concatenation.record(source_file, "world\n", 2)
|
19
|
+
assert_equal "hello\nworld\n", @concatenation.to_s
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_save_to
|
23
|
+
filename = File.join(FIXTURES_PATH, "output.js")
|
24
|
+
@concatenation.save_to(filename)
|
25
|
+
assert_equal @concatenation.to_s, IO.read(filename)
|
26
|
+
File.unlink(filename)
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class EnvironmentTest < Test::Unit::TestCase
|
4
|
+
def test_load_path_locations_become_pathnames_for_absolute_locations_from_the_root
|
5
|
+
environment = Sprockets::Environment.new("/root", ["/a", "b"])
|
6
|
+
assert_load_path_equals ["/a", "/root/b", "/root"], environment
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_pathname_from_for_location_with_leading_slash_should_return_a_pathname_with_the_location_unchanged
|
10
|
+
environment = Sprockets::Environment.new("/root")
|
11
|
+
assert_absolute_location "/a", environment.pathname_from("/a")
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_pathname_from_for_relative_location_should_return_a_pathname_for_the_expanded_absolute_location_from_root
|
15
|
+
environment = Sprockets::Environment.new("/root")
|
16
|
+
assert_absolute_location "/root/a", environment.pathname_from("a")
|
17
|
+
assert_absolute_location "/root/a", environment.pathname_from("./a")
|
18
|
+
assert_absolute_location "/a", environment.pathname_from("../a")
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_register_load_location_should_unshift_the_location_onto_the_load_path
|
22
|
+
environment = Sprockets::Environment.new("/root")
|
23
|
+
environment.register_load_location("a")
|
24
|
+
assert_load_path_equals ["/root/a", "/root"], environment
|
25
|
+
environment.register_load_location("b")
|
26
|
+
assert_load_path_equals ["/root/b", "/root/a", "/root"], environment
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_register_load_location_should_remove_already_existing_locations_before_unshifting
|
30
|
+
environment = Sprockets::Environment.new("/root")
|
31
|
+
environment.register_load_location("a")
|
32
|
+
environment.register_load_location("b")
|
33
|
+
assert_load_path_equals ["/root/b", "/root/a", "/root"], environment
|
34
|
+
environment.register_load_location("a")
|
35
|
+
assert_load_path_equals ["/root/a", "/root/b", "/root"], environment
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_find_should_return_the_first_matching_pathname_in_the_load_path
|
39
|
+
environment = environment_for_fixtures
|
40
|
+
first_pathname = environment.find("foo.js")
|
41
|
+
assert_absolute_location_ends_with "src/foo.js", first_pathname
|
42
|
+
|
43
|
+
environment.register_load_location(File.join(FIXTURES_PATH, "src", "foo"))
|
44
|
+
second_pathname = environment.find("foo.js")
|
45
|
+
assert_not_equal first_pathname, second_pathname
|
46
|
+
assert_absolute_location_ends_with "foo/foo.js", second_pathname
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_find_should_return_nil_when_no_matching_source_file_is_found
|
50
|
+
environment = environment_for_fixtures
|
51
|
+
assert_nil environment.find("nonexistent.js")
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_constants_should_return_a_hash_of_all_constants_defined_in_the_load_path
|
55
|
+
constants = environment_for_fixtures.constants
|
56
|
+
assert_kind_of Hash, constants
|
57
|
+
assert_equal %w(HELLO ONE TWO VERSION), constants.keys.sort
|
58
|
+
end
|
59
|
+
|
60
|
+
protected
|
61
|
+
def assert_load_path_equals(load_path_absolute_locations, environment)
|
62
|
+
assert load_path_absolute_locations.zip(environment.load_path).map { |location, pathname| File.expand_path(location) == pathname.absolute_location }.all?
|
63
|
+
end
|
64
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), *%w".. lib sprockets")
|
2
|
+
require "test/unit"
|
3
|
+
require "fileutils"
|
4
|
+
require "tmpdir"
|
5
|
+
|
6
|
+
class Test::Unit::TestCase
|
7
|
+
FIXTURES_PATH = File.expand_path(File.join(File.dirname(__FILE__), "fixtures")) unless defined?(FIXTURES_PATH)
|
8
|
+
|
9
|
+
protected
|
10
|
+
def location_for_fixture(fixture)
|
11
|
+
File.join(FIXTURES_PATH, fixture)
|
12
|
+
end
|
13
|
+
|
14
|
+
def content_of_fixture(fixture)
|
15
|
+
IO.read(location_for_fixture(fixture))
|
16
|
+
end
|
17
|
+
|
18
|
+
def environment_for_fixtures
|
19
|
+
Sprockets::Environment.new(FIXTURES_PATH, source_directories_in_fixtures_path)
|
20
|
+
end
|
21
|
+
|
22
|
+
def source_directories_in_fixtures_path
|
23
|
+
Dir[File.join(FIXTURES_PATH, "**", "src")]
|
24
|
+
end
|
25
|
+
|
26
|
+
def assert_absolute_location(location, pathname)
|
27
|
+
assert_equal File.expand_path(location), pathname.absolute_location
|
28
|
+
end
|
29
|
+
|
30
|
+
def assert_absolute_location_ends_with(location_ending, pathname)
|
31
|
+
assert pathname.absolute_location[/#{Regexp.escape(location_ending)}$/]
|
32
|
+
end
|
33
|
+
|
34
|
+
def pathname(location, environment = @environment)
|
35
|
+
Sprockets::Pathname.new(environment, File.join(FIXTURES_PATH, location))
|
36
|
+
end
|
37
|
+
|
38
|
+
def source_file(location = "dummy", environment = @environment)
|
39
|
+
Sprockets::SourceFile.new(environment, pathname(location, environment))
|
40
|
+
end
|
41
|
+
|
42
|
+
def with_temporary_directory
|
43
|
+
path = File.join(Dir.tmpdir, [caller[0][/`(.*)'/, 1], Time.now.to_f].join("_"))
|
44
|
+
begin
|
45
|
+
FileUtils.mkdir(path)
|
46
|
+
yield path
|
47
|
+
ensure
|
48
|
+
FileUtils.rm_rf(path)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class PathnameTest < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
@environment = environment_for_fixtures
|
6
|
+
end
|
7
|
+
|
8
|
+
def test_absolute_location_is_automatically_expanded
|
9
|
+
expanded_location = File.expand_path(File.join(FIXTURES_PATH, "foo"))
|
10
|
+
assert_absolute_location expanded_location, pathname("foo")
|
11
|
+
assert_absolute_location expanded_location, pathname("./foo")
|
12
|
+
assert_absolute_location expanded_location, pathname("./foo/../foo")
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_find_should_return_a_pathname_for_the_location_relative_to_the_absolute_location_of_the_pathname
|
16
|
+
assert_absolute_location_ends_with "src/foo/bar.js", pathname("src/foo").find("bar.js")
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_find_should_return_nil_when_the_location_relative_to_the_absolute_location_of_the_pathname_is_not_a_file_or_does_not_exist
|
20
|
+
assert_nil pathname("src/foo").find("nonexistent.js")
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_parent_pathname_should_return_a_pathname_for_the_parent_directory
|
24
|
+
assert_absolute_location_ends_with "src", pathname("src/foo").parent_pathname
|
25
|
+
assert_absolute_location_ends_with "foo", pathname("src/foo/foo.js").parent_pathname
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_source_file_should_return_a_source_file_for_the_pathname
|
29
|
+
source_file = pathname("src/foo.js").source_file
|
30
|
+
assert_kind_of Sprockets::SourceFile, source_file
|
31
|
+
assert_equal pathname("src/foo.js"), source_file.pathname
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_equality_of_pathnames
|
35
|
+
assert_equal pathname("src/foo.js"), pathname("src/foo.js")
|
36
|
+
assert_equal pathname("src/foo.js"), pathname("src/foo/../foo.js")
|
37
|
+
assert_not_equal pathname("src/foo.js"), pathname("src/foo/foo.js")
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_to_s_should_return_absolute_location
|
41
|
+
assert_equal pathname("src/foo.js").to_s, pathname("src/foo.js").absolute_location
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,107 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class PreprocessorTest < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
@environment = environment_for_fixtures
|
6
|
+
@preprocessor = Sprockets::Preprocessor.new(@environment)
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_double_slash_comments_that_are_not_requires_should_be_removed_by_default
|
10
|
+
require_file_for_this_test
|
11
|
+
assert_concatenation_does_not_contain_line "// This is a double-slash comment that should not appear in the resulting output file."
|
12
|
+
assert_concatenation_contains_line "/* This is a slash-star comment that should not appear in the resulting output file. */"
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_double_slash_comments_that_are_not_requires_should_be_ignored_when_strip_comments_is_false
|
16
|
+
@preprocessor = Sprockets::Preprocessor.new(@environment, :strip_comments => false)
|
17
|
+
require_file_for_this_test
|
18
|
+
assert_concatenation_contains_line "// This is a double-slash comment that should appear in the resulting output file."
|
19
|
+
assert_concatenation_contains_line "/* This is a slash-star comment that should appear in the resulting output file. */"
|
20
|
+
|
21
|
+
assert_concatenation_contains_line "/* This is multiline slash-star comment"
|
22
|
+
assert_concatenation_contains_line "* that should appear in the resulting"
|
23
|
+
assert_concatenation_contains_line "* output file */"
|
24
|
+
|
25
|
+
assert_concatenation_contains_line "This is not a PDoc comment that should appear in the resulting output file."
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_multiline_comments_should_be_removed_by_default
|
29
|
+
require_file_for_this_test
|
30
|
+
assert_concatenation_does_not_contain_line "/**"
|
31
|
+
assert_concatenation_does_not_contain_line " * This is a PDoc comment"
|
32
|
+
assert_concatenation_does_not_contain_line " * that should appear in the resulting output file."
|
33
|
+
assert_concatenation_does_not_contain_line "**/"
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_requiring_a_single_file_should_replace_the_require_comment_with_the_file_contents
|
37
|
+
require_file_for_this_test
|
38
|
+
assert_concatenation_contains <<-LINES
|
39
|
+
var before_require;
|
40
|
+
var Foo = { };
|
41
|
+
var after_require;
|
42
|
+
LINES
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_requiring_a_file_that_does_not_exist_should_raise_an_error
|
46
|
+
assert_raises(Sprockets::LoadError) do
|
47
|
+
require_file_for_this_test
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def test_requiring_the_current_file_should_do_nothing
|
52
|
+
require_file_for_this_test
|
53
|
+
assert_equal "", output_text
|
54
|
+
end
|
55
|
+
|
56
|
+
def test_requiring_a_file_after_it_has_already_been_required_should_do_nothing
|
57
|
+
require_file_for_this_test
|
58
|
+
assert_concatenation_contains <<-LINES
|
59
|
+
var before_first_require;
|
60
|
+
var Foo = { };
|
61
|
+
var after_first_require_and_before_second_require;
|
62
|
+
var after_second_require;
|
63
|
+
LINES
|
64
|
+
end
|
65
|
+
|
66
|
+
protected
|
67
|
+
attr_reader :environment, :preprocessor
|
68
|
+
|
69
|
+
def concatenation
|
70
|
+
preprocessor.concatenation
|
71
|
+
end
|
72
|
+
|
73
|
+
def output_text
|
74
|
+
preprocessor.concatenation.to_s
|
75
|
+
end
|
76
|
+
|
77
|
+
def source_lines_matching(line)
|
78
|
+
concatenation.lines.select { |l| l.strip == line }
|
79
|
+
end
|
80
|
+
|
81
|
+
def require_file(location)
|
82
|
+
preprocessor.require(environment.find(location).source_file)
|
83
|
+
end
|
84
|
+
|
85
|
+
def require_file_for_this_test
|
86
|
+
require_file(file_for_this_test)
|
87
|
+
end
|
88
|
+
|
89
|
+
def file_for_this_test
|
90
|
+
caller.map { |c| c[/`(.*?)'$/, 1] }.grep(/^test_/).first[5..-1] + ".js"
|
91
|
+
end
|
92
|
+
|
93
|
+
def assert_concatenation_does_not_contain_line(line)
|
94
|
+
assert source_lines_matching(line).empty?, "Expected #{line.inspect} not to exist"
|
95
|
+
end
|
96
|
+
|
97
|
+
def assert_concatenation_contains_line(line)
|
98
|
+
assert source_lines_matching(line).any?, "Expected #{line.inspect} to exist"
|
99
|
+
end
|
100
|
+
|
101
|
+
def assert_concatenation_contains(indented_text)
|
102
|
+
lines = indented_text.split($/)
|
103
|
+
initial_indent = lines.first[/^\s*/].length
|
104
|
+
unindented_text = lines.map { |line| line[initial_indent..-1] }.join($/)
|
105
|
+
assert output_text[unindented_text]
|
106
|
+
end
|
107
|
+
end
|
@@ -0,0 +1,89 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class SecretaryTest < Test::Unit::TestCase
|
4
|
+
def test_load_locations_are_not_expanded_when_expand_paths_is_false
|
5
|
+
secretary = Sprockets::Secretary.new(:root => FIXTURES_PATH)
|
6
|
+
secretary.add_load_location("src/**/", :expand_paths => false)
|
7
|
+
|
8
|
+
assert_equal [File.join(FIXTURES_PATH, "src/**"), FIXTURES_PATH],
|
9
|
+
secretary.environment.load_path.map { |pathname| pathname.absolute_location }
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_load_locations_are_expanded_when_expand_paths_is_true
|
13
|
+
secretary = Sprockets::Secretary.new(:root => FIXTURES_PATH)
|
14
|
+
secretary.add_load_location("src/**/", :expand_paths => true)
|
15
|
+
|
16
|
+
assert_equal [File.join(FIXTURES_PATH, "src", "foo"), File.join(FIXTURES_PATH, "src"), FIXTURES_PATH],
|
17
|
+
secretary.environment.load_path.map { |pathname| pathname.absolute_location }
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_source_files_are_not_expanded_when_expand_paths_is_false
|
21
|
+
secretary = Sprockets::Secretary.new(:root => FIXTURES_PATH)
|
22
|
+
assert_raises(Sprockets::LoadError) do
|
23
|
+
secretary.add_source_file("src/f*.js", :expand_paths => false)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_source_files_are_expanded_when_expand_paths_is_true
|
28
|
+
secretary = Sprockets::Secretary.new(:root => FIXTURES_PATH)
|
29
|
+
secretary.add_source_file("src/f*.js", :expand_paths => true)
|
30
|
+
|
31
|
+
assert_equal [File.join(FIXTURES_PATH, "src", "foo.js")],
|
32
|
+
secretary.preprocessor.source_files.map { |source_file| source_file.pathname.absolute_location }
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_install_assets_into_empty_directory
|
36
|
+
with_temporary_directory do |temp|
|
37
|
+
secretary = Sprockets::Secretary.new(:root => FIXTURES_PATH, :asset_root => temp)
|
38
|
+
secretary.add_source_file("src/script_with_assets.js")
|
39
|
+
|
40
|
+
assert_equal [], Dir[File.join(temp, "**", "*")]
|
41
|
+
secretary.install_assets
|
42
|
+
assert_equal paths_relative_to(temp,
|
43
|
+
"images", "images/script_with_assets", "images/script_with_assets/one.png",
|
44
|
+
"images/script_with_assets/two.png", "stylesheets", "stylesheets/script_with_assets.css"),
|
45
|
+
Dir[File.join(temp, "**", "*")].sort
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_install_assets_into_nonexistent_directory
|
50
|
+
with_temporary_directory do |temp|
|
51
|
+
temp = File.join(temp, "assets")
|
52
|
+
secretary = Sprockets::Secretary.new(:root => FIXTURES_PATH, :asset_root => temp)
|
53
|
+
secretary.add_source_file("src/script_with_assets.js")
|
54
|
+
|
55
|
+
assert_equal [], Dir[File.join(temp, "**", "*")]
|
56
|
+
secretary.install_assets
|
57
|
+
assert_equal paths_relative_to(temp,
|
58
|
+
"images", "images/script_with_assets", "images/script_with_assets/one.png",
|
59
|
+
"images/script_with_assets/two.png", "stylesheets", "stylesheets/script_with_assets.css"),
|
60
|
+
Dir[File.join(temp, "**", "*")].sort
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def test_install_assets_into_subdirectories_that_already_exist
|
65
|
+
with_temporary_directory do |temp|
|
66
|
+
secretary = Sprockets::Secretary.new(:root => FIXTURES_PATH, :asset_root => temp)
|
67
|
+
secretary.add_source_file("src/script_with_assets.js")
|
68
|
+
|
69
|
+
FileUtils.mkdir_p(File.join(temp, "images", "script_with_assets"))
|
70
|
+
assert_equal paths_relative_to(temp, "images", "images/script_with_assets"), Dir[File.join(temp, "**", "*")]
|
71
|
+
secretary.install_assets
|
72
|
+
assert_equal paths_relative_to(temp,
|
73
|
+
"images", "images/script_with_assets", "images/script_with_assets/one.png",
|
74
|
+
"images/script_with_assets/two.png", "stylesheets", "stylesheets/script_with_assets.css"),
|
75
|
+
Dir[File.join(temp, "**", "*")].sort
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def test_secretary_passes_strip_comments_option_through_to_preprocessor
|
80
|
+
secretary = Sprockets::Secretary.new(:root => FIXTURES_PATH, :strip_comments => false)
|
81
|
+
secretary.add_source_file("src/script_with_comments.js")
|
82
|
+
assert_equal content_of_fixture("src/script_with_comments.js"), secretary.concatenation.to_s
|
83
|
+
end
|
84
|
+
|
85
|
+
protected
|
86
|
+
def paths_relative_to(root, *paths)
|
87
|
+
paths.map { |path| File.join(root, path) }
|
88
|
+
end
|
89
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class SourceFileTest < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
@environment = environment_for_fixtures
|
6
|
+
end
|
7
|
+
|
8
|
+
def test_each_line
|
9
|
+
source_file_lines = []
|
10
|
+
source_file("src/foo/bar.js").each_line {|line, number| source_file_lines << line }
|
11
|
+
assert_equal content_of_fixture("src/foo/bar.js"), source_file_lines.join
|
12
|
+
assert_equal 4, source_file_lines.length
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_find_should_return_pathname_for_file_relative_to_the_current_pathname
|
16
|
+
assert_absolute_location_ends_with "test/fixtures/src/foo/bar.js", source_file("src/foo/foo.js").find("bar.js")
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_find_should_return_nil_for_nonexistent_file
|
20
|
+
assert_nil source_file("src/foo/foo.js").find("nonexistent.js")
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_equality_of_source_files
|
24
|
+
assert_equal source_file("src/foo/foo.js"), source_file("src/foo/foo.js")
|
25
|
+
assert_equal source_file("src/foo/foo.js"), source_file("src/foo/../foo/foo.js")
|
26
|
+
assert_not_equal source_file("src/foo/foo.js"), source_file("src/foo.js")
|
27
|
+
assert_not_equal source_file("src/foo/foo.js"), source_file("src/foo/bar.js")
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_mtime_should_return_now_if_file_does_not_exist
|
31
|
+
assert source_file("src/foo/nonexistent.js").mtime.instance_of?(Time)
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,89 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class SourceLineTest # < Test::Unit::TestCase
|
4
|
+
def test_line_that_begins_with_double_slash_should_be_a_comment
|
5
|
+
assert source_line("//").comment?
|
6
|
+
assert source_line("//test").comment?
|
7
|
+
assert source_line("//= require").comment?
|
8
|
+
assert source_line("//= require <foo>").comment?
|
9
|
+
assert source_line(" //").comment?
|
10
|
+
assert source_line("\t//").comment?
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_line_that_begins_a_multiline_comment
|
14
|
+
assert source_line(" /*").begins_multiline_comment?
|
15
|
+
assert source_line(" /**").begins_multiline_comment?
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_line_that_begins_a_pdoc_comment
|
19
|
+
assert !source_line(" /*").begins_pdoc_comment?
|
20
|
+
assert source_line(" /**").begins_pdoc_comment?
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_line_that_ends_a_multiline_comment
|
24
|
+
assert source_line(" */").ends_multiline_comment?
|
25
|
+
assert source_line(" **/").ends_multiline_comment?
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_line_that_ends_a_pdoc_comment
|
29
|
+
assert !source_line(" */").ends_pdoc_comment?
|
30
|
+
assert source_line(" **/").ends_pdoc_comment?
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_line_that_contains_but_does_not_begin_with_double_slash_should_not_be_a_comment
|
34
|
+
assert !source_line("f //").comment?
|
35
|
+
assert !source_line("f //= require <foo>").comment?
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_comment_should_be_extracted_from_comment_lines
|
39
|
+
assert_equal "test", source_line("//test").comment
|
40
|
+
assert_equal " test", source_line("// test").comment
|
41
|
+
assert_equal nil, source_line("f //test").comment
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_line_that_contains_require_comment_should_be_a_require
|
45
|
+
assert source_line("//= require <foo>").require?
|
46
|
+
assert !source_line("//= require<foo>").require?
|
47
|
+
assert source_line("//= require \"foo\"").require?
|
48
|
+
assert !source_line("//= require <foo> f").require?
|
49
|
+
end
|
50
|
+
|
51
|
+
def test_require_should_be_extracted_from_require_lines
|
52
|
+
assert_nil source_line("//= require").require
|
53
|
+
assert_equal "<foo>", source_line("//= require <foo>").require
|
54
|
+
assert_equal "<foo>", source_line("//= require <foo> ").require
|
55
|
+
assert_equal "\"foo\"", source_line("//= require \"foo\"").require
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_line_that_contains_a_provide_comment_should_be_a_provide
|
59
|
+
assert source_line("//= provide \"../assets\"").provide?
|
60
|
+
assert !source_line("//= provide").provide?
|
61
|
+
assert !source_line("//= provide <../assets>").provide?
|
62
|
+
end
|
63
|
+
|
64
|
+
def test_provide_should_be_extracted_from_provide_lines
|
65
|
+
assert_nil source_line("//= provide").provide
|
66
|
+
assert_equal "../assets", source_line("//= provide \"../assets\"").provide
|
67
|
+
end
|
68
|
+
|
69
|
+
def test_inspect_should_include_source_file_location_and_line_number
|
70
|
+
environment = environment_for_fixtures
|
71
|
+
pathname = Sprockets::Pathname.new(environment, "/a/b/c.js")
|
72
|
+
source_file = Sprockets::SourceFile.new(environment, pathname)
|
73
|
+
assert_equal "line 25 of #{File.expand_path("/a/b/c.js")}", source_line("hello", source_file, 25).inspect
|
74
|
+
end
|
75
|
+
|
76
|
+
def test_interpolation_of_constants
|
77
|
+
assert_equal %(var VERSION = "1.0";\n), source_line('var VERSION = "<%= VERSION %>";').to_s("VERSION" => "1.0")
|
78
|
+
end
|
79
|
+
|
80
|
+
def test_interpolation_of_missing_constant_raises_undefined_constant_error
|
81
|
+
assert_raises(Sprockets::UndefinedConstantError) do
|
82
|
+
source_line('<%= NONEXISTENT %>').to_s("VERSION" => "1.0")
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
def test_to_s_should_strip_trailing_whitespace_before_adding_line_ending
|
87
|
+
assert_equal "hello();\n", source_line("hello(); \t \r\n").to_s({})
|
88
|
+
end
|
89
|
+
end
|
@@ -0,0 +1,84 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{sprockets-foo}
|
8
|
+
s.version = "0.0.2"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Ben Curren"]
|
12
|
+
s.date = %q{2010-09-27}
|
13
|
+
s.description = %q{Enhancements for sprockets including templating support with ejs.}
|
14
|
+
s.email = %q{ben@outright.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".gitignore",
|
22
|
+
".gitmodules",
|
23
|
+
"LICENSE",
|
24
|
+
"README.rdoc",
|
25
|
+
"Rakefile",
|
26
|
+
"VERSION",
|
27
|
+
"ext/sprockets_with_directives/lib/sprockets.rb",
|
28
|
+
"ext/sprockets_with_directives/lib/sprockets/concatenation.rb",
|
29
|
+
"ext/sprockets_with_directives/lib/sprockets/directive.rb",
|
30
|
+
"ext/sprockets_with_directives/lib/sprockets/directives.rb",
|
31
|
+
"ext/sprockets_with_directives/lib/sprockets/directives/provide_directive.rb",
|
32
|
+
"ext/sprockets_with_directives/lib/sprockets/directives/relative_require_directive.rb",
|
33
|
+
"ext/sprockets_with_directives/lib/sprockets/directives/require_directive.rb",
|
34
|
+
"ext/sprockets_with_directives/lib/sprockets/environment.rb",
|
35
|
+
"ext/sprockets_with_directives/lib/sprockets/error.rb",
|
36
|
+
"ext/sprockets_with_directives/lib/sprockets/pathname.rb",
|
37
|
+
"ext/sprockets_with_directives/lib/sprockets/preprocessor.rb",
|
38
|
+
"ext/sprockets_with_directives/lib/sprockets/secretary.rb",
|
39
|
+
"ext/sprockets_with_directives/lib/sprockets/source_file.rb",
|
40
|
+
"ext/sprockets_with_directives/lib/sprockets/version.rb",
|
41
|
+
"ext/sprockets_with_directives/test/test_concatenation.rb",
|
42
|
+
"ext/sprockets_with_directives/test/test_environment.rb",
|
43
|
+
"ext/sprockets_with_directives/test/test_helper.rb",
|
44
|
+
"ext/sprockets_with_directives/test/test_pathname.rb",
|
45
|
+
"ext/sprockets_with_directives/test/test_preprocessor.rb",
|
46
|
+
"ext/sprockets_with_directives/test/test_secretary.rb",
|
47
|
+
"ext/sprockets_with_directives/test/test_source_file.rb",
|
48
|
+
"ext/sprockets_with_directives/test/test_source_line.rb",
|
49
|
+
"lib/sprockets-foo.rb",
|
50
|
+
"lib/sprockets/directives/template_directive.rb",
|
51
|
+
"sprockets-foo.gemspec",
|
52
|
+
"test/fixtures/requiring_a_template_should_compile_then_require.js",
|
53
|
+
"test/fixtures/requiring_a_template_with_namespace_should_compile_then_require.js",
|
54
|
+
"test/fixtures/src/MyTemplate.ejs",
|
55
|
+
"test/fixtures/src/MyTemplate.js",
|
56
|
+
"test/fixtures/src/or/ui/MyNamespacedTemplate.ejs",
|
57
|
+
"test/fixtures/src/or/ui/MyNamespacedTemplate.js",
|
58
|
+
"test/test_helper.rb",
|
59
|
+
"test/test_template_directive.rb"
|
60
|
+
]
|
61
|
+
s.homepage = %q{http://github.com/bcurren/sprockets-foo}
|
62
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
63
|
+
s.require_paths = ["lib"]
|
64
|
+
s.rubygems_version = %q{1.3.6}
|
65
|
+
s.summary = %q{Enhancements for sprockets including templating support with ejs.}
|
66
|
+
s.test_files = [
|
67
|
+
"test/test_helper.rb",
|
68
|
+
"test/test_template_directive.rb"
|
69
|
+
]
|
70
|
+
|
71
|
+
if s.respond_to? :specification_version then
|
72
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
73
|
+
s.specification_version = 3
|
74
|
+
|
75
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
76
|
+
s.add_runtime_dependency(%q<ejs-rcompiler>, [">= 0.1.1"])
|
77
|
+
else
|
78
|
+
s.add_dependency(%q<ejs-rcompiler>, [">= 0.1.1"])
|
79
|
+
end
|
80
|
+
else
|
81
|
+
s.add_dependency(%q<ejs-rcompiler>, [">= 0.1.1"])
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
File without changes
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: 0.0.
|
8
|
+
- 2
|
9
|
+
version: 0.0.2
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Ben Curren
|
@@ -48,8 +48,31 @@ files:
|
|
48
48
|
- README.rdoc
|
49
49
|
- Rakefile
|
50
50
|
- VERSION
|
51
|
+
- ext/sprockets_with_directives/lib/sprockets.rb
|
52
|
+
- ext/sprockets_with_directives/lib/sprockets/concatenation.rb
|
53
|
+
- ext/sprockets_with_directives/lib/sprockets/directive.rb
|
54
|
+
- ext/sprockets_with_directives/lib/sprockets/directives.rb
|
55
|
+
- ext/sprockets_with_directives/lib/sprockets/directives/provide_directive.rb
|
56
|
+
- ext/sprockets_with_directives/lib/sprockets/directives/relative_require_directive.rb
|
57
|
+
- ext/sprockets_with_directives/lib/sprockets/directives/require_directive.rb
|
58
|
+
- ext/sprockets_with_directives/lib/sprockets/environment.rb
|
59
|
+
- ext/sprockets_with_directives/lib/sprockets/error.rb
|
60
|
+
- ext/sprockets_with_directives/lib/sprockets/pathname.rb
|
61
|
+
- ext/sprockets_with_directives/lib/sprockets/preprocessor.rb
|
62
|
+
- ext/sprockets_with_directives/lib/sprockets/secretary.rb
|
63
|
+
- ext/sprockets_with_directives/lib/sprockets/source_file.rb
|
64
|
+
- ext/sprockets_with_directives/lib/sprockets/version.rb
|
65
|
+
- ext/sprockets_with_directives/test/test_concatenation.rb
|
66
|
+
- ext/sprockets_with_directives/test/test_environment.rb
|
67
|
+
- ext/sprockets_with_directives/test/test_helper.rb
|
68
|
+
- ext/sprockets_with_directives/test/test_pathname.rb
|
69
|
+
- ext/sprockets_with_directives/test/test_preprocessor.rb
|
70
|
+
- ext/sprockets_with_directives/test/test_secretary.rb
|
71
|
+
- ext/sprockets_with_directives/test/test_source_file.rb
|
72
|
+
- ext/sprockets_with_directives/test/test_source_line.rb
|
51
73
|
- lib/sprockets-foo.rb
|
52
74
|
- lib/sprockets/directives/template_directive.rb
|
75
|
+
- sprockets-foo.gemspec
|
53
76
|
- test/fixtures/requiring_a_template_should_compile_then_require.js
|
54
77
|
- test/fixtures/requiring_a_template_with_namespace_should_compile_then_require.js
|
55
78
|
- test/fixtures/src/MyTemplate.ejs
|
@@ -57,7 +80,7 @@ files:
|
|
57
80
|
- test/fixtures/src/or/ui/MyNamespacedTemplate.ejs
|
58
81
|
- test/fixtures/src/or/ui/MyNamespacedTemplate.js
|
59
82
|
- test/test_helper.rb
|
60
|
-
- test/
|
83
|
+
- test/test_template_directive.rb
|
61
84
|
has_rdoc: true
|
62
85
|
homepage: http://github.com/bcurren/sprockets-foo
|
63
86
|
licenses: []
|
@@ -90,4 +113,4 @@ specification_version: 3
|
|
90
113
|
summary: Enhancements for sprockets including templating support with ejs.
|
91
114
|
test_files:
|
92
115
|
- test/test_helper.rb
|
93
|
-
- test/
|
116
|
+
- test/test_template_directive.rb
|